Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Variant.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "Variant.h"
14 
15 
16 using namespace kome::objects;
17 
18 
19 #include <crtdbg.h>
20 #ifdef _DEBUG
21  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
22  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
23 #endif // _DEBUG
24 
25 
26 
27 // constructor
29  type = UNKNOWN;
30  prim.pt = NULL;
31  prim.dblVal = 0.0;
32 
33  arr = NULL;
34 }
35 
36 // copy constructor
37 Variant::Variant( const Variant& var ) {
38  // copy member
39  prim = var.prim;
40  type = var.type;
41  arr = NULL;
42 
43  if( var.arr != NULL && var.prim.intVal > 0 ) {
44  int size = var.prim.intVal;
45  if( var.type == ARRAY ) {
46  arr = new Variant[ size ];
47  for( int i = 0; i < size; i++ ) {
48  ( (Variant*)arr )[ i ] = ( (Variant*)var.arr )[ i ];
49  }
50  }
51  else if( var.type == STRING ) {
52  arr = new char[ size ];
53  memcpy( arr, var.arr, size );
54  }
55 
56  prim.intVal = size;
57  }
58 }
59 
60 // destructor
62  deleteArray();
63 }
64 
65 // substitution operator
67  // copy member
68  prim = other.prim;
69  type = other.type;
70  arr = NULL;
71 
72  if( other.arr != NULL && other.prim.intVal > 0 ) {
73  int size = other.prim.intVal;
74  if( other.type == ARRAY ) {
75  arr = new Variant[ size ];
76  for( int i = 0; i < size; i++ ) {
77  ( (Variant*)arr )[ i ] = ( (Variant*)other.arr )[ i ];
78  }
79  }
80  else if( other.type == STRING ) {
81  arr = new char[ size ];
82  memcpy( arr, other.arr, size );
83  }
84 
85  prim.intVal = size;
86  }
87 
88  return *this;
89 }
90 
91 // set string
92 void Variant::setString( const char* s ) {
93  // delete old array
94  deleteArray();
95 
96  // check parameter
97  if( s == NULL ) {
98  type = UNKNOWN;
99  return;
100  }
101 
102  // set type
103  type = STRING;
104 
105  // create array
106  int size = strlen( s ) + 1;
107  arr = new char[ size ];
108  prim.intVal = size;
109 
110  // copy
111  memcpy( arr, s, size );
112 }
113 
114 // get string
116  return (char*)arr;
117 }
118 
119 // create array
120 Variant* Variant::createArray( const unsigned int size ) {
121  // delete old array
122  deleteArray();
123 
124  // check parameter
125  if( size == 0 ) {
126  type = UNKNOWN;
127  return NULL;
128  }
129 
130  // type
131  type = ARRAY;
132 
133  // set size
134  prim.intVal = size;
135 
136  // create array
137  arr = new Variant[ size ];
138 
139  return getArray();
140 }
141 
142 // get array
144  return (Variant*)arr;
145 }
146 
147 // get element of the array
148 Variant* Variant::getElement( const unsigned int index ) {
149  // check parameter
150  if( index >= (unsigned int)prim.intVal ) {
151  return NULL;
152  }
153 
154  return ( getArray() + index );
155 }
156 
157 // get array size
158 unsigned int Variant::getArraySize() {
159  // check the member
160  if( arr == NULL ) {
161  return 0;
162  }
163 
164  return (unsigned int)prim.intVal;
165 }
166 
167 // delete array
169  // check array
170  if( arr == NULL ) {
171  return;
172  }
173 
174  // delete
175  delete[] arr;
176  arr = NULL;
177 }
Variant * getArray()
gets array
Definition: Variant.cpp:143
Variant * getElement(const unsigned int index)
gets element of the array
Definition: Variant.cpp:148
Variant()
constructor
Definition: Variant.cpp:28
interfaces of Variant class
Variant * createArray(const unsigned int size)
creates array
Definition: Variant.cpp:120
void deleteArray()
deletes array
Definition: Variant.cpp:168
Variant & operator=(const Variant &other)
substitution operator
Definition: Variant.cpp:66
#define NULL
Definition: CoreMacros.h:18
class that treats data of various types
Definition: Variant.h:26
void setString(const char *s)
sets character string
Definition: Variant.cpp:92
virtual ~Variant()
destructor
Definition: Variant.cpp:61
unsigned int getArraySize()
gets array size
Definition: Variant.cpp:158
char * getString()
gets character string
Definition: Variant.cpp:115