<-- back

Ownership

NAL: myStaticObject = MyPointerClass; // auto initialized to nullptr MyClass = module; .myOwnMember = MyPointerClass; .mySharedMember = share MyPointerClass; .constructor = (mutable this){ myOwnMember = new MyPointerClass(); mySharedMember = nullptr; } .getShared = MyPointerClass (this) { return mySharedMember; } .getOwned = MyPointerClass (this){ return myOwnMember; } .getSingleton = MyPointerClass (){ if(!myStaticObject) myStaticObject = new MyPointerClass(); return myStaticObject; } .revokeOwned = own MyPointerClass (mutable this){ return myOwnMember; } .setShared = (mutable this, MyPointerClass newPointer){ mySharedMember = newPointer; } .setOwned = (mutable this, own MyPointerClass newPointer){ myOwnMember = newPointer; } { myGlobalPointer = MyClass.getSingleton(); myPointer = new MyPointerClass(); myObject = new MyClass(); myObject.setOwned(own myPointer); someSharedPointer = myObject.getShared(); someSharedPointer = myObject.getOwned(); myPurelySharedPointer = share myObject.revokeOwned(); myScopeOwnedPointer = myObject.revokeOwned(); } C: struct MyClass{ MyPointerClass* myOwnMember; const MyPointerClass* mySharedMember; }; MyClass* _nal_MyClass_constructor(){ this = _nal_alloc(MyClass); this->myOwnMember = _nal_own(_nal_MyPointerClass_constructor()); this->mySharedMember = NULL; return this; } const MyPointerClass* _nal_MyClass_getShared(const MyClass* this){ _nal_incRef(this->mySharedMember); return this->mySharedMember; } const MyPointerClass* _nal_MyClass_getOwned(const MyClass* this){ _nal_incRef(this->myOwnMember); return this->myOwnMember; } MyPointerClass* revokeOwned(this MyClass mutable){ MyPointerClass* _retValue = this->myOwnMember; this->myOwnMember = NULL; return retValue; } void _nal_MyClass_setShared(MyClass* this, const MyPointerClass* newPointer){ _nal_decRef(this->mySharedMember); this->mySharedMember = newPointer; _nal_incRef(this->mySharedMember); } void _nal_MyClass_setOwned(MyClass* this, MyPointerClass* newPointer){ _nal_destroyOwning(this->myOwnMember); this->myOwnMember = newPointer; newPointer = NULL; } { MyPointerClass* myPointer = _nal_constructor_MyPointerClass(); MyClass* myObject = _nal_constructor_MyClass(); _nal_MyClass_setOwned(myObject, myPointer); myPointer = NULL; const MyPointerClass* someSharedPointer = _nal_MyClass_getShared(myObject); _nal_decRef(someSharedPointer); someSharedPointer = getOwned(myObject); MyPointerClass* myPurelySharedPointer = _nal_MyClass_getShared(myObject); _nal_decRef(myPurelySharedPointer); MyPointerClass* myPurelySharedPointer = _nal_pureShare(_nal_MyClass_revokeOwned(myObject)); MyPointerClass* myScopeOwnedPointer = _nal_MyClass_revokeOwned(myObject); _nal_decRef(someSharedPointer); _nal_decRef(myPurelySharedPointer); // myPurelySharedPointer might be leaking if there is a cyclic reference. _nal_destroyOwning_MyClass(myScopeOwnedPointer); }

Discussion