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 //# variant.h: multi-typed values
2 //#
3 //# Copyright (C) 2011
4 //# Associated Universities, Inc. Washington DC, USA.
5 //#
6 //# This library is free software; you can redistribute it and/or modify it
7 //# under the terms of the GNU Library General Public License as published by
8 //# the Free Software Foundation; either version 2 of the License, or (at your
9 //# option) any later version.
10 //#
11 //# This library is distributed in the hope that it will be useful, but WITHOUT
12 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 //# License for more details.
15 //#
16 //# You should have received a copy of the GNU Library General Public License
17 //# along with this library; if not, write to the Free Software Foundation,
18 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
19 //#
20 //# Correspondence concerning AIPS++ should be addressed as follows:
21 //# Internet email: aips2-request@nrao.edu.
22 //# Postal address: AIPS++ Project Office
23 //# National Radio Astronomy Observatory
24 //# 520 Edgemont Road
25 //# Charlottesville, VA 22903-2475 USA
26 //#
27 //# $Id$
28 
29 #ifndef __casadbus_variant_h__
30 #define __casadbus_variant_h__
31 
32 #include <string>
33 #include <vector>
34 #include <complex>
35 
36 namespace casa {
37  namespace dbus {
38 
39  class record;
40 
41  class variant {
42 
43  public:
44 
46 
47  static TYPE compatible_type( TYPE one, TYPE two );
48 
49  class error {
50  public:
51  error( std::string msg ) : message_(msg) { }
52  const std::string &message( ) const { return message_; }
53  private:
54  std::string message_;
55  };
56 
57  class overflow : public error {
58  public:
59  overflow( std::string lbl ) : error(lbl + ": overflow error") { }
60  };
61 
62  variant *clone() const { return new variant(*this); }
63  int compare(const variant*) const;
64 
65  variant( );
66  variant(const variant &);
67 
68  variant(bool arg) : typev(BOOL), shape_(1,1) { val.b = arg; }
69  variant(int arg) : typev(INT), shape_(1,1) { val.i = arg; }
70  variant(double arg) : typev(DOUBLE), shape_(1,1) { val.d = arg; }
71  variant(std::complex<double> arg) : typev(COMPLEX) { val.c = new std::complex<double>(arg); }
72  variant(const char *arg) : typev(STRING), shape_(1,1)
73  { val.s = new std::string(arg); }
74  variant(const std::string &arg) : typev(STRING), shape_(1,1)
75  { val.s = new std::string(arg); }
76 //
77  variant(const std::vector<bool> &arg) : typev(BOOLVEC), shape_(1,arg.size())
78  { val.bv = new std::vector<bool>(arg); }
79  variant(const std::vector<bool> &arg, const std::vector<int> &theshape) : typev(BOOLVEC), shape_(theshape)
80  { val.bv = new std::vector<bool>(arg); }
81  variant(std::vector<bool> *arg) : typev(BOOLVEC), shape_(1,arg->size())
82  { val.bv = arg; }
83  variant(std::vector<bool> *arg, std::vector<int> &theshape) : typev(BOOLVEC), shape_(theshape)
84  { val.bv = arg; }
85 //
86  variant(const std::vector<int> &arg) : typev(INTVEC), shape_(1,arg.size())
87  { val.iv = new std::vector<int>(arg); }
88  variant(const std::vector<int> &arg, const std::vector<int> &theshape) : typev(INTVEC), shape_(theshape)
89  { val.iv = new std::vector<int>(arg); }
90  variant(std::vector<int> *arg) : typev(INTVEC), shape_(1, arg->size())
91  { val.iv = arg; }
92  variant(std::vector<int> *arg, std::vector<int> &theshape) : typev(INTVEC), shape_(theshape)
93  { val.iv = arg; }
94 //
95  variant(const std::vector<double> &arg) : typev(DOUBLEVEC), shape_(1,arg.size())
96  { val.dv = new std::vector<double>(arg); }
97  variant(const std::vector<double> &arg, const std::vector<int> &theshape) : typev(DOUBLEVEC), shape_(theshape)
98  { val.dv = new std::vector<double>(arg); }
99  variant(std::vector<double> *arg) : typev(DOUBLEVEC), shape_(1,arg->size())
100  { val.dv = arg; }
101  variant(std::vector<double> *arg, std::vector<int> &theshape) : typev(DOUBLEVEC), shape_(theshape)
102  { val.dv = arg; }
103 
104  variant(const std::vector<std::complex<double> > &arg) : typev(COMPLEXVEC), shape_(1, arg.size())
105  { val.cv = new std::vector<std::complex<double> >(arg); }
106  variant(const std::vector<std::complex<double> > &arg, const std::vector<int> &theshape) : typev(COMPLEXVEC), shape_(theshape)
107  { val.cv = new std::vector<std::complex<double> >(arg); }
108  variant(std::vector<std::complex<double> > *arg) : typev(COMPLEXVEC), shape_(1,arg->size())
109  { val.cv = arg; }
110  variant(std::vector<std::complex<double> > *arg, std::vector<int> &theshape) : typev(COMPLEXVEC), shape_(theshape)
111  { val.cv = arg; }
112 //
113  variant(const std::vector<std::string> &arg, const std::vector<int> &theshape) : typev(STRINGVEC), shape_(theshape)
114  { val.sv = new std::vector<std::string>(arg); }
115  variant(const std::vector<std::string> &arg) : typev(STRINGVEC), shape_(1,arg.size())
116  { val.sv = new std::vector<std::string>(arg); }
117  variant(std::vector<std::string> *arg) : typev(STRINGVEC), shape_(1, arg->size())
118  { val.sv = arg; }
119  variant(std::vector<std::string> *arg, std::vector<int> &theshape) : typev(STRINGVEC), shape_(theshape)
120  { val.sv = arg; }
121 //
122  variant(record &arg);
123  variant(record *arg);
124 
125  ~variant( );
126 
127  bool toBool( ) const;
128  int toInt( ) const;
129  double toDouble( ) const;
130  std::complex<double> toComplex( ) const;
131  std::string toString( bool no_brackets=false ) const;
132  std::vector<bool> toBoolVec( ) const;
133  std::vector<int> toIntVec( ) const;
134  std::vector<double> toDoubleVec( ) const;
135  std::vector<std::complex<double> > toComplexVec( ) const;
136  std::vector<std::string> toStringVec( ) const;
137 
138  // Yet to be implemented
139 
140 // Modify
141 // ---------------------------------------------------
142  bool &asBool( );
143  int &asInt( );
144  double &asDouble( );
145  std::complex<double> &asComplex( );
146  std::string &asString( );
147  std::vector<int> &asIntVec( int size=-1 );
148  std::vector<bool> &asBoolVec( int size=-1 );
149  std::vector<double> &asDoubleVec( int size=-1 );
150  std::vector<std::complex<double> > &asComplexVec( int size=-1 );
151  std::vector<std::string> &asStringVec( int size=-1 );
152  record &asRecord( );
153 
154  void as( TYPE t, int size=-1 );
155 
156 // Const
157 // ---------------------------------------------------
158  bool getBool( ) const throw(error);
159  int getInt( ) const throw(error);
160  double getDouble( ) const throw(error);
161  const std::complex<double> &getComplex( ) const throw(error);
162  const std::string &getString( ) const throw(error);
163  const std::vector<int> &getIntVec( ) const throw(error);
164  const std::vector<bool> &getBoolVec( ) const throw(error);
165  const std::vector<double> &getDoubleVec( ) const throw(error);
166  const std::vector<std::complex<double> > &getComplexVec( ) const throw(error);
167  const std::vector<std::string> &getStringVec( ) const throw(error);
168  const record &getRecord( ) const throw(error);
169  const std::vector<int> &shape() const;
170  const std::vector<int> &arrayshape() const {return shape();}
171 
172 // Modify
173 // ---------------------------------------------------
174  bool &getBoolMod( ) throw(error);
175  int &getIntMod( ) throw(error);
176  double &getDoubleMod( ) throw(error);
177  std::complex<double> &getComplexMod( ) throw(error);
178  std::string &getStringMod( ) throw(error);
179  std::vector<int> &getIntVecMod( ) throw(error);
180  std::vector<bool> &getBoolVecMod( ) throw(error);
181  std::vector<double> &getDoubleVecMod( ) throw(error);
182  std::vector<std::complex<double> > &getComplexVecMod( ) throw(error);
183  std::vector<std::string> &getStringVecMod( ) throw(error);
184  record &getRecordMod( ) throw(error);
185  std::vector<int> &shape();
186  std::vector<int> &arrayshape() {return shape();}
187 
188  const std::string &typeString( ) const;
189  const char *sig( ) const;
190  TYPE type( ) const { return typev; }
191 
192  void push(bool, bool conform = true);
193  void push(int, bool conform = true);
194  void push(double, bool conform = true);
195  void push(std::complex<double>, bool conform = true);
196  void push(const std::string&, bool conform = true);
197 
198  void place(bool, unsigned int index, bool conform = true);
199  void place(int, unsigned int index, bool conform = true);
200  void place(double, unsigned int index, bool conform = true);
201  void place(std::complex<double>, unsigned int index, bool conform = true);
202  void place(const std::string&, unsigned int index, bool conform = true);
203 
204  int size( ) const { return typev >= BOOLVEC ? vec_size() : 1; }
205  void resize( int size );
206 
207  private:
208 
209  // what size does the shape imply
210  int shape_size( ) const;
211 
212  // 4294967295
213  static unsigned int record_id_count;
214 
215  int vec_size( ) const;
217  union {
218  bool b;
219  std::vector<bool> *bv;
220  int i;
221  std::vector<int> *iv;
222  double d;
223  std::vector<double> *dv;
224  std::complex<double> *c;
225  std::vector<std::complex<double> > *cv;
226  std::string *s;
227  std::vector<std::string> *sv;
229  } val;
230  std::vector<int> shape_;
231 
232  std::string create_message( const std::string s ) const;
233  };
234 
235  } // dbus namespace
236 } // casa namespace
237 #endif
void place(bool, unsigned int index, bool conform=true)
std::vector< std::complex< double > > * cv
Definition: variant.h:225
static unsigned int record_id_count
4294967295
Definition: variant.h:213
double getDouble() const
variant(const char *arg)
Definition: variant.h:72
std::complex< double > & getComplexMod()
LatticeExprNode arg(const LatticeExprNode &expr)
TYPE type() const
Definition: variant.h:190
variant(std::vector< std::complex< double > > *arg)
Definition: variant.h:108
bool getBool() const
Const
record & getRecordMod()
std::vector< std::complex< double > > & getComplexVecMod()
const std::vector< int > & getIntVec() const
variant(const std::vector< std::complex< double > > &arg, const std::vector< int > &theshape)
Definition: variant.h:106
bool & getBoolMod()
Modify
void push(bool, bool conform=true)
std::vector< std::string > & getStringVecMod()
variant(const std::string &arg)
Definition: variant.h:74
std::vector< bool > toBoolVec() const
variant * clone() const
Definition: variant.h:62
std::vector< bool > & asBoolVec(int size=-1)
union casa::dbus::variant::@61 val
std::vector< int > & getIntVecMod()
const std::vector< int > & arrayshape() const
Definition: variant.h:170
const std::vector< std::complex< double > > & getComplexVec() const
std::vector< double > & getDoubleVecMod()
variant(std::vector< int > *arg, std::vector< int > &theshape)
Definition: variant.h:92
variant(const std::vector< std::complex< double > > &arg)
Definition: variant.h:104
variant(const std::vector< double > &arg)
Definition: variant.h:95
const std::vector< std::string > & getStringVec() const
variant(std::complex< double > arg)
Definition: variant.h:71
std::string * s
Definition: variant.h:226
variant(std::vector< std::complex< double > > *arg, std::vector< int > &theshape)
Definition: variant.h:110
std::vector< bool > & getBoolVecMod()
todo: o create python to/from record functions o implement compare() o implement record_to_string() o...
Definition: record.h:48
const record & getRecord() const
variant(bool arg)
Definition: variant.h:68
std::vector< int > toIntVec() const
bool toBool() const
std::string message_
Definition: variant.h:54
bool & asBool()
Yet to be implemented.
variant(const std::vector< double > &arg, const std::vector< int > &theshape)
Definition: variant.h:97
double toDouble() const
int compare(const variant *) const
variant(const std::vector< int > &arg)
Definition: variant.h:86
std::vector< int > & asIntVec(int size=-1)
error(std::string msg)
Definition: variant.h:51
void as(TYPE t, int size=-1)
const std::string & typeString() const
overflow(std::string lbl)
Definition: variant.h:59
std::string & asString()
variant(const std::vector< int > &arg, const std::vector< int > &theshape)
Definition: variant.h:88
variant(std::vector< std::string > *arg, std::vector< int > &theshape)
Definition: variant.h:119
variant(const std::vector< std::string > &arg)
Definition: variant.h:115
std::vector< double > * dv
Definition: variant.h:223
std::vector< std::string > toStringVec() const
void resize(int size)
int getInt() const
const std::complex< double > & getComplex() const
std::vector< bool > * bv
Definition: variant.h:219
int toInt() const
const std::vector< int > & shape() const
std::complex< double > toComplex() const
std::string toString(bool no_brackets=false) const
double & getDoubleMod()
std::vector< std::complex< double > > & asComplexVec(int size=-1)
variant(std::vector< std::string > *arg)
Definition: variant.h:117
variant(int arg)
Definition: variant.h:69
static TYPE compatible_type(TYPE one, TYPE two)
std::complex< double > & asComplex()
variant(double arg)
Definition: variant.h:70
const std::vector< double > & getDoubleVec() const
std::vector< int > shape_
Definition: variant.h:230
int vec_size() const
const std::string & message() const
Definition: variant.h:52
std::vector< double > toDoubleVec() const
variant(std::vector< double > *arg, std::vector< int > &theshape)
Definition: variant.h:101
variant(std::vector< double > *arg)
Definition: variant.h:99
std::vector< std::string > & asStringVec(int size=-1)
variant(std::vector< bool > *arg, std::vector< int > &theshape)
Definition: variant.h:83
std::vector< std::string > * sv
Definition: variant.h:227
std::string & getStringMod()
std::vector< std::complex< double > > toComplexVec() const
std::string create_message(const std::string s) const
variant(const std::vector< bool > &arg, const std::vector< int > &theshape)
Definition: variant.h:79
int size() const
Definition: variant.h:204
const char * sig() const
const std::string & getString() const
int shape_size() const
what size does the shape imply
std::vector< double > & asDoubleVec(int size=-1)
std::complex< double > * c
Definition: variant.h:224
variant(const std::vector< std::string > &arg, const std::vector< int > &theshape)
Definition: variant.h:113
std::vector< int > * iv
Definition: variant.h:221
const std::vector< bool > & getBoolVec() const
variant(std::vector< int > *arg)
Definition: variant.h:90
variant(const std::vector< bool > &arg)
Definition: variant.h:77
record * recordv
Definition: variant.h:228
variant(std::vector< bool > *arg)
Definition: variant.h:81