GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
ExpressionArray.hpp
Go to the documentation of this file.
1 // This file is part of GranOO, a workbench for DEM simulation.
2 //
3 // Author(s) : - Damien Andre IRCER/UNILIM, Limoges France
4 // <damien.andre@unilim.fr>
5 // - Jean-luc Charles Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
6 // <jean-luc.charles@ensam.eu>
7 // - Jeremie Girardot Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
8 // <jeremie.girardot@ensam.eu>
9 // - Cedric Hubert LAMIH/UPHF, Valenciennes France
10 // <cedric.hubert@uphf.fr>
11 // - Ivan Iordanoff Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
12 // <ivan.iordanoff@ensam.eu>
13 //
14 // Copyright (C) 2008-2019 D. Andre, JL. Charles, J. Girardot, C. Hubert, I. Iordanoff
15 //
16 // This program is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 // This program is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with this program. If not, see <http://www.gnu.org/licenses/>.
28 
29 
30 
31 #ifndef GranOO_Math_ExpressionArray_hpp
32 #define GranOO_Math_ExpressionArray_hpp
33 
34 
35 #include <string>
36 #include <boost/assign.hpp>
37 
39 
42 
43 
44 namespace GranOO3
45 {
46  namespace Math
47  {
48 
49  template<int N>
51  {
52 
53  public:
54  ExpressionArray(const std::string& l1, const std::string& l2, const std::string& l3);
55  ExpressionArray(const std::string& l1, const std::string& l2, const std::string& l3, const std::string& l4);
56  ExpressionArray(const std::string& l1, const std::string& l2, const std::string& l3, const std::string& l4, const std::string& l5);
57  ExpressionArray(const std::string& l1, const std::string& l2, const std::string& l3, const std::string& l4, const std::string& l5, const std::string& l6);
58 
59  void parse_xml();
60  bool expression_exist(const std::string& l) const;
61  double value(const std::string& l);
62  double value(const std::string& l) const;
63 
64 
65  Expression& get_expression(const std::string& l);
66  const Expression& get_expression(const std::string& l) const;
67 
68  private:
69  ExpressionArray() = delete;
70  ExpressionArray(const ExpressionArray&) = delete;
72 
73  private:
74  std::array<std::string, N> _label;
75  std::array<Expression*, N> _expression;
76  std::map<const std::string, int> _map;
77  };
78 
79  template<int N>
80  inline void
82  int number = 0;
83  std::string labels = "";
85  for (unsigned int i = 0; i < N; ++i) {
86  const std::string& attr = _label[i];
87  labels += attr + " ";
88  Expression*& expr = _expression[i];
89 
90  if (parser.attribute_exist(attr) == false) {
91  expr = nullptr;
92  } else {
93  number++;
94  const std::string str = parser.read_attribute<std::string>(attr, "");
95  AssertMsg(str != "", "Internal error, the string can not be empty");
96 
97  // check if the expression is from an id
100  } else {
101  // ok, trying to instanciate the expression
102  parser.XmlAssert(Expression::Check(str),
103  "The formula " + Core::String::quote(str) + " in not valid");
104  expr = new Expression(str);
105  }
106  }
107  }
108  }
109 
110  template<int N>
111  inline bool
112  ExpressionArray<N>::expression_exist(const std::string& l) const {
113  AssertMsg(_map.count(l) == 1, "Can't find the " + Core::String::quote(l) + " label");
114  const unsigned int i = _map.at(l);
115  return (_expression[i] != nullptr);
116  }
117 
118  template<int N>
119  inline double
120  ExpressionArray<N>::value(const std::string& l) {
121  AssertMsg(_map.count(l) == 1, "Can't find the " + Core::String::quote(l) + " label");
122  const unsigned int i = _map.at(l);
123  AssertMsg(_expression[i] != nullptr,
124  "The expression labeled " + Core::String::quote(l) + " does not exist");
125  return (*_expression[i])();
126  }
127 
128  template<int N>
129  inline double
130  ExpressionArray<N>::value(const std::string& l) const {
131  AssertMsg(_map.count(l) == 1, "Can't find the " + Core::String::quote(l) + " label");
132  const unsigned int i = _map.at(l);
133  AssertMsg(_expression[i] != nullptr,
134  "The expression labeled " + Core::String::quote(l) + " does not exist");
135  return (*_expression[i])();
136  }
137 
138  template<int N>
139  inline Expression&
140  ExpressionArray<N>::get_expression(const std::string& l) {
141  AssertMsg(_map.count(l) == 1, "Can't find the " + Core::String::quote(l) + " label");
142  const unsigned int i = _map.at(l);
143  AssertMsg(_expression[i] != nullptr,
144  "The expression labeled " + Core::String::quote(l) + " does not exist");
145  return *_expression[i];
146  }
147 
148  template<int N>
149  inline const Expression&
150  ExpressionArray<N>::get_expression(const std::string& l) const {
151  AssertMsg(_map.count(l) == 1, "Can't find the " + Core::String::quote(l) + " label");
152  const unsigned int i = _map.at(l);
153  AssertMsg(_expression[i] != nullptr,
154  "The expression labeled " + Core::String::quote(l) + " does not exist");
155  return *_expression[i];
156  }
157 
158  }
159 
160 }
161 
162 
163 
164 
165 #endif
#define AssertMsg(condition, message)
Definition: Macro.hpp:67
Definition: XmlParser.hpp:84
static T * get_item(const std::string &)
Definition: XmlObjectManager.hpp:124
Definition: XmlParser.hpp:122
bool attribute_exist(const std::string &)
static XmlParser & get()
void read_attribute(const Attribute::State, const std::string &, T &)
Definition: ExpressionArray.hpp:51
ExpressionArray(const std::string &l1, const std::string &l2, const std::string &l3, const std::string &l4, const std::string &l5, const std::string &l6)
ExpressionArray(const std::string &l1, const std::string &l2, const std::string &l3, const std::string &l4)
ExpressionArray(const ExpressionArray &)=delete
ExpressionArray & operator=(const ExpressionArray &)=delete
const Expression & get_expression(const std::string &l) const
Definition: ExpressionArray.hpp:150
std::array< std::string, N > _label
Definition: ExpressionArray.hpp:74
std::array< Expression *, N > _expression
Definition: ExpressionArray.hpp:75
double value(const std::string &l) const
Definition: ExpressionArray.hpp:130
Expression & get_expression(const std::string &l)
Definition: ExpressionArray.hpp:140
ExpressionArray(const std::string &l1, const std::string &l2, const std::string &l3)
double value(const std::string &l)
Definition: ExpressionArray.hpp:120
bool expression_exist(const std::string &l) const
Definition: ExpressionArray.hpp:112
ExpressionArray(const std::string &l1, const std::string &l2, const std::string &l3, const std::string &l4, const std::string &l5)
std::map< const std::string, int > _map
Definition: ExpressionArray.hpp:76
void parse_xml()
Definition: ExpressionArray.hpp:81
Definition: Expression.hpp:46
static bool Check(const std::string &str)
Definition: Expression.cpp:62
Definition: Common.hpp:198
static std::string quote(const std::string &str)
Definition: String.cpp:73