-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.cpp
More file actions
50 lines (42 loc) · 1.21 KB
/
usage.cpp
File metadata and controls
50 lines (42 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include "dynobject.hpp"
int main()
{
/* create the factory */
dog0752::dynobj::ObjectFactory factory;
/* intern some identifiers */
dog0752::dynobj::ObjectFactory::Identifier id_name = factory.intern("name");
dog0752::dynobj::ObjectFactory::Identifier id_sayHi =
factory.intern("sayHi");
/* create a new dynamic object */
std::unique_ptr<dog0752::dynobj::ObjectFactory::DynObject> obj =
factory.createObject();
/* set a property */
obj->set(factory, id_name, std::string("Cirno"));
/* add a method */
obj->set(factory, id_sayHi,
dog0752::dynobj::ObjectFactory::DynObject::Method(
[](dog0752::dynobj::ObjectFactory::DynObject &self,
dog0752::dynobj::ObjectFactory::DynObject::Args /*args*/)
-> std::any
{
auto maybe_name =
self.get<std::string>(0); /* id 0 = "name" */
if (maybe_name.has_value())
{
return std::string("hello from ") + maybe_name.value();
}
return std::string("hello from ???");
}));
/* call the method */
auto result = obj->call<std::string>(id_sayHi);
if (result.has_value())
{
std::cout << result.value() << "\n";
}
else
{
std::cerr << "error: " << result.error() << "\n";
}
return 0;
}