RxDock 0.1.0
A fast, versatile, and open-source program for docking ligands to proteins and nucleic acids
Loading...
Searching...
No Matches
Variant.h
1/***********************************************************************
2 * The rDock program was developed from 1998 - 2006 by the software team
3 * at RiboTargets (subsequently Vernalis (R&D) Ltd).
4 * In 2006, the software was licensed to the University of York for
5 * maintenance and distribution.
6 * In 2012, Vernalis and the University of York agreed to release the
7 * program as Open Source software.
8 * This version is licensed under GNU-LGPL version 3.0 with support from
9 * the University of Barcelona.
10 * http://rdock.sourceforge.net/
11 ***********************************************************************/
12
13// Variant class along the lines of the Visual Basic variant data type.
14// Handles int, double, const char*, string and std::vector<string> types
15// Underlying value is stored as double and std::vector<string>
16
17#ifndef _RBTVARIANT_H_
18#define _RBTVARIANT_H_
19
20#include <sstream>
21
22#include "rxdock/Coord.h"
23
24namespace rxdock {
25
26// Strings representing TRUE and FALSE
27const std::string _TRUE("TRUE");
28const std::string _FALSE("FALSE");
29
30class Variant {
31public:
33 // Constructors/destructors
34 Variant() : m_d(0.0) {}
35 Variant(int i) { SetDouble(i); }
36 Variant(double d) { SetDouble(d); }
37 Variant(const std::string &s) { SetString(s); }
38 Variant(const char *c) { SetString(std::string(c)); }
39 Variant(const std::vector<std::string> &sl) { SetStringList(sl); }
40 Variant(bool b) { SetBool(b); }
41 Variant(const Coord &c) { SetCoord(c); }
42 // Renders a vector of doubles into comma-separated strings of maxCols in
43 // length
44 Variant(const std::vector<double> &dl, int maxCols, int precision) {
45 SetDoubleList(dl, maxCols, precision);
46 }
47 virtual ~Variant() { m_sl.clear(); }
48
50 // Stream functions
52
53 // Insertion operator
54 friend std::ostream &operator<<(std::ostream &s, const Variant &v) {
55 // Try and guess the most appropriate output format
56 // Empty variant
57 if (v.isEmpty())
58 s << "Undefined variant";
59 // String/double
60 else if (v.Size() == 1)
61 s << v.m_sl.front();
62 // String list
63 else {
64 s << std::endl;
65 for (std::vector<std::string>::const_iterator iter = v.m_sl.begin();
66 iter != v.m_sl.end(); iter++)
67 s << *iter << std::endl;
68 }
69 return s;
70 }
71
73 // Operator functions:
75
76 // Set methods
77 Variant &operator=(int i) {
78 SetDouble(i);
79 return *this;
80 }
81 Variant &operator=(double d) {
82 SetDouble(d);
83 return *this;
84 }
85 Variant &operator=(const std::string &s) {
86 SetString(s);
87 return *this;
88 }
89 Variant &operator=(const char *c) {
90 SetString(std::string(c));
91 return *this;
92 }
93 Variant &operator=(const std::vector<std::string> &sl) {
94 SetStringList(sl);
95 return *this;
96 }
97 Variant &operator=(bool b) {
98 SetBool(b);
99 return *this;
100 }
101 Variant &operator=(const Coord &c) {
102 SetCoord(c);
103 return *this;
104 }
105
106 void operator+=(const Variant &v) {
107 std::copy(v.m_sl.begin(), v.m_sl.end(), std::back_inserter(m_sl));
108 m_d = std::atof(GetString().c_str());
109 }
110
111 // Conversion operators to convert back to basic types
112 operator int() const { return int(GetDouble()); }
113 operator unsigned int() const { return (unsigned int)(GetDouble()); }
114 operator double() const { return GetDouble(); }
115 operator std::string() const { return GetString(); }
116 operator std::vector<std::string>() const { return GetStringList(); }
117 operator bool() const { return GetBool(); }
118 operator Coord() const { return GetCoord(); }
119
120 friend void to_json(json &j, const Variant &v) {
121 j = json{{"double", v.m_d}, {"string-list", v.m_sl}};
122 }
123
124 friend void from_json(const json &j, Variant &v) {
125 j.at("double").get_to(v.m_d);
126 j.at("string-list").get_to(v.m_sl);
127 }
128
130 // Public methods
132
133 // Get methods
134 double GetDouble() const { return m_d; }
135 std::string GetString() const {
136 return m_sl.empty() ? std::string() : m_sl.front();
137 }
138 std::vector<std::string> GetStringList() const { return m_sl; }
139 bool GetBool() const {
140 return m_d != 0.0 || GetString() == _TRUE || GetString() == "true";
141 }
142 Coord GetCoord() const {
143 Coord c;
144 if (m_sl.empty())
145 return c;
146 else {
147 std::istringstream(m_sl.front()) >> c;
148 return c;
149 }
150 }
151
152 unsigned int Size() const { return m_sl.size(); }
153 bool isEmpty() const { return m_sl.empty(); }
154
155protected:
157 // Protected methods
159
160private:
162 // Private methods
164 void SetDouble(double d) {
165 m_d = d;
166 m_sl.clear();
167 std::ostringstream ostr;
168 // Don't need "ends" with std::ostringstream apparently
169 //(was introducing a non-ASCII \0 char into log files
170 // ostr << d << ends;
171 ostr << d;
172 std::string s(ostr.str());
173 // delete ostr.str();
174 m_sl.push_back(s);
175 }
176
177 void SetString(const std::string &s) {
178 m_sl.clear();
179 if (!s.empty())
180 m_sl.push_back(s);
181 m_d = std::atof(GetString().c_str());
182 }
183
184 void SetStringList(const std::vector<std::string> &sl) {
185 m_sl = sl;
186 m_d = std::atof(GetString().c_str());
187 }
188
189 void SetBool(bool b) {
190 if (b) {
191 SetString(_TRUE);
192 } else {
193 SetString(_FALSE);
194 }
195 }
196
197 void SetCoord(const Coord &c) {
198 m_d = c.Length();
199 m_sl.clear();
200 std::ostringstream ostr;
201 // Don't need "ends" with std::ostringstream apparently
202 //(was introducing a non-ASCII \0 char into log files
203 // ostr << c << ends;
204 ostr << c;
205 std::string s(ostr.str());
206 // delete ostr.str();
207 m_sl.push_back(s);
208 }
209
210 void SetDoubleList(const std::vector<double> &dl, int maxColumns,
211 int precision) {
212 int nValues = dl.size();
213 m_d = (nValues > 0) ? dl[0] : 0.0;
214 m_sl.clear();
215 std::ostringstream ostr;
216 ostr.precision(precision);
217 ostr.setf(std::ios_base::fixed, std::ios_base::floatfield);
218 int lastIndex = nValues - 1;
219 for (int i = 0; i < nValues; ++i) {
220 ostr << dl[i];
221 if ((ostr.tellp() >= maxColumns) || (i == lastIndex)) {
222 m_sl.push_back(ostr.str());
223 ostr.str("");
224 } else {
225 ostr << ",";
226 }
227 }
228 }
229
230protected:
232 // Protected data
234
235private:
237 // Private data
239 double m_d;
240 std::vector<std::string> m_sl;
241};
242
243// Useful typedefs
244// typedef SharedPtr<Variant> VariantPtr;//Smart pointer
245typedef std::vector<Variant> VariantList; // Vector of variants
246typedef VariantList::iterator VariantListIter;
247typedef VariantList::const_iterator VariantListConstIter;
248
249// Map of (key=string, value=variant)
250typedef std::map<std::string, Variant> StringVariantMap;
251typedef StringVariantMap::iterator StringVariantMapIter;
252typedef StringVariantMap::const_iterator StringVariantMapConstIter;
253
254} // namespace rxdock
255
256#endif //_RBTVARIANT_H_
Definition Coord.h:45
Definition Variant.h:30