GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
FunctionT.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_FunctionT_hpp_
32 #define _GranOO_Math_FunctionT_hpp_
33 
34 #include <type_traits>
35 
37 #include "GranOO3/Math/Symbol.hpp"
38 #include "GranOO3/Core/Macro.hpp"
39 #include "GranOO3/Core/Doc.hpp"
40 
41 namespace GranOO3
42 {
43  namespace Math
44  {
45 
46  template<class T>
47  class FunctionT : public Function
48  {
49  public:
50  FunctionT(unsigned int varNumber);
51  FunctionT(unsigned int varNumber, const std::string& symbol, const std::string desc = "");
52  virtual ~FunctionT();
53 
54  void record(const std::string&);
55 
56  private:
57  FunctionT() = delete;
58  FunctionT(const FunctionT&) = delete;
59  FunctionT& operator=(const FunctionT&) = delete;
60 
61  private:
62  std::string _symbol;
63  bool _recorded;
64  };
65 
66  template<class T>
67  inline
68  FunctionT<T>::FunctionT(unsigned int varNumber)
69  : Function(varNumber),
70  _symbol(""),
71  _recorded(false) {
72  static_assert(std::is_base_of<FunctionT<T>, T>::value,
73  "T must be a descendant of Function<T>");
74  }
75 
76  template<class T>
77  inline
78  FunctionT<T>::FunctionT(unsigned int varNumber, const std::string& symbol, const std::string desc)
79  : Function(varNumber),
80  _symbol(""),
81  _recorded(false) {
82  static_assert(std::is_base_of<FunctionT<T>, T>::value,
83  "T must be a descendant of Function<T>");
84  record(symbol);
85 
86  if (desc != "")
87  Core::Doc::add("Math::Function", T::class_ID(), __FILE__, desc);
88  }
89 
90  template<class T>
91  inline
93  if (_recorded) {
94  const bool ok = Symbol::Table().remove_function(_symbol);
95  AssertMsg(ok == true, "Can't deregister this function");
96  AssertMsg(_recorded_function.count(_symbol)==1, "Can't find this symbol in map");
97  _recorded_function.erase(_symbol);
98  }
99  }
100 
101  template<class T>
102  inline void
103  FunctionT<T>::record(const std::string& symbol) {
104  AssertMsg(_recorded_function.count(symbol) == 0, "This symbol is already used");
105  AssertMsg(_recorded == false, "This function was already registered");
106  _symbol = symbol;
107  AssertMsg(symbol != "", "The symbol can not be empty");
108  _recorded = true;
109  const bool ok = Symbol::Table().add_function(_symbol, static_cast<T&>(*this));
110  AssertMsg(ok == true, "Can't register this function");
111  _recorded_function[symbol] = this;
112  }
113 
114  }
115 }
116 #endif
#define AssertMsg(condition, message)
Definition: Macro.hpp:67
static bool add(const std::string &section, const std::string &subsection, const std::string &file_name, const std::string &description)
Definition: Doc.cpp:60
Definition: Function.hpp:42
Definition: FunctionT.hpp:48
void record(const std::string &)
Definition: FunctionT.hpp:103
virtual ~FunctionT()
Definition: FunctionT.hpp:92
FunctionT & operator=(const FunctionT &)=delete
bool _recorded
Definition: FunctionT.hpp:63
FunctionT(unsigned int varNumber)
Definition: FunctionT.hpp:68
std::string _symbol
Definition: FunctionT.hpp:62
FunctionT(unsigned int varNumber, const std::string &symbol, const std::string desc="")
Definition: FunctionT.hpp:78
FunctionT(const FunctionT &)=delete
static exprtk::symbol_table< double > & Table()
Definition: Symbol.cpp:41
bool add_function(const std::string &function_name, function_t &function)
Definition: Exprtk.hpp:20132
bool remove_function(const std::string &function_name)
Definition: Exprtk.hpp:20340
static const char * desc
Definition: Between2SetOf.cpp:37
Definition: Common.hpp:198
T value(details::expression_node< T > *n)
Definition: Exprtk.hpp:15070