blob: 39aa6d53f38e9d8d08bf257261b7564fd781b380 (
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/* Swinder - Portable library for spreadsheet
Copyright (C) 2003 Ariya Hidayat <ariya@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/
#include "value.h"
#include "ustring.h"
#include <iostream>
namespace Swinder
{
// helper class for Value
class ValueData
{
public:
Value::Type type;
// someday move to use union to reduce memory consumption
bool b;
int i;
double f;
UString s;
// create empty data
ValueData(){
count = 0;
b = false;
i = 0;
f = 0.0;
s = UString::null;
type = Value::Empty;
ref();
};
// destroys data
~ValueData(){
if( this == s_null ) s_null = 0;
}
void ref(){ count++; }
// static empty data to be shared
static ValueData* null()
{ if( !s_null) s_null = new ValueData; else s_null->ref(); return s_null; }
// decrease reference count
void unref()
{ --count; if( !count ) delete this; }
// true if it's null (which is shared)
bool isNull(){ return this == s_null; }
unsigned count; // reference count
private:
static ValueData* s_null;
};
}
using namespace Swinder;
// to be shared between all empty value
ValueData* ValueData::s_null = 0;
// static things
Value ks_value_empty;
Value ks_error_div0;
Value ks_error_na;
Value ks_error_name;
Value ks_error_null;
Value ks_error_num;
Value ks_error_ref;
Value ks_error_value;
// create an empty value
Value::Value()
{
d = ValueData::null();
}
// destructor
Value::~Value()
{
d->unref();
}
// create value of certain type
Value::Value( Value::Type _type )
{
d = new ValueData;
d->type = _type;
}
// copy constructor
Value::Value( const Value& _value )
{
d = ValueData::null();
assign( _value );
}
// assignment operator
Value& Value::operator=( const Value& _value )
{
return assign( _value );
}
// create a boolean value
Value::Value( bool b )
{
d = ValueData::null();
setValue( b );
}
// create an integer value
Value::Value( int i )
{
d = ValueData::null();
setValue ( i );
}
// create a floating-point value
Value::Value( double f )
{
d = ValueData::null();
setValue( f );
}
// create a string value
Value::Value( const UString& s )
{
d = ValueData::null();
setValue( s );
}
// assign value from other
// shallow copy: only copy the data pointer
Value& Value::assign( const Value& _value )
{
d->unref();
d = _value.d;
d->ref();
return *this;
}
// return type of the value
Value::Type Value::type() const
{
return d ? d->type : Empty;
}
// set the value to boolean
void Value::setValue( bool b )
{
detach();
d->type = Boolean;
d->b = b;
}
// get the value as boolean
bool Value::asBoolean() const
{
bool result = false;
if( type() == Value::Boolean )
result = d->b;
return result;
}
// set the value to integer
void Value::setValue( int i )
{
detach();
d->type = Integer;
d->i = i;
}
// get the value as integer
int Value::asInteger() const
{
int result = 0;
if( type() == Value::Integer )
result = d->i;
if( type() == Value::Float )
result = static_cast<int>(d->f);
return result;
}
void Value::setValue( const Value& v )
{
assign( v );
}
// set the value as floating-point
void Value::setValue( double f )
{
detach();
d->type = Float;
d->f = f;
}
// get the value as floating-point
double Value::asFloat() const
{
double result = 0.0;
if( type() == Value::Float )
result = d->f;
if( type() == Value::Integer )
result = static_cast<double>(d->i);
return result;
}
// set the value as string
void Value::setValue( const UString& s )
{
detach();
d->type = String;
d->s = s;
}
// get the value as string
UString Value::asString() const
{
UString result;
if( type() == Value::String )
result = d->s;
return result;
}
// set error message
void Value::setError( const UString& msg )
{
detach();
d->type = Error;
d->s = msg;
}
// get error message
UString Value::errorMessage() const
{
UString result;
if( type() == Value::Error )
result = d->s;
return result;
}
// reference to empty value
const Value& Value::empty()
{
return ks_value_empty;
}
// reference to #DIV/0! error
const Value& Value::errorDIV0()
{
if( !ks_error_div0.isError() )
ks_error_div0.setError( UString("#DIV/0!") );
return ks_error_div0;
}
// reference to #N/A error
const Value& Value::errorNA()
{
if( !ks_error_na.isError() )
ks_error_na.setError( UString("#N/A") );
return ks_error_na;
}
// reference to #NAME? error
const Value& Value::errorNAME()
{
if( !ks_error_name.isError() )
ks_error_name.setError( UString("#NAME?") );
return ks_error_name;
}
// reference to #NUM! error
const Value& Value::errorNUM()
{
if( !ks_error_num.isError() )
ks_error_num.setError( UString("#NUM!") );
return ks_error_num;
}
// reference to #NULL! error
const Value& Value::errorNULL()
{
if( !ks_error_null.isError() )
ks_error_null.setError( UString("#NULL!") );
return ks_error_null;
}
// reference to #REF! error
const Value& Value::errorREF()
{
if( !ks_error_ref.isError() )
ks_error_ref.setError( UString("#REF!") );
return ks_error_ref;
}
// reference to #VALUE! error
const Value& Value::errorVALUE()
{
if( !ks_error_value.isError() )
ks_error_value.setError( UString("#VALUE!") );
return ks_error_value;
}
// detach, create deep copy of ValueData
void Value::detach()
{
if( d->isNull() || ( d->count > 1 ) )
{
ValueData* n;
n = new ValueData;
n->type = d->type;
switch( n->type )
{
case Empty: break;
case Boolean: n->b = d->b; break;
case Integer: n->i = d->i; break;
case Float: n->f = d->f; break;
case String: n->s = d->s; break;
case Error: n->s = d->s; break;
default: break;
}
d->unref();
d = n;
}
}
std::ostream& Swinder::operator<<( std::ostream& s, Swinder::Value value )
{
switch( value.type() )
{
case Value::Empty:
s << "Empty";
break;
case Value::Boolean:
s << "Boolean: " << (value.asBoolean()?"True":"False");
break;
case Value::Integer:
s << "Integer: " << value.asInteger();
break;
case Value::Float:
s << "Float: " << value.asFloat();
break;
case Value::String:
s << "String: " << value.asString().ascii();
break;
case Value::Error:
s << "Error: " << value.errorMessage().ascii();
break;
default:
break;
};
return s;
}
|