Logo Search packages:      
Sourcecode: afnix version File versions

Byte.cpp

// ---------------------------------------------------------------------------
// - Byte.cpp                                                                -
// - standard object library - byte class implementation                     -
// ---------------------------------------------------------------------------
// - This program is free software;  you can redistribute it  and/or  modify -
// - it provided that this copyright notice is kept intact.                  -
// -                                                                         -
// - This program  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.  In no event shall -
// - the copyright holder be liable for any  direct, indirect, incidental or -
// - special damages arising in any way out of the use of this software.     -
// ---------------------------------------------------------------------------
// - copyright (c) 1999-2007 amaury darsch                                   -
// ---------------------------------------------------------------------------

#include "Byte.hpp"
#include "Ascii.hpp"
#include "Input.hpp"
#include "Output.hpp"
#include "Stdsid.hxx"
#include "Vector.hpp"
#include "Recycle.hpp"
#include "Integer.hpp"
#include "Boolean.hpp"
#include "Runnable.hpp"
#include "QuarkZone.hpp"
#include "Character.hpp"
#include "Exception.hpp"

namespace afnix {

  // -------------------------------------------------------------------------
  // - memory section                                                        -
  // -------------------------------------------------------------------------

  // the byte recycler
  static Recycle recycler;

  // allocate a new byte
  void* Byte::operator new (const t_size size) {
    return recycler.pop (size);
  }

  // delete a byte
  void Byte::operator delete (void* handle) {
    recycler.push (handle);
  }

  // -------------------------------------------------------------------------
  // - class section                                                         -
  // -------------------------------------------------------------------------

  // create a new default byte

00056   Byte::Byte (void) {
    d_value = nilc;
  }

  // create a new byte from a character

00062   Byte::Byte (const char value) {
    d_value = value;
  }

  // create a new byte from a native byte
  
00068   Byte::Byte (const t_byte value) {
    d_value = value;
  }
  
  // copy constructor for this byte class

00074   Byte::Byte (const Byte& that) {
    that.rdlock ();
    d_value = that.d_value;
    that.unlock ();
  }

  // return the class name

00082   String Byte::repr (void) const {
    return "Byte";
  }

  // return a clone of this byte

00088   Object* Byte::clone (void) const {
    return new Byte (*this);
  }

  // return the byte serial code

00094   t_byte Byte::serialid (void) const {
    return SERIAL_BYTE_ID;
  }

  // serialize this byte

00100   void Byte::wrstream (Output& os) const {
    rdlock ();
    os.write ((char) d_value);
    unlock ();
  }

  // deserialize this byte

00108   void Byte::rdstream (Input& is) {
    wrlock ();
    d_value = (t_byte) is.read ();
    unlock ();
  }

  // return a native byte

00116   t_byte Byte::tobyte (void) const {
    rdlock ();
    t_byte result = d_value;
    unlock ();
    return result;
  }

  // return a native quad

00125   t_quad Byte::toquad (void) const {
    rdlock ();
    t_quad result = d_value;
    unlock ();
    return result;
  }

  // set an byte with a native value

00134   Byte& Byte::operator = (const char value) {
    wrlock ();
    d_value = value;
    unlock ();
    return *this;
  }

  // set an byte with a byte

00143   Byte& Byte::operator = (const Byte& value) {
    wrlock ();
    value.rdlock ();
    d_value = value.d_value;
    value.unlock ();
    unlock ();
    return *this;
  }
  
  // add a byte with a native value

00154   Byte Byte::operator + (const long value) {
    wrlock ();
    char result = d_value + value;
    unlock ();
    return Byte (result);
  }
  
  // sub a byte with a native value

00163   Byte Byte::operator - (const long value) {
    wrlock ();
    char result = d_value - value;
    unlock ();
    return Byte (result);
  }
  
  // add a native value to this byte
  
00172   Byte& Byte::operator += (const long value) {
    wrlock ();
    d_value = d_value + value;
    unlock ();
    return *this;
  }
  
  // sub a native value to this byte

00181   Byte& Byte::operator -= (const long value) {
    wrlock ();
    d_value = d_value - value;
    unlock ();
    return *this;
  }

  // prefix add one to the byte

00190   Byte& Byte::operator ++ (void) {
    wrlock ();
    ++d_value;
    unlock ();
    return *this;
  }

  // postfix add one to the byte

00199   Byte Byte::operator ++ (int) {
    wrlock ();
    Byte result = *this;
    d_value++;
    unlock ();
    return result;
  }

  // prefix sub one to the byte

00209   Byte& Byte::operator -- (void) {
    wrlock ();
    --d_value;
    unlock ();
    return *this;
  }

  // postfix sub one to the byte

00218   Byte Byte::operator -- (int) {
    wrlock ();
    Byte result = *this;
    d_value--;
    unlock ();
    return result;
  }

  // compare an byte with a native value
  
00228   bool Byte::operator == (const char value) const {
    rdlock ();
    bool result = (d_value == value);
    unlock ();
    return result;
  }
  
00235   bool Byte::operator != (const char value) const {
    rdlock ();
    bool result = (d_value != value);
    unlock ();
    return result;
  }
  
  // compare two byte bytes
  
00244   bool Byte::operator == (const Byte& value) const {
    rdlock ();
    value.rdlock ();
    bool result = (d_value == value.d_value);
    value.unlock ();
    unlock ();
    return result;
  }
  
00253   bool Byte::operator != (const Byte& value) const {
    rdlock ();
    value.rdlock ();
    bool result = (d_value != value.d_value);
    value.unlock ();
    unlock ();
    return result;
  }
  
00262   bool Byte::operator < (const Byte& value) const {
    rdlock ();
    value.rdlock ();
    bool result = (d_value < value.d_value);
    value.unlock ();
    unlock ();
    return result;
  }
  
00271   bool Byte::operator <= (const Byte& value) const {
    rdlock ();
    value.rdlock ();
    bool result = (d_value <= value.d_value);
    value.unlock ();
    unlock ();
    return result;
  }
  
00280   bool Byte::operator > (const Byte& value) const {
    rdlock ();
    value.rdlock ();
    bool result = (d_value > value.d_value);
    value.unlock ();
    unlock ();
    return result;
  }
  
00289   bool Byte::operator >= (const Byte& value) const {
    rdlock ();
    value.rdlock ();
    bool result = (d_value >= value.d_value);
    value.unlock ();
    unlock ();
    return result;
  }
  
  // return true if the byte is an eof

00300   bool Byte::iseof (void) const {
    rdlock ();
    bool result = (d_value == eofc);
    unlock ();
    return result;
  }

  // return true if the byte is nil

00309   bool Byte::isnil (void) const {
    rdlock ();
    bool result = (d_value == nilc);
    unlock ();
    return result;
  }

  // -------------------------------------------------------------------------
  // - object section                                                        -
  // -------------------------------------------------------------------------

  // the quark zone
  static const long QUARK_ZONE_LENGTH = 16;
  static QuarkZone  zone (QUARK_ZONE_LENGTH);

  // the object supported quarks
  static const long QUARK_OPP    = zone.intern ("++");
  static const long QUARK_OMM    = zone.intern ("--");
  static const long QUARK_ADD    = zone.intern ("+");
  static const long QUARK_SUB    = zone.intern ("-");
  static const long QUARK_EQL    = zone.intern ("==");
  static const long QUARK_NEQ    = zone.intern ("!=");
  static const long QUARK_LTH    = zone.intern ("<");
  static const long QUARK_LEQ    = zone.intern ("<=");
  static const long QUARK_GTH    = zone.intern (">");
  static const long QUARK_GEQ    = zone.intern (">=");
  static const long QUARK_AEQ    = zone.intern ("+=");
  static const long QUARK_SEQ    = zone.intern ("-=");
  static const long QUARK_EOFP   = zone.intern ("eof-p");
  static const long QUARK_NILP   = zone.intern ("nil-p");
  static const long QUARK_TOINTG = zone.intern ("to-integer");
  static const long QUARK_TOCHAR = zone.intern ("to-character");

  // create a new object in a generic way

00344   Object* Byte::mknew (Vector* argv) {
    if ((argv == nilp) || (argv->length () == 0)) return new Byte;
    if (argv->length () != 1) 
      throw Exception ("argument-error", 
                   "too many argument with byte constructor");
    // try to map the byte argument
    Object* obj = argv->get (0);
    if (obj == nilp) return new Byte;

    // try an integer object
    Integer* ival = dynamic_cast <Integer*> (obj);
    if (ival != nilp) return new Byte ((t_byte) ival->tointeger ());

    // try a byte object
    Byte* cval = dynamic_cast <Byte*> (obj);
    if (cval != nilp) return new Byte (*cval);

    // illegal object
    throw Exception ("type-error", "illegal object with byte constructor",
                 obj->repr ());
  }

  // return true if the given quark is defined

00368   bool Byte::isquark (const long quark, const bool hflg) const {
    rdlock ();
    if (zone.exists (quark) == true) {
      unlock ();
      return true;
    }
    bool result = hflg ? Serial::isquark (quark, hflg) : false;
    unlock ();
    return result;
  }

  // operate this object with another object

00381   Object* Byte::oper (t_oper type, Object* object) {
    Integer* iobj = dynamic_cast <Integer*> (object);
    Byte*    cobj = dynamic_cast <Byte*>    (object);
    switch (type) {
    case Object::ADD:
      if (iobj != nilp) return new Byte (*this + iobj->tointeger ());
      break;
    case Object::SUB:
      if (iobj != nilp) return new Byte (*this - iobj->tointeger ());
      break;
    case Object::EQL:
      if (cobj != nilp) return new Boolean (*this == *cobj);
      break;
    case Object::NEQ:
      if (cobj != nilp) return new Boolean (*this != *cobj);
      break;
    case Object::GEQ:
      if (cobj != nilp) return new Boolean (*this >= *cobj);
      break;
    case Object::GTH:
      if (cobj != nilp) return new Boolean (*this > *cobj);
      break;
    case Object::LEQ:
      if (cobj != nilp) return new Boolean (*this <= *cobj);
      break;
    case Object::LTH:
      if (cobj != nilp) return new Boolean (*this < *cobj);
      break;
    default:
      throw Exception ("operator-error", "unsupported byte operator");
    }
    throw Exception ("type-error", "invalid operand with byte",
                 Object::repr (object));
  }

  // set an object to this byte

00418   Object* Byte::vdef (Runnable* robj, Nameset* nset, Object* object) {
    Byte* cobj = dynamic_cast <Byte*> (object);
    if (cobj != nilp) {
      *this = *cobj;
      robj->post (this);
      return this;
    }
    throw Exception ("type-error", "invalid object with byte vdef",
                 Object::repr (object));
  }

  // apply this object with a set of arguments and a quark

00431   Object* Byte::apply (Runnable* robj, Nameset* nset, const long quark,
                      Vector* argv) {
    // get the number of arguments
    long argc = (argv == nilp) ? 0 : argv->length ();

    // dispatch 0 argument
    if (argc == 0) {
      if (quark == QUARK_TOINTG) return new Integer   (tobyte ());
      if (quark == QUARK_TOCHAR) return new Character (toquad ());
      
      if (quark == QUARK_OPP) {
      ++(*this);
      robj->post (this);
      return this;
      }
      if (quark == QUARK_OMM) {
      --(*this);
      robj->post (this);
      return this;
      }
      if (quark == QUARK_EOFP)   return new Boolean (iseof ());
      if (quark == QUARK_NILP)   return new Boolean (isnil ());
    }
    // dispatch 1 argument
    if (argc == 1) {
      if (quark == QUARK_ADD) return oper (Object::ADD, argv->get (0));
      if (quark == QUARK_SUB) return oper (Object::SUB, argv->get (0));
      if (quark == QUARK_EQL) return oper (Object::EQL, argv->get (0));
      if (quark == QUARK_NEQ) return oper (Object::NEQ, argv->get (0));
      if (quark == QUARK_LTH) return oper (Object::LTH, argv->get (0));
      if (quark == QUARK_LEQ) return oper (Object::LEQ, argv->get (0));
      if (quark == QUARK_GTH) return oper (Object::GTH, argv->get (0));
      if (quark == QUARK_GEQ) return oper (Object::GEQ, argv->get (0));

      if (quark == QUARK_AEQ) {
      long val = argv->getint (0);
      *this += val;
      robj->post (this);
      return this;
      }
      if (quark == QUARK_SEQ) {
      long val = argv->getint (0);
      *this -= val;
      robj->post (this);
      return this;
      }
    }
    // call the serial method
    return Serial::apply (robj, nset, quark, argv);
  }
}

Generated by  Doxygen 1.6.0   Back to index