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