Code Monkey home page Code Monkey logo

raii.h's Introduction

raii.h / raii_thread_safe.h

Try to use C with scoped constructor & destructor.

Implemented with heap memory & inline data structures, making it easier to comprehend.

#include <raii.h>
#include <raii_thread_safe.h>

typedef struct {int x;} some_struct;
some_struct* some_struct_init(int, int, int);
void some_struct_final(some_struct*);

int main(int argc, char** argv) {
    // Ptr with with a scoped destructor
    RAII(some_struct*, a, some_struct_final) {
        a = some_struct_init(1, 2, 3);
        // Safe to use the "continue;" clause to jump out.
        continue;
        a->x = 1;
    }

    // Ptr with scoped constructor & destructor
    SCOPE(some_struct*, b, some_struct_init(1,2,3), some_struct_final) {
        // Also safe to use the "break;" clause to jump out.
        break;
        b->x = 0;
    }

    // You need an additional preparation step to utilize the raii_thread_safe.h
    USE_TRAII();
    TRAII(some_struct*, c, some_struct_final) {
        c = some_struct_init(1, 2, 3);
        c->x = 1;
        TSCOPE(some_struct*, d, some_struct_init(1,2,3), some_struct_final) {
            d->x = 0;
            // Use customized "return" to make destructors called correctly;
            // Note that destructors are called BEFORE the return clause.
            const int sth_to_return = d->x;   // RAII / SCOPE -> RETURN
            TRETURN sth_to_return;            // TRAII/TSCOPE -> TRETURN
        }
    }
}

Experimental / raii_rc.h

Refcount Ptr implementation with stack-value handle & scoped macro which refs & derefs the Ptr automatically.

  • For cross-platform thread safety concerns, this requires stdatomic.h (C11) .
#include <stdio.h>
#include <raii_rc.h>

typedef struct {int x;} some_struct;
void customized_free(some_struct* tofree) {
    printf("FREE %d\n", tofree->x);
    free(tofree);
}

int main(int argc, char** argv) {
    RcPtrf(my_handle, sizeof(some_struct), customized_free);
    Ref(my_handle, some_struct*, instance) {
        toInit {
            instance->x = 168;
        }
        const int sth_to_return = instance->x;
        RETURN sth_to_return;
    }
}

raii.h's People

Contributors

fuxfantx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.