casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
variant.h
Go to the documentation of this file.
1 #ifndef __casac_variant_h__
2 #define __casac_variant_h__
3 
4 #include <string>
5 #include <vector>
6 #include <complex>
7 
8 namespace casac {
9 
10 class record;
11 
12 class variant {
13 
14  public:
15 
17 
18  static TYPE compatible_type( TYPE one, TYPE two );
19 
20  class error {
21  public:
22  error( std::string msg ) : message_(msg) { }
23  const std::string &message( ) const { return message_; }
24  private:
25  std::string message_;
26  };
27 
28  class overflow : public error {
29  public:
30  overflow( std::string lbl ) : error(lbl + ": overflow error") { }
31  };
32 
33  variant *clone() const { return new variant(*this); }
34  int compare(const variant*) const;
35 
36  variant( );
37  variant(const variant &);
38 
39  variant(bool arg) : typev(BOOL), shape_(1,1) { val.b = arg; }
40  variant(int arg) : typev(INT), shape_(1,1) { val.i = arg; }
41  variant(unsigned int arg) : typev(UINT), shape_(1,1) { val.ui = arg; }
42  variant(long long arg) : typev(LONG), shape_(1,1) { val.l = arg; }
43  variant(double arg) : typev(DOUBLE), shape_(1,1) { val.d = arg; }
44  variant(std::complex<double> arg) : typev(COMPLEX) { val.c = new std::complex<double>(arg); }
45  variant(const char *arg) : typev(STRING), shape_(1,1)
46  { val.s = new std::string(arg); }
47  variant(const std::string &arg) : typev(STRING), shape_(1,1)
48  { val.s = new std::string(arg); }
49 //
50  variant(const std::vector<bool> &arg) : typev(BOOLVEC), shape_(1,arg.size())
51  { val.bv = new std::vector<bool>(arg); }
52  variant(const std::vector<bool> &arg, const std::vector<int> &theshape) : typev(BOOLVEC), shape_(theshape)
53  { val.bv = new std::vector<bool>(arg); }
54  variant(std::vector<bool> *arg) : typev(BOOLVEC), shape_(1,arg->size())
55  { val.bv = arg; }
56  variant(std::vector<bool> *arg, std::vector<int> &theshape) : typev(BOOLVEC), shape_(theshape)
57  { val.bv = arg; }
58 //
59  variant(const std::vector<int> &arg) : typev(INTVEC), shape_(1,arg.size())
60  { val.iv = new std::vector<int>(arg); }
61  variant(const std::vector<int> &arg, const std::vector<int> &theshape) : typev(INTVEC), shape_(theshape)
62  { val.iv = new std::vector<int>(arg); }
63  variant(std::vector<int> *arg) : typev(INTVEC), shape_(1, arg->size())
64  { val.iv = arg; }
65  variant(std::vector<int> *arg, std::vector<int> &theshape) : typev(INTVEC), shape_(theshape)
66  { val.iv = arg; }
67 
68  variant(const std::vector<unsigned int> &arg) : typev(UINTVEC), shape_(1,arg.size())
69  { val.uiv = new std::vector<unsigned int>(arg); }
70  variant(const std::vector<unsigned int> &arg, const std::vector<int> &theshape) : typev(UINTVEC), shape_(theshape)
71  { val.uiv = new std::vector<unsigned int>(arg); }
72  variant(std::vector<unsigned int> *arg) : typev(UINTVEC), shape_(1, arg->size())
73  { val.uiv = arg; }
74  variant(std::vector<unsigned int> *arg, std::vector<int> &theshape) : typev(UINTVEC), shape_(theshape)
75  { val.uiv = arg; }
76 
77 //
78  variant(const std::vector<long long> &arg) : typev(LONGVEC), shape_(1,arg.size())
79  { val.lv = new std::vector<long long>(arg); }
80  variant(const std::vector<long long> &arg, const std::vector<int> &theshape) : typev(LONGVEC), shape_(theshape)
81  { val.lv = new std::vector<long long>(arg); }
82  variant(std::vector<long long> *arg) : typev(LONGVEC), shape_(1, arg->size())
83  { val.lv = arg; }
84  variant(std::vector<long long> *arg, std::vector<int> &theshape) : typev(LONGVEC), shape_(theshape)
85  { val.lv = arg; }
86 //
87  variant(const std::vector<double> &arg) : typev(DOUBLEVEC), shape_(1,arg.size())
88  { val.dv = new std::vector<double>(arg); }
89  variant(const std::vector<double> &arg, const std::vector<int> &theshape) : typev(DOUBLEVEC), shape_(theshape)
90  { val.dv = new std::vector<double>(arg); }
91  variant(std::vector<double> *arg) : typev(DOUBLEVEC), shape_(1,arg->size())
92  { val.dv = arg; }
93  variant(std::vector<double> *arg, std::vector<int> &theshape) : typev(DOUBLEVEC), shape_(theshape)
94  { val.dv = arg; }
95 
96  variant(const std::vector<std::complex<double> > &arg) : typev(COMPLEXVEC), shape_(1, arg.size())
97  { val.cv = new std::vector<std::complex<double> >(arg); }
98  variant(const std::vector<std::complex<double> > &arg, const std::vector<int> &theshape) : typev(COMPLEXVEC), shape_(theshape)
99  { val.cv = new std::vector<std::complex<double> >(arg); }
100  variant(std::vector<std::complex<double> > *arg) : typev(COMPLEXVEC), shape_(1,arg->size())
101  { val.cv = arg; }
102  variant(std::vector<std::complex<double> > *arg, std::vector<int> &theshape) : typev(COMPLEXVEC), shape_(theshape)
103  { val.cv = arg; }
104 //
105  variant(const std::vector<std::string> &arg, const std::vector<int> &theshape) : typev(STRINGVEC), shape_(theshape)
106  { val.sv = new std::vector<std::string>(arg); }
107  variant(const std::vector<std::string> &arg) : typev(STRINGVEC), shape_(1,arg.size())
108  { val.sv = new std::vector<std::string>(arg); }
109  variant(std::vector<std::string> *arg) : typev(STRINGVEC), shape_(1, arg->size())
110  { val.sv = arg; }
111  variant(std::vector<std::string> *arg, std::vector<int> &theshape) : typev(STRINGVEC), shape_(theshape)
112  { val.sv = arg; }
113 //
114  variant(const record &arg);
115  variant(record *arg);
116 
117  ~variant( );
118 
119  variant & operator= (const variant &other);
120 
121  bool toBool( ) const;
122  int toInt( ) const;
123  unsigned int touInt( ) const;
124  long long toLong( ) const;
125  double toDouble( ) const;
126  std::complex<double> toComplex( ) const;
127  std::string toString( bool no_brackets=false ) const;
128  std::vector<bool> toBoolVec( ) const;
129  std::vector<int> toIntVec( ) const;
130  std::vector<unsigned int> touIntVec( ) const;
131  std::vector<long long> toLongVec( ) const;
132  std::vector<double> toDoubleVec( ) const;
133  std::vector<std::complex<double> > toComplexVec( ) const;
134  std::vector<std::string> toStringVec( ) const;
135 
136  // Yet to be implemented
137 
138 // Modify
139 // ---------------------------------------------------
140  bool &asBool( );
141  int &asInt( );
142  unsigned int &asuInt( );
143  long long &asLong( );
144  double &asDouble( );
145  std::complex<double> &asComplex( );
146  std::string &asString( );
147  std::vector<int> &asIntVec( int size=-1 );
148  std::vector<unsigned int> &asuIntVec( int size=-1 );
149  std::vector<long long> &asLongVec( int size=-1 );
150  std::vector<bool> &asBoolVec( int size=-1 );
151  std::vector<double> &asDoubleVec( int size=-1 );
152  std::vector<std::complex<double> > &asComplexVec( int size=-1 );
153  std::vector<std::string> &asStringVec( int size=-1 );
155 
156  void as( TYPE t, int size=-1 );
157 
158 // Const
159 // ---------------------------------------------------
160  bool getBool( ) const throw(error);
161  int getInt( ) const throw(error);
162  unsigned int getuInt( ) const throw(error);
163  long long getLong( ) const throw(error);
164  double getDouble( ) const throw(error);
165  const std::complex<double> &getComplex( ) const throw(error);
166  const std::string &getString( ) const throw(error);
167  const std::vector<int> &getIntVec( ) const throw(error);
168  const std::vector<unsigned int> &getuIntVec( ) const throw(error);
169  const std::vector<long long> &getLongVec( ) const throw(error);
170  const std::vector<bool> &getBoolVec( ) const throw(error);
171  const std::vector<double> &getDoubleVec( ) const throw(error);
172  const std::vector<std::complex<double> > &getComplexVec( ) const throw(error);
173  const std::vector<std::string> &getStringVec( ) const throw(error);
174  const record &getRecord( ) const throw(error);
175  const std::vector<int> &shape() const;
176  const std::vector<int> &arrayshape() const {return shape();}
177 
178 // Modify
179 // ---------------------------------------------------
180  bool &getBoolMod( ) throw(error);
181  int &getIntMod( ) throw(error);
182  unsigned int &getuIntMod( ) throw(error);
183  double &getDoubleMod( ) throw(error);
184  std::complex<double> &getComplexMod( ) throw(error);
185  std::string &getStringMod( ) throw(error);
186  std::vector<int> &getIntVecMod( ) throw(error);
187  std::vector<unsigned int> &getuIntVecMod( ) throw(error);
188  std::vector<bool> &getBoolVecMod( ) throw(error);
189  std::vector<double> &getDoubleVecMod( ) throw(error);
190  std::vector<std::complex<double> > &getComplexVecMod( ) throw(error);
191  std::vector<std::string> &getStringVecMod( ) throw(error);
192  record &getRecordMod( ) throw(error);
193  std::vector<int> &shape();
194  std::vector<int> &arrayshape() {return shape();}
195 
196  const std::string &typeString( ) const;
197  TYPE type( ) const { return typev; }
198 
199  void push(bool, bool conform = true);
200  void push(int, bool conform = true);
201  void push(unsigned int, bool conform = true);
202  void push(long long, bool conform = true);
203  void push(double, bool conform = true);
204  void push(std::vector<long long>, bool conform = true);
205  void push(std::complex<double>, bool conform = true);
206  void push(const std::string&, bool conform = true);
207 
208  void place(bool, unsigned int index, bool conform = true);
209  void place(int, unsigned int index, bool conform = true);
210  void place(unsigned int, unsigned int index, bool conform = true);
211  void place(long long, unsigned int index, bool conform = true);
212  void place(double, unsigned int index, bool conform = true);
213  void place(std::vector<long long>, unsigned int index, bool conform = true);
214  void place(std::complex<double>, unsigned int index, bool conform = true);
215  void place(const std::string&, unsigned int index, bool conform = true);
216 
217  int size( ) const { return typev >= BOOLVEC ? vec_size() : 1; }
218  void resize( int size );
219  void dump() const;
220 
221  // return true if empty string, empty record, or size 0 vector.
222  // always returns false if object is a non-array bool or numerical type
223  bool empty() const;
224 
225  private:
226 
227  void freeStorage ();
228 
229  // what size does the shape imply
230  int shape_size( ) const;
231 
232  // 4294967295
233  static unsigned int record_id_count;
234 
235  int vec_size( ) const;
237  union {
238  bool b;
239  std::vector<bool> *bv;
240  int i;
241  unsigned int ui;
242  long long l;
243  std::vector<int> *iv;
244  std::vector<unsigned int> *uiv;
245  std::vector<long long> *lv;
246  double d;
247  std::vector<double> *dv;
248  std::complex<double> *c;
249  std::vector<std::complex<double> > *cv;
250  std::string *s;
251  std::vector<std::string> *sv;
253  } val;
254  std::vector<int> shape_;
255 
256  std::string create_message( const std::string s ) const;
257 };
258 
259 variant initialize_variant( const std::string & );
260 } // casac namespace
261 
262 #endif
std::vector< double > * dv
Definition: variant.h:247
long long & asLong()
std::vector< int > & getIntVecMod()
std::vector< bool > & getBoolVecMod()
variant(const char *arg)
Definition: variant.h:45
variant(unsigned int arg)
Definition: variant.h:41
double & getDoubleMod()
const std::vector< long long > & getLongVec() const
std::vector< std::string > & asStringVec(int size=-1)
unsigned int getuInt() const
variant(const std::vector< double > &arg, const std::vector< int > &theshape)
Definition: variant.h:89
double toDouble() const
std::string message_
Definition: variant.h:25
LatticeExprNode arg(const LatticeExprNode &expr)
variant(const std::vector< int > &arg)
Definition: variant.h:59
std::complex< double > & getComplexMod()
record * recordv
Definition: variant.h:252
std::vector< unsigned int > & asuIntVec(int size=-1)
int vec_size() const
std::vector< std::complex< double > > & asComplexVec(int size=-1)
const std::vector< int > & arrayshape() const
Definition: variant.h:176
std::complex< double > toComplex() const
const std::vector< unsigned int > & getuIntVec() const
std::complex< double > & asComplex()
variant(std::vector< int > *arg, std::vector< int > &theshape)
Definition: variant.h:65
variant(long long arg)
Definition: variant.h:42
variant(const std::vector< bool > &arg)
Definition: variant.h:50
variant(std::vector< long long > *arg, std::vector< int > &theshape)
Definition: variant.h:84
void resize(int size)
std::string & getStringMod()
variant(std::vector< int > *arg)
Definition: variant.h:63
bool & asBool()
Yet to be implemented.
const std::vector< double > & getDoubleVec() const
const std::vector< int > & getIntVec() const
bool & getBoolMod()
Modify
unsigned int & asuInt()
int getInt() const
std::complex< double > * c
Definition: variant.h:248
std::vector< int > * iv
Definition: variant.h:243
unsigned int touInt() const
todo: o create python to/from record functions o implement compare() o implement record_to_string() o...
Definition: record.h:19
variant(std::complex< double > arg)
Definition: variant.h:44
double getDouble() const
variant(std::vector< unsigned int > *arg)
Definition: variant.h:72
int compare(const variant *) const
std::vector< bool > * bv
Definition: variant.h:239
std::vector< std::string > toStringVec() const
variant(const std::vector< long long > &arg)
Definition: variant.h:78
std::vector< unsigned int > * uiv
Definition: variant.h:244
int shape_size() const
what size does the shape imply
void place(bool, unsigned int index, bool conform=true)
variant & operator=(const variant &other)
overflow(std::string lbl)
Definition: variant.h:30
std::vector< bool > & asBoolVec(int size=-1)
int size() const
Definition: variant.h:217
std::vector< std::string > * sv
Definition: variant.h:251
std::string * s
Definition: variant.h:250
variant(bool arg)
Definition: variant.h:39
void push(bool, bool conform=true)
std::string toString(bool no_brackets=false) const
std::vector< long long > toLongVec() const
variant(std::vector< std::complex< double > > *arg, std::vector< int > &theshape)
Definition: variant.h:102
long long getLong() const
variant(std::vector< unsigned int > *arg, std::vector< int > &theshape)
Definition: variant.h:74
std::vector< int > & asIntVec(int size=-1)
bool getBool() const
Const
std::vector< int > shape_
Definition: variant.h:254
variant(int arg)
Definition: variant.h:40
union casac::variant::@67 val
const std::string & typeString() const
const std::vector< bool > & getBoolVec() const
std::vector< double > & asDoubleVec(int size=-1)
void dump() const
variant(const std::vector< long long > &arg, const std::vector< int > &theshape)
Definition: variant.h:80
std::vector< int > toIntVec() const
std::string create_message(const std::string s) const
record & getRecordMod()
variant initialize_variant(const std::string &)
void freeStorage()
variant(const std::vector< unsigned int > &arg, const std::vector< int > &theshape)
Definition: variant.h:70
variant(std::vector< bool > *arg, std::vector< int > &theshape)
Definition: variant.h:56
unsigned int ui
Definition: variant.h:241
variant(std::vector< double > *arg)
Definition: variant.h:91
variant(const std::vector< std::complex< double > > &arg, const std::vector< int > &theshape)
Definition: variant.h:98
int & getIntMod()
const std::string & message() const
Definition: variant.h:23
variant(const std::string &arg)
Definition: variant.h:47
bool toBool() const
std::vector< long long > & asLongVec(int size=-1)
double d
Definition: variant.h:246
const std::complex< double > & getComplex() const
int toInt() const
variant(const std::vector< int > &arg, const std::vector< int > &theshape)
Definition: variant.h:61
static TYPE compatible_type(TYPE one, TYPE two)
variant(std::vector< std::complex< double > > *arg)
Definition: variant.h:100
std::vector< std::string > & getStringVecMod()
TYPE type() const
Definition: variant.h:197
variant(double arg)
Definition: variant.h:43
const std::string & getString() const
unsigned int & getuIntMod()
const std::vector< std::complex< double > > & getComplexVec() const
variant(const std::vector< std::string > &arg)
Definition: variant.h:107
std::vector< unsigned int > touIntVec() const
const std::vector< int > & shape() const
std::vector< bool > toBoolVec() const
void as(TYPE t, int size=-1)
variant * clone() const
Definition: variant.h:33
std::vector< unsigned int > & getuIntVecMod()
variant(std::vector< std::string > *arg)
Definition: variant.h:109
variant(std::vector< long long > *arg)
Definition: variant.h:82
variant(std::vector< std::string > *arg, std::vector< int > &theshape)
Definition: variant.h:111
double & asDouble()
long long toLong() const
std::vector< std::complex< double > > toComplexVec() const
std::vector< double > toDoubleVec() const
const record & getRecord() const
std::vector< std::complex< double > > * cv
Definition: variant.h:249
static unsigned int record_id_count
4294967295
Definition: variant.h:233
error(std::string msg)
Definition: variant.h:22
variant(const std::vector< double > &arg)
Definition: variant.h:87
variant(const std::vector< std::complex< double > > &arg)
Definition: variant.h:96
variant(std::vector< double > *arg, std::vector< int > &theshape)
Definition: variant.h:93
std::vector< long long > * lv
Definition: variant.h:245
std::vector< double > & getDoubleVecMod()
const std::vector< std::string > & getStringVec() const
bool empty() const
return true if empty string, empty record, or size 0 vector.
long long l
Definition: variant.h:242
std::string & asString()
variant(const std::vector< bool > &arg, const std::vector< int > &theshape)
Definition: variant.h:52
variant(std::vector< bool > *arg)
Definition: variant.h:54
casac::record & asRecord()
variant(const std::vector< unsigned int > &arg)
Definition: variant.h:68
variant(const std::vector< std::string > &arg, const std::vector< int > &theshape)
Definition: variant.h:105
std::vector< std::complex< double > > & getComplexVecMod()