/builddir/build/BUILD/raul-0.4.0/raul/Atom.hpp

00001 /* This file is part of Raul.
00002  * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
00003  * 
00004  * Raul is free software; you can redistribute it and/or modify it under the
00005  * terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  * 
00009  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
00010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
00012  * 
00013  * You should have received a copy of the GNU General Public License along
00014  * with this program; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00016  */
00017 
00018 #ifndef RAUL_ATOM_HPP
00019 #define RAUL_ATOM_HPP
00020 
00021 #include <cstdlib>
00022 #include <cassert>
00023 #include <cstring>
00024 #include <string>
00025 #include <sstream>
00026 #include <iostream>
00027 
00028 #include CONFIG_H_PATH
00029 
00030 #define CUC(x) ((const unsigned char*)(x))
00031 
00032 namespace Raul {
00033 
00034 
00039 class Atom {
00040 public:
00041 
00042     enum Type {
00043         NIL,
00044         INT,
00045         FLOAT,
00046         BOOL,
00047         STRING,
00048         BLOB
00049     };
00050 
00051     Atom()                       : _type(NIL),    _blob_val(0)                     {}
00052     Atom(int32_t val)            : _type(INT),    _int_val(val)                    {}
00053     Atom(float val)              : _type(FLOAT),  _float_val(val)                  {}
00054     Atom(bool val)               : _type(BOOL),   _bool_val(val)                   {}
00055     Atom(const char* val)        : _type(STRING), _string_val(strdup(val))         {}
00056     Atom(const std::string& val) : _type(STRING), _string_val(strdup(val.c_str())) {}
00057     
00058     Atom(void* val) : _type(BLOB), _blob_size(sizeof(val)), _blob_val(malloc(_blob_size))
00059     { memcpy(_blob_val, val, sizeof(_blob_size)); }
00060 
00061     ~Atom() {
00062         if (_type == STRING)
00063             free(_string_val);
00064         else if (_type == BLOB)
00065             free(_blob_val);
00066     }
00067 
00068     // Gotta love C++ boilerplate:
00069     
00070     Atom(const Atom& copy)
00071         : _type(copy._type)
00072         , _blob_size(copy._blob_size)
00073     {
00074         switch (_type) {
00075         case NIL:    _blob_val   = 0;                        break;
00076         case INT:    _int_val    = copy._int_val;            break;
00077         case FLOAT:  _float_val  = copy._float_val;          break;
00078         case BOOL:   _bool_val   = copy._bool_val;           break;
00079         case STRING: _string_val = strdup(copy._string_val); break;
00080 
00081         case BLOB:   _blob_val = malloc(_blob_size);
00082                      memcpy(_blob_val, copy._blob_val, _blob_size);
00083                      break;
00084 
00085         default: break;
00086         }
00087     }
00088     
00089     Atom& operator=(const Atom& other) {
00090         if (_type == BLOB)
00091             free(_blob_val);
00092         else if (_type == STRING)
00093             free(_string_val);
00094 
00095         _type = other._type;
00096         _blob_size = other._blob_size;
00097 
00098         switch (_type) {
00099         case NIL:    _blob_val   = 0;                         break;
00100         case INT:    _int_val    = other._int_val;            break;
00101         case FLOAT:  _float_val  = other._float_val;          break;
00102         case BOOL:   _bool_val   = other._bool_val;           break;
00103         case STRING: _string_val = strdup(other._string_val); break;
00104 
00105         case BLOB:   _blob_val = malloc(_blob_size);
00106                      memcpy(_blob_val, other._blob_val, _blob_size);
00107                      break;
00108 
00109         default: break;
00110         }
00111         return *this;
00112     }
00113 
00117     Type type() const { return _type; }
00118 
00119     inline int32_t     get_int32()  const { assert(_type == INT);    return _int_val; }
00120     inline float       get_float()  const { assert(_type == FLOAT);  return _float_val; }
00121     inline bool        get_bool()   const { assert(_type == BOOL);   return _bool_val; }
00122     inline const char* get_string() const { assert(_type == STRING); return _string_val; }
00123     inline const void* get_blob()   const { assert(_type == BLOB);   return _blob_val; }
00124 
00125     inline operator bool() const { return (_type != NIL); }
00126 
00127 private:
00128     Type _type;
00129     
00130     size_t _blob_size; 
00131     
00132     union {
00133         int32_t _int_val;
00134         float   _float_val;
00135         bool    _bool_val;
00136         char*   _string_val;
00137         void*   _blob_val;
00138     };
00139 };
00140 
00141 
00142 } // namespace Raul
00143 
00144 #endif // RAUL_ATOM_HPP

Generated on Wed Apr 9 08:14:41 2008 for RAUL by  doxygen 1.5.1