cortoproject / corto

A hierarchical object store for connecting realtime machine data with web applications, historians & more
https://www.corto.io
MIT License
87 stars 14 forks source link

Add support for singleton members #690

Closed SanderMertens closed 6 years ago

SanderMertens commented 6 years ago

Sometimes the value of a class member needs to be shared across all class instances. To make it easy to support this usecase, a new modifier will be introduced that lets members be singletons.

Example:

class MyType {
    singleton_member: int32, singleton
}

The singleton member will be an extension of observable. This will cause the member to point to an object of the member type (an object of type int32 in the example). That way, the singleton can be locked/updated by using regular corto functions:

Initialize the singleton:

corto_update_begin(this->singleton_member);
if (!*this->singleton_member) { *this->singleton_member = 10; }
corto_update_end(this->singleton_member);

A class can use the object refcount to check if it needs to be deinitialized:

corto_update_begin(this->singleton_member);
if (corto_countof(this->singleton_member) {
    // deinitialize code
}
corto_update_end(this->singleton_member);