UNIVERS  15.3
UNIVERS base processing software API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AnyValue.hpp
1 /* AnyValue.hpp */
2 /* $Id: AnyValue.hpp,v 1.12 2005/01/21 11:45:17 vlad Exp $ */
3 #ifndef __AnyValue_hpp
4 #define __AnyValue_hpp
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <malloc.h>
9 
10 #include <mix/units.h>
11 #include <mix/DmnDef.h>
12 #include <mix/Types.h>
13 #include <mix/Serial.hpp>
14 #include <mix/TextRepres.hpp>
15 
16 
23 class AnyValue : public SerialThis, public TextRepres
24 {
25 public:
26 
28  AnyValue (AtomType at = INT_4, unsigned count = 1,
29  CategoryId catid = UC_NONE);
30 
32  AnyValue (double val, CategoryId catid = UC_NONE);
33  AnyValue (int val, CategoryId catid = UC_NONE);
34 #ifdef CompilerIs64bitReady
35  AnyValue (long long val, CategoryId catid = UC_NONE);
36 #endif /* CompilerIs64bitReady */
37 
40  AnyValue (const DomainDef& dmn, void* rec_ptr = NULL);
41 
43  AnyValue (const AnyValue& av);
44 
47  AnyValue& operator= (const AnyValue& av);
48 
49 
51  virtual ~AnyValue ();
52 
54  void new_def (AtomType at, unsigned count = 1,
55  CategoryId catid = UC_NONE);
56 
58  void new_def (const AnyValue& av);
59 
60 
61  /***********************************************************************
62  * Information about the value
63  ***********************************************************************/
64 
66  AtomType get_type () const;
67 
69  unsigned get_dim () const;
70 
72  CategoryId get_categ () const;
73 
74 
75  /***********************************************************************
76  * Setting the AnyValue at the arbitrary pointer of memory
77  ***********************************************************************/
78 
80  void attach (void* val_ptr);
81 
83  void detach ();
84 
85 
86  /***********************************************************************
87  * Managing changes. Moving the pointer is not counted as a change
88  ***********************************************************************/
89 
91  bool is_changed () const;
92 
94  void changes ();
95 
98  void reset_changes ();
99 
100 
101  /***********************************************************************
102  * Operations over the value
103  ***********************************************************************/
104 
105  /*
106  * Get the value
107  */
108 
109  /* Scalar values (dim=1) and an item from vector value (dim>1);
110  * automatic convertion between int and float scalar numbers and
111  * ASCII<->EBCDIC mappings is performed */
112  char get_char (unsigned i = 0) const;
113  Char2 get_unicode (unsigned i = 0) const;
114  double get_float (unsigned i = 0) const;
115  int get_int (unsigned i = 0) const;
116 #ifdef CompilerIs64bitReady
117  long long get_int8 (unsigned i = 0) const;
118  long long get_key (unsigned i = 0) const{
119  return get_int8(i);
120  }
121 #endif /* CompilerIs64bitReady */
122 
123  /* Aliases for scalar values */
124  operator char () const;
125  operator float () const;
126  operator double () const;
127  operator short () const;
128  operator int () const;
129  operator long () const;
130 #ifdef CompilerIs64bitReady
131  operator long long () const;
132 #endif /* CompilerIs64bitReady */
133 
134  /* Always scalar */
135  BlobRef get_blob_ref () const;
136 
137  /* Vector values (dim>=1) */
138  void get_int1 (char* val) const;
139  void get_int2 (short* val) const;
140  void get_int4 (int* val) const;
141 #ifdef CompilerIs64bitReady
142  void get_int8 (long long* val) const;
143  void get_key (long long* val) const{
144  get_int8(val);
145  }
146 #endif /* CompilerIs64bitReady */
147  void get_real4 (float* val) const;
148  void get_real8 (double* val) const;
149  void get_char (char* val) const;
150  void get_unicode (Char2* val) const;
151 
152  /* Allocate-and-get operations (release memory by free() */
153  char* get_alloc_int1 () const;
154  short* get_alloc_int2 () const;
155  int* get_alloc_int4 () const;
156 #ifdef CompilerIs64bitReady
157  long long* get_alloc_int8 () const;
158  long long* get_alloc_key () const{
159  return get_alloc_int8();
160  }
161 #endif /* CompilerIs64bitReady */
162  float* get_alloc_real4 () const;
163  double* get_alloc_real8 () const;
164  char* get_alloc_char () const;
165  Char2* get_alloc_unicode () const;
166 
167  /*
168  * Set the value
169  */
170 
171  /* Scalar values (dim=1) and an item from vector value (dim>1);
172  * automatic convertion between int and float scalar numbers and
173  * ASCII<->EBCDIC mappings is performed */
174  void set_char (char val, unsigned i = 0);
175  void set_unicode (Char2 val, unsigned i = 0);
176  void set_float (double val, unsigned i = 0);
177  void set_int (int val, unsigned i = 0);
178 #ifdef CompilerIs64bitReady
179  void set_int8 (long long val, unsigned i = 0);
180  void set_key (long long val, unsigned i = 0){
181  set_int8(val, i);
182  }
183 #endif /* CompilerIs64bitReady */
184 
185  /* Always scalar */
186  void set_blob_ref (BlobRef val);
187 
188  /* Vector values (dim>=1) */
189  void set_int1 (const char* val);
190  void set_int2 (const short* val);
191  void set_int4 (const int* val);
192 #ifdef CompilerIs64bitReady
193  void set_int8 (const long long* val);
194  void set_key (const long long* val){
195  set_int8(val);
196  }
197 #endif /* CompilerIs64bitReady */
198  void set_real4 (const float* val);
199  void set_real8 (const double* val);
200  /* Automatic length determination by EOL */
201  void set_char (const char* val);
202  void set_unicode (const Char2* val);
203 
204  /***********************************************************************
205  * Comparison operation
206  ***********************************************************************/
207 
210  virtual bool operator== (const AnyValue& av) const;
211 
213  bool operator!= (const AnyValue& av) const;
214 
218  static Compar compare (const AnyValue& av1, const AnyValue& av2);
219 
220 
221  /***********************************************************************
222  * Stream input/output operations are performed depending the
223  * attached status: in case of own memory (attached=NULL) definition
224  * will be put and get (so, type may change on deserialize());
225  * attached object stores definition too but can't restore it since
226  * doesn't own the memory.
227  ***********************************************************************/
228 
230  virtual void serialize (OutputStream& ost) const;
231 
233  virtual void deserialize (InputStream& ist);
234 
237  void definition (bool f);
238 
239 
240  /***********************************************************************
241  * Textual representation
242  ***********************************************************************/
243 
248  virtual char* check_string (const char* s, int* pDim = NULL) const;
249 
253  virtual char* from_string (const char* s);
254 
258  virtual char* to_string () const;
259 
260 
261  /***********************************************************************
262  * Empty value management.
263  ***********************************************************************/
264 
266  bool is_empty_value () const;
267 
269  void empty_value ();
270 
271 
272  /***********************************************************************
273  * Printing the value.
274  ***********************************************************************/
275 
278  void print_self (FILE* stream = NULL,
279  const char* szPre = NULL,
280  const char* szPost = NULL) const;
281 
282 private:
283 
284  AtomType atype;
285  unsigned dim;
286  CategoryId categ;
287  union Value {
288  Real4 r4;
289  Real8 r8;
290  Int1 i1;
291  Int2 i2;
292  Int4 i4;
293 #ifdef CompilerIs64bitReady
294  Int8 i8;
295 #endif /* CompilerIs64bitReady */
296  Char c1;
297  Char2 c2;
298  BlobRef blob;
300  Real4 *ptr_r4;
301  Real8 *ptr_r8;
302  Int1 *ptr_i1;
303  Int2 *ptr_i2;
304  Int4 *ptr_i4;
305  Int8 *ptr_i8;
306  BlobRef *ptr_blob;
307  Char *ptr_c1;
308  Char2 *ptr_c2;
310  void *ptr;
311  } v;
314  bool changed;
315 
317  void *attached;
318 
320  bool def_in_stream;
321 
322 };
323 
324 
325 /***********************************************************************
326  * Inline implementation of some methods
327  ***********************************************************************/
328 
329 /* Detach true value storage */
330 inline void
332 {
333  attached = NULL;
334 
335  empty_value();
336 }
337 
338 
339 /* Get status of changes */
340 inline bool
342 {
343  return changed;
344 }
345 
346 
347 /* Set changes flag */
348 inline void
350 {
351  changed = TRUE;
352 }
353 
354 
355 /* Reset status of changes, leads to FALSE at next call to
356  is_changed() */
357 inline void
359 {
360  changed = FALSE;
361 }
362 
363 
364 /* Return type of the value */
365 inline AtomType
367 {
368  return atype;
369 }
370 
371 
372 /* Return dimension of the value */
373 inline unsigned
375 {
376  return dim;
377 }
378 
379 
380 /* Return category of the value */
381 inline CategoryId
383 {
384  return categ;
385 }
386 
387 
388 /* Turn on (by default) or off putting/getting value definition
389  to/from the stream */
390 inline void
392 {
393  def_in_stream = !!f;
394 }
395 
396 
397 /* Change value definition */
398 inline void
400 {
401  new_def(av.get_type(), av.get_dim(), av.get_categ());
402 }
403 
404 
405 inline
406 AnyValue::operator char () const
407 {
408  return get_char();
409 }
410 
411 inline
412 AnyValue::operator double () const
413 {
414  return get_float();
415 }
416 
417 inline
418 AnyValue::operator float () const
419 {
420  return get_float();
421 }
422 
423 inline
424 AnyValue::operator long () const
425 {
426  return get_int();
427 }
428 
429 inline
430 AnyValue::operator int () const
431 {
432  return get_int();
433 }
434 
435 inline
436 AnyValue::operator short () const
437 {
438  return get_int();
439 }
440 
441 #ifdef CompilerIs64bitReady
442 inline
443 AnyValue::operator long long () const
444 {
445  return get_int8();
446 }
447 #endif /* CompilerIs64bitReady */
448 
449 
450 #endif /* AnyValue.hpp */
void changes()
Definition: AnyValue.hpp:349
bool is_empty_value() const
bool is_changed() const
Definition: AnyValue.hpp:341
unsigned get_dim() const
Definition: AnyValue.hpp:374
CategoryId get_categ() const
Definition: AnyValue.hpp:382
void definition(bool f)
Definition: AnyValue.hpp:391
bool operator!=(const AnyValue &av) const
virtual char * check_string(const char *s, int *pDim=NULL) const
Definition: AnyValue.hpp:23
void reset_changes()
Definition: AnyValue.hpp:358
Definition: Serial.hpp:22
static Compar compare(const AnyValue &av1, const AnyValue &av2)
virtual void serialize(OutputStream &ost) const
AnyValue(AtomType at=INT_4, unsigned count=1, CategoryId catid=UC_NONE)
void detach()
Definition: AnyValue.hpp:331
Definition: DmnDef.h:25
Definition: Serial.hpp:69
AnyValue & operator=(const AnyValue &av)
virtual void deserialize(InputStream &ist)
void new_def(AtomType at, unsigned count=1, CategoryId catid=UC_NONE)
virtual char * from_string(const char *s)
AtomType get_type() const
Definition: AnyValue.hpp:366
virtual bool operator==(const AnyValue &av) const
void attach(void *val_ptr)
Definition: TextRepres.hpp:19
virtual char * to_string() const
virtual ~AnyValue()
void empty_value()
void print_self(FILE *stream=NULL, const char *szPre=NULL, const char *szPost=NULL) const
Definition: Serial.hpp:131