GeoTessCPP: include/DataValue.h Source File

GeoTessCPP 2.0.0 Software to facilitate storage and retrieval of 3D information about the Earth.

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Friends Macros
DataValue.h
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 Sandia Corporation. Under the terms of Contract
4 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
5 //- retains certain rights in this software.
6 //-
7 //- BSD Open Source License.
8 //- All rights reserved.
9 //-
10 //- Redistribution and use in source and binary forms, with or without
11 //- modification, are permitted provided that the following conditions are met:
12 //-
13 //- * Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //- * Redistributions in binary form must reproduce the above copyright
16 //- notice, this list of conditions and the following disclaimer in the
17 //- documentation and/or other materials provided with the distribution.
18 //- * Neither the name of Sandia National Laboratories nor the names of its
19 //- contributors may be used to endorse or promote products derived from
20 //- this software without specific prior written permission.
21 //-
22 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //- POSSIBILITY OF SUCH DAMAGE.
33 //-
34 //- ****************************************************************************
35
36 #ifndef DATAVALUE_OBJECT_H
37 #define DATAVALUE_OBJECT_H
38
39 // **** _SYSTEM INCLUDES_ ******************************************************
40
41 #include <iostream>
42 #include <fstream>
43 #include <string>
44 #include <climits>
45
46 // use standard library objects
47 using namespace std;
48
49 // **** _LOCAL INCLUDES_ *******************************************************
50
51 #include "DataType.h"
52 #include "Data.h"
53 #include "IFStreamAscii.h"
54 #include "IFStreamBinary.h"
55 #include "GeoTessUtils.h"
56 #include "DataArray.h"
57
58 // **** _BEGIN GEOTESS NAMESPACE_ *********************************************
59
60 namespace geotess {
61
62 // **** _FORWARD REFERENCES_ ***************************************************
63
64 // **** _CLASS DEFINITION_ *****************************************************
65
73 template<typename T>
74 class GEOTESS_EXP_IMP DataValue : public Data
75 {
76 private:
77
81 T value;
82
83 public:
84
88 DataValue(T v) : Data(), value(v) {};
89
93 DataValue() : Data(), value(0) {};
94
96
100 DataValue(IFStreamBinary& ifs) : Data(), value(0)
101 { ifs.readType(value); };
102
106 DataValue(IFStreamAscii& ifs) : Data(), value(0)
107 { ifs.readType(value); };
108
112 DataValue(IFStreamBinary& ifs, vector<int>& filter) : Data(), value(0)
113 {
114 T val;
115 for (int i=0; i<(int)filter.size(); ++i)
116 {
117 ifs.readType(val);
118 if (filter[i] == 0)
119 value = val;
120 }
121 }
122
126 DataValue(IFStreamAscii& ifs, vector<int>& filter) : Data(), value(0)
127 {
128 T val;
129 for (int i=0; i<(int)filter.size(); ++i)
130 {
131 ifs.readType(val);
132 if (filter[i] == 0)
133 value = val;
134 }
135 }
136
140 virtual ~DataValue() {};
141
145 virtual void write(IFStreamAscii& ofs)
146 { ofs.writeString(" "); ofs.writeType(value); };
147
151 virtual void write(IFStreamBinary& ofs) { ofs.writeType(value); };
152
154
155 /*
156 * Return DataType.
157 */
158 virtual const DataType& getDataType() const { return DataType::NONE; };
159
163 virtual int size() const { return 1; };
164
169 bool operator == (const DataValue<T>& d) const
170 { return (Data::operator==(d) && ((value == d.value) || (isNaN(0) && d.isNaN(0)))); }
171
176 virtual bool operator == (const Data& d) const { return operator==(*((const DataValue<T>*) &d)); }
177
181 virtual double getDouble(int attributeIndex) const
182 { return attributeIndex == 0 ? (double) value : NaN_DOUBLE; };
183
184 virtual float getFloat(int attributeIndex) const
185 { return attributeIndex == 0 ? (float) value : NaN_FLOAT; };
186
187 virtual LONG_INT getLong(int attributeIndex) const
188 { return attributeIndex == 0 ? (LONG_INT) value : LONG_MIN; };
189
190 virtual int getInt(int attributeIndex) const
191 { return attributeIndex == 0 ? (int) value : INT_MIN; };
192
193 virtual short getShort(int attributeIndex) const
194 { return attributeIndex == 0 ? (short) value : SHRT_MIN; };
195
196 virtual byte getByte(int attributeIndex) const
197 { return attributeIndex == 0 ? (byte) value : SCHAR_MIN; };
198
199
203 virtual void getValue(int attributeIndex, double& val) const
204 { val = attributeIndex == 0 ? (double) value : NaN_DOUBLE; };
205
209 virtual void getValue(int attributeIndex, float& val) const
210 { val = attributeIndex == 0 ? (float) value : NaN_FLOAT; };
211
215 virtual void getValue(int attributeIndex, LONG_INT& val) const
216 { val = attributeIndex == 0 ? (LONG_INT) value : LONG_MIN; };
217
221 virtual void getValue(int attributeIndex, int& val) const
222 { val = attributeIndex == 0 ? (int) value : INT_MIN; };
223
227 virtual void getValue(int attributeIndex, short& val) const
228 { val = attributeIndex == 0 ? (short) value : SHRT_MIN; };
229
233 virtual void getValue(int attributeIndex, byte& val) const
234 { val = attributeIndex == 0 ? (byte) value : SCHAR_MIN; };
235
239 virtual void getValues(double values[], const int& n) { values[0] = (double) value; }
240
244 virtual void getValues(float values[], const int& n) { values[0] = (float) value; }
245
249 virtual void getValues(LONG_INT values[], const int& n) { values[0] = (LONG_INT) value; }
250
254 virtual void getValues(int values[], const int& n) { values[0] = (int) value; }
255
259 virtual void getValues(short values[], const int& n) { values[0] = (short) value; }
260
264 virtual void getValues(byte values[], const int& n) { values[0] = (byte) value; }
265
269 virtual Data& setValue(int attributeIndex, double v)
270 { if (attributeIndex == 0) value = (T) v; return *this; };
271
272 virtual Data& setValue(int attributeIndex, float v)
273 { if (attributeIndex == 0) value = (T) v; return *this; };
274
275 virtual Data& setValue(int attributeIndex, LONG_INT v)
276 { if (attributeIndex == 0) value = (T) v; return *this; };
277
278 virtual Data& setValue(int attributeIndex, int v)
279 { if (attributeIndex == 0) value = (T) v; return *this; };
280
281 virtual Data& setValue(int attributeIndex, short v)
282 { if (attributeIndex == 0) value = (T) v; return *this; };
283
284 virtual Data& setValue(int attributeIndex, byte v)
285 { if (attributeIndex == 0) value = (T) v; return *this; };
286
298 virtual bool isNaN(int attributeIndex) const {return false;};
299
303 virtual Data* copy()
304 {
305 return new DataValue<T>(value);
306 }
307
308 }; // end class DataValue
309
311
315 template<>
316 inline bool DataValue<double>::isNaN(int attributeIndex) const
317 {
318 return (isnan(value));
319 }
320
324 template<>
325 inline bool DataValue<float>::isNaN(int attributeIndex) const
326 {
327 double v = (double) value;
328 return (isnan(v));
329 }
330
334 template<>
335 inline const DataType& DataValue<double>::getDataType() const
336 {
337 return DataType::DOUBLE;
338 }
339
343 template<>
344 inline const DataType& DataValue<float>::getDataType() const
345 {
346 return DataType::FLOAT;
347 }
348
352 template<>
353 inline const DataType& DataValue<LONG_INT>::getDataType() const
354 {
355 return DataType::LONG;
356 }
357
361 template<>
362 inline const DataType& DataValue<int>::getDataType() const
363 {
364 return DataType::INT;
365 }
366
370 template<>
371 inline const DataType& DataValue<short>::getDataType() const
372 {
373 return DataType::SHORT;
374 }
375
379 template<>
380 inline const DataType& DataValue<byte>::getDataType() const
381 {
382 return DataType::BYTE;
383 }
384
386
387 } // end namespace geotess
388
389 #endif // DATAVALUE_OBJECT_H