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 _OSGFIELDCONTAINERMAPFIELDTRAITS_H_
29 #define _OSGFIELDCONTAINERMAPFIELDTRAITS_H_
34 //---------------------------------------------------------------------------
36 //---------------------------------------------------------------------------
39 #include "OSGBaseFieldTraits.h"
41 #include "OSGFieldContainerFactory.h"
42 #include "OSGFieldContainer.h"
43 #include "OSGContainerIdMapper.h"
50 typedef std::map
<Int32
, FieldContainerRecPtr
> FieldContainerMap
;
52 // The FieldTraits class contains the methods needed to implement
53 // the features a Field data element needs to have
56 struct FieldTraits
<FieldContainerMap
> :
57 public FieldTraitsTemplateBase
<FieldContainerMap
>
59 // Static DataType descriptor, see OSGNewFieldType.cpp for implementation
60 static DataType _type
;
62 typedef FieldTraits
<std::string
> Self
;
64 // Define whether string conversions are available. It is strongly
65 // recommended to implement both.
66 enum { Convertible
= (Self::ToStreamConvertible
|
67 Self::FromStringConvertible
) };
69 // access method for the DataType
70 static OSG_BASE_DLLMAPPING
71 DataType
&getType (void);
73 // Access to the names of the actual Fields
74 static const Char8
*getSName (void) { return "SFFieldContainerMap"; }
75 static const Char8
*getMName (void) { return "MFFieldContainerMap"; }
77 // Create a default instance of the class, needed for Field creation
78 static FieldContainerMap
getDefault(void) { return FieldContainerMap(); }
81 // This is where it gets interesting: the conversion functions
84 // Output inVal into outVal
85 // the exact mapping doesn't matter,
86 // Our recommendation is to output as a string,
87 // i.e. start and stop with ", as this simplifies integration into the
89 static void putToStream (const FieldContainerMap
&inVal
,
92 //Loop through all of the map elelments
93 FieldContainerMap::const_iterator it
= inVal
.begin();
95 for(; it
!= inVal
.end(); ++it
)
97 if(it
!= inVal
.begin())
102 FieldTraits
<FieldContainerMap::key_type
>::putToStream(it
->first
,
106 if(it
->second
== NULL
)
108 FieldTraits
<UInt32
>::putToStream(0, outVal
);
112 FieldTraits
<UInt32
>::putToStream(it
->second
->getId(), outVal
);
117 static void putToStream (const FieldContainerMap
&inVal
,
119 const ContainerIdMapper
&oIDMap
)
121 //Loop through all of the map elelments
122 FieldContainerMap::const_iterator it
= inVal
.begin();
124 for(; it
!= inVal
.end(); ++it
)
126 if(it
!= inVal
.begin())
131 FieldTraits
<FieldContainerMap::key_type
>::putToStream(
132 oIDMap
.map(it
->first
),
137 if(it
->second
== NULL
)
139 FieldTraits
<UInt32
>::putToStream(0, outVal
);
143 FieldTraits
<UInt32
>::putToStream(
144 oIDMap
.map(it
->second
->getId()),
150 // Setup outVal from the contents of inVal
151 // For complicated classes it makes sense to implement this function
152 // as a class method and just call that from here
153 static bool getFromCString( FieldContainerMap
&outVal
,
154 const Char8
*&inVal
)
158 //Loop through all of the map elelments
159 const Char8
*curInString(inVal
);
162 FieldContainer
*pValue
= NULL
;
163 UInt32 uiFieldContainerID
= 0;
165 while(curInString
!= NULL
)
168 /*! \todo This is a hack so the Constraints field of SpringLayout
171 FieldTraits
<FieldContainerMap::key_type
>::getFromCString(
175 pValue
= FieldContainerFactory::the()->getMappedContainer(iKey
);
179 iKey
= pValue
->getId();
182 //Move past the ; seperator
183 curInString
= strchr(curInString
, ',');
187 if(curInString
== NULL
)
193 FieldTraits
<UInt32
>::getFromCString(uiFieldContainerID
,
196 pValue
= FieldContainerFactory::the()->getMappedContainer(
201 SWARNING
<< "ERROR in FieldContainerMap::getFromCString(): "
202 << "Could not find Container referenced with Id: "
203 << uiFieldContainerID
207 //Add the Key/Value pair
208 outVal
[iKey
] = pValue
;
210 //Move past the ; seperator
211 curInString
= strchr(curInString
, ',');
213 if(curInString
!= NULL
)
222 static SizeT
getBinSize (const FieldContainerMap
&oObject
)
224 SizeT uiNumPublicObjects
= oObject
.size();
226 return sizeof(UInt32
) + // Number of elements in the map
227 uiNumPublicObjects
* (sizeof(Int32
) + sizeof(UInt32
));
230 static SizeT
getBinSize (const FieldContainerMap
*pObjectStore
,
235 // defaut: individual field sizes
236 for(SizeT i
= 0; i
< uiNumObjects
; ++i
)
238 uiSize
+= getBinSize(pObjectStore
[i
]);
244 static void copyToBin ( BinaryDataHandler
&pMem
,
245 const FieldContainerMap
&pObject
)
248 UInt32 uiNumPublicObjects
= UInt32(pObject
.size());
250 pMem
.putValue(uiNumPublicObjects
); //Number of Key/Value pairs
252 FieldContainerMap::const_iterator mapIt
= pObject
.begin();
253 FieldContainerMap::const_iterator mapEnd
= pObject
.end ();
255 for(; mapIt
!= mapEnd
; ++mapIt
)
257 uiId
= mapIt
->second
->getId();
259 pMem
.putValue(mapIt
->first
); //Key
260 pMem
.putValue(uiId
); //Value = Field Container ID
264 static void copyToBin ( BinaryDataHandler
&pMem
,
265 const FieldContainerMap
*pObjectStore
,
268 for(SizeT i
= 0; i
< uiNumObjects
; ++i
)
270 copyToBin(pMem
, pObjectStore
[i
]);
274 static void copyFromBin ( BinaryDataHandler
&pMem
,
275 FieldContainerMap
&pObject
)
277 FieldContainer
*pFC
= NULL
;
282 pMem
.getValue(uiSize
);
286 for(UInt32 i
= 0; i
< uiSize
; ++i
)
291 pFC
= FieldContainerFactory::the()->getMappedContainer(uiId
);
297 static void copyFromBin ( BinaryDataHandler
&pMem
,
298 FieldContainerMap
*pObjectStore
,
301 for(SizeT i
= 0; i
< uiNumObjects
; ++i
)
303 copyFromBin(pMem
, pObjectStore
[i
]);
310 #endif /* _OSGFIELDCONTAINERMAPFIELDTRAITS_H_ */