summaryrefslogtreecommitdiffstats
path: root/sdo-cpp/tags/cpp-sdo-1.0.incubating-M3/runtime/core/src/commonj/sdo/SDOValue.h
blob: 4b675974ddf121389022542885408dccd6c813f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
*
*  Copyright 2007 The Apache Software Foundation or its licensors, as applicable.
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/

/* $Rev$ $Date$ */

#ifndef _SDOValue_H_
#define _SDOValue_H_

#include "commonj/sdo/export.h"
#include "commonj/sdo/SDODate.h"
#include "commonj/sdo/SDOString.h"
#include "commonj/sdo/SDODataConverter.h"
#include "commonj/sdo/DataTypeInfo.h"

#include <iostream>

namespace commonj
{
   namespace sdo
   {

      // The SDOValue class provides a discriminated data type that wraps the
      // many different primitive data types that SDO must use. This allows
      // common treatment of SDO values by all methods except those that
      // really must do different things for different types. The key point is
      // that instances of the class combine a union with and enum that says
      // which member of the union is actually present. The enum also allows
      // for the possibility that the value is unset or has been explicitly
      // set to null.

      class SDOValue
      {
         private:
            // Entities with copy constructors/destructors are not allowed in a union,
            // since in general, the compiler doesn't know what type is
            // actually in there so it can't know which constructor/destructor
            // to call, hence the use of pointers for certain datatypes.
            DataTypeInfo::SDODataTypeUnion value;

            DataTypeInfo::RawDataType typeOfValue;

            mutable SDOString* transient_buffer;

         public:

            // Constructors
            SDO_API SDOValue(bool inValue) : 
               typeOfValue(DataTypeInfo::SDObool), transient_buffer(0)
            {
               value.Boolean = inValue;
            }
            SDO_API SDOValue(float inValue) : 
               typeOfValue(DataTypeInfo::SDOfloat), transient_buffer(0)
            {
               value.Float = inValue;
            }
            SDO_API SDOValue(long double inValue) : 
               typeOfValue(DataTypeInfo::SDOdouble), transient_buffer(0)
            {
               value.Double = inValue;
            }
            SDO_API SDOValue(short inValue) : 
               typeOfValue(DataTypeInfo::SDOshort), transient_buffer(0)
            {
               value.Short = inValue;
            }
#if __WORDSIZE != 64
            SDO_API SDOValue(long inValue) : 
               typeOfValue(DataTypeInfo::SDOlong), transient_buffer(0)
            {
               value.Integer = inValue;
            }
#endif
            SDO_API SDOValue(int64_t inValue) : 
               typeOfValue(DataTypeInfo::SDOint64_t), transient_buffer(0)
            {
               value.Int64 = inValue;
            }
            SDO_API SDOValue(char inValue) : 
               typeOfValue(DataTypeInfo::SDOchar), transient_buffer(0)
            {
               value.Character = inValue;
            }
            SDO_API SDOValue(wchar_t inValue) : 
               typeOfValue(DataTypeInfo::SDOwchar_t), transient_buffer(0)
            {
               value.WideChar = inValue;
            }
            SDO_API SDOValue(const SDODate inValue) : 
               typeOfValue(DataTypeInfo::SDOSDODate), transient_buffer(0)
            {
               value.Date = new SDODate(inValue.getTime());
            }
            SDO_API SDOValue(const SDOString& inValue) : 
               typeOfValue(DataTypeInfo::SDOCString), transient_buffer(0)
            {
               value.TextString = new SDOString(inValue);
            }

            SDO_API SDOValue(const char* inValue) : 
               typeOfValue(DataTypeInfo::SDOCString), transient_buffer(0)
            {
               value.TextString = new SDOString(inValue);
            }

            SDO_API SDOValue(const char* inValue, unsigned int len);

            SDO_API SDOValue(const wchar_t* inValue, unsigned int len);

            SDO_API SDOValue() : typeOfValue(DataTypeInfo::SDOunset), transient_buffer(0)
            {
            }
            //End of Constructors

            // Copy constructor
            SDO_API SDOValue(const SDOValue& inValue);
            
            // Copy assignment
            SDO_API SDOValue& operator=(const SDOValue& inValue);
            
            // Destructor
            SDO_API virtual ~SDOValue();

            inline SDO_API bool isSet() const
            {
               return (typeOfValue != DataTypeInfo::SDOunset);
            }
            inline SDO_API bool isNull() const
            {
               return (typeOfValue == DataTypeInfo::SDOnull);
            }

            // Get methods to retrieve the stored value.
            SDO_API bool getBoolean() const
            {
               return SDODataConverter::convertToBoolean(value,
                                                         (DataTypeInfo::TrueDataType) typeOfValue);
            }

            SDO_API float getFloat() const
            {
               return SDODataConverter::convertToFloat(value,
                                                       (DataTypeInfo::TrueDataType) typeOfValue);
            }

            SDO_API long double getDouble() const
            {
               return SDODataConverter::convertToDouble(value,
                                                        (DataTypeInfo::TrueDataType) typeOfValue);
            }

            SDO_API const SDODate getDate() const
            {
               return SDODataConverter::convertToDate(value,
                                                      (DataTypeInfo::TrueDataType) typeOfValue);
            }

            SDO_API short getShort() const
            {
               return SDODataConverter::convertToShort(value,
                                                      (DataTypeInfo::TrueDataType) typeOfValue);
            }

            SDO_API long getInteger() const
            {
               return SDODataConverter::convertToInteger(value,
                                                         (DataTypeInfo::TrueDataType) typeOfValue);
            }

            SDO_API char getByte() const
            {
               return SDODataConverter::convertToByte(value,
                                                      (DataTypeInfo::TrueDataType) typeOfValue);
            }

            SDO_API wchar_t getCharacter() const
            {
               return SDODataConverter::convertToCharacter(value,
                                                           (DataTypeInfo::TrueDataType) typeOfValue);
            }

            SDO_API int64_t getLong() const
            {
               return SDODataConverter::convertToLong(value,
                                                      (DataTypeInfo::TrueDataType) typeOfValue);
            }

            // The following method is regrettably necessary to provide the
            // CString style interface for the V2.01 spec.
            SDO_API const char* getCString() const
            {
               if (transient_buffer != 0)
               {
                  delete transient_buffer;
               }
               transient_buffer =
                  SDODataConverter::convertToSDOString(value,
                                                       (DataTypeInfo::TrueDataType) typeOfValue);
               return transient_buffer->c_str();
            }

            // This method is the preferred way to retrieve a string value
            SDO_API SDOString getString() const
            {
               return *SDODataConverter::convertToSDOString(value,
                                                            (DataTypeInfo::TrueDataType) typeOfValue);
            }

            SDO_API unsigned int getString(wchar_t* outptr, const unsigned int max_length) const
            {
               return SDODataConverter::convertToString(value,
                                                        (DataTypeInfo::TrueDataType) typeOfValue,
                                                        outptr,
                                                        max_length);
            }

            SDO_API unsigned int getBytes(char* outptr, const unsigned int max_length) const
            {
               return SDODataConverter::convertToBytes(value,
                                                       (DataTypeInfo::TrueDataType) typeOfValue,
                                                       outptr,
                                                       max_length);
            }

            // Beware, the array does not contain values for all the
            // enumeration values and it is the callers job to avoid
            // triggering that.
            SDO_API const SDOString& convertTypeEnumToString() const
            {
               return DataTypeInfo::convertTypeEnumToString((DataTypeInfo::TrueDataType) typeOfValue);
            }
            
            static SDO_API const SDOValue nullSDOValue;
            static SDO_API const SDOValue unsetSDOValue;
            // static SDO_API const SDOString rawTypeNames[];

         private:
            SDO_API SDOValue(DataTypeInfo::RawDataType rdt) : typeOfValue(rdt), transient_buffer(0) {}

      };
   } // End - namespace sdo
} // End - namespace commonj

#endif // _SDOValue_H_