changed: gcc8 base update
[opensg.git] / Source / Base / FieldContainer / Fields / OSGFieldContainerMapFieldTraits.h
blob50946f28555bcb0c439b8ab64c120bde783cb9c1
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 _OSGFIELDCONTAINERMAPFIELDTRAITS_H_
29 #define _OSGFIELDCONTAINERMAPFIELDTRAITS_H_
30 #ifdef __sgi
31 #pragma once
32 #endif
34 //---------------------------------------------------------------------------
35 // Includes
36 //---------------------------------------------------------------------------
39 #include "OSGBaseFieldTraits.h"
41 #include "OSGFieldContainerFactory.h"
42 #include "OSGFieldContainer.h"
43 #include "OSGContainerIdMapper.h"
45 #include <string>
46 #include <map>
48 OSG_BEGIN_NAMESPACE
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
55 template <>
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
83 // String conversion
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
88 // OSG Loader.
89 static void putToStream (const FieldContainerMap &inVal,
90 OutStream &outVal )
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())
99 outVal << ",";
102 FieldTraits<FieldContainerMap::key_type>::putToStream(it->first,
103 outVal );
105 outVal << ",";
106 if(it->second == NULL)
108 FieldTraits<UInt32>::putToStream(0, outVal);
110 else
112 FieldTraits<UInt32>::putToStream(it->second->getId(), outVal);
117 static void putToStream (const FieldContainerMap &inVal,
118 OutStream &outVal,
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())
128 outVal << ",";
131 FieldTraits<FieldContainerMap::key_type>::putToStream(
132 oIDMap.map(it->first),
133 outVal );
135 outVal << ",";
137 if(it->second == NULL)
139 FieldTraits<UInt32>::putToStream(0, outVal);
141 else
143 FieldTraits<UInt32>::putToStream(
144 oIDMap.map(it->second->getId()),
145 outVal );
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 )
156 outVal.clear();
158 //Loop through all of the map elelments
159 const Char8 *curInString(inVal);
161 Int32 iKey = 0;
162 FieldContainer *pValue = NULL;
163 UInt32 uiFieldContainerID = 0;
165 while(curInString != NULL)
167 //Get the key value
168 /*! \todo This is a hack so the Constraints field of SpringLayout
169 * loads correctly */
171 FieldTraits<FieldContainerMap::key_type>::getFromCString(
172 iKey,
173 curInString);
175 pValue = FieldContainerFactory::the()->getMappedContainer(iKey);
177 if(pValue != NULL)
179 iKey = pValue->getId();
182 //Move past the ; seperator
183 curInString = strchr(curInString, ',');
185 ++curInString;
187 if(curInString == NULL)
189 return false;
192 //Get the map value
193 FieldTraits<UInt32>::getFromCString(uiFieldContainerID,
194 curInString);
196 pValue = FieldContainerFactory::the()->getMappedContainer(
197 uiFieldContainerID);
199 if(pValue == NULL)
201 SWARNING << "ERROR in FieldContainerMap::getFromCString(): "
202 << "Could not find Container referenced with Id: "
203 << uiFieldContainerID
204 << std::endl;
207 //Add the Key/Value pair
208 outVal[iKey] = pValue;
210 //Move past the ; seperator
211 curInString = strchr(curInString, ',');
213 if(curInString != NULL)
215 ++curInString;
218 return true;
221 // Binary conversion
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,
231 SizeT uiNumObjects)
233 SizeT uiSize = 0;
235 // defaut: individual field sizes
236 for(SizeT i = 0; i < uiNumObjects; ++i)
238 uiSize += getBinSize(pObjectStore[i]);
241 return uiSize;
244 static void copyToBin ( BinaryDataHandler &pMem,
245 const FieldContainerMap &pObject )
247 UInt32 uiId = 0;
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,
266 SizeT uiNumObjects)
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;
278 Int32 iKey = 0;
279 UInt32 uiId = 0;
280 UInt32 uiSize = 0;
282 pMem.getValue(uiSize);
284 pObject.clear();
286 for(UInt32 i = 0; i < uiSize; ++i)
288 pMem.getValue(iKey);
289 pMem.getValue(uiId);
291 pFC = FieldContainerFactory::the()->getMappedContainer(uiId);
293 pObject[iKey] = pFC;
297 static void copyFromBin ( BinaryDataHandler &pMem,
298 FieldContainerMap *pObjectStore,
299 SizeT uiNumObjects)
301 for(SizeT i = 0; i < uiNumObjects; ++i)
303 copyFromBin(pMem, pObjectStore[i]);
308 OSG_END_NAMESPACE
310 #endif /* _OSGFIELDCONTAINERMAPFIELDTRAITS_H_ */