lbp_1_extension.cpp
Go to the documentation of this file.
1/** @file lbp_1_extension.cpp
2
3Registering a lua script with the libFAUDES run-time interface
4
5This tutorial loads a luascript from file an registers it
6with the libFAUDES run-time interface. Thus, the script can be
7invoked 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
21using namespace faudes;
22
23
24/////////////////
25// main program
26/////////////////
27
28
29int 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
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)
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;
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
int main()
#define FAUDES_TEST_DUMP(mes, dat)
Definition cfl_utils.h:505
Function * NewFunction() const
void ParamValue(int n, Type *param)
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
void Write(const Type *pContext=0) const
void SWrite(TokenWriter &rTw) const
TaNameSet< AttributeCFlags > Alphabet
vGenerator Generator
void LoadRegistry(const std::string &rPath)

libFAUDES 2.33k --- 2025.09.16 --- c++ api documentaion by doxygen