Salome HOME
6ce6db2279f571534b01ccb520f0d0db2eba5661
[modules/gui.git] / src / SUIT / SUIT_DataObjectKey.cxx
1 #include "SUIT_DataObjectKey.h"
2
3 #include <qobject.h>
4
5 #ifndef WNT
6 #include <typeinfo>
7 #define _typeinfo std::type_info
8 #else
9 #include <typeinfo.h>
10 #define _typeinfo type_info
11 #endif
12
13 /*!\class SUIT_DataObjectKey
14  * Key for personal idetfication of SUIT_DataObject.
15  */
16
17 /*!
18   Constructor.
19 */
20 SUIT_DataObjectKey::SUIT_DataObjectKey()
21 : refCounter( 0 )
22 {
23 }
24
25 /*!
26   Destructor.
27 */
28 SUIT_DataObjectKey::~SUIT_DataObjectKey()
29 {
30 }
31
32 /*!\class SUIT_DataObjectKeyHandle
33  * Wrapper around the pointer of class SUIT_DataObjectKey.
34  */
35
36 /*!
37   Constructor. 
38 */
39 SUIT_DataObjectKeyHandle::SUIT_DataObjectKeyHandle()
40 : myKey( 0 )
41 {
42 }
43
44 /*!
45   Copy Constructor.
46 */
47 SUIT_DataObjectKeyHandle::SUIT_DataObjectKeyHandle( const SUIT_DataObjectKeyHandle& other )
48 : myKey( other.myKey )
49 {
50   myKey = other.myKey;
51
52   beginScope();
53 }
54
55 /*!
56   Constructor. Initialize by key \a key.
57 */
58 SUIT_DataObjectKeyHandle::SUIT_DataObjectKeyHandle( SUIT_DataObjectKey* key )
59 : myKey( key )
60 {
61   beginScope();
62 }
63
64 /*!
65   Destructor.
66 */
67 SUIT_DataObjectKeyHandle::~SUIT_DataObjectKeyHandle()
68 {
69   nullify();
70 }
71
72 /*!
73  * Checks: Is key null?
74  *\retval TRUE - if null, esle false.
75 */
76 bool SUIT_DataObjectKeyHandle::isNull() const
77 {
78   return !myKey;
79 }
80
81 /*!
82   Nullify key.
83 */
84 void SUIT_DataObjectKeyHandle::nullify()
85 {
86   endScope();
87 }
88
89 /*!
90  *  Operator less.
91  *\retval boolean. TRUE - If current key less than \a kw.
92  */
93 bool SUIT_DataObjectKeyHandle::operator<( const SUIT_DataObjectKeyHandle& kw ) const
94 {
95   if ( myKey == kw.myKey )
96     return false;
97
98   if ( !myKey || !kw.myKey )
99     return myKey < kw.myKey;
100
101   const _typeinfo& i1 = typeid( *myKey );
102   const _typeinfo& i2 = typeid( *kw.myKey );
103
104   int cmp = strcmp( i1.name(), i2.name() );
105   if ( cmp < 0 )
106     return true;
107   else if ( cmp > 0 )
108     return false;
109   else
110     return myKey->isLess( kw.myKey );
111 }
112
113 /*!
114  * Operator is equal.
115  *\retval boolean. TRUE - If current key equal \a kw.
116  */
117 bool SUIT_DataObjectKeyHandle::operator==( const SUIT_DataObjectKeyHandle& kw ) const
118 {
119   if ( myKey == kw.myKey )
120     return true;
121
122   if ( !myKey || !kw.myKey )
123     return false;
124
125   if ( typeid( *myKey ) != typeid( *kw.myKey ) )
126     return false;
127
128   return myKey->isEqual( kw.myKey );
129 }
130
131 /*!
132  * Copy value of key \a kw to current.
133  */
134 SUIT_DataObjectKeyHandle& SUIT_DataObjectKeyHandle::operator=( const SUIT_DataObjectKeyHandle& kw )
135 {
136   if ( myKey != kw.myKey )
137   {
138     endScope();
139
140     myKey = kw.myKey;
141
142     beginScope();
143   }
144   return *this;
145 }
146
147 /*!
148  * Inctrement reference counter for current key.
149  */
150 void SUIT_DataObjectKeyHandle::beginScope()
151 {
152   if ( myKey )
153     myKey->refCounter++;
154 }
155
156 /*!
157  * Decrement reference counter for current key.
158  */
159 void SUIT_DataObjectKeyHandle::endScope()
160 {
161   if ( !myKey )
162     return;
163
164   myKey->refCounter--;
165
166   if ( !myKey->refCounter )
167   {
168     delete myKey;
169     myKey = 0;
170   }
171 }