NCBI C++ ToolKit
muParserDef.h
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /*
2  __________
3  _____ __ __\______ \_____ _______ ______ ____ _______
4  / \ | | \| ___/\__ \ \_ __ \/ ___/_/ __ \\_ __ \
5  | Y Y \| | /| | / __ \_| | \/\___ \ \ ___/ | | \/
6  |__|_| /|____/ |____| (____ /|__| /____ > \___ >|__|
7  \/ \/ \/ \/
8  Copyright (C) 2004-2008 Ingo Berg
9 
10  Permission is hereby granted, free of charge, to any person obtaining a copy of this
11  software and associated documentation files (the "Software"), to deal in the Software
12  without restriction, including without limitation the rights to use, copy, modify,
13  merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
14  permit persons to whom the Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in all copies or
17  substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
20  NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #ifndef MUP_DEF_H
26 #define MUP_DEF_H
27 
28 #include <iostream>
29 #include <string>
30 #include <sstream>
31 #include <map>
32 
33 #include "muParserFixes.h"
34 
35 /** \file
36  \brief This file contains standard definitions used by the parser.
37 */
38 
39 /** \brief Define the base datatype for values.
40 
41  This datatype must be a built in value type. You can not use custom classes.
42  It has been tested with float, double and long double types, int should
43  work as well.
44 */
45 #define MUP_BASETYPE double
46 
47 /** \brief Definition of the basic bytecode datatype.
48 
49  This defines the smalles entity used in the bytecode.
50 */
51 #define MUP_BYTECODE_TYPE long
52 
53 #if defined(MUP_UNICODE)
54  /** \brief Definition of the basic parser string type. */
55  #define MUP_STRING_TYPE std::wstring
56 
57  #if !defined(MUP_T)
58  #define MUP_T(x) L##x
59  #endif // not defined MUP_T
60 #else
61  #ifndef MUP_T
62  #define MUP_T
63  #endif
64 
65  /** \brief Definition of the basic parser string type. */
66  #define MUP_STRING_TYPE std::string
67 #endif
68 
69 #if defined(__SUNPRO_CC) && defined(_RWSTD_NO_TEMPLATE_ON_RETURN_TYPE)
70 # define MUP_USE_FACET(FACET, LOCALE) std::use_facet(LOCALE, (FACET*)0)
71 #else
72 # define MUP_USE_FACET(FACET, LOCALE) std::use_facet< FACET >(LOCALE)
73 #endif
74 
75 #if defined(_DEBUG)
76  /** \brief Debug macro to force an abortion of the programm with a certain message.
77  */
78  #define MUP_FAIL(MSG) \
79  bool MSG=false; \
80  assert(MSG);
81 
82  #ifndef MUP_UNICODE
83  /** \brief An assertion that does not kill the program.
84 
85  This macro is neutralised in UNICODE builds. It's
86  too difficult to translate.
87  */
88  #define MUP_ASSERT(COND) \
89  if (!(COND)) \
90  { \
91  stringstream_type ss; \
92  ss << "Assertion \""#COND"\" failed: " \
93  << __FILE__ << " line " \
94  << __LINE__ << "."; \
95  throw ParserError( ss.str() ); \
96  }
97  #else
98  #define MUP_ASSERT(COND)
99  #endif // MUP_UNICODE
100 #else
101  #define MUP_FAIL(MSG)
102  #define MUP_ASSERT(COND)
103 #endif
104 
105 //------------------------------------------------------------------------------
106 //
107 // do not change anything beyond this point...
108 //
109 // !!! This section is devoted to macros that are used for debugging
110 // !!! or for features that are not fully implemented yet.
111 //
112 //#define MUP_DUMP_STACK
113 //#define MUP_DUMP_CMDCODE
114 
115 
116 namespace mu
117 {
118 #if defined(MUP_UNICODE)
119 
120  //------------------------------------------------------------------------------
121  /** \brief Encapsulate wcout. */
122  inline std::wostream& console()
123  {
124  return std::wcout;
125  }
126 
127  /** \brief Encapsulate cin. */
128  inline std::wistream& console_in()
129  {
130  return std::wcin;
131  }
132 
133 #else
134 
135  /** \brief Encapsulate cout.
136 
137  Used for supporting UNICODE more easily.
138  */
139  inline std::ostream& console()
140  {
141  return std::cout;
142  }
143 
144  /** \brief Encapsulate cin.
145 
146  Used for supporting UNICODE more easily.
147  */
148  inline std::istream& console_in()
149  {
150  return std::cin;
151  }
152 
153 #endif
154 
155  //------------------------------------------------------------------------------
156  /** \brief Bytecode values.
157 
158  \attention The order of the operator entries must match the order in ParserBase::c_DefaultOprt!
159  */
160  enum ECmdCode
161  {
162  // The following are codes for built in binary operators
163  // apart from built in operators the user has the opportunity to
164  // add user defined operators.
165  cmLE = 0, ///< Operator item: less or equal
166  cmGE = 1, ///< Operator item: greater or equal
167  cmNEQ = 2, ///< Operator item: not equal
168  cmEQ = 3, ///< Operator item: equals
169  cmLT = 4, ///< Operator item: less than
170  cmGT = 5, ///< Operator item: greater than
171  cmADD = 6, ///< Operator item: add
172  cmSUB = 7, ///< Operator item: subtract
173  cmMUL = 8, ///< Operator item: multiply
174  cmDIV = 9, ///< Operator item: division
175  cmPOW = 10, ///< Operator item: y to the power of ...
176  cmAND = 11, ///< Operator item: logical and
177  cmOR = 12, ///< Operator item: logical or
178  cmXOR = 13, ///< Operator item: logical xor
179  cmASSIGN = 14, ///< Operator item: Assignment operator
180  cmBO = 15, ///< Operator item: opening bracket
181  cmBC = 16, ///< Operator item: closing bracket
182  cmARG_SEP, ///< function argument separator
183  cmVAR, ///< variable item
184  cmVAL, ///< value item
185  cmFUNC, ///< Code for a function item
186  cmFUNC_STR, ///< Code for a function with a string parameter
187  cmSTRING, ///< Code for a string token
188  cmOPRT_BIN, ///< user defined binary operator
189  cmOPRT_POSTFIX, ///< code for postfix operators
190  cmOPRT_INFIX, ///< code for infix operators
191  cmEND, ///< end of formula
192  cmUNKNOWN ///< uninitialized item
193  };
194 
195  //------------------------------------------------------------------------------
196  /** \brief Types internally used by the parser.
197  */
199  {
200  tpSTR = 0, ///< String type (Function arguments and constants only, no string variables)
201  tpDBL = 1, ///< Floating point variables
202  tpVOID = 2 ///< Undefined type.
203  };
204 
205  //------------------------------------------------------------------------------
206  /** \brief Parser operator precedence values. */
207  enum EPrec
208  {
209  // binary operators
210  prLOGIC = 1, ///< logic operators
211  prCMP = 2, ///< comparsion operators
212  prADD_SUB = 3, ///< addition
213  prMUL_DIV = 4, ///< multiplication/division
214  prPOW = 5, ///< power operator priority (highest)
215 
216  // infix operators
217  prINFIX = 4, ///< Signs have a higher priority than ADD_SUB, but lower than power operator
218  prPOSTFIX = 4 ///< Postfix operator priority (currently unused)
219  };
220 
221  //------------------------------------------------------------------------------
222  // basic types
223 
224  /** \brief The numeric datatype used by the parser.
225 
226  Normally this is a floating point type either single or double precision.
227  */
229 
230  /** \brief The stringtype used by the parser.
231 
232  Depends on wether UNICODE is used or not.
233  */
235 
236  /** \brief The bytecode type used by the parser.
237 
238  The bytecode type depends on the value_type.
239  */
241 
242  /** \brief The character type used by the parser.
243 
244  Depends on wether UNICODE is used or not.
245  */
247 
248  /** \brief Typedef for easily using stringstream that respect the parser stringtype. */
249  typedef std::basic_stringstream<char_type,
250  std::char_traits<char_type>,
251  std::allocator<char_type> > stringstream_type;
252 
253  // Data container types
254 
255  /** \brief Type used for storing variables. */
256  typedef std::map<string_type, value_type*> varmap_type;
257 
258  /** \brief Type used for storing constants. */
259  typedef std::map<string_type, value_type> valmap_type;
260 
261  /** \brief Type for assigning a string name to an index in the internal string table. */
262  typedef std::map<string_type, std::size_t> strmap_type;
263 
264  // Parser callbacks
265 
266  /** \brief Callback type used for functions without arguments. */
267  typedef value_type (*fun_type0)();
268 
269  /** \brief Callback type used for functions with a single arguments. */
271 
272  /** \brief Callback type used for functions with two arguments. */
274 
275  /** \brief Callback type used for functions with three arguments. */
277 
278  /** \brief Callback type used for functions with four arguments. */
280 
281  /** \brief Callback type used for functions with five arguments. */
283 
284  /** \brief Callback type used for functions with a variable argument list. */
285  typedef value_type (*multfun_type)(const value_type*, int);
286 
287  /** \brief Callback type used for functions taking a string as an argument. */
288  typedef value_type (*strfun_type1)(const char_type*);
289 
290  /** \brief Callback type used for functions taking a string and a value as arguments. */
292 
293  /** \brief Callback type used for functions taking a string and two values as arguments. */
295 
296  /** \brief Callback used for functions that identify values in a string. */
297  typedef int (*identfun_type)(const char_type *sExpr, int *nPos, value_type *fVal);
298 
299  /** \brief Callback used for variable creation factory functions. */
300  typedef value_type* (*facfun_type)(const char_type*, void*);
301 
302  //------------------------------------------------------------------------------
303  /** \brief Static type checks
304 
305  I took the static assert from boost, but did not want to
306  add boost as a dependency to this project. For the original go to:
307 
308  http://www.boost.org/doc/html/boost_staticassert.html
309  */
310  template <bool> struct STATIC_ASSERTION_FAILURE;
311  template <> struct STATIC_ASSERTION_FAILURE<true> {};
312 
313  /** \brief This is a static typecheck.
314 
315  If you get a compiler error here you tried to use an unsigned bytecode map type!
316  */
318 } // end fo namespace
319 
320 #endif
321 
#define true
Definition: bool.h:35
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
#define MUP_BYTECODE_TYPE
Definition of the basic bytecode datatype.
Definition: muParserDef.h:51
#define MUP_STRING_TYPE
Definition of the basic parser string type.
Definition: muParserDef.h:66
#define MUP_BASETYPE
Define the base datatype for values.
Definition: muParserDef.h:45
This file contains compatibility fixes for some platforms.
Namespace for mathematical applications.
Definition: muParser.h:41
value_type(* multfun_type)(const value_type *, int)
Callback type used for functions with a variable argument list.
Definition: muParserDef.h:285
std::ostream & console()
Encapsulate cout.
Definition: muParserDef.h:139
int(* identfun_type)(const char_type *sExpr, int *nPos, value_type *fVal)
Callback used for functions that identify values in a string.
Definition: muParserDef.h:297
value_type(* strfun_type2)(const char_type *, value_type)
Callback type used for functions taking a string and a value as arguments.
Definition: muParserDef.h:291
value_type(* strfun_type3)(const char_type *, value_type, value_type)
Callback type used for functions taking a string and two values as arguments.
Definition: muParserDef.h:294
std::istream & console_in()
Encapsulate cin.
Definition: muParserDef.h:148
value_type(* strfun_type1)(const char_type *)
Callback type used for functions taking a string as an argument.
Definition: muParserDef.h:288
std::map< string_type, value_type > valmap_type
Type used for storing constants.
Definition: muParserDef.h:259
long bytecode_type
The bytecode type used by the parser.
Definition: muParserDef.h:240
value_type(* fun_type5)(value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:282
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:246
std::basic_stringstream< char_type, std::char_traits< char_type >, std::allocator< char_type > > stringstream_type
Typedef for easily using stringstream that respect the parser stringtype.
Definition: muParserDef.h:251
char MAP_TYPE_CANT_BE_UNSIGNED[sizeof(STATIC_ASSERTION_FAILURE< bytecode_type(-1)< 0 >)]
This is a static typecheck.
Definition: muParserDef.h:317
std::map< string_type, value_type * > varmap_type
Type used for storing variables.
Definition: muParserDef.h:256
ECmdCode
Bytecode values.
Definition: muParserDef.h:161
@ cmADD
Operator item: add.
Definition: muParserDef.h:171
@ cmGE
Operator item: greater or equal.
Definition: muParserDef.h:166
@ cmLT
Operator item: less than.
Definition: muParserDef.h:169
@ cmPOW
Operator item: y to the power of ...
Definition: muParserDef.h:175
@ cmASSIGN
Operator item: Assignment operator.
Definition: muParserDef.h:179
@ cmARG_SEP
function argument separator
Definition: muParserDef.h:182
@ cmLE
Operator item: less or equal.
Definition: muParserDef.h:165
@ cmBO
Operator item: opening bracket.
Definition: muParserDef.h:180
@ cmMUL
Operator item: multiply.
Definition: muParserDef.h:173
@ cmEND
end of formula
Definition: muParserDef.h:191
@ cmXOR
Operator item: logical xor.
Definition: muParserDef.h:178
@ cmSTRING
Code for a string token.
Definition: muParserDef.h:187
@ cmDIV
Operator item: division.
Definition: muParserDef.h:174
@ cmOPRT_INFIX
code for infix operators
Definition: muParserDef.h:190
@ cmAND
Operator item: logical and.
Definition: muParserDef.h:176
@ cmBC
Operator item: closing bracket.
Definition: muParserDef.h:181
@ cmOPRT_BIN
user defined binary operator
Definition: muParserDef.h:188
@ cmVAL
value item
Definition: muParserDef.h:184
@ cmUNKNOWN
uninitialized item
Definition: muParserDef.h:192
@ cmOR
Operator item: logical or.
Definition: muParserDef.h:177
@ cmNEQ
Operator item: not equal.
Definition: muParserDef.h:167
@ cmGT
Operator item: greater than.
Definition: muParserDef.h:170
@ cmEQ
Operator item: equals.
Definition: muParserDef.h:168
@ cmSUB
Operator item: subtract.
Definition: muParserDef.h:172
@ cmFUNC_STR
Code for a function with a string parameter.
Definition: muParserDef.h:186
@ cmFUNC
Code for a function item.
Definition: muParserDef.h:185
@ cmOPRT_POSTFIX
code for postfix operators
Definition: muParserDef.h:189
@ cmVAR
variable item
Definition: muParserDef.h:183
EPrec
Parser operator precedence values.
Definition: muParserDef.h:208
@ prPOSTFIX
Postfix operator priority (currently unused)
Definition: muParserDef.h:218
@ prINFIX
Signs have a higher priority than ADD_SUB, but lower than power operator.
Definition: muParserDef.h:217
@ prCMP
comparsion operators
Definition: muParserDef.h:211
@ prLOGIC
logic operators
Definition: muParserDef.h:210
@ prPOW
power operator priority (highest)
Definition: muParserDef.h:214
@ prADD_SUB
addition
Definition: muParserDef.h:212
@ prMUL_DIV
multiplication/division
Definition: muParserDef.h:213
value_type(* fun_type2)(value_type, value_type)
Callback type used for functions with two arguments.
Definition: muParserDef.h:273
ETypeCode
Types internally used by the parser.
Definition: muParserDef.h:199
@ tpVOID
Undefined type.
Definition: muParserDef.h:202
@ tpDBL
Floating point variables.
Definition: muParserDef.h:201
@ tpSTR
String type (Function arguments and constants only, no string variables)
Definition: muParserDef.h:200
value_type(* fun_type3)(value_type, value_type, value_type)
Callback type used for functions with three arguments.
Definition: muParserDef.h:276
double value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:228
value_type(* fun_type4)(value_type, value_type, value_type, value_type)
Callback type used for functions with four arguments.
Definition: muParserDef.h:279
std::string string_type
The stringtype used by the parser.
Definition: muParserDef.h:234
std::map< string_type, std::size_t > strmap_type
Type for assigning a string name to an index in the internal string table.
Definition: muParserDef.h:262
value_type(* fun_type0)()
Callback type used for functions without arguments.
Definition: muParserDef.h:267
value_type(* fun_type1)(value_type)
Callback type used for functions with a single arguments.
Definition: muParserDef.h:270
Static type checks.
Definition: muParserDef.h:310
Modified on Sun Apr 21 03:42:15 2024 by modify_doxy.py rev. 669887