LOOS  v2.3.2
KernelValue.hpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2008, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 
23 
24 
25 #if !defined(LOOS_KERNELVALUE_HPP)
26 #define LOOS_KERNELVALUE_HPP
27 
28 #include <iostream>
29 #include <string>
30 #include <stdexcept>
31 #include <vector>
32 #include <cmath>
33 
34 
35 #include <exceptions.hpp>
36 #include <loos_defs.hpp>
37 
38 
39 namespace loos {
40 
41  namespace internal {
42 
43  const float FLT_THRESHOLD = 1e-10; // Threshold for floating equality
44 
46 
51  struct Value {
53  enum ValueType { NONE, STRING, INT, FLOAT }; // What type of data
54  // the union contains...
55 
56  ValueType type;
57  union {
58  std::string *str;
59  float flt;
60  long itg;
61  };
62 
64  void copy(const Value& v) {
65  switch(v.type) {
66  case STRING:
67  str = new std::string(*(v.str)); break;
68  case INT:
69  itg = v.itg; break;
70  case FLOAT:
71  flt = v.flt; break;
72  case NONE:
73  default:
74  ;
75  }
76  type = v.type;
77  }
78 
79 
80  Value() : type(NONE) { }
81  ~Value() { if (type == STRING) delete str; }
82 
83  Value(const Value& v) { copy(v); }
84 
85  const Value& operator=(const Value& v) { copy(v); return(*this); }
86 
87  Value(const std::string s) { setString(s); }
88  Value(const float f) { setFloat(f); }
89  Value(const long i) { setInt(i); }
90  Value(const int i) { setInt(i); }
91 
92  void setString(const std::string s) { str = new std::string(s); type = STRING; }
93  void setFloat(const float f) { flt = f; type = FLOAT; }
94  void setInt(const long i) { itg = i; type = INT; }
95 
97  std::string getString(void) const {
98  if (type != STRING)
99  throw(LOOSError("Expected a string value..."));
100  return(*str);
101  }
102 
104  float getFloat(void) const {
105  if (type != FLOAT)
106  throw(LOOSError("Expected a float value..."));
107  return(flt);
108  }
109 
111  long getInt(void) const {
112  if (type != INT)
113  throw(LOOSError("Expected an int value..."));
114  return(itg);
115  }
116 
118  friend std::ostream& operator<<(std::ostream& os, const Value& v) {
119  switch(v.type) {
120  case STRING:
121  os << "<VALUE TYPE='STRING'>" << *(v.str) << "</VALUE>";
122  break;
123  case FLOAT:
124  os << "<VALUE TYPE='FLOAT'>" << v.flt << "</VALUE>";
125  break;
126  case INT:
127  os << "<VALUE TYPE='INT'>" << v.itg << "</VALUE>";
128  break;
129  case NONE:
130  default:
131  os << "<VALUE TYPE='NONE'/>";
132  }
133  return(os);
134  }
135 
136  };
137 
138  // There really oughtta be a better way to handle this...
139 
140 
142 
153  int compare(const Value&, const Value&);
154 
155 
156  }
157 }
158 
159 
160 
161 
162 #endif
std::string getString(void) const
Retrieve data, throwing an error if the Value is of the incorrect type.
Definition: KernelValue.hpp:97
float getFloat(void) const
Retrieve data, throwing an error if the Value is of the incorrect type.
Generic LOOS exception.
Definition: exceptions.hpp:40
int compare(const Value &x, const Value &y)
Compare two Value objects, depending on their types.
Definition: KernelValue.cpp:31
friend std::ostream & operator<<(std::ostream &os, const Value &v)
Output in pseudo-XML.
Namespace for most things not already encapsulated within a class.
Value class for the LOOS Kernel (virtual machine)
Definition: KernelValue.hpp:51
ValueType
Type of data this Value contains...
Definition: KernelValue.hpp:53
void copy(const Value &v)
Make a copy (clone) of a Value.
Definition: KernelValue.hpp:64
long getInt(void) const
Retrieve data, throwing an error if the Value is of the incorrect type.