1 /*---------------------------------------------------------------------------*\
2 * OpenSG ToolBox Toolbox *
7 * Authors: David Kabala *
9 \*---------------------------------------------------------------------------*/
10 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
26 \*---------------------------------------------------------------------------*/
28 #ifndef _OSGINT32TOSTRINGMAP_H_
29 #define _OSGINT32TOSTRINGMAP_H_
31 //---------------------------------------------------------------------------
33 //---------------------------------------------------------------------------
35 #include "OSGBaseTypes.h"
36 #include "OSGSysFieldTraits.h"
37 #include "OSGBaseFieldTraits.h"
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
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();
87 // Output inVal into outVal
88 static void putToStream (const Int32ToStringMap
&inVal
,
91 //Put the Size of the map
92 FieldTraits
<UInt32
>::putToStream(static_cast<UInt32
>(inVal
.size()),
95 //Loop through all of the map elelments
96 Int32ToStringMap::const_iterator it
= inVal
.begin();
98 for(; it
!= inVal
.end(); ++it
)
101 FieldTraits
<Int32ToStringMap::key_type
>::putToStream(it
->first
,
105 FieldTraits
<Int32ToStringMap::mapped_type
>::putToStream(
111 // Setup outVal from the contents of inVal
112 static bool getFromCString( Int32ToStringMap
&outVal
,
113 const Char8
*&inVal
)
115 //Get Size of the map
118 if(sscanf(inVal
,"%u", &uiSize
) != 1)
125 //Loop through all of the map elelments
126 const Char8
*curInString
= inVal
;
131 for(UInt32 i
= 0; i
< uiSize
; ++i
)
133 //Move past the ; seperator
134 curInString
= strchr(curInString
, ',');
138 if(curInString
== NULL
)
144 FieldTraits
<Int32ToStringMap::key_type
>::getFromCString(
148 //Move past the ; seperator
149 curInString
= strchr(curInString
, ',');
153 if(curInString
== NULL
)
158 //Move past the ; seperator
159 curInString
= strchr(curInString
, '\"');
163 if(curInString
== NULL
)
169 szValue
.assign(curInString
,
170 (strchr(curInString
, '\"') - curInString
));
172 //Move past the map value
173 curInString
= strchr(curInString
, '\"');
175 //if(curInString == NULL)
180 //Add the Key/Value pair
181 outVal
[iKey
] = szValue
;
189 // Return the size of the binary version in byte
190 static SizeT
getBinSize (const Int32ToStringMap
&obj
)
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
,
211 //Sum of all the objs
214 for(SizeT i
= 0; i
< num
; ++i
)
216 uiSizeSum
+= getBinSize(obj
[i
]);
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
)
235 static_cast<Int32ToStringMap::key_type
>(it
->first
));
238 static_cast<Int32ToStringMap::mapped_type
>(it
->second
));
242 static void copyToBin ( BinaryDataHandler
&bdh
,
243 const Int32ToStringMap
*objs
,
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
259 bdh
.getValue(uiSize
);
266 //Loop through all of the map elelments
267 for(UInt32 i
= 0; i
< uiSize
; ++i
)
271 FieldTraits
<Int32ToStringMap::mapped_type
>::copyFromBin(bdh
,
278 static void copyFromBin ( BinaryDataHandler
&bdh
,
279 Int32ToStringMap
*objs
,
282 for(SizeT i
= 0; i
< num
; ++i
)
284 copyFromBin(bdh
, objs
[i
]);
293 #endif /* _OSG_TOOLBOX_STRING_MAP_TYPE_H_ */