Salome HOME
merge from branch BR_V5_DEV
[modules/kernel.git] / src / DF / DF_Container.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 "DF_definitions.hxx"
23 #include "DF_Label.hxx"
24 #include "DF_Container.hxx"
25
26 using namespace std;
27
28 //Static method that returns an ID of the give type of attributes
29 const string& DF_Container::GetID()
30 {
31   static string id = "DF_Container_srn";
32   return id;
33 }
34
35 //Creates if not exists a Container attribute and places if is not placed it the Label
36 DF_Container* DF_Container::Set(DF_Label& theLabel) 
37 {
38   DF_Attribute* attr = NULL;
39   if(!(attr = theLabel.FindAttribute(DF_Container::GetID()))) {
40     attr = new DF_Container;
41     theLabel.AddAttribute(attr);
42   }
43   
44   return dynamic_cast<DF_Container*>(attr);
45 }
46
47 //Constructor
48 DF_Container::DF_Container() 
49 {
50   _ints.clear();
51   _doubles.clear();
52   _bools.clear();
53   _strings.clear();
54 }
55
56 //Destructor
57 DF_Container::~DF_Container() 
58 {
59   _ints.clear();
60   _doubles.clear();
61   _bools.clear();
62   _strings.clear();
63 }
64
65 //Sets an integer value of the attribute with given ID
66 void DF_Container::SetInt(const string& theID, int theValue) 
67 {
68   _ints[theID] = theValue;
69 }
70
71 //Returns an integer value of the attribute with given ID
72 int DF_Container::GetInt(const string& theID) 
73 {
74   if(!HasIntID(theID)) 
75     return 0;
76   return _ints[theID];
77 }
78
79 //Returns True if there is an integer with given ID
80 bool DF_Container::HasIntID(const string& theID) 
81 {
82   if(_ints.find(theID) != _ints.end()) return true;
83   return false;
84 }
85
86 //Sets a double value of the attribute with given ID
87 void DF_Container::SetDouble(const string& theID, const double& theValue) 
88 {
89   _doubles[theID] = theValue;
90 }
91
92
93 //Returns a double value of the attribute with given ID
94 double DF_Container::GetDouble(const string& theID) 
95 {
96   if(!HasDoubleID(theID)) return 0.0;
97   return _doubles[theID];
98 }
99
100 //Returns True if there is a double with given ID
101 bool DF_Container::HasDoubleID(const string& theID) 
102 {
103   if(_doubles.find(theID) != _doubles.end()) return true;
104   return false;
105 }
106  
107 //Sets a string value of the attribute with given ID
108 void DF_Container::SetString(const string& theID, const string& theValue) 
109 {
110   _strings[theID] = theValue;
111 }
112
113 //Returns a string  value of the attribute with given ID
114 string DF_Container::GetString(const string& theID) 
115 {
116   if(!HasStringID(theID)) return "";
117   return _strings[theID];
118 }
119
120 //Returns True if there is a string with given ID
121 bool DF_Container::HasStringID(const string& theID) 
122 {
123   if(_strings.find(theID) != _strings.end()) return true;
124   return false;
125 }
126
127 //Sets a boolean value of the attribute with given ID
128 void DF_Container::SetBool(const string& theID, bool theValue) 
129 {
130   _bools[theID] = theValue;
131 }
132
133 //Returns a boolean value of the attribute with given ID
134 bool DF_Container::GetBool(const string& theID) 
135 {
136   if(!HasBoolID(theID)) return false;
137   return _bools[theID];
138 }
139
140 //Returns True if there is a boolean value with given ID
141 bool DF_Container::HasBoolID(const string& theID) 
142 {
143   if(_bools.find(theID) != _bools.end()) return true;
144   return false;
145 }
146
147 //Clears a content of the attribute
148 void DF_Container::Clear()
149 {
150   _ints.clear();
151   _doubles.clear();
152   _strings.clear();
153   _bools.clear();
154 }
155
156 //ID is a string that uniquely identify the given type of Attributes within the Application.
157 const string& DF_Container::ID() const
158 {
159   return GetID();
160 }
161
162 //Restores a content of this Attribute from another Attribute
163 void DF_Container::Restore(DF_Attribute* theAttribute) 
164 {
165   Clear();
166
167   DF_Container* attr = dynamic_cast<DF_Container*>(theAttribute);
168   if(!attr) return;
169
170   typedef map<string, int>::const_iterator SI;
171   for(SI p = attr->_ints.begin(); p != attr->_ints.end(); p++) 
172     _ints[p->first] = p->second;
173
174   typedef map<string, double>::const_iterator SD;
175   for(SD p = attr->_doubles.begin(); p != attr->_doubles.end(); p++) 
176     _doubles[p->first] = p->second;
177
178   typedef map<string, string>::const_iterator SS;
179   for(SS p = attr->_strings.begin(); p != attr->_strings.end(); p++) 
180     _strings[p->first] = p->second;
181
182   typedef map<string, bool>::const_iterator SB;
183   for(SB p = attr->_bools.begin(); p != attr->_bools.end(); p++) 
184     _bools[p->first] = p->second;
185 }
186
187 //Creates a new empty copy oà this Attribute
188 DF_Attribute* DF_Container::NewEmpty() const 
189 {
190   return new DF_Container();
191 }
192
193 //Pastes a content of this Attribute into another Attribute 
194 void DF_Container::Paste(DF_Attribute* theIntoAttribute) 
195 {
196   DF_Container* attr = dynamic_cast<DF_Container*>(theIntoAttribute);
197   if(!attr) return;  
198
199   attr->Clear();
200
201   typedef map<string, int>::const_iterator SI;
202   for(SI p = _ints.begin(); p != _ints.end(); p++) 
203     attr->_ints[p->first] = p->second;
204
205   typedef map<string, double>::const_iterator SD;
206   for(SD p = _doubles.begin(); p != _doubles.end(); p++) 
207     attr->_doubles[p->first] = p->second;
208
209   typedef map<string, string>::const_iterator SS;
210   for(SS p = _strings.begin(); p != _strings.end(); p++) 
211     attr->_strings[p->first] = p->second;
212
213   typedef map<string, bool>::const_iterator SB;
214   for(SB p = _bools.begin(); p != _bools.end(); p++) 
215    attr-> _bools[p->first] = p->second;
216 }
217