muParser API -  1.35
muParserInt.h
Go to the documentation of this file.
1 /*
2 
3  _____ __ _____________ _______ ______ ___________
4  / \| | \____ \__ \\_ __ \/ ___// __ \_ __ \
5  | Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
6  |__|_| /____/| __(____ /__| /____ >\___ >__|
7  \/ |__| \/ \/ \/
8  Copyright (C) 2004 - 2020 Ingo Berg
9 
10  Redistribution and use in source and binary forms, with or without modification, are permitted
11  provided that the following conditions are met:
12 
13  * Redistributions of source code must retain the above copyright notice, this list of
14  conditions and the following disclaimer.
15  * Redistributions in binary form must reproduce the above copyright notice, this list of
16  conditions and the following disclaimer in the documentation and/or other materials provided
17  with the distribution.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef MU_PARSER_INT_H
30 #define MU_PARSER_INT_H
31 
32 #include "muParserBase.h"
33 #include <vector>
34 
35 
36 /** \file
37  \brief Definition of a parser using integer value.
38 */
39 
40 
41 namespace mu
42 {
43 
44  /** \brief Mathematical expressions parser.
45 
46  This version of the parser handles only integer numbers. It disables the built in operators thus it is
47  slower than muParser. Integer values are stored in the double value_type and converted if needed.
48  */
49  class ParserInt : public ParserBase
50  {
51  private:
52  static int Round(value_type v) { return (int)(v + ((v >= 0) ? 0.5 : -0.5)); };
53 
54  static value_type Abs(value_type);
55  static value_type Sign(value_type);
57  // !! The unary Minus is a MUST, otherwise you can't use negative signs !!
58  static value_type UnaryMinus(value_type);
59  // Functions with variable number of arguments
60  static value_type Sum(const value_type* a_afArg, int a_iArgc); // sum
61  static value_type Min(const value_type* a_afArg, int a_iArgc); // minimum
62  static value_type Max(const value_type* a_afArg, int a_iArgc); // maximum
63  // binary operator callbacks
64  static value_type Add(value_type v1, value_type v2);
65  static value_type Sub(value_type v1, value_type v2);
66  static value_type Mul(value_type v1, value_type v2);
67  static value_type Div(value_type v1, value_type v2);
68  static value_type Mod(value_type v1, value_type v2);
69  static value_type Pow(value_type v1, value_type v2);
70  static value_type Shr(value_type v1, value_type v2);
71  static value_type Shl(value_type v1, value_type v2);
72  static value_type LogAnd(value_type v1, value_type v2);
73  static value_type LogOr(value_type v1, value_type v2);
74  static value_type And(value_type v1, value_type v2);
75  static value_type Or(value_type v1, value_type v2);
76  static value_type Xor(value_type v1, value_type v2);
77  static value_type Less(value_type v1, value_type v2);
78  static value_type Greater(value_type v1, value_type v2);
79  static value_type LessEq(value_type v1, value_type v2);
80  static value_type GreaterEq(value_type v1, value_type v2);
81  static value_type Equal(value_type v1, value_type v2);
82  static value_type NotEqual(value_type v1, value_type v2);
83  static value_type Not(value_type v1);
84 
85  static int IsHexVal(const char_type* a_szExpr, int* a_iPos, value_type* a_iVal);
86  static int IsBinVal(const char_type* a_szExpr, int* a_iPos, value_type* a_iVal);
87  static int IsVal(const char_type* a_szExpr, int* a_iPos, value_type* a_iVal);
88 
89  /** \brief A facet class used to change decimal and thousands separator. */
90  template<class TChar>
91  class change_dec_sep : public std::numpunct<TChar>
92  {
93  public:
94 
95  explicit change_dec_sep(char_type cDecSep, char_type cThousandsSep = 0, int nGroup = 3)
96  :std::numpunct<TChar>()
97  , m_cDecPoint(cDecSep)
98  , m_cThousandsSep(cThousandsSep)
99  , m_nGroup(nGroup)
100  {}
101 
102  protected:
103 
104  virtual char_type do_decimal_point() const
105  {
106  return m_cDecPoint;
107  }
108 
109  virtual char_type do_thousands_sep() const
110  {
111  return m_cThousandsSep;
112  }
113 
114  virtual std::string do_grouping() const
115  {
116  // fix for issue 4: https://code.google.com/p/muparser/issues/detail?id=4
117  // courtesy of Jens Bartsch
118  // original code:
119  // return std::string(1, (char)m_nGroup);
120  // new code:
121  return std::string(1, (char)(m_cThousandsSep > 0 ? m_nGroup : CHAR_MAX));
122  }
123 
124  private:
125 
126  int m_nGroup;
127  char_type m_cDecPoint;
128  char_type m_cThousandsSep;
129  };
130 
131  public:
132  ParserInt();
133 
134  virtual void InitFun();
135  virtual void InitOprt();
136  virtual void InitConst();
137  virtual void InitCharSets();
138  };
139 
140 } // namespace mu
141 
142 #endif
143 
ParserInt()
Constructor.
virtual void InitFun()
Initialize the default functions.
virtual void InitOprt()
Initialize operators.
This file contains the class definition of the muparser engine.
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:294
Mathematical expressions parser.
Definition: muParserInt.h:49
Namespace for mathematical applications.
Definition: muParser.cpp:46
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:306
Mathematical expressions parser (base parser engine).
Definition: muParserBase.h:63