1
// ---------------------------------------------------------------------------
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. -
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
// ---------------------------------------------------------------------------
19
#include "Boolean.hpp"
20
#include "Runnable.hpp"
21
#include "QuarkZone.hpp"
22
#include "Exception.hpp"
23
#include "InputStream.hpp"
24
#include "OutputStream.hpp"
28
// -------------------------------------------------------------------------
30
// -------------------------------------------------------------------------
32
// create a new boolean - the initial value is false
34
Boolean::Boolean (void) {
38
// create a boolean from a native value
40
Boolean::Boolean (const bool value) {
44
// create a boolean from a string
46
Boolean::Boolean (const String& value) {
47
if (value == "false") {
50
if (value == "true") {
53
throw Exception ("literal-error","illegal boolean value", value);
58
// copy constructor for this boolean
60
Boolean::Boolean (const Boolean& that) {
63
d_value = that.d_value;
71
// return the class name
73
String Boolean::repr (void) const {
77
// return a clone of this object
79
Object* Boolean::clone (void) const {
80
return new Boolean (*this);
83
// return a literal representation of this boolean
85
String Boolean::toliteral (void) const {
88
String result = tostring ();
97
// return a string representation of this boolean
99
String Boolean::tostring (void) const {
102
String result = d_value ? "true" : "false";
111
// return the boolean serial code
113
t_byte Boolean::serialid (void) const {
114
return SERIAL_BOOL_ID;
117
// serialize this boolean
119
void Boolean::wrstream (OutputStream& os) const {
122
char c = d_value ? 0x01 : nilc;
131
// deserialize this boolean
133
void Boolean::rdstream (InputStream& is) {
137
d_value = (c == nilc) ? false : true;
145
// return this boolean value
147
bool Boolean::tobool (void) const {
150
bool result = d_value;
159
// assign a boolean with a native value
161
Boolean& Boolean::operator = (const bool value) {
173
// assign a boolean with a boolean
175
Boolean& Boolean::operator = (const Boolean& that) {
176
// check for self assignation
177
if (this == &that) return *this;
182
d_value = that.d_value;
193
// compare a boolean with a native value
195
bool Boolean::operator == (const bool value) const {
198
bool result = (d_value == value);
207
// compare two boolean
209
bool Boolean::operator == (const Boolean& value) const {
213
bool result = (d_value == value.d_value);
224
// compare a boolean with a native value
226
bool Boolean::operator != (const bool value) const {
229
bool result = (d_value != value);
238
// compare two boolean
240
bool Boolean::operator != (const Boolean& value) const {
244
bool result = (d_value != value.d_value);
255
// -------------------------------------------------------------------------
256
// - object section -
257
// -------------------------------------------------------------------------
260
static const long QUARK_ZONE_LENGTH = 2;
261
static QuarkZone zone (QUARK_ZONE_LENGTH);
263
// the object supported quarks
264
static const long QUARK_EQL = zone.intern ("==");
265
static const long QUARK_NEQ = zone.intern ("!=");
267
// evaluate an object to a boolean value
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 ();
276
// create a new object in a generic way
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;
287
// try a boolean object
288
Boolean* bval = dynamic_cast <Boolean*> (obj);
289
if (bval != nilp) return new Boolean (*bval);
291
// try a string object
292
String* sval = dynamic_cast <String*> (obj);
293
if (sval != nilp) return new Boolean (*sval);
296
throw Exception ("type-error", "illegal object with boolean constructor",
300
// return true if the given quark is defined
302
bool Boolean::isquark (const long quark, const bool hflg) const {
304
if (zone.exists (quark) == true) {
308
bool result = hflg ? Literal::isquark (quark, hflg) : false;
313
// operate this object with another object
315
Object* Boolean::oper (t_oper type, Object* object) {
316
Boolean* bobj = dynamic_cast <Boolean*> (object);
319
if (bobj != nilp) return new Boolean (*this == *bobj);
322
if (bobj != nilp) return new Boolean (*this != *bobj);
325
throw Exception ("operator-error", "unsupported boolean operator");
327
throw Exception ("type-error", "invalid operand with boolean",
328
Object::repr (object));
331
// set an object to this boolean
333
Object* Boolean::vdef (Runnable* robj, Nameset* nset, Object* object) {
336
Boolean* bobj = dynamic_cast <Boolean*> (object);
343
throw Exception ("type-error", "invalid object with boolean vdef",
344
Object::repr (object));
351
// apply this object with a set of arguments and a quark
353
Object* Boolean::apply (Runnable* robj, Nameset* nset, const long quark,
355
// get the number of arguments
356
long argc = (argv == nilp) ? 0 : argv->length ();
358
// dispatch 1 argument
360
if (quark == QUARK_EQL) return oper (Object::EQL, argv->get (0));
361
if (quark == QUARK_NEQ) return oper (Object::NEQ, argv->get (0));
363
// call the literal method
364
return Literal::apply (robj, nset, quark, argv);