1 /*---------------------------------------------------------------------------*\
5 * Authors: David Kabala *
7 \*---------------------------------------------------------------------------*/
8 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
24 \*---------------------------------------------------------------------------*/
26 #ifndef _OSGSTRINGTOUINT32MAP_H_
27 #define _OSGSTRINGTOUINT32MAP_H_
29 //---------------------------------------------------------------------------
31 //---------------------------------------------------------------------------
33 #include "OSGBaseTypes.h"
34 #include "OSGSysFieldTraits.h"
35 #include "OSGBaseFieldTraits.h"
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
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();
85 // Output inVal into outVal
86 static void putToStream (const StringToUInt32Map
&inVal
,
89 //Put the Size of the map
90 FieldTraits
<UInt32
>::putToStream(static_cast<UInt32
>(inVal
.size()),
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(
106 // Setup outVal from the contents of inVal
107 static bool getFromCString( StringToUInt32Map
&outVal
,
108 const Char8
*&inVal
)
110 //Get Size of the map
113 if(sscanf(inVal
,"%u", &uiSize
) != 1)
120 //Loop through all of the map elelments
121 const Char8
*curInString
= inVal
;
126 for(UInt32 i
= 0; i
< uiSize
; ++i
)
128 //Move past the , seperator
129 curInString
= strchr(curInString
, ',');
133 if(curInString
== NULL
)
138 //Move past the " seperator
139 //curInString = strchr(curInString, '\"');
141 if(curInString
== NULL
)
147 szKey
.assign(curInString
, (strchr(curInString
, ',') - curInString
));
149 //Move past the map value
150 //curInString = strchr(curInString, '\"');
153 //Move past the , seperator
154 curInString
= strchr(curInString
, ',');
158 if(curInString
== NULL
)
165 FieldTraits
<StringToUInt32Map::mapped_type
>::getFromCString(
169 //Add the Key/Value pair
170 outVal
[szKey
] = uiValue
;
178 // Return the size of the binary version in byte
179 static SizeT
getBinSize (const StringToUInt32Map
&obj
)
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
,
199 //Sum of all the objs
202 for(UInt32 i
= 0; i
< num
; ++i
)
204 uiSizeSum
+= getBinSize(obj
[i
]);
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
)
223 static_cast<StringToUInt32Map::key_type
>(it
->first
));
226 static_cast<StringToUInt32Map::mapped_type
>(it
->second
));
230 static void copyToBin ( BinaryDataHandler
&bdh
,
231 const StringToUInt32Map
*objs
,
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
248 bdh
.getValue(uiSize
);
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
,
271 for(SizeT i
= 0; i
< num
; ++i
)
273 copyFromBin(bdh
, objs
[i
]);
280 #endif /* _OSG_STRING_TO_UINT32_MAP_TYPE_H_ */