ug4
global_function.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3  * Authors: Sebastian Reiter, Andreas Vogel
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG_BRIDGE__GLOBAL_FUNCTION__
34 #define __H__UG_BRIDGE__GLOBAL_FUNCTION__
35 
36 #include <string>
37 #include <vector>
38 #include <sstream>
39 #include "parameter_stack.h"
40 #include "function_traits.h"
42 #include "common/ug_config.h"
43 #include <iostream>
44 
45 #ifdef PROFILE_BRIDGE
46 #ifndef UG_PROFILER
47  #error "You need to define UG_PROFILER to use PROFILE_BRIDGE"
48 #endif
50 
51 #endif
52 
53 namespace ug
54 {
55 namespace bridge
56 {
57 
60 
63 
66 {
67  public:
68  ExportedFunctionBase(const std::string& funcName, const std::string& funcOptions,
69  const std::string& retValInfos, const std::string& paramInfos,
70  const std::string& tooltip, const std::string& help);
71 
73  const std::string& name() const {return m_name;}
74 
76  const std::string& options() const {return m_methodOptions;}
77 
79  const std::string& return_name() const {return return_info(0);}
80 
82  const std::string& return_info(size_t i) const {return m_vRetValInfo.at(i);}
83 
85  const std::vector<std::string>& return_info_vec() const {return m_vRetValInfo;}
86 
88  size_t num_parameter() const {return m_vvParamInfo.size();}
89 
91  size_t num_infos(size_t i) const {return m_vvParamInfo.at(i).size();}
92 
94  const char* parameter_class_name(size_t i) const {return params_in().class_name((int)i);}
95 
97  const std::string& parameter_name(size_t i) const {return parameter_info(i, 0);}
98 
100  const std::string& parameter_info(size_t i, size_t j) const {return m_vvParamInfo.at(i).at(j);}
101 
103  const std::vector<std::string>& parameter_info_vec(size_t i) const {return m_vvParamInfo.at(i);}
104 
106  const std::string& parameter_info_string() const {return m_paramInfos;}
107 
109  const std::string& tooltip() const {return m_tooltip;}
110 
112  const std::string& help() const {return m_help;}
113 
115  const ParameterInfo& params_in() const {return m_paramsIn;}
116 
118  const ParameterInfo& params_out() const {return m_paramsOut;}
119 
120  // todo: we export non-const here, since we can not make ExportedClass<TClass> a friend
122  ParameterInfo& params_in() {return m_paramsIn;}
123 
125  bool check_consistency(std::string classname = "") const;
126 
127  protected:
128  template <typename TFunc>
130  {
132  // Create parameter stack for PARAMETERS
134  typedef typename func_traits<TFunc>::params_type params_type;
136 
137  // arbitrary choosen minimum number of infos exported
138  // (If values non given we set them to an empty string)
139  const size_t MinNumInfos = 3; // for "name | style | options"
140 
141  // Fill missing Parameter
142  m_vvParamInfo.resize(m_paramsIn.size());
143  // resize missing infos for each parameter
144  for(int i = 0; i < (int)m_vvParamInfo.size(); ++i)
145  for(size_t j = m_vvParamInfo.at(i).size(); j < MinNumInfos; ++j)
146  m_vvParamInfo.at(i).push_back(std::string(""));
147 
149  // Create parameter stack for RETURN VALUES
151  typedef typename func_traits<TFunc>::return_type return_type;
153 
154  // resize missing infos for return value
155  for(size_t j = m_vRetValInfo.size(); j < MinNumInfos; ++j)
156  m_vRetValInfo.push_back(std::string(""));
157  }
158 
159  // help function to tokenize the parameter string
160  void tokenize(const std::string& str, std::vector<std::string>& tokens,
161  const char delimiter);
162 
163  protected:
164  std::string m_name;
165  std::string m_methodOptions;
166 
167  std::string m_retValInfos; // string with Infos about return type
168  std::vector<std::string> m_vRetValInfo; // tokenized Infos
169 
170  // string with Infos about parameter
171  std::string m_paramInfos;
172 
173  // tokenized strings for each Parameter and each Info (name | #style | options | ...)
174  std::vector<std::vector<std::string> > m_vvParamInfo;
175 
176  std::string m_tooltip;
177  std::string m_help;
178 
181 };
182 
183 
186 {
187  public:
188  // all c++ functions are wrapped by a proxy function of the following type
189  typedef void (*ProxyFunc)(void* func, const ParameterStack& in, ParameterStack& out);
190 
191  template <typename TFunc>
192  ExportedFunction( TFunc f, ProxyFunc pf,
193  const std::string& name, const std::string& funcOptions,
194  const std::string& group,
195  const std::string& retValInfos, const std::string& paramInfos,
196  const std::string& tooltip, const std::string& help)
197  : ExportedFunctionBase(name, funcOptions, retValInfos,
198  paramInfos, tooltip, help),
199  m_group(group), m_func((void*)f), m_proxy_func(pf)
200 #ifdef PROFILE_BRIDGE
201  ,m_dpi(ExportedFunctionBase::name().c_str(), true, "registry", false)
202 #endif
203  {
204  create_parameter_stack<TFunc>();
205  }
206 
208  void execute(const ParameterStack& paramsIn, ParameterStack& paramsOut) const
209  {
210 #ifdef PROFILE_BRIDGE
211  m_dpi.beginNode();
212 #endif
213  m_proxy_func(m_func, paramsIn, paramsOut);
214 
215 #ifdef PROFILE_BRIDGE
216  m_dpi.endNode();
217 #endif
218 
219  }
220 
222  const std::string& group() const {return m_group;}
223 
224  protected:
226  std::string m_group;
227 
229  void* m_func;
230 
232  ProxyFunc m_proxy_func;
233 
234 #ifdef PROFILE_BRIDGE
235  mutable RuntimeProfileInfo m_dpi;
236 #endif
237 };
238 
240 // ExportedFunctionGroup (sreiter)
243 {
244  public:
246  ExportedFunctionGroup(const std::string& name) : m_name(name){}
247 
250  {
251  for(size_t i = 0; i < m_overloads.size(); ++i)
252  delete m_overloads[i].m_func;
253  }
254 
256  const std::string& name() const {return m_name;}
257 
259  template <class TFunc>
262  const std::string& funcOptions, const std::string& group,
263  const std::string& retValInfos, const std::string& paramInfos,
264  const std::string& tooltip, const std::string& help)
265  {
266  size_t typeID = GetUniqueTypeID<TFunc>();
267 
268  // make sure that the overload didn't exist
269  if(get_overload_by_type_id(typeID))return NULL;
270 
271  // create a new overload
272  ExportedFunction* func = new ExportedFunction(f, pf, m_name,
273  funcOptions, group, retValInfos,
274  paramInfos, tooltip, help);
275 
276  m_overloads.push_back(Overload(func, typeID));
277  return func;
278  }
279 
280  size_t num_overloads() const {return m_overloads.size();}
281 
283  {return m_overloads.at(index).m_func;}
284 
285  const ExportedFunction* get_overload(size_t index) const
286  {return m_overloads.at(index).m_func;}
287 
288  template <class TType>
290  {
291  size_t typeID = GetUniqueTypeID<TType>();
292  return get_overload_by_type_id(typeID);
293  }
294 
295  template <class TType>
297  {
298  size_t typeID = GetUniqueTypeID<TType>();
299  return get_overload_by_type_id(typeID);
300  }
301 
303  {
304  for(size_t i = 0; i < m_overloads.size(); ++i){
305  if(m_overloads[i].m_typeID == typeID)
306  return m_overloads[i].m_func;
307  }
308  return NULL;
309  }
310 
311  const ExportedFunction* get_overload_by_type_id(size_t typeID) const
312  {
313  for(size_t i = 0; i < m_overloads.size(); ++i){
314  if(m_overloads[i].m_typeID == typeID)
315  return m_overloads[i].m_func;
316  }
317  return NULL;
318  }
319 
320  size_t get_overload_type_id(size_t index) const
321  {return m_overloads.at(index).m_typeID;}
322 
323  private:
324  struct Overload{
325  Overload() {}
326  Overload(ExportedFunction* func, size_t typeID) :
327  m_func(func), m_typeID(typeID) {}
329  size_t m_typeID;
330  };
331 
332  std::string m_name;
333  std::vector<Overload> m_overloads;
334 };
335 
339 template <typename TFunc, typename TRet = typename func_traits<TFunc>::return_type>
341 {
342  static void apply(void* func, const ParameterStack& in, ParameterStack& out)
343  {
344  typedef typename func_traits<TFunc>::params_type params_type;
345  TFunc fp = (TFunc) func;
346 
347  // convert parameter stack
349 
350  // apply
351  TRet res = func_traits<TFunc>::apply(fp, args);
352 
353  // write result
354  out.push<TRet>(res);
355  }
356 };
357 
358 // specialization if no return value given
359 template <typename TFunc>
360 struct FunctionProxy<TFunc, void>
361 {
362  static void apply(void* func, const ParameterStack& in, ParameterStack& out)
363  {
364  typedef typename func_traits<TFunc>::params_type params_type;
365  TFunc fp = (TFunc) func;
366 
367  // convert parameter stack
369 
370  // apply
371  func_traits<TFunc>::apply(fp, args);
372  }
373 };
374 
375 // end group registry
377 
378 } // end namespace bridge
379 } // end namespace ug
380 
381 
382 #endif /* __H__UG_BRIDGE__GLOBAL_FUNCTION__ */
location name
Definition: checkpoint_util.lua:128
Base class for function/method export.
Definition: global_function.h:66
const ParameterInfo & params_out() const
parameter list for input values
Definition: global_function.h:118
const std::string & options() const
name of function
Definition: global_function.h:76
std::vector< std::string > m_vRetValInfo
Definition: global_function.h:168
ParameterInfo m_paramsIn
Definition: global_function.h:179
std::string m_tooltip
Definition: global_function.h:176
std::string m_methodOptions
Definition: global_function.h:165
ParameterInfo m_paramsOut
Definition: global_function.h:180
std::string m_name
Definition: global_function.h:164
std::string m_help
Definition: global_function.h:177
size_t num_parameter() const
number of parameters.
Definition: global_function.h:88
void create_parameter_stack()
Definition: global_function.h:129
std::string m_retValInfos
Definition: global_function.h:167
const std::string & help() const
help informations
Definition: global_function.h:112
const std::string & parameter_name(size_t i) const
name of parameter i
Definition: global_function.h:97
const std::string & name() const
name of function
Definition: global_function.h:73
std::vector< std::vector< std::string > > m_vvParamInfo
Definition: global_function.h:174
const std::string & return_name() const
name of return value
Definition: global_function.h:79
size_t num_infos(size_t i) const
number of info strings for one parameter
Definition: global_function.h:91
const std::vector< std::string > & return_info_vec() const
type info of return value
Definition: global_function.h:85
const std::string & parameter_info_string() const
whole string of all type infos for of all parameters
Definition: global_function.h:106
const std::string & return_info(size_t i) const
type info of return type
Definition: global_function.h:82
const std::string & parameter_info(size_t i, size_t j) const
type info of all parameters
Definition: global_function.h:100
const char * parameter_class_name(size_t i) const
class name of parameter i
Definition: global_function.h:94
ParameterInfo & params_in()
non-const export of param list
Definition: global_function.h:122
const std::string & tooltip() const
gives some information to the exported functions
Definition: global_function.h:109
const ParameterInfo & params_in() const
parameter list for input values
Definition: global_function.h:115
std::string m_paramInfos
Definition: global_function.h:171
const std::vector< std::string > & parameter_info_vec(size_t i) const
type info of i th parameters
Definition: global_function.h:103
Groups of Functions - useful to realize overloaded functions.
Definition: global_function.h:243
std::string m_name
Definition: global_function.h:332
size_t get_overload_type_id(size_t index) const
Definition: global_function.h:320
ExportedFunction * get_overload_by_type_id(size_t typeID)
Definition: global_function.h:302
std::vector< Overload > m_overloads
Definition: global_function.h:333
ExportedFunction * add_overload(TFunc f, ExportedFunction::ProxyFunc pf, const std::string &funcOptions, const std::string &group, const std::string &retValInfos, const std::string &paramInfos, const std::string &tooltip, const std::string &help)
adds an overload. Returns false if the overload already existed.
Definition: global_function.h:261
const ExportedFunction * get_overload_by_type_id(size_t typeID) const
Definition: global_function.h:311
ExportedFunction * get_overload_by_type()
Definition: global_function.h:289
ExportedFunction * get_overload(size_t index)
Definition: global_function.h:282
const ExportedFunction * get_overload_by_type() const
Definition: global_function.h:296
ExportedFunctionGroup(const std::string &name)
constructor
Definition: global_function.h:246
~ExportedFunctionGroup()
destructor
Definition: global_function.h:249
size_t num_overloads() const
Definition: global_function.h:280
const ExportedFunction * get_overload(size_t index) const
Definition: global_function.h:285
const std::string & name() const
name of function group
Definition: global_function.h:256
This class describes a wrapper for a c++ - function, that is exported by ug.
Definition: global_function.h:186
ExportedFunction(TFunc f, ProxyFunc pf, const std::string &name, const std::string &funcOptions, const std::string &group, const std::string &retValInfos, const std::string &paramInfos, const std::string &tooltip, const std::string &help)
Definition: global_function.h:192
std::string m_group
save groups
Definition: global_function.h:226
void(* ProxyFunc)(void *func, const ParameterStack &in, ParameterStack &out)
Definition: global_function.h:189
void execute(const ParameterStack &paramsIn, ParameterStack &paramsOut) const
executes the function
Definition: global_function.h:208
ProxyFunc m_proxy_func
proxy function
Definition: global_function.h:232
const std::string & group() const
return groups
Definition: global_function.h:222
void * m_func
pointer to to function
Definition: global_function.h:229
a stack holding parameter infos about a parameter stack
Definition: parameter_stack.h:73
A stack that can hold values together with their type-id.
Definition: parameter_stack.h:270
void push(void *ptr, const ClassNameNode *classNameNode)
Definition: parameter_stack.h:367
#define UG_API
Definition: ug_config.h:65
the ug namespace
function func(x, y, z, t, si)
static void create(ParameterInfo &in)
Definition: param_to_type_value_list.h:92
static void create(ParameterInfo &stack)
Definition: param_to_type_value_list.h:115
Definition: global_function.h:324
size_t m_typeID
Definition: global_function.h:329
Overload()
Definition: global_function.h:325
ExportedFunction * m_func
Definition: global_function.h:328
Overload(ExportedFunction *func, size_t typeID)
Definition: global_function.h:326
static void apply(void *func, const ParameterStack &in, ParameterStack &out)
Definition: global_function.h:362
Definition: global_function.h:341
static void apply(void *func, const ParameterStack &in, ParameterStack &out)
Definition: global_function.h:342
Definition: param_to_type_value_list.h:71
Exception throw, if method name has not been given.
Definition: global_function.h:62
Definition: function_traits.h:59