fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / OSB / OSGOSBTypedGeoIntegralPropertyElement.inl
bloba24601fc0f981887ff64a603a105ad48f193e898
1 /*---------------------------------------------------------------------------*\
2  *                                OpenSG                                     *
3  *                                                                           *
4  *                                                                           *
5  *                   Copyright (C) 2007 by the OpenSG Forum                  *
6  *                                                                           *
7  *                            www.opensg.org                                 *
8  *                                                                           *
9  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
10  *                                                                           *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13  *                                License                                    *
14  *                                                                           *
15  * This library is free software; you can redistribute it and/or modify it   *
16  * under the terms of the GNU Library General Public License as published    *
17  * by the Free Software Foundation, version 2.                               *
18  *                                                                           *
19  * This library is distributed in the hope that it will be useful, but       *
20  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
22  * Library General Public License for more details.                          *
23  *                                                                           *
24  * You should have received a copy of the GNU Library General Public         *
25  * License along with this library; if not, write to the Free Software       *
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
27  *                                                                           *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30  *                                Changes                                    *
31  *                                                                           *
32  *                                                                           *
33  *                                                                           *
34  *                                                                           *
35  *                                                                           *
36  *                                                                           *
37 \*---------------------------------------------------------------------------*/
39 OSG_BEGIN_NAMESPACE
41 /*-------------------------------------------------------------------------*/
42 /* OSBTypedGeoIntegralPropertyElement                                     */
43 /*-------------------------------------------------------------------------*/
45 /*-------------------------------------------------------------------------*/
46 /* Constructor                                                             */
48 template <class GeoPropertyTypeT>
49 OSBTypedGeoIntegralPropertyElement<GeoPropertyTypeT>::
50     OSBTypedGeoIntegralPropertyElement(OSBRootElement *root)
51     : Inherited(root, OSGOSBHeaderVersion200)
53     // nothing to do.
56 /*-------------------------------------------------------------------------*/
57 /* Destructor                                                              */
59 template <class GeoPropertyTypeT>
60 OSBTypedGeoIntegralPropertyElement<GeoPropertyTypeT>::
61     ~OSBTypedGeoIntegralPropertyElement(void)
63     // nothing to do.
66 /*-------------------------------------------------------------------------*/
67 /* Reading                                                                 */
69 template <class GeoPropertyTypeT>
70 void
71 OSBTypedGeoIntegralPropertyElement<GeoPropertyTypeT>::read(
72     const std::string &typeName)
74     OSG_OSB_LOG(("OSBTypedGeoIntegralPropertyElement<>::read\n"));
76     BinaryReadHandler *rh = editRoot()->getReadHandler();
78     UInt8  ptrTypeId;
79     UInt16 version;
80     UInt8  flags;
82     rh->getValue(ptrTypeId);
83     rh->getValue(version  );
85     if(version >= OSGOSBHeaderVersion200)
86     {
87         if(version > OSGOSBHeaderVersion200)
88         {
89             FINFO(("OSBTypedGeoIntegralPropertyElement<>::read: "
90                    "Unknown version, trying to read as latest.\n"));
91         }
92     }
93     else
94     {
95         FFATAL(("OSBTypedGeoIntegralPropertyElement<>::read: "
96                 "Can not read pre-OpenSG 2 GeoProperty data.\n"));
97         return;
98     }
100     rh->getValue(flags);
102     GeoPropertyUnrecPtrType prop = GeoPropertyType::create();
103     setContainer(prop);
105     while(true)
106     {
107         std::string    fieldName;
108         std::string    fieldTypeName;
109         UInt32         fieldSize;
110         PtrFieldListIt ptrFieldIt;
112         if(!readFieldHeader("", fieldName, fieldTypeName, fieldSize))
113         {
114             OSG_OSB_LOG(("OSBTypedGeoIntegralPropertyElement<>::read: "
115                     "Reading stopped at field: [%s].\n", fieldName.c_str()));
116             break;
117         }
119         if(fieldName == "values")
120         {
121             // "values" can be packed and require special handling
122             if((flags & FlagPackedMask) == FlagPacked)
123             {
124                 OSBGeometryHelper gh;
125                 UInt32             maxValue;
126                 UInt32             propSize;
127                 UInt32             byteSize;
129                 gh.readPackedIntegralPropertyHeader(
130                     rh, maxValue, propSize, byteSize);
132                 gh.readPackedIntegralProperty(
133                     rh, prop, maxValue, propSize, byteSize);
134             }
135             else
136             {
137                 readFieldContent(fieldName, fieldTypeName,
138                                  fieldSize, "", ptrFieldIt);
139             }
140         }
141         else
142         {
143             // all other fields can be read normally
144             readFieldContent(fieldName, fieldTypeName,
145                              fieldSize, "", ptrFieldIt);
146         }
147     }
150 template <class GeoPropertyTypeT>
151 void
152 OSBTypedGeoIntegralPropertyElement<GeoPropertyTypeT>::postRead(void)
154     OSG_OSB_LOG(("OSBTypedGeoIntegralPropertyElement<>::postRead\n"));
157 /*-------------------------------------------------------------------------*/
158 /* Writing                                                                 */
160 template <class GeoPropertyTypeT>
161 void
162 OSBTypedGeoIntegralPropertyElement<GeoPropertyTypeT>::preWrite(
163     FieldContainer * const fc)
165     OSG_OSB_LOG(("OSBTypedGeoIntegralPropertyElement<>::preWrite\n"));
167     preWriteFieldContainer(fc, "");
170 template <class GeoPropertyTypeT>
171 void
172 OSBTypedGeoIntegralPropertyElement<GeoPropertyTypeT>::write(void)
174     OSG_OSB_LOG(("OSBTypedGeoIntegralPropertyElement<>::write\n"));
176     BinaryWriteHandler    *wh    = editRoot()->getWriteHandler();
177     UInt8                  flags = 0;
178     GeoPropertyPtrType     prop  =
179         dynamic_cast<GeoPropertyPtrType>(getContainer());
181     wh->putValue(getFCPtrType(getContainer()));
182     wh->putValue(getVersion()                );
184     if(getPackData() == true)
185     {
186         flags |= FlagPacked;
187     }
188     else
189     {
190         flags &= ~FlagPackedMask;
191     }
193     wh->putValue(flags);
195     std::string excludedFields("'values'");
197     // write all other fields
198     writeFields(excludedFields, false);
200     // get info to write "values" field
201     const FieldDescriptionBase *fieldDesc     =
202         getContainer()->getFieldDescription("values");
203     UInt32                      fieldId       = fieldDesc->getFieldId  ();
204     const FieldType            &fieldType     = fieldDesc->getFieldType();
205     const std::string          &fieldName     = fieldDesc->getName     ();
206     const std::string          &fieldTypeName = fieldType .getName     ();
207     BitVector                   fieldMask     = fieldDesc->getFieldMask();
209     if(getPackData() == true)
210     {
211         UInt32 propSize = prop->size32();
213         if(propSize > 0)
214         {
215             IntegralType                maxValue;
216             const GeoPropertyFieldType &propField = *(prop->getFieldPtr());
218             // propField is not empty, so there is a max -> we do not deref end()
219             maxValue = *(std::max_element(propField.begin(), propField.end()));
221             // pack values
222             BitPacker packer(propSize, maxValue);
223             for(UInt32 i = 0; i < propSize; ++i)
224                 packer.pack(propField[i]);
226             const BitPacker::BufferType &buffer   = packer.getBuffer();
227             UInt32                       byteSize = UInt32(buffer.size());
229             // fieldSize: flags + maxValue + propSize + byteSize
230             UInt32 fieldSize = sizeof(UInt8) + sizeof(IntegralType)
231                 + sizeof(UInt32) + sizeof(UInt8) * byteSize;
233             // write everything
234             writeFieldHeader(fieldName, fieldTypeName, fieldSize);
235             wh->putValue(maxValue);
236             wh->putValue(propSize);
237             wh->putValue(byteSize);
238             wh->putValues(&buffer.front(), byteSize);
239         }
240         else
241         {
242             UInt32 fieldSize = sizeof(IntegralType) + 2 * sizeof(UInt32);
244             writeFieldHeader(fieldName, fieldTypeName, fieldSize);
245             wh->putValue(static_cast<IntegralType>(0));  // maxValue
246             wh->putValue(static_cast<UInt32>      (0));  // propSize
247             wh->putValue(static_cast<UInt32>      (0));  // byteSize
248         }
249     }
250     else
251     {
252         UInt32 fieldSize = UInt32(getContainer()->getBinSize(fieldMask));
254         writeFieldHeader (fieldName, fieldTypeName, fieldSize);
255         writeFieldContent(fieldId);
256     }
258     writeEndMarker();
261 OSG_END_NAMESPACE