0022627: Change OCCT memory management defaults
[occt.git] / src / TObj / TObj_Partition.cxx
1 // File:        TObj_Partition.cxx
2 // Created:     Tue Nov  23 11:27:33 2004
3 // Author:      Pavel TELKOV
4 // Copyright:   Open CASCADE  2007
5 // The original implementation Copyright: (C) RINA S.p.A
6
7 #include <TObj_Partition.hxx>
8
9 #include <TObj_Model.hxx>
10 #include <TObj_TNameContainer.hxx>
11
12 #include <TDataStd_Name.hxx>
13
14 IMPLEMENT_STANDARD_HANDLE(TObj_Partition,TObj_Object)
15 IMPLEMENT_STANDARD_RTTIEXT(TObj_Partition,TObj_Object)
16 IMPLEMENT_TOBJOCAF_PERSISTENCE(TObj_Partition)
17
18 //=======================================================================
19 //function : TObj_Partition
20 //purpose  :
21 //=======================================================================
22
23 TObj_Partition::TObj_Partition (const TDF_Label& theLabel)
24      : TObj_Object( theLabel )
25 {
26 }
27
28 //=======================================================================
29 //function : Create
30 //purpose  :
31 //=======================================================================
32
33 Handle(TObj_Partition) TObj_Partition::Create
34                            (const TDF_Label& theLabel)
35 {
36   Handle(TObj_Partition) aPartition =
37     new TObj_Partition(theLabel);
38   aPartition->SetLastIndex(0);
39   return aPartition;
40 }
41
42 //=======================================================================
43 //function : NewLabel
44 //purpose  :
45 //=======================================================================
46
47 TDF_Label TObj_Partition::NewLabel() const
48 {
49   TDF_Label aLabel;
50   TDF_TagSource aTag;
51   aLabel = aTag.NewChild(GetChildLabel());
52   return aLabel;
53 }
54
55 //=======================================================================
56 //function : SetNamePrefix
57 //purpose  :
58 //=======================================================================
59
60 void TObj_Partition::SetNamePrefix
61                         (const Handle(TCollection_HExtendedString)& thePrefix)
62 { myPrefix = thePrefix; }
63
64 //=======================================================================
65 //function : NewName
66 //purpose  :
67 //=======================================================================
68
69 Handle(TCollection_HExtendedString) TObj_Partition::GetNewName
70 ( const Standard_Boolean theIsToChangeCount )
71 {
72   if ( myPrefix.IsNull() ) return 0;
73
74   Standard_Integer aRank = GetLastIndex()+1;
75   Standard_Integer saveRank = aRank;
76   Handle(TCollection_HExtendedString) aName;
77   do
78   {
79     aName = new TCollection_HExtendedString(myPrefix->String()+aRank++);
80   } while( GetModel()->IsRegisteredName( aName, GetDictionary() ) );
81
82   // the last index is increased taking into account only names that are
83   // actually set; the name requested by the current operation can be
84   // dropped later and this will not cause index to be increased
85   if ( theIsToChangeCount && --aRank > saveRank )
86     SetLastIndex ( aRank );
87   return aName;
88 }
89
90 //=======================================================================
91 //function : GetPartition
92 //purpose  :
93 //=======================================================================
94
95 Handle(TObj_Partition) TObj_Partition::GetPartition
96                         (const Handle(TObj_Object)& theObject)
97 {
98   Handle(TObj_Partition) aPartition;
99   if(!theObject.IsNull())
100   {
101     TDF_Label aLabel = theObject->GetLabel().Father();
102
103     // find partition which contains the object
104     while(aPartition.IsNull() && !aLabel.IsNull())
105     {
106       Handle(TObj_Object) anObject;
107       if(TObj_Object::GetObj(aLabel,anObject,Standard_True))
108         aPartition = Handle(TObj_Partition)::DownCast(anObject);
109
110       if(aPartition.IsNull())
111         aLabel = aLabel.Father();
112     }
113   }
114   return aPartition;
115 }
116
117 //=======================================================================
118 //function : GetLastIndex
119 //purpose  :
120 //=======================================================================
121
122 Standard_Integer TObj_Partition::GetLastIndex() const
123 {
124   return getInteger(DataTag_LastIndex);
125 }
126
127 //=======================================================================
128 //function : SetLastIndex
129 //purpose  :
130 //=======================================================================
131
132 void TObj_Partition::SetLastIndex(const Standard_Integer theIndex)
133 {
134   setInteger(theIndex,DataTag_LastIndex);
135 }
136
137 //=======================================================================
138 //function : copyData
139 //purpose  : protected
140 //=======================================================================
141
142 Standard_Boolean TObj_Partition::copyData
143                 (const Handle(TObj_Object)& theTargetObject)
144 {
145   Standard_Boolean IsDone;
146   Handle(TObj_Partition) aTargetPartition =
147     Handle(TObj_Partition)::DownCast(theTargetObject);
148   IsDone = aTargetPartition.IsNull() ? Standard_False : Standard_True;
149   if(IsDone) 
150   {
151     IsDone = TObj_Object::copyData(theTargetObject);
152     if ( IsDone ) 
153     {
154       aTargetPartition->myPrefix = myPrefix;
155     }
156   }
157   return IsDone;
158 }
159
160 //=======================================================================
161 //function : SetName
162 //purpose  : do not register a name in the dictionary
163 //=======================================================================
164
165 Standard_Boolean TObj_Partition::SetName(const Handle(TCollection_HExtendedString)& theName) const
166 {
167   Handle(TCollection_HExtendedString) anOldName = GetName();
168   if( !anOldName.IsNull() && theName->String().IsEqual(anOldName->String()) )
169     return Standard_True;
170
171   TDataStd_Name::Set(GetLabel(),theName->String());
172   return Standard_True;
173 }
174
175 //=======================================================================
176 //function : AfterRetrieval
177 //purpose  : do not register a name in the dictionary
178 //=======================================================================
179
180 void TObj_Partition::AfterRetrieval()
181 {
182 }