lbp_1_extension.cpp
Go to the documentation of this file.
1 /** @file lbp_1_extension.cpp
2 
3 Registering a lua script with the libFAUDES run-time interface
4 
5 This tutorial loads a luascript from file an registers it
6 with the libFAUDES run-time interface. Thus, the script can be
7 invoked by an application in the same way as built in functions.
8 
9 @ingroup Tutorials
10 
11 @include lbp_1_extension.cpp
12 
13 */
14 
15 
16 
17 // libfaudes include
18 #include "libfaudes.h"
19 
20 // we make the faudes namespace available to our program
21 using namespace faudes;
22 
23 
24 /////////////////
25 // main program
26 /////////////////
27 
28 
29 int main (int argc, char **argv) {
30 
31  // ******************** load/inspect lua function definition
32 
33  // load std registry for data types
34  LoadRegistry("../../../include/libfaudes.rti");
35 
36  // initialize a lua function definition from file
37  LuaFunctionDefinition lfdef1;
38  lfdef1.Read("data/largegen.rti");
39 
40  // report to console
41  std::cout << "################################\n";
42  std::cout << "# lua extension from rti file\n";
43  lfdef1.Write();
44  std::cout << "################################\n";
45 
46  // run syntax check on script
47  std::string err=lfdef1.SyntaxCheck();
48  if(err=="")
49  std::cout << "script seems ok\n";
50  else {
51  std::cout << err << "\n";
52  return 1;
53  }
54 
55  // test faudes type interface (assignment/equality)
56  LuaFunctionDefinition lfdef2;
57  lfdef2 = lfdef1;
58  if(lfdef2!=lfdef1) {
59  std::cout << "# ERR: the two definitions should be equal\n";
60  exit(1);
61  }
62 
63  // report to console
64  std::cout << "################################\n";
65  std::cout << "# copy of lua extension\n";
66  lfdef2.Write();
67  std::cout << "################################\n";
68 
69 
70  // ******************** execute lua function via rti
71 
72  // instantiate a function object
73  faudes::Function* fnct = lfdef2.NewFunction();
74 
75  // prepare arguments
76  Integer cntq=10;
77  Integer cnts=5;
78  Generator gres;
79 
80  // set argumants
81  fnct->Variant(0);
82  fnct->ParamValue(0,&cntq);
83  fnct->ParamValue(1,&cnts);
84  fnct->ParamValue(2,&gres);
85 
86  // execute lua script
87  fnct->Execute();
88 
89  // report statistics on result
90  std::cout << "################################\n";
91  std::cout << "# statistics \n";
92  gres.SWrite();
93  std::cout << "################################\n";
94 
95  // report test case
96  FAUDES_TEST_DUMP("large gen",gres);
97 
98 
99  // ******************** execute lua function via rti
100 
101  // load/execute another function definition from file
102  NameSet sres;
103  LuaFunctionDefinition lfdef3;
104  lfdef3.Read("data/xtractalph.rti");
105  lfdef3.Write();
106  faudes::Function* fnct3 = lfdef3.NewFunction();
107  fnct3->Variant(0);
108  fnct3->ParamValue(0,&gres);
109  fnct3->ParamValue(1,&sres);
110  fnct3->Execute();
111 
112  // on result
113  std::cout << "################################\n";
114  std::cout << "# alphabet \n";
115  sres.Write();
116  std::cout << "################################\n";
117 
118  // report test case
119  FAUDES_TEST_DUMP("alphabet",sres);
120 
121 
122 
123  // ******************** advanced: C++ rtti / vtables / multiple inheritance
124 
125  // experiment with compiler's memory layout (base matches void, intermediate cast is fine)
126  Generator* vgen_vgen = new Generator();
127  Type* vgen_ftype = ((Type*)(vgen_vgen));
128  void* vgen_void = ((void*)(vgen_vgen));
129  void* vgen_void_ftype = ((void*)(vgen_ftype));
130  std::cout << "C++ casting of a Generator: " <<
131  " vgen at " << &vgen_vgen << ", ftype at " << vgen_ftype << ", void at " << vgen_void <<
132  ", void via ftype at " << vgen_void_ftype << "\n";
133 
134  // experiment with compiler's memory layout (base does not match void for multiple inheritance)
135  Alphabet* cevs_cevs = new Alphabet();
136  Type* cevs_ftype = ((Type*)(cevs_cevs));
137  void* cevs_void = ((void*)(cevs_cevs));
138  void* cevs_void_ftype = ( (void*)(cevs_ftype));
139  void* cevs_dvoid_ftype = ( dynamic_cast<void*>(cevs_ftype));
140  std::cout << "C++ casting a Alphabet: " <<
141  " cevs at " << &cevs_cevs << ", ftype at " << cevs_ftype << ", void at " << cevs_void <<
142  ", void via ftype at " << cevs_void_ftype <<
143  ", dynamic-void via ftype at " << cevs_dvoid_ftype << "\n";
144 
145  // conclusion: depending on the compiler, casting to void* via different paths may
146  // very well lead to a different result; when using dynamic_cast however, the compiler
147  // has a chance and (at least) gcc manages; thus, dynamic cast is not only for down casts,
148  // but also for up casts.
149 
150  // record test case
151  FAUDES_TEST_DUMP("dynamic up-cast (a)", vgen_void_ftype== vgen_void);
152  FAUDES_TEST_DUMP("dynamic up-cast (a)", cevs_dvoid_ftype== cevs_void);
153 
154  // delete my objects
155  delete vgen_vgen;
156  delete cevs_cevs;
157 
158 
159 }
160 
161 
#define FAUDES_TEST_DUMP(mes, dat)
Test protocol record macro ("mangle" filename for platform independance)
Definition: cfl_helper.h:483
Function * NewFunction() const
Construct function on heap.
A faudes-function hosts parameter values of some faudes type and provides a method to perform an oper...
void Execute(void)
Perform operation.
void Variant(int n)
Set signature from function definition.
void ParamValue(int n, Type *param)
Set parameter at certain position.
Elementary type.
A LuaFunctionDefinition is derived from FunctionDefinition to define a faudes-function by a Lua scrip...
Definition: lbp_function.h:140
std::string SyntaxCheck(void)
Syntax check lua code.
Set of indices with symbolic names.
Definition: cfl_nameset.h:69
Set of indices with symbolic names and attributes.
Definition: cfl_nameset.h:564
Base class of all libFAUDES objects that participate in the run-time interface.
Definition: cfl_types.h:239
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
Read configuration data from file with label specified.
Definition: cfl_types.cpp:261
void Write(const Type *pContext=0) const
Write configuration data to console.
Definition: cfl_types.cpp:139
void SWrite(TokenWriter &rTw) const
Write statistics comment to TokenWriter.
Definition: cfl_types.cpp:256
Base class of all FAUDES generators.
TaNameSet< AttributeCFlags > Alphabet
Convenience typedef for event sets with controllability attributes.
vGenerator Generator
Plain generator, api typedef for generator with no attributes.
void LoadRegistry(const std::string &rPath)
Load all registered types and functions.
int main(int argc, char **argv)
Includes all libFAUDES headers, incl plugings
libFAUDES resides within the namespace faudes.

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen