RAUL  0.5.1
Atom.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
3  *
4  * Raul is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_ATOM_HPP
19 #define RAUL_ATOM_HPP
20 
21 #include <cstdlib>
22 #include <cassert>
23 #include <cstring>
24 #include <string>
25 #include <sstream>
26 #include <iostream>
27 
28 #include CONFIG_H_PATH
29 
30 #define CUC(x) ((const unsigned char*)(x))
31 
32 namespace Raul {
33 
34 
39 class Atom {
40 public:
41  enum Type {
42  NIL,
43  INT,
44  FLOAT,
45  BOOL,
46  STRING,
47  BLOB
48  };
49 
50  Atom() : _type(NIL), _blob_val(0) {}
51  Atom(int32_t val) : _type(INT), _int_val(val) {}
52  Atom(float val) : _type(FLOAT), _float_val(val) {}
53  Atom(bool val) : _type(BOOL), _bool_val(val) {}
54  Atom(const char* val) : _type(STRING), _string_val(strdup(val)) {}
55  Atom(const std::string& val) : _type(STRING), _string_val(strdup(val.c_str())) {}
56 
57  Atom(const char* type_uri, size_t size, void* val) : _type(BLOB) {
58  _blob_type_length = strlen(type_uri) + 1; // + 1 for \0
59  _blob_size = size;
60  _blob_val = malloc(_blob_type_length + _blob_size);
61  memcpy(_blob_val, type_uri, _blob_type_length);
62  memcpy((char*)_blob_val + _blob_type_length, val, size);
63  }
64 
65  ~Atom() {
66  if (_type == STRING)
67  free(_string_val);
68  else if (_type == BLOB)
69  free(_blob_val);
70  }
71 
72  // Gotta love C++ boilerplate:
73 
74  Atom(const Atom& copy)
75  : _type(copy._type)
76  {
77  switch (_type) {
78  case NIL: _blob_val = 0; break;
79  case INT: _int_val = copy._int_val; break;
80  case FLOAT: _float_val = copy._float_val; break;
81  case BOOL: _bool_val = copy._bool_val; break;
82  case STRING: _string_val = strdup(copy._string_val); break;
83  case BLOB: _blob_size = copy._blob_size;
84  _blob_type_length = copy._blob_type_length;
85  _blob_val = malloc(_blob_type_length + _blob_size);
86  memcpy(_blob_val, copy._blob_val, _blob_type_length + _blob_size);
87  break;
88  }
89  }
90 
91  Atom& operator=(const Atom& other) {
92  if (_type == BLOB)
93  free(_blob_val);
94  else if (_type == STRING)
95  free(_string_val);
96 
97  _type = other._type;
98 
99  switch (_type) {
100  case NIL: _blob_val = 0; break;
101  case INT: _int_val = other._int_val; break;
102  case FLOAT: _float_val = other._float_val; break;
103  case BOOL: _bool_val = other._bool_val; break;
104  case STRING: _string_val = strdup(other._string_val); break;
105  case BLOB: _blob_size = other._blob_size;
106  _blob_type_length = other._blob_type_length;
107  _blob_val = malloc(_blob_type_length + _blob_size);
108  memcpy(_blob_val, other._blob_val, _blob_type_length + _blob_size);
109  break;
110  }
111  return *this;
112  }
113 
114  inline bool operator==(const Atom& other) const {
115  if (_type == other.type()) {
116  switch (_type) {
117  case NIL: return true;
118  case INT: return _int_val == other._int_val;
119  case FLOAT: return _float_val == other._float_val;
120  case BOOL: return _bool_val == other._bool_val;
121  case STRING: return strcmp(_string_val, other._string_val) == 0;
122  case BLOB: return _blob_val == other._blob_val;
123  }
124  }
125  return false;
126  }
127 
128  inline bool operator!=(const Atom& other) const { return ! operator==(other); }
129 
130  inline bool operator<(const Atom& other) const {
131  if (_type == other.type()) {
132  switch (_type) {
133  case NIL: return true;
134  case INT: return _int_val < other._int_val;
135  case FLOAT: return _float_val < other._float_val;
136  case BOOL: return _bool_val < other._bool_val;
137  case STRING: return strcmp(_string_val, other._string_val) < 0;
138  case BLOB: return _blob_val < other._blob_val;
139  }
140  }
141  return _type < other.type();
142  }
143 
144  inline size_t data_size() const {
145  switch (_type) {
146  case NIL: return 0;
147  case INT: return sizeof(uint32_t);
148  case FLOAT: return sizeof(float);
149  case BOOL: return sizeof(bool);
150  case STRING: return strlen(_string_val);
151  case BLOB: return _blob_size;
152  }
153  return 0;
154  }
155 
156  inline bool is_valid() const { return (_type != NIL); }
157 
161  Type type() const { return _type; }
162 
163  inline int32_t get_int32() const { assert(_type == INT); return _int_val; }
164  inline float get_float() const { assert(_type == FLOAT); return _float_val; }
165  inline bool get_bool() const { assert(_type == BOOL); return _bool_val; }
166  inline const char* get_string() const { assert(_type == STRING); return _string_val; }
167 
168  inline const char* get_blob_type() const { assert(_type == BLOB); return (const char*)_blob_val; }
169  inline const void* get_blob() const { assert(_type == BLOB); return (const char*)_blob_val + _blob_type_length; }
170 
171 private:
172  Type _type;
173 
174  union {
175  int32_t _int_val;
176  float _float_val;
177  bool _bool_val;
178  char* _string_val;
179  struct {
180  size_t _blob_type_length; // length of type string (first part of buffer, inc. \0)
181  size_t _blob_size; // length of data after type string
182  void* _blob_val; // buffer
183  };
184  };
185 };
186 
187 
188 } // namespace Raul
189 
190 #endif // RAUL_ATOM_HPP
An OSC atom (fundamental data types OSC messages are composed of).
Definition: Atom.hpp:39
Type type() const
Type of this atom.
Definition: Atom.hpp:161