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