Salome HOME
Initial version
[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 /*!
14     Class: SUIT_DataObjectKey
15     Descr: Key for personal idetfication of SUIT_DataObject.
16 */
17
18 SUIT_DataObjectKey::SUIT_DataObjectKey()
19 : refCounter( 0 )
20 {
21 }
22
23 SUIT_DataObjectKey::~SUIT_DataObjectKey()
24 {
25 }
26
27 /*!
28     Class: SUIT_DataObjectKeyHandle
29     Descr: Wrapper around the pointer of class SUIT_DataObjectKey.
30 */
31
32 SUIT_DataObjectKeyHandle::SUIT_DataObjectKeyHandle()
33 : myKey( 0 )
34 {
35 }
36
37 SUIT_DataObjectKeyHandle::SUIT_DataObjectKeyHandle( const SUIT_DataObjectKeyHandle& other )
38 : myKey( other.myKey )
39 {
40   myKey = other.myKey;
41
42   beginScope();
43 }
44
45 SUIT_DataObjectKeyHandle::SUIT_DataObjectKeyHandle( SUIT_DataObjectKey* key )
46 : myKey( key )
47 {
48   beginScope();
49 }
50
51 SUIT_DataObjectKeyHandle::~SUIT_DataObjectKeyHandle()
52 {
53   nullify();
54 }
55
56 bool SUIT_DataObjectKeyHandle::isNull() const
57 {
58   return !myKey;
59 }
60
61 void SUIT_DataObjectKeyHandle::nullify()
62 {
63   endScope();
64 }
65
66 bool SUIT_DataObjectKeyHandle::operator<( const SUIT_DataObjectKeyHandle& kw ) const
67 {
68   if ( myKey == kw.myKey )
69     return false;
70
71   if ( !myKey || !kw.myKey )
72     return myKey < kw.myKey;
73
74   const _typeinfo& i1 = typeid( *myKey );
75   const _typeinfo& i2 = typeid( *kw.myKey );
76
77   int cmp = strcmp( i1.name(), i2.name() );
78   if ( cmp < 0 )
79     return true;
80   else if ( cmp > 0 )
81     return false;
82   else
83     return myKey->isLess( kw.myKey );
84 }
85
86 bool SUIT_DataObjectKeyHandle::operator==( const SUIT_DataObjectKeyHandle& kw ) const
87 {
88   if ( myKey == kw.myKey )
89     return true;
90
91   if ( !myKey || !kw.myKey )
92     return false;
93
94   if ( typeid( *myKey ) != typeid( *kw.myKey ) )
95     return false;
96
97   return myKey->isEqual( kw.myKey );
98 }
99
100 SUIT_DataObjectKeyHandle& SUIT_DataObjectKeyHandle::operator=( const SUIT_DataObjectKeyHandle& kw )
101 {
102   if ( myKey != kw.myKey )
103   {
104     endScope();
105
106     myKey = kw.myKey;
107
108     beginScope();
109   }
110   return *this;
111 }
112
113 void SUIT_DataObjectKeyHandle::beginScope()
114 {
115   if ( myKey )
116     myKey->refCounter++;
117 }
118
119 void SUIT_DataObjectKeyHandle::endScope()
120 {
121   if ( !myKey )
122     return;
123
124   myKey->refCounter--;
125
126   if ( !myKey->refCounter )
127   {
128     delete myKey;
129     myKey = 0;
130   }
131 }