Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<class T>
- class Reference {
- T* _pointer;
- size_t _count;
- public:
- Reference(T* pointer) {
- pointer = pointer;
- _count = 1;
- }
- ~Reference() {
- delete _pointer;
- }
- void retain() {
- _count++;
- }
- bool release() {
- _count--;
- return _count == 0;
- }
- size_t count() {
- return _count;
- }
- };
- template <class T>
- class StrongPointer {
- Reference<T>* _reference;
- public:
- StrongPointer() {
- _reference = NULL;
- }
- StrongPointer(T* pointer) {
- _reference = new Reference<T>(pointer);
- }
- ~StrongPointer() {
- release();
- }
- void operator=(const StrongPointer<T>& other) {
- assert(_reference != other._reference);
- release();
- _reference = other._reference;
- _reference->retain();
- }
- void operator=(T* pointer) {
- release();
- _reference = new Reference<T>(pointer);
- }
- void release() {
- if (_reference && _reference->release()) {
- delete _reference;
- _reference = NULL;
- }
- }
- };
Add Comment
Please, Sign In to add comment