Salome HOME
updated copyright message
[modules/kernel.git] / src / DF / DF_Container.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "DF_definitions.hxx"
21 #include "DF_Label.hxx"
22 #include "DF_Container.hxx"
23
24 //Static method that returns an ID of the give type of attributes
25 const std::string& DF_Container::GetID()
26 {
27   static std::string id = "DF_Container_srn";
28   return id;
29 }
30
31 //Creates if not exists a Container attribute and places if is not placed it the Label
32 DF_Container* DF_Container::Set(DF_Label& theLabel) 
33 {
34   DF_Attribute* attr = NULL;
35   if(!(attr = theLabel.FindAttribute(DF_Container::GetID()))) {
36     attr = new DF_Container;
37     theLabel.AddAttribute(attr);
38   }
39   
40   return dynamic_cast<DF_Container*>(attr);
41 }
42
43 //Constructor
44 DF_Container::DF_Container() 
45 {
46   _ints.clear();
47   _doubles.clear();
48   _bools.clear();
49   _strings.clear();
50 }
51
52 //Destructor
53 DF_Container::~DF_Container() 
54 {
55   _ints.clear();
56   _doubles.clear();
57   _bools.clear();
58   _strings.clear();
59 }
60
61 //Sets an integer value of the attribute with given ID
62 void DF_Container::SetInt(const std::string& theID, int theValue) 
63 {
64   _ints[theID] = theValue;
65 }
66
67 //Returns an integer value of the attribute with given ID
68 int DF_Container::GetInt(const std::string& theID) 
69 {
70   if(!HasIntID(theID)) 
71     return 0;
72   return _ints[theID];
73 }
74
75 //Returns True if there is an integer with given ID
76 bool DF_Container::HasIntID(const std::string& theID) 
77 {
78   if(_ints.find(theID) != _ints.end()) return true;
79   return false;
80 }
81
82 //Sets a double value of the attribute with given ID
83 void DF_Container::SetDouble(const std::string& theID, const double& theValue) 
84 {
85   _doubles[theID] = theValue;
86 }
87
88
89 //Returns a double value of the attribute with given ID
90 double DF_Container::GetDouble(const std::string& theID) 
91 {
92   if(!HasDoubleID(theID)) return 0.0;
93   return _doubles[theID];
94 }
95
96 //Returns True if there is a double with given ID
97 bool DF_Container::HasDoubleID(const std::string& theID) 
98 {
99   if(_doubles.find(theID) != _doubles.end()) return true;
100   return false;
101 }
102  
103 //Sets a string value of the attribute with given ID
104 void DF_Container::SetString(const std::string& theID, const std::string& theValue) 
105 {
106   _strings[theID] = theValue;
107 }
108
109 //Returns a string  value of the attribute with given ID
110 std::string DF_Container::GetString(const std::string& theID) 
111 {
112   if(!HasStringID(theID)) return "";
113   return _strings[theID];
114 }
115
116 //Returns True if there is a string with given ID
117 bool DF_Container::HasStringID(const std::string& theID) 
118 {
119   if(_strings.find(theID) != _strings.end()) return true;
120   return false;
121 }
122
123 //Sets a boolean value of the attribute with given ID
124 void DF_Container::SetBool(const std::string& theID, bool theValue) 
125 {
126   _bools[theID] = theValue;
127 }
128
129 //Returns a boolean value of the attribute with given ID
130 bool DF_Container::GetBool(const std::string& theID) 
131 {
132   if(!HasBoolID(theID)) return false;
133   return _bools[theID];
134 }
135
136 //Returns True if there is a boolean value with given ID
137 bool DF_Container::HasBoolID(const std::string& theID) 
138 {
139   if(_bools.find(theID) != _bools.end()) return true;
140   return false;
141 }
142
143 //Clears a content of the attribute
144 void DF_Container::Clear()
145 {
146   _ints.clear();
147   _doubles.clear();
148   _strings.clear();
149   _bools.clear();
150 }
151
152 //ID is a string that uniquely identify the given type of Attributes within the Application.
153 const std::string& DF_Container::ID() const
154 {
155   return GetID();
156 }
157
158 //Restores a content of this Attribute from another Attribute
159 void DF_Container::Restore(DF_Attribute* theAttribute) 
160 {
161   Clear();
162
163   DF_Container* attr = dynamic_cast<DF_Container*>(theAttribute);
164   if(!attr) return;
165
166   typedef std::map<std::string, int>::const_iterator SI;
167   for(SI p = attr->_ints.begin(); p != attr->_ints.end(); p++) 
168     _ints[p->first] = p->second;
169
170   typedef std::map<std::string, double>::const_iterator SD;
171   for(SD p = attr->_doubles.begin(); p != attr->_doubles.end(); p++) 
172     _doubles[p->first] = p->second;
173
174   typedef std::map<std::string, std::string>::const_iterator SS;
175   for(SS p = attr->_strings.begin(); p != attr->_strings.end(); p++) 
176     _strings[p->first] = p->second;
177
178   typedef std::map<std::string, bool>::const_iterator SB;
179   for(SB p = attr->_bools.begin(); p != attr->_bools.end(); p++) 
180     _bools[p->first] = p->second;
181 }
182
183 //Creates a new empty copy oà this Attribute
184 DF_Attribute* DF_Container::NewEmpty() const 
185 {
186   return new DF_Container();
187 }
188
189 //Pastes a content of this Attribute into another Attribute 
190 void DF_Container::Paste(DF_Attribute* theIntoAttribute) 
191 {
192   DF_Container* attr = dynamic_cast<DF_Container*>(theIntoAttribute);
193   if(!attr) return;  
194
195   attr->Clear();
196
197   typedef std::map<std::string, int>::const_iterator SI;
198   for(SI p = _ints.begin(); p != _ints.end(); p++) 
199     attr->_ints[p->first] = p->second;
200
201   typedef std::map<std::string, double>::const_iterator SD;
202   for(SD p = _doubles.begin(); p != _doubles.end(); p++) 
203     attr->_doubles[p->first] = p->second;
204
205   typedef std::map<std::string, std::string>::const_iterator SS;
206   for(SS p = _strings.begin(); p != _strings.end(); p++) 
207     attr->_strings[p->first] = p->second;
208
209   typedef std::map<std::string, bool>::const_iterator SB;
210   for(SB p = _bools.begin(); p != _bools.end(); p++) 
211    attr-> _bools[p->first] = p->second;
212 }