1/**************** Value H Declares Source Code File (.H) ***************/
2/* Name: VALUE.H Version 2.3 */
3/* */
4/* (C) Copyright to the author Olivier BERTRAND 2001-2017 */
5/* */
6/* This file contains the VALUE and derived classes declares. */
7/***********************************************************************/
8#ifndef __VALUE__H__
9#define __VALUE__H__
10
11/***********************************************************************/
12/* Include required application header files */
13/* assert.h is header required when using the assert function. */
14/* block.h is header containing Block global declarations. */
15/***********************************************************************/
16#include "assert.h"
17#include "block.h"
18
19/***********************************************************************/
20/* This should list the processors accepting unaligned numeral values.*/
21/***********************************************************************/
22#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64) || defined(_M_IA64)
23#define UNALIGNED_OK
24#endif
25
26/***********************************************************************/
27/* Types used in some class definitions. */
28/***********************************************************************/
29enum CONV {CNV_ANY = 0, /* Convert to any type */
30 CNV_CHAR = 1, /* Convert to character type */
31 CNV_NUM = 2}; /* Convert to numeric type */
32
33/***********************************************************************/
34/* Types used in some class definitions. */
35/***********************************************************************/
36class CONSTANT; // For friend setting
37typedef struct _datpar *PDTP; // For DTVAL
38
39/***********************************************************************/
40/* Utilities used to test types and to allocated values. */
41/***********************************************************************/
42// Exported functions
43DllExport PCSZ GetTypeName(int);
44DllExport int GetTypeSize(int, int);
45#ifdef ODBC_SUPPORT
46/* This function is exported for use in OEM table type DLLs */
47DllExport int TranslateSQLType(int stp, int prec,
48 int& len, char& v, bool& w);
49#endif
50DllExport const char *GetFormatType(int);
51DllExport int GetFormatType(char);
52DllExport bool IsTypeChar(int type);
53DllExport bool IsTypeNum(int type);
54DllExport int ConvertType(int, int, CONV, bool match = false);
55DllExport PVAL AllocateValue(PGLOBAL, void *, short, short = 2);
56DllExport PVAL AllocateValue(PGLOBAL, PVAL, int = TYPE_VOID, int = 0);
57DllExport PVAL AllocateValue(PGLOBAL, int, int len = 0, int prec = 0,
58 bool uns = false, PCSZ fmt = NULL);
59DllExport ulonglong CharToNumber(PCSZ, int, ulonglong, bool,
60 bool *minus = NULL, bool *rc = NULL);
61DllExport BYTE OpBmp(PGLOBAL g, OPVAL opc);
62
63/***********************************************************************/
64/* Class VALUE represents a constant or variable of any valid type. */
65/***********************************************************************/
66class DllExport VALUE : public BLOCK {
67 friend class CONSTANT; // The only object allowed to use SetConstFormat
68 public:
69 // Constructors
70
71 // Implementation
72 virtual bool IsTypeNum(void) = 0;
73 virtual bool IsZero(void) = 0;
74 virtual bool IsCi(void) {return false;}
75 virtual bool IsUnsigned(void) {return Unsigned;}
76 virtual void Reset(void) = 0;
77 virtual int GetSize(void) = 0;
78 virtual int GetValLen(void) = 0;
79 virtual int GetValPrec(void) = 0;
80 virtual int GetLength(void) {return 1;}
81 virtual PSZ GetCharValue(void) {assert(false); return NULL;}
82 virtual char GetTinyValue(void) {assert(false); return 0;}
83 virtual uchar GetUTinyValue(void) {assert(false); return 0;}
84 virtual short GetShortValue(void) {assert(false); return 0;}
85 virtual ushort GetUShortValue(void) {assert(false); return 0;}
86 virtual int GetIntValue(void) = 0;
87 virtual uint GetUIntValue(void) = 0;
88 virtual longlong GetBigintValue(void) = 0;
89 virtual ulonglong GetUBigintValue(void) = 0;
90 virtual double GetFloatValue(void) = 0;
91 virtual void *GetTo_Val(void) = 0;
92 virtual void SetPrec(int prec) {Prec = prec;}
93 bool IsNull(void) {return (Nullable && Null);}
94 void SetNull(bool b) {Null = (Nullable ? b : false);}
95 bool GetNullable(void) {return Nullable;}
96 void SetNullable(bool b) {Nullable = b;}
97 int GetType(void) {return Type;}
98 int GetClen(void) {return Clen;}
99 void SetGlobal(PGLOBAL g) {Global = g;}
100
101 // Methods
102 virtual bool SetValue_pval(PVAL valp, bool chktype = false) = 0;
103 virtual bool SetValue_char(const char *p, int n) = 0;
104 virtual void SetValue_psz(PCSZ s) = 0;
105 virtual void SetValue_bool(bool) {assert(false);}
106 virtual int CompareValue(PVAL vp) = 0;
107 virtual BYTE TestValue(PVAL vp);
108 virtual void SetValue(char) {assert(false);}
109 virtual void SetValue(uchar) {assert(false);}
110 virtual void SetValue(short) {assert(false);}
111 virtual void SetValue(ushort) {assert(false);}
112 virtual void SetValue(int) {assert(false);}
113 virtual void SetValue(uint) {assert(false);}
114 virtual void SetValue(longlong) {assert(false);}
115 virtual void SetValue(ulonglong) {assert(false);}
116 virtual void SetValue(double) {assert(false);}
117 virtual void SetValue_pvblk(PVBLK blk, int n) = 0;
118 virtual void SetBinValue(void *p) = 0;
119 virtual bool GetBinValue(void *buf, int buflen, bool go) = 0;
120 virtual char *ShowValue(char *buf, int len = 0) = 0;
121 virtual char *GetCharString(char *p) = 0;
122 virtual bool IsEqual(PVAL vp, bool chktype) = 0;
123 virtual bool Compute(PGLOBAL g, PVAL *vp, int np, OPVAL op);
124 virtual bool FormatValue(PVAL vp, PCSZ fmt) = 0;
125 virtual void Printf(PGLOBAL g, FILE *, uint);
126 virtual void Prints(PGLOBAL g, char *ps, uint z);
127
128 /**
129 Set value from a non-aligned in-memory value in the machine byte order.
130 TYPE can be either of:
131 - int, short, longlong
132 - uint, ushort, ulonglong
133 - float, double
134 @param - a pointer to a non-aligned value of type TYPE.
135 */
136 template<typename TYPE>
137 void SetValueNonAligned(const char *p)
138 {
139#if defined(UNALIGNED_OK)
140 SetValue(*((TYPE*)p)); // x86 can cast non-aligned memory directly
141#else
142 TYPE tmp; // a slower version for non-x86 platforms
143 memcpy(&tmp, p, sizeof(tmp));
144 SetValue(tmp);
145#endif
146 } // end of SetValueNonAligned
147
148 /**
149 Get value from a non-aligned in-memory value in the machine byte order.
150 TYPE can be either of:
151 - int, short, longlong
152 - uint, ushort, ulonglong
153 - float, double
154 @params - a pointer to a non-aligned value of type TYPE, the TYPE value.
155 */
156 template<typename TYPE>
157 void GetValueNonAligned(char *p, TYPE n)
158 {
159#if defined(UNALIGNED_OK)
160 *(TYPE *)p = n; // x86 can cast non-aligned memory directly
161#else
162 TYPE tmp = n; // a slower version for non-x86 platforms
163 memcpy(p, &tmp, sizeof(tmp));
164#endif
165 } // end of SetValueNonAligned
166
167protected:
168 virtual bool SetConstFormat(PGLOBAL, FORMAT&) = 0;
169 const char *GetXfmt(void);
170
171 // Constructor used by derived classes
172 VALUE(int type, bool un = false);
173
174 // Members
175 PGLOBAL Global; // To reduce arglist
176 const char *Fmt;
177 const char *Xfmt;
178 bool Nullable; // True if value can be null
179 bool Null; // True if value is null
180 bool Unsigned; // True if unsigned
181 int Type; // The value type
182 int Clen; // Internal value length
183 int Prec;
184 }; // end of class VALUE
185
186/***********************************************************************/
187/* Class TYPVAL: represents a typed value. */
188/***********************************************************************/
189template <class TYPE>
190class DllExport TYPVAL : public VALUE {
191 public:
192 // Constructor
193 TYPVAL(TYPE n, int type, int prec = 0, bool un = false);
194
195 // Implementation
196 virtual bool IsTypeNum(void) {return true;}
197 virtual bool IsZero(void) {return Tval == 0;}
198 virtual void Reset(void) {Tval = 0;}
199 virtual int GetValLen(void);
200 virtual int GetValPrec() {return Prec;}
201 virtual int GetSize(void) {return sizeof(TYPE);}
202//virtual PSZ GetCharValue(void) {return VALUE::GetCharValue();}
203 virtual char GetTinyValue(void) {return (char)Tval;}
204 virtual uchar GetUTinyValue(void) {return (uchar)Tval;}
205 virtual short GetShortValue(void) {return (short)Tval;}
206 virtual ushort GetUShortValue(void) {return (ushort)Tval;}
207 virtual int GetIntValue(void) {return (int)Tval;}
208 virtual uint GetUIntValue(void) {return (uint)Tval;}
209 virtual longlong GetBigintValue(void) {return (longlong)Tval;}
210 virtual ulonglong GetUBigintValue(void) {return (ulonglong)Tval;}
211 virtual double GetFloatValue(void) {return (double)Tval;}
212 virtual void *GetTo_Val(void) {return &Tval;}
213
214 // Methods
215 virtual bool SetValue_pval(PVAL valp, bool chktype);
216 virtual bool SetValue_char(const char *p, int n);
217 virtual void SetValue_psz(PCSZ s);
218 virtual void SetValue_bool(bool b) {Tval = (b) ? 1 : 0;}
219 virtual int CompareValue(PVAL vp);
220 virtual void SetValue(char c) {Tval = (TYPE)c; Null = false;}
221 virtual void SetValue(uchar c) {Tval = (TYPE)c; Null = false;}
222 virtual void SetValue(short i) {Tval = (TYPE)i; Null = false;}
223 virtual void SetValue(ushort i) {Tval = (TYPE)i; Null = false;}
224 virtual void SetValue(int n) {Tval = (TYPE)n; Null = false;}
225 virtual void SetValue(uint n) {Tval = (TYPE)n; Null = false;}
226 virtual void SetValue(longlong n) {Tval = (TYPE)n; Null = false;}
227 virtual void SetValue(ulonglong n) {Tval = (TYPE)n; Null = false;}
228 virtual void SetValue(double f) {Tval = (TYPE)f; Null = false;}
229 virtual void SetValue_pvblk(PVBLK blk, int n);
230 virtual void SetBinValue(void *p);
231 virtual bool GetBinValue(void *buf, int buflen, bool go);
232 virtual char *ShowValue(char *buf, int);
233 virtual char *GetCharString(char *p);
234 virtual bool IsEqual(PVAL vp, bool chktype);
235 virtual bool Compute(PGLOBAL g, PVAL *vp, int np, OPVAL op);
236 virtual bool SetConstFormat(PGLOBAL, FORMAT&);
237 virtual bool FormatValue(PVAL vp, PCSZ fmt);
238
239 protected:
240 static TYPE MinMaxVal(bool b);
241 TYPE SafeAdd(TYPE n1, TYPE n2);
242 TYPE SafeMult(TYPE n1, TYPE n2);
243 bool Compall(PGLOBAL g, PVAL *vp, int np, OPVAL op);
244
245 // Default constructor not to be used
246 TYPVAL(void) : VALUE(TYPE_ERROR) {}
247
248 // Specialized functions
249 static ulonglong MaxVal(void);
250 TYPE GetTypedValue(PVAL vp);
251 TYPE GetTypedValue(PVBLK blk, int n);
252// TYPE GetTypedValue(PSZ s);
253
254 // Members
255 TYPE Tval;
256 }; // end of class TYPVAL
257
258/***********************************************************************/
259/* Specific STRING class. */
260/***********************************************************************/
261template <>
262class DllExport TYPVAL<PSZ>: public VALUE {
263 public:
264 // Constructors
265 TYPVAL(PSZ s, short c = 0);
266 TYPVAL(PGLOBAL g, PSZ s, int n, int c);
267
268 // Implementation
269 virtual bool IsTypeNum(void) {return false;}
270 virtual bool IsZero(void) {return *Strp == 0;}
271 virtual void Reset(void) {*Strp = 0;}
272 virtual int GetValLen(void) {return Len;};
273 virtual int GetValPrec() {return (Ci) ? 1 : 0;}
274 virtual int GetSize(void) {return (Strp) ? (int)strlen(Strp) : 0;}
275 virtual PSZ GetCharValue(void) {return Strp;}
276 virtual char GetTinyValue(void);
277 virtual uchar GetUTinyValue(void);
278 virtual short GetShortValue(void);
279 virtual ushort GetUShortValue(void);
280 virtual int GetIntValue(void);
281 virtual uint GetUIntValue(void);
282 virtual longlong GetBigintValue(void);
283 virtual ulonglong GetUBigintValue(void);
284 virtual double GetFloatValue(void) {return atof(Strp);}
285 virtual void *GetTo_Val(void) {return Strp;}
286 virtual void SetPrec(int prec) {Ci = prec != 0;}
287
288 // Methods
289 virtual bool SetValue_pval(PVAL valp, bool chktype);
290 virtual bool SetValue_char(const char *p, int n);
291 virtual void SetValue_psz(PCSZ s);
292 virtual void SetValue_pvblk(PVBLK blk, int n);
293 virtual void SetValue(char c);
294 virtual void SetValue(uchar c);
295 virtual void SetValue(short i);
296 virtual void SetValue(ushort i);
297 virtual void SetValue(int n);
298 virtual void SetValue(uint n);
299 virtual void SetValue(longlong n);
300 virtual void SetValue(ulonglong n);
301 virtual void SetValue(double f);
302 virtual void SetBinValue(void *p);
303 virtual int CompareValue(PVAL vp);
304 virtual bool GetBinValue(void *buf, int buflen, bool go);
305 virtual char *ShowValue(char *buf, int);
306 virtual char *GetCharString(char *p);
307 virtual bool IsEqual(PVAL vp, bool chktype);
308 virtual bool Compute(PGLOBAL g, PVAL *vp, int np, OPVAL op);
309 virtual bool FormatValue(PVAL vp, PCSZ fmt);
310 virtual bool SetConstFormat(PGLOBAL, FORMAT&);
311 virtual void Prints(PGLOBAL g, char *ps, uint z);
312
313 protected:
314 // Members
315 PSZ Strp;
316 bool Ci; // true if case insensitive
317 int Len;
318 }; // end of class TYPVAL<PSZ>
319
320/***********************************************************************/
321/* Specific DECIMAL class. */
322/***********************************************************************/
323class DllExport DECVAL: public TYPVAL<PSZ> {
324 public:
325 // Constructors
326 DECVAL(PSZ s);
327 DECVAL(PGLOBAL g, PSZ s, int n, int prec, bool uns);
328
329 // Implementation
330 virtual bool IsTypeNum(void) {return true;}
331 virtual bool IsZero(void);
332 virtual void Reset(void);
333 virtual int GetValPrec() {return Prec;}
334
335 // Methods
336 virtual bool GetBinValue(void *buf, int buflen, bool go);
337 virtual char *ShowValue(char *buf, int);
338 virtual bool IsEqual(PVAL vp, bool chktype);
339 virtual int CompareValue(PVAL vp);
340
341 protected:
342 // Members
343 }; // end of class DECVAL
344
345/***********************************************************************/
346/* Specific BINARY class. */
347/***********************************************************************/
348class DllExport BINVAL: public VALUE {
349 public:
350 // Constructors
351//BINVAL(void *p);
352 BINVAL(PGLOBAL g, void *p, int cl, int n);
353
354 // Implementation
355 virtual bool IsTypeNum(void) {return false;}
356 virtual bool IsZero(void);
357 virtual void Reset(void);
358 virtual int GetValLen(void) {return Clen;};
359 virtual int GetValPrec() {return 0;}
360 virtual int GetSize(void) {return Len;}
361 virtual PSZ GetCharValue(void) {return (PSZ)Binp;}
362 virtual char GetTinyValue(void);
363 virtual uchar GetUTinyValue(void);
364 virtual short GetShortValue(void);
365 virtual ushort GetUShortValue(void);
366 virtual int GetIntValue(void);
367 virtual uint GetUIntValue(void);
368 virtual longlong GetBigintValue(void);
369 virtual ulonglong GetUBigintValue(void);
370 virtual double GetFloatValue(void);
371 virtual void *GetTo_Val(void) {return Binp;}
372
373 // Methods
374 virtual bool SetValue_pval(PVAL valp, bool chktype);
375 virtual bool SetValue_char(const char *p, int n);
376 virtual void SetValue_psz(PCSZ s);
377 virtual void SetValue_pvblk(PVBLK blk, int n);
378 virtual void SetValue(char c);
379 virtual void SetValue(uchar c);
380 virtual void SetValue(short i);
381 virtual void SetValue(ushort i);
382 virtual void SetValue(int n);
383 virtual void SetValue(uint n);
384 virtual void SetValue(longlong n);
385 virtual void SetValue(ulonglong n);
386 virtual void SetValue(double f);
387 virtual void SetBinValue(void *p);
388 virtual bool GetBinValue(void *buf, int buflen, bool go);
389 virtual int CompareValue(PVAL) {assert(false); return 0;}
390 virtual char *ShowValue(char *buf, int);
391 virtual char *GetCharString(char *p);
392 virtual bool IsEqual(PVAL vp, bool chktype);
393 virtual bool FormatValue(PVAL vp, PCSZ fmt);
394 virtual bool SetConstFormat(PGLOBAL, FORMAT&);
395
396 protected:
397 // Members
398 void *Binp;
399 char *Chrp;
400 int Len;
401 }; // end of class BINVAL
402
403/***********************************************************************/
404/* Class DTVAL: represents a time stamp value. */
405/***********************************************************************/
406class DllExport DTVAL : public TYPVAL<int> {
407 public:
408 // Constructors
409 DTVAL(PGLOBAL g, int n, int p, PCSZ fmt);
410 DTVAL(int n);
411
412 // Implementation
413 virtual bool SetValue_pval(PVAL valp, bool chktype);
414 virtual bool SetValue_char(const char *p, int n);
415 virtual void SetValue_psz(PCSZ s);
416 virtual void SetValue_pvblk(PVBLK blk, int n);
417 virtual char *GetCharString(char *p);
418 virtual char *ShowValue(char *buf, int);
419 virtual bool FormatValue(PVAL vp, PCSZ fmt);
420 bool SetFormat(PGLOBAL g, PCSZ fmt, int len, int year = 0);
421 bool SetFormat(PGLOBAL g, PVAL valp);
422 bool IsFormatted(void) {return Pdtp != NULL;}
423 bool MakeTime(struct tm *ptm);
424 static void SetTimeShift(void);
425 static int GetShift(void) {return Shift;}
426
427 // Methods
428 bool MakeDate(PGLOBAL g, int *val, int nval);
429
430 struct tm *GetGmTime(struct tm *);
431
432 protected:
433 // Default constructor not to be used
434 DTVAL(void) : TYPVAL<int>() {}
435
436 // Members
437 static int Shift; // Time zone shift in seconds
438 PDTP Pdtp; // To the DATPAR structure
439 char *Sdate; // Utility char buffer
440 int DefYear; // Used by ExtractDate
441 int Len; // Used by CHAR scalar function
442 }; // end of class DTVAL
443
444#endif // __VALUE__H__
445