Pol  Revision:3cfda13
execmodl.h
Go to the documentation of this file.
1 
8 #ifndef BSCRIPT_EXECMODL_H
9 #define BSCRIPT_EXECMODL_H
10 
11 #ifndef BSCRIPT_BOBJECT_H
12 #include "bobject.h"
13 #endif
14 
15 #include <map>
16 #include <string>
17 #include <vector>
18 
19 #include "../clib/boostutils.h"
20 #include "../clib/compilerspecifics.h"
21 #include "../clib/maputil.h"
22 #include "executor.h"
23 
24 namespace Pol
25 {
26 namespace Bscript
27 {
28 class Executor;
29 } // namespace Bscript
30 } // namespace Pol
31 
32 namespace Pol
33 {
34 namespace Bscript
35 {
36 class ExecutorModule;
37 class String;
38 
40 
42 {
43 public:
44  virtual ~ExecutorModule() = default;
45 
46  BObjectImp* getParamImp( unsigned param );
47  BObjectImp* getParamImp( unsigned param, BObjectImp::BObjectType type );
48  bool getParamImp( unsigned param, BObjectImp*& imp );
49 
50  const String* getStringParam( unsigned param );
51  void* getApplicPtrParam( unsigned param, const BApplicObjType* pointer_type );
52  BApplicObjBase* getApplicObjParam( unsigned param, const BApplicObjType* object_type );
53  bool getStringParam( unsigned param, const String*& pstr );
54  bool getRealParam( unsigned param, double& value );
55  bool getObjArrayParam( unsigned param, ObjArray*& pobjarr );
56 
57  bool getParam( unsigned param, int& value );
58  bool getParam( unsigned param, int& value, int maxval );
59  bool getParam( unsigned param, int& value, int minval, int maxval );
60 
61  bool getParam( unsigned param, unsigned& value );
62 
63  bool getParam( unsigned param, short& value );
64  bool getParam( unsigned param, short& value, short maxval );
65  bool getParam( unsigned param, short& value, short minval, short maxval );
66 
67  bool getParam( unsigned param, unsigned short& value );
68  bool getParam( unsigned param, unsigned short& value, unsigned short maxval );
69  bool getParam( unsigned param, unsigned short& value, unsigned short minval,
70  unsigned short maxval );
71 
72  const std::string& scriptname() const;
74 
75 protected:
76  ExecutorModule( const char* moduleName, Executor& iExec );
77 
79 
80  friend class Executor;
81 
82  virtual int functionIndex( const std::string& funcname ) = 0; // returns -1 on not found
83  virtual BObjectImp* execFunc( unsigned idx ) = 0;
84  virtual std::string functionName( unsigned idx ) = 0;
85 
86 private: // not implemented
87  ExecutorModule( const ExecutorModule& exec ) = delete;
88  ExecutorModule& operator=( const ExecutorModule& exec ) = delete;
89 };
90 
91 // FIXME: this function doesn't seem to work.
92 template <class T>
94  const BApplicObjType* object_type )
95 {
96  return static_cast<BApplicObj<T>*>( ex.getApplicObjParam( param, object_type ) );
97 }
98 
99 template <class T>
101 {
102 protected:
103  TmplExecutorModule( const char* modname, Executor& exec );
104 
105 
106 private:
107  struct FunctionDef
108  {
109  std::string funcname;
110  BObjectImp* ( T::*fptr )();
111  };
112  using FunctionTable = std::vector<FunctionDef>;
113 
114  static FunctionTable function_table;
115  static std::map<std::string, int, Clib::ci_cmp_pred> _func_idx_map;
116  static bool _func_map_init;
117 
118 protected:
119  virtual int functionIndex( const std::string& funcname ) POL_OVERRIDE;
120  virtual BObjectImp* execFunc( unsigned idx ) POL_OVERRIDE;
121  virtual std::string functionName( unsigned idx ) POL_OVERRIDE;
122 };
123 
124 template <class T>
125 std::map<std::string, int, Clib::ci_cmp_pred> TmplExecutorModule<T>::_func_idx_map;
126 
127 template <class T>
129 
130 template <class T>
132  : ExecutorModule( modname, ex )
133 {
134  if ( !_func_map_init )
135  {
136  for ( unsigned idx = 0; idx < function_table.size(); idx++ )
137  {
138  _func_idx_map[function_table[idx].funcname] = idx;
139  }
140  _func_map_init = true;
141  }
142 }
143 
144 template <class T>
145 inline int TmplExecutorModule<T>::functionIndex( const std::string& name )
146 {
147  auto itr = _func_idx_map.find( name );
148  if ( itr != _func_idx_map.end() )
149  return itr->second;
150  return -1;
151 }
152 
153 template <class T>
154 inline BObjectImp* TmplExecutorModule<T>::execFunc( unsigned funcidx )
155 {
156  T* derived = static_cast<T*>( this );
157  return ( ( *derived ).*( function_table[funcidx].fptr ) )();
158 };
159 
160 template <class T>
161 inline std::string TmplExecutorModule<T>::functionName( unsigned idx )
162 {
163  return function_table[idx].funcname;
164 }
165 }
166 }
167 
168 #endif
BObject *(ExecutorModule::* ExecutorModuleFn)()
Definition: execmodl.h:39
#define POL_OVERRIDE
virtual BObjectImp * execFunc(unsigned idx) POL_OVERRIDE
Definition: execmodl.h:154
static FunctionTable function_table
Definition: execmodl.h:114
static std::map< std::string, int, Clib::ci_cmp_pred > _func_idx_map
Definition: execmodl.h:115
TmplExecutorModule(const char *modname, Executor &exec)
Definition: execmodl.h:131
virtual std::string functionName(unsigned idx) POL_OVERRIDE
Definition: execmodl.h:161
virtual int functionIndex(const std::string &funcname) POL_OVERRIDE
Definition: execmodl.h:145
BApplicObjBase * getApplicObjParam(unsigned param, const BApplicObjType *object_type)
Definition: execmodl.cpp:44
boost_utils::function_name_flystring moduleName
Definition: execmodl.h:78
TmplExecutorModule< AttributeExecutorModule >::FunctionTable function_table
std::string name
Definition: osmod.cpp:943
Definition: berror.cpp:12
BApplicObj< T > * getApplicObjParam(ExecutorModule &ex, unsigned param, const BApplicObjType *object_type)
Definition: execmodl.h:93
boost::flyweight< std::string, boost::flyweights::tag< function_name_tag >, FLYWEIGHT_HASH_FACTORY > function_name_flystring
Definition: boostutils.h:160