changed: auto add updateData callback to stages so that stagedata can be updated...
[opensg.git] / Source / Base / Field / OSGStringToUInt32MapFieldTraits.h
blobed9c066c45a80aaf8c49ca2f3a85bdb0d5ab8423
1 /*---------------------------------------------------------------------------*\
2 * OpenSG Toolbox *
3 * *
4 * *
5 * Authors: David Kabala *
6 * *
7 \*---------------------------------------------------------------------------*/
8 /*---------------------------------------------------------------------------*\
9 * License *
10 * *
11 * This library is free software; you can redistribute it and/or modify it *
12 * under the terms of the GNU Library General Public License as published *
13 * by the Free Software Foundation, version 2. *
14 * *
15 * This library is distributed in the hope that it will be useful, but *
16 * WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
18 * Library General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU Library General Public *
21 * License along with this library; if not, write to the Free Software *
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
23 * *
24 \*---------------------------------------------------------------------------*/
26 #ifndef _OSGSTRINGTOUINT32MAP_H_
27 #define _OSGSTRINGTOUINT32MAP_H_
29 //---------------------------------------------------------------------------
30 // Includes
31 //---------------------------------------------------------------------------
33 #include "OSGBaseTypes.h"
34 #include "OSGSysFieldTraits.h"
35 #include "OSGBaseFieldTraits.h"
37 #include <string>
38 #include <map>
40 OSG_BEGIN_NAMESPACE
42 typedef std::map<std::string, UInt32> StringToUInt32Map;
44 // The FieldTraits class contains the methods needed to implement
45 // the features a Field data element needs to have
47 template <>
48 struct FieldTraits<StringToUInt32Map> :
49 public FieldTraitsTemplateBase<StringToUInt32Map>
51 // Static DataType descriptor, see OSGNewFieldType.cpp for implementation
52 static DataType _type;
54 typedef FieldTraits<StringToUInt32Map> Self;
56 // Define whether string conversions are available. It is strongly
57 // recommended to implement both.
58 enum { Convertible = (Self::ToStreamConvertible |
59 Self::FromStringConvertible) };
61 // access method for the DataType
62 static OSG_BASE_DLLMAPPING
63 DataType &getType (void);
65 // Access to the names of the actual Fields
66 static const Char8 *getSName (void)
68 return "SFStringToUInt32Map";
71 static const Char8 *getMName (void)
73 return "MFStringToUInt32Map";
76 // Create a default instance of the class, needed for Field creation
77 static StringToUInt32Map getDefault(void)
79 return StringToUInt32Map();
83 // String conversion
85 // Output inVal into outVal
86 static void putToStream (const StringToUInt32Map &inVal,
87 OutStream &outVal)
89 //Put the Size of the map
90 FieldTraits<UInt32>::putToStream(static_cast<UInt32>(inVal.size()),
91 outVal);
93 //Loop through all of the map elelments
94 StringToUInt32Map::const_iterator it = inVal.begin();
96 for(; it != inVal.end(); ++it)
98 outVal << "," << it->first << ",";
100 FieldTraits<StringToUInt32Map::mapped_type>::putToStream(
101 it->second,
102 outVal );
106 // Setup outVal from the contents of inVal
107 static bool getFromCString( StringToUInt32Map &outVal,
108 const Char8 *&inVal )
110 //Get Size of the map
111 UInt32 uiSize = 0;
113 if(sscanf(inVal,"%d", &uiSize) != 1)
115 return false;
118 outVal.clear();
120 //Loop through all of the map elelments
121 const Char8 *curInString = inVal;
123 std::string szKey;
124 UInt32 uiValue;
126 for(UInt32 i = 0; i < uiSize ; ++i)
128 //Move past the , seperator
129 curInString = strchr(curInString, ',');
131 ++curInString;
133 if(curInString == NULL)
135 return false;
138 //Move past the " seperator
139 //curInString = strchr(curInString, '\"');
140 //++curInString;
141 if(curInString == NULL)
143 return false;
146 //Get the key value
147 szKey.assign(curInString, (strchr(curInString, ',') - curInString));
149 //Move past the map value
150 //curInString = strchr(curInString, '\"');
151 //++curInString;
153 //Move past the , seperator
154 curInString = strchr(curInString, ',');
156 ++curInString;
158 if(curInString == NULL)
160 return false;
164 //Get the map value
165 FieldTraits<StringToUInt32Map::mapped_type>::getFromCString(
166 uiValue,
167 curInString);
169 //Add the Key/Value pair
170 outVal[szKey] = uiValue;
173 return true;
176 // Binary conversion
178 // Return the size of the binary version in byte
179 static SizeT getBinSize (const StringToUInt32Map &obj )
181 //Size:
182 //Size of a UInt32 -> number of items in the Map
183 //Sum of all the sizes of the strings
184 SizeT uiStringSizeSum = 0;
185 StringToUInt32Map::const_iterator it = obj.begin();
187 for(; it != obj.end() ; ++it)
189 uiStringSizeSum += FieldTraits<std::string>::getBinSize(it->first);
192 return sizeof(UInt32) + obj.size() * sizeof(UInt32) + uiStringSizeSum;
195 static SizeT getBinSize (const StringToUInt32Map *obj,
196 SizeT num )
198 //Size:
199 //Sum of all the objs
200 SizeT uiSizeSum = 0;
202 for(UInt32 i = 0; i < num; ++i)
204 uiSizeSum += getBinSize(obj[i]);
207 return uiSizeSum;
210 // Copy the object into the BinaryDataHandler
211 static void copyToBin ( BinaryDataHandler &bdh,
212 const StringToUInt32Map &obj )
214 //Number of items in the map
215 bdh.putValue(static_cast<UInt32>(obj.size()));
217 //Loop through all of the map elelments
218 StringToUInt32Map::const_iterator it = obj.begin();
220 for(; it != obj.end(); ++it)
222 bdh.putValue(
223 static_cast<StringToUInt32Map::key_type >(it->first ));
225 bdh.putValue(
226 static_cast<StringToUInt32Map::mapped_type>(it->second));
230 static void copyToBin ( BinaryDataHandler &bdh,
231 const StringToUInt32Map *objs,
232 SizeT num )
234 for(SizeT i = 0; i < num; ++i)
236 copyToBin(bdh, objs[i]);
241 // Copy the object from the BinaryDataHandler
242 static void copyFromBin ( BinaryDataHandler &bdh,
243 StringToUInt32Map &obj )
245 //Number of items in the list
246 UInt32 uiSize = 0;
248 bdh.getValue(uiSize);
250 obj.clear();
253 std::string szKey;
254 UInt32 uiValue;
256 //Loop through all of the map elelments
257 for(UInt32 i = 0; i < uiSize ; ++i)
259 FieldTraits<StringToUInt32Map::key_type>::copyFromBin(bdh, szKey);
261 bdh.getValue(uiValue);
263 obj[szKey] = uiValue;
267 static void copyFromBin ( BinaryDataHandler &bdh,
268 StringToUInt32Map *objs,
269 SizeT num )
271 for(SizeT i = 0; i < num; ++i)
273 copyFromBin(bdh, objs[i]);
278 OSG_END_NAMESPACE
280 #endif /* _OSG_STRING_TO_UINT32_MAP_TYPE_H_ */