fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Base / Field / OSGInt32ToStringMapFieldTraits.h
blobad0e8ada0d3eca08cde17e5c719ee3180dad86bf
1 /*---------------------------------------------------------------------------*\
2 * OpenSG ToolBox Toolbox *
3 * *
4 * *
5 * *
6 * *
7 * Authors: David Kabala *
8 * *
9 \*---------------------------------------------------------------------------*/
10 /*---------------------------------------------------------------------------*\
11 * License *
12 * *
13 * This library is free software; you can redistribute it and/or modify it *
14 * under the terms of the GNU Library General Public License as published *
15 * by the Free Software Foundation, version 2. *
16 * *
17 * This library is distributed in the hope that it will be useful, but *
18 * WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
20 * Library General Public License for more details. *
21 * *
22 * You should have received a copy of the GNU Library General Public *
23 * License along with this library; if not, write to the Free Software *
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
25 * *
26 \*---------------------------------------------------------------------------*/
28 #ifndef _OSGINT32TOSTRINGMAP_H_
29 #define _OSGINT32TOSTRINGMAP_H_
31 //---------------------------------------------------------------------------
32 // Includes
33 //---------------------------------------------------------------------------
35 #include "OSGBaseTypes.h"
36 #include "OSGSysFieldTraits.h"
37 #include "OSGBaseFieldTraits.h"
39 #include <string>
40 #include <map>
42 OSG_BEGIN_NAMESPACE
44 typedef std::map<Int32, std::string> Int32ToStringMap;
46 // The FieldDataTraits class contains the methods needed to implement
47 // the features a Field data element needs to have
49 template <>
50 struct FieldTraits<Int32ToStringMap> :
51 public FieldTraitsTemplateBase<Int32ToStringMap>
53 // Static DataType descriptor, see OSGNewFieldType.cpp for implementation
54 static DataType _type;
56 typedef FieldTraits<Int32ToStringMap> Self;
58 // Define whether string conversions are available. It is strongly
59 // recommended to implement both.
60 enum { Convertible = (Self::ToStreamConvertible |
61 Self::FromStringConvertible) };
63 // access method for the DataType
64 static OSG_BASE_DLLMAPPING
65 DataType &getType (void);
67 // Access to the names of the actual Fields
68 static const Char8 *getSName (void)
70 return "SFInt32ToStringMap";
73 static const Char8 *getMName (void)
75 return "MFInt32ToStringMap";
78 // Create a default instance of the class, needed for Field creation
79 static Int32ToStringMap getDefault(void)
81 return Int32ToStringMap();
85 // String conversion
87 // Output inVal into outVal
88 static void putToStream (const Int32ToStringMap &inVal,
89 OutStream &outVal)
91 //Put the Size of the map
92 FieldTraits<UInt32>::putToStream(static_cast<UInt32>(inVal.size()),
93 outVal);
95 //Loop through all of the map elelments
96 Int32ToStringMap::const_iterator it = inVal.begin();
98 for(; it != inVal.end(); ++it)
100 outVal << ",";
101 FieldTraits<Int32ToStringMap::key_type>::putToStream(it->first,
102 outVal );
104 outVal << ",";
105 FieldTraits<Int32ToStringMap::mapped_type>::putToStream(
106 it->second,
107 outVal );
111 // Setup outVal from the contents of inVal
112 static bool getFromCString( Int32ToStringMap &outVal,
113 const Char8 *&inVal )
115 //Get Size of the map
116 UInt32 uiSize = 0;
118 if(sscanf(inVal,"%u", &uiSize) != 1)
120 return false;
123 outVal.clear();
125 //Loop through all of the map elelments
126 const Char8 *curInString = inVal;
128 Int32 iKey;
129 std::string szValue;
131 for(UInt32 i = 0; i < uiSize ; ++i)
133 //Move past the ; seperator
134 curInString = strchr(curInString, ',');
136 ++curInString;
138 if(curInString == NULL)
140 return false;
143 //Get the key value
144 FieldTraits<Int32ToStringMap::key_type>::getFromCString(
145 iKey,
146 curInString);
148 //Move past the ; seperator
149 curInString = strchr(curInString, ',');
151 ++curInString;
153 if(curInString == NULL)
155 return false;
158 //Move past the ; seperator
159 curInString = strchr(curInString, '\"');
161 ++curInString;
163 if(curInString == NULL)
165 return false;
168 //Get the map value
169 szValue.assign(curInString,
170 (strchr(curInString, '\"') - curInString));
172 //Move past the map value
173 curInString = strchr(curInString, '\"');
174 ++curInString;
175 //if(curInString == NULL)
177 // return false;
180 //Add the Key/Value pair
181 outVal[iKey] = szValue;
184 return true;
187 // Binary conversion
189 // Return the size of the binary version in byte
190 static SizeT getBinSize (const Int32ToStringMap &obj )
192 //Size:
193 //Size of a Int32 -> number of items in the Map
194 //Sum of all the sizes of the strings
195 SizeT uiStringSizeSum = 0;
197 Int32ToStringMap::const_iterator it = obj.begin();
199 for(; it != obj.end() ; ++it)
201 uiStringSizeSum += FieldTraits<std::string>::getBinSize(it->second);
204 return sizeof(UInt32) + obj.size() * sizeof(Int32) + uiStringSizeSum;
207 static SizeT getBinSize (const Int32ToStringMap *obj,
208 SizeT num )
210 //Size:
211 //Sum of all the objs
212 SizeT uiSizeSum = 0;
214 for(SizeT i = 0; i < num; ++i)
216 uiSizeSum += getBinSize(obj[i]);
219 return uiSizeSum;
222 // Copy the object into the BinaryDataHandler
223 static void copyToBin ( BinaryDataHandler &bdh,
224 const Int32ToStringMap &obj )
226 //Number of items in the map
227 bdh.putValue(static_cast<UInt32>(obj.size()));
229 //Loop through all of the map elelments
230 Int32ToStringMap::const_iterator it = obj.begin();
232 for(; it != obj.end(); ++it)
234 bdh.putValue(
235 static_cast<Int32ToStringMap::key_type >(it->first));
237 bdh.putValue(
238 static_cast<Int32ToStringMap::mapped_type>(it->second));
242 static void copyToBin ( BinaryDataHandler &bdh,
243 const Int32ToStringMap *objs,
244 SizeT num )
246 for(UInt32 i = 0; i < num; ++i)
248 copyToBin(bdh, objs[i]);
252 // Copy the object from the BinaryDataHandler
253 static void copyFromBin ( BinaryDataHandler &bdh,
254 Int32ToStringMap &obj )
256 //Number of items in the list
257 UInt32 uiSize = 0;
259 bdh.getValue(uiSize);
261 obj.clear();
263 Int32 iKey;
264 std::string szValue;
266 //Loop through all of the map elelments
267 for(UInt32 i = 0; i < uiSize ; ++i)
269 bdh.getValue(iKey);
271 FieldTraits<Int32ToStringMap::mapped_type>::copyFromBin(bdh,
272 szValue);
274 obj[iKey] = szValue;
278 static void copyFromBin ( BinaryDataHandler &bdh,
279 Int32ToStringMap *objs,
280 SizeT num )
282 for(SizeT i = 0; i < num; ++i)
284 copyFromBin(bdh, objs[i]);
291 OSG_END_NAMESPACE
293 #endif /* _OSG_TOOLBOX_STRING_MAP_TYPE_H_ */