Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/gui.git] / src / SUIT / SUIT_DataObjectKey.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "SUIT_DataObjectKey.h"
20
21 #include <qobject.h>
22
23 #ifndef WNT
24 #include <typeinfo>
25 #define _typeinfo std::type_info
26 #else
27 #include <typeinfo.h>
28 #define _typeinfo type_info
29 #endif
30
31 /*!\class SUIT_DataObjectKey
32  * Key for personal idetfication of SUIT_DataObject.
33  */
34
35 /*!
36   Constructor.
37 */
38 SUIT_DataObjectKey::SUIT_DataObjectKey()
39 : refCounter( 0 )
40 {
41 }
42
43 /*!
44   Destructor.
45 */
46 SUIT_DataObjectKey::~SUIT_DataObjectKey()
47 {
48 }
49
50 /*!\class SUIT_DataObjectKeyHandle
51  * Wrapper around the pointer of class SUIT_DataObjectKey.
52  */
53
54 /*!
55   Constructor. 
56 */
57 SUIT_DataObjectKeyHandle::SUIT_DataObjectKeyHandle()
58 : myKey( 0 )
59 {
60 }
61
62 /*!
63   Copy Constructor.
64 */
65 SUIT_DataObjectKeyHandle::SUIT_DataObjectKeyHandle( const SUIT_DataObjectKeyHandle& other )
66 : myKey( other.myKey )
67 {
68   myKey = other.myKey;
69
70   beginScope();
71 }
72
73 /*!
74   Constructor. Initialize by key \a key.
75 */
76 SUIT_DataObjectKeyHandle::SUIT_DataObjectKeyHandle( SUIT_DataObjectKey* key )
77 : myKey( key )
78 {
79   beginScope();
80 }
81
82 /*!
83   Destructor.
84 */
85 SUIT_DataObjectKeyHandle::~SUIT_DataObjectKeyHandle()
86 {
87   nullify();
88 }
89
90 /*!
91  * Checks: Is key null?
92  *\retval TRUE - if null, esle false.
93 */
94 bool SUIT_DataObjectKeyHandle::isNull() const
95 {
96   return !myKey;
97 }
98
99 /*!
100   Nullify key.
101 */
102 void SUIT_DataObjectKeyHandle::nullify()
103 {
104   endScope();
105 }
106
107 /*!
108  *  Operator less.
109  *\retval boolean. TRUE - If current key less than \a kw.
110  */
111 bool SUIT_DataObjectKeyHandle::operator<( const SUIT_DataObjectKeyHandle& kw ) const
112 {
113   if ( myKey == kw.myKey )
114     return false;
115
116   if ( !myKey || !kw.myKey )
117     return myKey < kw.myKey;
118
119   const _typeinfo& i1 = typeid( *myKey );
120   const _typeinfo& i2 = typeid( *kw.myKey );
121
122   int cmp = strcmp( i1.name(), i2.name() );
123   if ( cmp < 0 )
124     return true;
125   else if ( cmp > 0 )
126     return false;
127   else
128     return myKey->isLess( kw.myKey );
129 }
130
131 /*!
132  * Operator is equal.
133  *\retval boolean. TRUE - If current key equal \a kw.
134  */
135 bool SUIT_DataObjectKeyHandle::operator==( const SUIT_DataObjectKeyHandle& kw ) const
136 {
137   if ( myKey == kw.myKey )
138     return true;
139
140   if ( !myKey || !kw.myKey )
141     return false;
142
143   if ( typeid( *myKey ) != typeid( *kw.myKey ) )
144     return false;
145
146   return myKey->isEqual( kw.myKey );
147 }
148
149 /*!
150  * Copy value of key \a kw to current.
151  */
152 SUIT_DataObjectKeyHandle& SUIT_DataObjectKeyHandle::operator=( const SUIT_DataObjectKeyHandle& kw )
153 {
154   if ( myKey != kw.myKey )
155   {
156     endScope();
157
158     myKey = kw.myKey;
159
160     beginScope();
161   }
162   return *this;
163 }
164
165 /*!
166  * Inctrement reference counter for current key.
167  */
168 void SUIT_DataObjectKeyHandle::beginScope()
169 {
170   if ( myKey )
171     myKey->refCounter++;
172 }
173
174 /*!
175  * Decrement reference counter for current key.
176  */
177 void SUIT_DataObjectKeyHandle::endScope()
178 {
179   if ( !myKey )
180     return;
181
182   myKey->refCounter--;
183
184   if ( !myKey->refCounter )
185   {
186     delete myKey;
187     myKey = 0;
188   }
189 }