ARTS  2.3.1285(git:92a29ea9-dirty)
token.cc
Go to the documentation of this file.
1 /* Copyright (C) 2000-2012 Stefan Buehler <sbuehler@ltu.se>
2 
3  This program is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License as published by the
5  Free Software Foundation; either version 2, or (at your option) any
6  later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16  USA. */
17 
18 #include "token.h"
19 #include "arts.h"
20 #include "messages.h"
21 
26 String TokValTypeName[8] = {"String",
27  "Index",
28  "Numeric",
29  "ArrayOfString",
30  "ArrayOfIndex",
31  "Vector",
32  "Matrix",
33  "undefined"};
34 
35 // Conversion functions to read TokVal for the 6 different types:
36 
37 TokVal::operator String() const {
38  assert(mtype == String_t);
39  return ms;
40 }
41 
42 TokVal::operator Index() const {
43  assert(mtype == Index_t);
44  return mn;
45 }
46 
47 TokVal::operator Numeric() const {
48  assert(mtype == Numeric_t);
49  return mx;
50 }
51 
52 TokVal::operator ArrayOfString() const {
53  assert(mtype == Array_String_t);
54  return msv;
55 }
56 
57 TokVal::operator ArrayOfIndex() const {
58  assert(mtype == Array_Index_t);
59  return mnv;
60 }
61 
62 TokVal::operator Vector() const {
63  assert(mtype == Vector_t);
64  return mxv;
65 }
66 
67 TokVal::operator Matrix() const {
68  assert(mtype == Matrix_t);
69  return mm;
70 }
71 
72 ostream& operator<<(ostream& os, const TokVal& a) {
73  // This is needed for nice formating:
74  bool first = true;
75 
76  switch (a.mtype) {
77  case String_t:
78  os << "\"" << a.ms << "\"";
79  break;
80  case Index_t:
81  os << a.mn;
82  break;
83  case Numeric_t:
84  os << a.mx;
85  break;
86  case Array_String_t:
87  os << "[";
88  for (Index i = 0; i < a.msv.nelem(); ++i) {
89  if (first)
90  first = false;
91  else
92  os << ",";
93  os << "\"" << a.msv[i] << "\"";
94  }
95  os << "]";
96  break;
97  case Array_Index_t:
98  os << "[";
99  for (Index i = 0; i < a.mnv.nelem(); ++i) {
100  if (first)
101  first = false;
102  else
103  os << ",";
104 
105  os << a.mnv[i];
106  }
107  os << "]";
108  break;
109  case Vector_t:
110  os << "[";
111  for (Index i = 0; i < a.mxv.nelem(); ++i) {
112  if (first)
113  first = false;
114  else
115  os << ",";
116 
117  os << a.mxv[i];
118  }
119  os << "]";
120  break;
121  case Matrix_t:
122  os << "[";
123  for (Index i = 0; i < a.mm.nrows(); ++i) {
124  for (Index j = 0; i < a.mm.ncols(); ++j) {
125  if (first)
126  first = false;
127  else {
128  if (j == 0)
129  os << ";";
130  else
131  os << ",";
132  }
133 
134  os << a.mm(i, j);
135  }
136  }
137  os << "]";
138  break;
139  default:
140  cerr << "Undefined token type.\n";
141  arts_exit();
142  }
143  return os;
144 }
145 
146 // main()
147 // {
148 // String a("Test");
149 // TokVal tv(a);
150 
151 // String b=tv;
152 // cout << b << '\n';
153 // Numeric c = 3.8;
154 // TokVal tvtv(c);
155 // tv = tvtv;
156 // Numeric d = tv;
157 // cout << d << '\n';
158 // b = tv; // should cause an error because of wrong type
159 // }
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
Index mn
Definition: token.h:116
Numeric mx
Definition: token.h:117
Index nelem() const
Number of elements.
Definition: array.h:195
void arts_exit(int status)
This is the exit function of ARTS.
Definition: arts.cc:42
Declarations having to do with the four output streams.
ArrayOfIndex mnv
Definition: token.h:119
invlib::Vector< ArtsVector > Vector
invlib wrapper type for ARTS vectors.
Definition: oem.h:32
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:51
Array< Index > ArrayOfIndex
An array of Index.
Definition: array.h:40
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:432
The global header file for ARTS.
String ms
Definition: token.h:115
Definition: token.h:29
Vector mxv
Definition: token.h:120
Definition: token.h:34
ostream & operator<<(ostream &os, const TokVal &a)
Definition: token.cc:72
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
Array< String > ArrayOfString
An array of Strings.
Definition: mystring.h:283
Matrix mm
Definition: token.h:121
ArrayOfString msv
Definition: token.h:118
TokValType mtype
Definition: token.h:114
String TokValTypeName[8]
The name of the type associated with the different tokens.
Definition: token.cc:26
Definition: token.h:33
This stores arbitrary token values and remembers the type.
Definition: token.h:40
invlib::Matrix< ArtsMatrix > Matrix
invlib wrapper type for ARTS matrices.
Definition: oem.h:34
Definition: token.h:28
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:429
my_basic_string< char > String
The String type for ARTS.
Definition: mystring.h:280