summaryrefslogtreecommitdiff
path: root/src/sharedcore.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/sharedcore.h')
-rw-r--r--src/sharedcore.h48
1 files changed, 28 insertions, 20 deletions
diff --git a/src/sharedcore.h b/src/sharedcore.h
index 9f42345..3b60c42 100644
--- a/src/sharedcore.h
+++ b/src/sharedcore.h
@@ -9,7 +9,6 @@
9#define BU_SHARED_CORE_H 9#define BU_SHARED_CORE_H
10 10
11#include "bu/util.h" 11#include "bu/util.h"
12#include "bu/sio.h"
13 12
14namespace Bu 13namespace Bu
15{ 14{
@@ -19,13 +18,13 @@ namespace Bu
19 typedef class SharedCore<Core> _SharedType; 18 typedef class SharedCore<Core> _SharedType;
20 public: 19 public:
21 SharedCore() : 20 SharedCore() :
22 data( new Core ), 21 core( _allocateCore() ),
23 iRefCount( new int(1) ) 22 iRefCount( new int(1) )
24 { 23 {
25 } 24 }
26 25
27 SharedCore( const _SharedType &rSrc ) : 26 SharedCore( const _SharedType &rSrc ) :
28 data( NULL ), 27 core( NULL ),
29 iRefCount( NULL ) 28 iRefCount( NULL )
30 { 29 {
31 _softCopy( rSrc ); 30 _softCopy( rSrc );
@@ -48,48 +47,57 @@ namespace Bu
48 } 47 }
49 48
50 protected: 49 protected:
51 Core *data; 50 Core *core;
52 void _hardCopy() 51 void _hardCopy()
53 { 52 {
54 if( !data || !iRefCount ) 53 if( !core || !iRefCount )
55 return; 54 return;
56 sio << "_hardCopy()" << sio.nl; 55 if( (*iRefCount) == 1 )
57 Core *copy = new Core( *data ); 56 return;
57 Core *copy = _copyCore( core );
58 _deref(); 58 _deref();
59 data = copy; 59 core = copy;
60 iRefCount = new int( 1 ); 60 iRefCount = new int( 1 );
61 } 61 }
62 62
63 virtual Core *_allocateCore()
64 {
65 return new Core();
66 }
67
68 virtual Core *_copyCore( Core *pSrc )
69 {
70 return new Core( *pSrc );
71 }
72
73 virtual void _deallocateCore( Core *pSrc )
74 {
75 delete pSrc;
76 }
77
63 private: 78 private:
64 void _deref() 79 void _deref()
65 { 80 {
66 sio << "_deref()" << sio.nl;
67 if( (--(*iRefCount)) == 0 ) 81 if( (--(*iRefCount)) == 0 )
68 { 82 {
69 sio << " --> iRefCount == 0, cleaning up." << sio.nl; 83 _deallocateCore( core );
70 delete data;
71 delete iRefCount; 84 delete iRefCount;
72 } 85 }
73 else 86 core = NULL;
74 sio << " --> iRefCount == " << *iRefCount << sio.nl;
75 data = NULL;
76 iRefCount = NULL; 87 iRefCount = NULL;
77 } 88 }
78 89
79 void _incRefCount() 90 void _incRefCount()
80 { 91 {
81 sio << "_incRefCount()" << sio.nl; 92 if( iRefCount && core )
82 if( iRefCount && data )
83 ++(*iRefCount); 93 ++(*iRefCount);
84 sio << " --> iRefCount == " << *iRefCount << sio.nl;
85 } 94 }
86 95
87 void _softCopy( const _SharedType &rSrc ) 96 void _softCopy( const _SharedType &rSrc )
88 { 97 {
89 sio << "_softCopy()" << sio.nl; 98 if( core )
90 if( data )
91 _deref(); 99 _deref();
92 data = rSrc.data; 100 core = rSrc.core;
93 iRefCount = rSrc.iRefCount; 101 iRefCount = rSrc.iRefCount;
94 _incRefCount(); 102 _incRefCount();
95 } 103 }