~ubuntu-branches/ubuntu/wily/afnix/wily

« back to all changes in this revision

Viewing changes to src/lib/std/shl/Boolean.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anibal Monsalve Salazar
  • Date: 2011-03-16 21:31:18 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110316213118-gk4k3ez3e5d2huna
Tags: 2.0.0-1
* QA upload.
* New upstream release
* Debian source format is 3.0 (quilt)
* Fix debhelper-but-no-misc-depends
* Fix ancient-standards-version
* Fix package-contains-linda-override
* debhelper compatibility is 7
* Fix dh-clean-k-is-deprecated

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ---------------------------------------------------------------------------
 
2
// - Boolean.cpp                                                             -
 
3
// - standard object library - boolean class implementation                  -
 
4
// ---------------------------------------------------------------------------
 
5
// - This program is free software;  you can redistribute it  and/or  modify -
 
6
// - it provided that this copyright notice is kept intact.                  -
 
7
// -                                                                         -
 
8
// - This program  is  distributed in  the hope  that it will be useful, but -
 
9
// - without  any  warranty;  without  even   the   implied    warranty   of -
 
10
// - merchantability or fitness for a particular purpose.  In no event shall -
 
11
// - the copyright holder be liable for any  direct, indirect, incidental or -
 
12
// - special damages arising in any way out of the use of this software.     -
 
13
// ---------------------------------------------------------------------------
 
14
// - copyright (c) 1999-2011 amaury darsch                                   -
 
15
// ---------------------------------------------------------------------------
 
16
 
 
17
#include "Stdsid.hxx"
 
18
#include "Vector.hpp"
 
19
#include "Boolean.hpp"
 
20
#include "Runnable.hpp"
 
21
#include "QuarkZone.hpp"
 
22
#include "Exception.hpp"
 
23
#include "InputStream.hpp"
 
24
#include "OutputStream.hpp"
 
25
 
 
26
namespace afnix {
 
27
 
 
28
  // -------------------------------------------------------------------------
 
29
  // - class section                                                         -
 
30
  // -------------------------------------------------------------------------
 
31
 
 
32
  // create a new boolean - the initial value is false
 
33
  
 
34
  Boolean::Boolean (void) {
 
35
    d_value = false;
 
36
  }
 
37
  
 
38
  // create a boolean from a native value
 
39
  
 
40
  Boolean::Boolean (const bool value) {
 
41
    d_value = value;
 
42
  }
 
43
  
 
44
  // create a boolean from a string
 
45
 
 
46
  Boolean::Boolean (const String& value) {
 
47
    if (value == "false") {
 
48
      d_value = false;
 
49
    } else {
 
50
      if (value == "true") {
 
51
        d_value = true;
 
52
      } else {
 
53
        throw Exception ("literal-error","illegal boolean value", value);
 
54
      }
 
55
    }
 
56
  }
 
57
 
 
58
  // copy constructor for this boolean
 
59
 
 
60
  Boolean::Boolean (const Boolean& that) {
 
61
    that.rdlock ();
 
62
    try {
 
63
      d_value = that.d_value;
 
64
      that.unlock ();
 
65
    } catch (...) {
 
66
      that.unlock ();
 
67
      throw;
 
68
    }
 
69
  }
 
70
 
 
71
  // return the class name
 
72
 
 
73
  String Boolean::repr (void) const {
 
74
    return "Boolean";
 
75
  }
 
76
 
 
77
  // return a clone of this object
 
78
 
 
79
  Object* Boolean::clone (void) const {
 
80
    return new Boolean (*this);
 
81
  }
 
82
 
 
83
  // return a literal representation of this boolean
 
84
  
 
85
  String Boolean::toliteral (void) const {
 
86
    rdlock ();
 
87
    try {
 
88
      String result = tostring ();
 
89
      unlock ();
 
90
      return result;
 
91
    } catch (...) {
 
92
      unlock ();
 
93
      throw;
 
94
    }
 
95
  }
 
96
 
 
97
  // return a string representation of this boolean
 
98
  
 
99
  String Boolean::tostring (void) const {
 
100
    rdlock ();
 
101
    try {
 
102
      String result = d_value ? "true" : "false";
 
103
      unlock ();
 
104
      return result;
 
105
    } catch (...) {
 
106
      unlock ();
 
107
      throw;
 
108
    }
 
109
  }
 
110
 
 
111
  // return the boolean serial code
 
112
 
 
113
  t_byte Boolean::serialid (void) const {
 
114
    return SERIAL_BOOL_ID;
 
115
  }
 
116
 
 
117
  // serialize this boolean
 
118
 
 
119
  void Boolean::wrstream (OutputStream& os) const {
 
120
    rdlock ();
 
121
    try {
 
122
      char c = d_value ? 0x01 : nilc;
 
123
      os.write (c);
 
124
      unlock ();
 
125
    } catch (...) {
 
126
      unlock ();
 
127
      throw;
 
128
    }
 
129
  }
 
130
 
 
131
  // deserialize this boolean
 
132
 
 
133
  void Boolean::rdstream (InputStream& is) {
 
134
    wrlock ();
 
135
    try {
 
136
      char c = is.read ();
 
137
      d_value = (c == nilc) ? false : true;
 
138
      unlock ();
 
139
    } catch (...) {
 
140
      unlock ();
 
141
      throw;
 
142
    }
 
143
  }
 
144
 
 
145
  // return this boolean value
 
146
 
 
147
  bool Boolean::tobool (void) const {
 
148
    rdlock ();
 
149
    try {
 
150
      bool result = d_value;
 
151
      unlock ();
 
152
      return result;
 
153
    } catch (...) {
 
154
      unlock ();
 
155
      throw;
 
156
    }
 
157
  }
 
158
 
 
159
  // assign a boolean with a native value
 
160
 
 
161
  Boolean& Boolean::operator = (const bool value) {
 
162
    wrlock ();
 
163
    try {
 
164
      d_value = value;
 
165
      unlock ();
 
166
      return *this;
 
167
    } catch (...) {
 
168
      unlock ();
 
169
      throw;
 
170
    }
 
171
  }
 
172
  
 
173
  // assign a boolean with a boolean
 
174
  
 
175
  Boolean& Boolean::operator = (const Boolean& that) {
 
176
    // check for self assignation
 
177
    if (this == &that) return *this;
 
178
    // lock and assign
 
179
    wrlock ();
 
180
    that.rdlock ();
 
181
    try {
 
182
      d_value = that.d_value;
 
183
      unlock ();
 
184
      that.unlock ();
 
185
      return *this;
 
186
    } catch (...) {
 
187
      unlock ();
 
188
      that.unlock ();
 
189
      throw;
 
190
    }
 
191
  }
 
192
  
 
193
  // compare a boolean with a native value
 
194
  
 
195
  bool Boolean::operator == (const bool value) const {
 
196
    rdlock ();
 
197
    try {
 
198
      bool result = (d_value == value);
 
199
      unlock ();
 
200
      return result;
 
201
    } catch (...) {
 
202
      unlock ();
 
203
      throw;
 
204
    }
 
205
  }
 
206
  
 
207
  // compare two boolean
 
208
  
 
209
  bool Boolean::operator == (const Boolean& value) const {
 
210
    rdlock ();
 
211
    value.rdlock ();
 
212
    try {
 
213
      bool result = (d_value == value.d_value);
 
214
      unlock ();
 
215
      value.unlock ();
 
216
      return result;
 
217
    } catch (...) {
 
218
      unlock ();
 
219
      value.unlock ();
 
220
      throw;
 
221
    }
 
222
  }
 
223
  
 
224
  // compare a boolean with a native value
 
225
  
 
226
  bool Boolean::operator != (const bool value) const {
 
227
    rdlock ();
 
228
    try {
 
229
      bool result = (d_value != value);
 
230
      unlock ();
 
231
      return result;
 
232
    } catch (...) {
 
233
      unlock ();
 
234
      throw;
 
235
    }
 
236
  }
 
237
  
 
238
  // compare two boolean
 
239
  
 
240
  bool Boolean::operator != (const Boolean& value) const {
 
241
    rdlock ();
 
242
    value.rdlock ();
 
243
    try {
 
244
      bool result = (d_value != value.d_value);
 
245
      unlock ();
 
246
      value.unlock ();
 
247
      return result;
 
248
    } catch (...) {
 
249
      unlock ();
 
250
      value.unlock ();
 
251
      throw;
 
252
    }
 
253
  }
 
254
 
 
255
  // -------------------------------------------------------------------------
 
256
  // - object section                                                        -
 
257
  // -------------------------------------------------------------------------
 
258
 
 
259
  // the quark zone
 
260
  static const long QUARK_ZONE_LENGTH = 2;
 
261
  static QuarkZone  zone (QUARK_ZONE_LENGTH);
 
262
 
 
263
  // the object supported quarks
 
264
  static const long QUARK_EQL = zone.intern ("==");
 
265
  static const long QUARK_NEQ = zone.intern ("!=");
 
266
 
 
267
  // evaluate an object to a boolean value
 
268
 
 
269
  bool Boolean::evalto (Runnable* robj, Nameset* nset, Object* object) {
 
270
    Object* obj = (object == nilp) ? nilp : object->eval (robj, nset);
 
271
    Boolean* val = dynamic_cast <Boolean*> (obj);
 
272
    if (val == nilp) throw Exception ("type-error", "nil object to evaluate");
 
273
    return val->tobool ();
 
274
  }
 
275
 
 
276
  // create a new object in a generic way
 
277
 
 
278
  Object* Boolean::mknew (Vector* argv) {
 
279
    if ((argv == nilp) || (argv->length () == 0)) return new Boolean;
 
280
    if (argv->length () != 1) 
 
281
      throw Exception ("argument-error", 
 
282
                       "too many argument with boolean constructor");
 
283
    // try to map the boolean argument
 
284
    Object* obj = argv->get (0);
 
285
    if (obj == nilp) return new Boolean;
 
286
 
 
287
    // try a boolean object
 
288
    Boolean* bval = dynamic_cast <Boolean*> (obj);
 
289
    if (bval != nilp) return new Boolean (*bval);
 
290
 
 
291
    // try a string object
 
292
    String* sval = dynamic_cast <String*> (obj);
 
293
    if (sval != nilp) return new Boolean (*sval);
 
294
 
 
295
    // illegal object
 
296
    throw Exception ("type-error", "illegal object with boolean constructor",
 
297
                     obj->repr ());
 
298
  }
 
299
 
 
300
  // return true if the given quark is defined
 
301
 
 
302
  bool Boolean::isquark (const long quark, const bool hflg) const {
 
303
    rdlock ();
 
304
    if (zone.exists (quark) == true) {
 
305
      unlock ();
 
306
      return true;
 
307
    }      
 
308
    bool result = hflg ? Literal::isquark (quark, hflg) : false;
 
309
    unlock ();
 
310
    return result;
 
311
  }
 
312
 
 
313
  // operate this object with another object
 
314
 
 
315
  Object* Boolean::oper (t_oper type, Object* object) {
 
316
    Boolean* bobj = dynamic_cast <Boolean*> (object);
 
317
    switch (type) {
 
318
    case Object::EQL:
 
319
      if (bobj != nilp) return new Boolean (*this == *bobj);
 
320
      break;
 
321
    case Object::NEQ:
 
322
      if (bobj != nilp) return new Boolean (*this != *bobj);
 
323
      break;
 
324
    default:
 
325
      throw Exception ("operator-error", "unsupported boolean operator");
 
326
    }
 
327
    throw Exception ("type-error", "invalid operand with boolean",
 
328
                     Object::repr (object));
 
329
  }
 
330
 
 
331
  // set an object to this boolean
 
332
 
 
333
  Object* Boolean::vdef (Runnable* robj, Nameset* nset, Object* object) {
 
334
    wrlock ();
 
335
    try {
 
336
      Boolean* bobj = dynamic_cast <Boolean*> (object);
 
337
      if (bobj != nilp) {
 
338
        *this = *bobj;
 
339
        robj->post (this);
 
340
        unlock ();
 
341
        return this;
 
342
      }
 
343
      throw Exception ("type-error", "invalid object with boolean vdef",
 
344
                       Object::repr (object));
 
345
    } catch (...) {
 
346
      unlock ();
 
347
      throw;
 
348
    }
 
349
  }
 
350
 
 
351
  // apply this object with a set of arguments and a quark
 
352
 
 
353
  Object* Boolean::apply (Runnable* robj, Nameset* nset, const long quark,
 
354
                          Vector* argv) {
 
355
    // get the number of arguments
 
356
    long argc = (argv == nilp) ? 0 : argv->length ();
 
357
 
 
358
    // dispatch 1 argument
 
359
    if (argc == 1) {
 
360
      if (quark == QUARK_EQL) return oper (Object::EQL, argv->get (0));
 
361
      if (quark == QUARK_NEQ) return oper (Object::NEQ, argv->get (0));
 
362
    }
 
363
    // call the literal method
 
364
    return Literal::apply (robj, nset, quark, argv);
 
365
  }
 
366
}