-
Notifications
You must be signed in to change notification settings - Fork 1
/
mrubynativeobject.hpp
66 lines (53 loc) · 1.06 KB
/
mrubynativeobject.hpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef __MRUBYNATIVEOBJECT_HPP__
#define __MRUBYNATIVEOBJECT_HPP__
#include <mruby.h>
#include <string>
#include <memory>
template<class TClass>
class NativeObject
{
std::string classname;
mrb_data_type datatype;
std::shared_ptr<TClass> instance;
static void destructor(mrb_state* mrb, void* ptr)
{
delete (NativeObject<TClass>*)ptr;
}
public:
NativeObject(const std::string& classname, std::shared_ptr<TClass> instance) :
classname(classname),
instance(instance)
{
datatype.dfree = destructor;
datatype.struct_name = classname.c_str();
}
~NativeObject()
{
//printf("NativeObject<%s> %p deleted\n", typeid(TClass).name(), this);
}
mrb_data_type* get_type_ptr()
{
return &datatype;
}
TClass* get_instance() const
{
return instance.get();
}
std::shared_ptr<TClass> get_shared_instance() const
{
return instance;
}
std::string get_classname() const
{
return classname;
}
TClass* operator->()
{
return instance.operator->();
}
TClass &operator*()
{
return instance.operator*();
}
};
#endif // __MRUBYNATIVEOBJECT_HPP__