ARTS  2.2.66
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 "arts.h"
19 #include "messages.h"
20 #include "token.h"
21 
26 String TokValTypeName[8] = {"String", "Index", "Numeric",
27  "ArrayOfString", "ArrayOfIndex",
28  "Vector", "Matrix", "undefined"};
29 
30 
31 // Conversion functions to read TokVal for the 6 different types:
32 
33 TokVal::operator String() const {
34  assert (mtype == String_t);
35  return ms;
36 }
37 
38 TokVal::operator Index() const {
39  assert (mtype == Index_t);
40  return mn;
41 }
42 
43 TokVal::operator Numeric() const {
44  assert (mtype == Numeric_t);
45  return mx;
46 }
47 
48 
49 TokVal::operator ArrayOfString() const {
50  assert (mtype == Array_String_t);
51  return msv;
52 }
53 
54 TokVal::operator ArrayOfIndex() const {
55  assert (mtype == Array_Index_t);
56  return mnv;
57 }
58 
59 TokVal::operator Vector() const {
60  assert (mtype == Vector_t);
61  return mxv;
62 }
63 
64 TokVal::operator Matrix() const {
65  assert (mtype == Matrix_t);
66  return mm;
67 }
68 
69 ostream& operator<<(ostream& os, const TokVal& a)
70 {
71  // This is needed for nice formating:
72  bool first = true;
73 
74  switch (a.mtype)
75  {
76  case String_t:
77  os << "\"" << a.ms << "\"";
78  break;
79  case Index_t:
80  os << a.mn;
81  break;
82  case Numeric_t:
83  os << a.mx;
84  break;
85  case Array_String_t:
86  os << "[";
87  for ( Index i=0; i<a.msv.nelem(); ++i )
88  {
89  if (first) first=false;
90  else os << ",";
91  os << "\"" << a.msv[i] << "\"";
92  }
93  os << "]";
94  break;
95  case Array_Index_t:
96  os << "[";
97  for ( Index i=0; i<a.mnv.nelem(); ++i )
98  {
99  if (first) first=false;
100  else os << ",";
101 
102  os << a.mnv[i];
103  }
104  os << "]";
105  break;
106  case Vector_t:
107  os << "[";
108  for ( Index i=0; i<a.mxv.nelem(); ++i )
109  {
110  if (first) first=false;
111  else os << ",";
112 
113  os << a.mxv[i];
114  }
115  os << "]";
116  break;
117  case Matrix_t:
118  os << "[";
119  for ( Index i=0; i<a.mm.nrows(); ++i )
120  {
121  for ( Index j=0; i<a.mm.ncols(); ++j )
122  {
123  if (first) first=false;
124  else {
125  if (j == 0)
126  os << ";";
127  else
128  os << ",";
129  }
130 
131  os << a.mm(i, j);
132  }
133  }
134  os << "]";
135  break;
136  default:
137  cerr << "Undefined token type.\n";
138  arts_exit ();
139  }
140  return os;
141 }
142 
143 
144 // main()
145 // {
146 // String a("Test");
147 // TokVal tv(a);
148 
149 // String b=tv;
150 // cout << b << '\n';
151 // Numeric c = 3.8;
152 // TokVal tvtv(c);
153 // tv = tvtv;
154 // Numeric d = tv;
155 // cout << d << '\n';
156 // b = tv; // should cause an error because of wrong type
157 // }
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
Index mn
Definition: token.h:96
Numeric mx
Definition: token.h:97
Index nelem() const
Number of elements.
Definition: array.h:176
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.
The Vector class.
Definition: matpackI.h:556
ArrayOfIndex mnv
Definition: token.h:99
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:180
Array< Index > ArrayOfIndex
An array of Index.
Definition: array.h:40
The implementation for String, the ARTS string class.
Definition: mystring.h:63
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:838
The global header file for ARTS.
String ms
Definition: token.h:95
Definition: token.h:27
Vector mxv
Definition: token.h:100
Definition: token.h:28
ostream & operator<<(ostream &os, const TokVal &a)
Definition: token.cc:69
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:29
The Matrix class.
Definition: matpackI.h:788
Array< String > ArrayOfString
An array of Strings.
Definition: mystring.h:321
Matrix mm
Definition: token.h:101
ArrayOfString msv
Definition: token.h:98
TokValType mtype
Definition: token.h:94
String TokValTypeName[8]
The name of the type associated with the different tokens.
Definition: token.cc:26
Definition: token.h:28
This stores arbitrary token values and remembers the type.
Definition: token.h:33
Definition: token.h:27
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:832
my_basic_string< char > String
The String type for ARTS.
Definition: mystring.h:318