-
Notifications
You must be signed in to change notification settings - Fork 120
/
ptr_traits.hpp
115 lines (89 loc) · 2.75 KB
/
ptr_traits.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifndef V8PP_PTR_TRAITS_HPP_INCLUDED
#define V8PP_PTR_TRAITS_HPP_INCLUDED
#include <memory>
namespace v8pp {
template<typename T, typename Enable = void>
struct convert;
struct raw_ptr_traits
{
using pointer_type = void*;
using const_pointer_type = void const*;
template<typename T>
using object_pointer_type = T*;
template<typename T>
using object_const_pointer_type = T const*;
using object_id = void*;
static object_id pointer_id(void* ptr) { return ptr; }
static pointer_type key(object_id id) { return id; }
static pointer_type const_pointer_cast(const_pointer_type ptr) { return const_cast<void*>(ptr); }
template<typename T, typename U>
static T* static_pointer_cast(U* ptr) { return static_cast<T*>(ptr); }
template<typename T>
using convert_ptr = convert<T*>;
template<typename T>
using convert_ref = convert<T&>;
template<typename T, typename... Args>
static object_pointer_type<T> create(Args&&... args)
{
return new T(std::forward<Args>(args)...);
}
template<typename T>
static object_pointer_type<T> clone(T const& src)
{
return new T(src);
}
template<typename T>
static void destroy(object_pointer_type<T> const& ptr)
{
delete ptr;
}
template<typename T>
static size_t object_size(object_pointer_type<T> const&)
{
return sizeof(T);
}
};
struct ref_from_shared_ptr
{
};
struct shared_ptr_traits
{
using pointer_type = std::shared_ptr<void>;
using const_pointer_type = std::shared_ptr<void const>;
template<typename T>
using object_pointer_type = std::shared_ptr<T>;
template<typename T>
using object_const_pointer_type = std::shared_ptr<T const>;
using object_id = void*;
static object_id pointer_id(pointer_type const& ptr) { return ptr.get(); }
static pointer_type key(object_id id) { return std::shared_ptr<void>(id, [](void*) {}); }
static pointer_type const_pointer_cast(const_pointer_type const& ptr) { return std::const_pointer_cast<void>(ptr); }
template<typename T, typename U>
static std::shared_ptr<T> static_pointer_cast(std::shared_ptr<U> const& ptr) { return std::static_pointer_cast<T>(ptr); }
template<typename T>
using convert_ptr = convert<std::shared_ptr<T>>;
template<typename T>
using convert_ref = convert<T, ref_from_shared_ptr>;
template<typename T, typename... Args>
static object_pointer_type<T> create(Args&&... args)
{
return std::make_shared<T>(std::forward<Args>(args)...);
}
template<typename T>
static object_pointer_type<T> clone(T const& src)
{
return std::make_shared<T>(src);
}
template<typename T>
static void destroy(object_pointer_type<T> const&)
{
// do nothing with reference-counted object
}
template<typename T>
static size_t object_size(object_pointer_type<T> const&)
{
return sizeof(T);
}
};
} //namespace v8pp
#endif // V8PP_PTR_TRAITS_HPP_INCLUDED