summaryrefslogtreecommitdiff
path: root/src/bindings/eina_cxx/eina_clone_allocators.hh
blob: 76ff620f982d05f0b0f1c50532ef50747e176f23 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#ifndef EINA_CLONE_ALLOCATORS_HH_
#define EINA_CLONE_ALLOCATORS_HH_

#include <Eo.h>

#include <memory>
#include <cstring>
#include <cstdlib>
#include <type_traits>

/**
 * @addtogroup Eina_Cxx_Containers_Group
 *
 * @{
 */

namespace efl { namespace eina {

/**
 * @defgroup Eina_Cxx_Clone_Allocators_Group Clone Allocators
 * @ingroup Eina_Cxx_Containers_Group
 *
 * Clone allocators is a formalized way to pointer containers control
 * the memory of the stored objects, allowing users to apply custom
 * allocators/deallocators for the cloned objects.
 *
 * @{
 */

/*
 * @internal
 */
struct default_clone_allocator_placeholder;
    
/**
 * This allocator creates copies of objects on the heap, calling their
 * copy constructor to make then equivalent to the given reference.
 *
 * The created objects are released with the default delete.
 */
struct heap_copy_allocator
{
  template <typename T>
  static T* allocate_clone(T const& v)
  {
    return new T(v);
  }

  template <typename T>
  static void deallocate_clone(T* p)
  {
#ifdef EFL_EINA_CXX11
    std::default_delete<T>()(p);
#else
    delete p;
#endif
  }
};

/**
 * This allocator creates copies of Eo classes through eo_ref
 *
 * The created objects are released using eo_unref
 */
struct eo_clone_allocator
{
  static Eo* allocate_clone(Eo const& v)
  {
    return ::eo_ref(&v);
  }

  static void deallocate_clone(Eo const* p)
  {
    ::eo_unref(const_cast<Eo*>(p));
  }
};

/**
 * This allocator allows users to create custom allocation schemes by
 * overloading the <tt>new_clone(T const& v)</tt> and
 * <tt>delete_clone(T* p)</tt> functions.
 */
struct heap_clone_allocator
{
  template <typename T>
  static T* allocate_clone(T const& v)
  {
    return new_clone(v);
  }
  template <typename T>
  static void deallocate_clone(T* p)
  {
    delete_clone(p);
  }
};

/**
 * This allocator does not allocate or deallocate anything. It simple
 * gets non-const-qualified pointers for objects, which allow
 * containers to hold elements without having ownership on them.
 *
 * It is commonly used to create a pointer container that is a view into
 * another existing container.
 */
struct view_clone_allocator
{
  template <typename T>
  static T* allocate_clone(T const& v)
  {
    return const_cast<T*>(&v);
  }
  template <typename T>
  static void deallocate_clone(T*)
  {
  }
};

/**
 * This allocator does not define an @c allocate_clone member function,
 * so it should be used to disable operations that require elements to
 * be cloned.
 */
struct heap_no_copy_allocator
{
  template <typename T>
  static void deallocate_clone(T* p)
  {
#ifdef EFL_EINA_CXX11
    std::default_delete<T>()(p);
#else
    delete p;
#endif
  }
};

/**
 * Manages allocation and deallocation of memory using the function
 * @c malloc and @c free. This allocator does not calls constructors,
 * the content of the newly allocated objects are assigned using
 * @c memcpy, so it has to be used with types that have
 * <em>standard-layout</em>.
 */
struct malloc_clone_allocator
{
  template <typename T>
  static T* allocate_clone(T const& v)
  {
    static_assert(std::is_pod<T>::value, "malloc_clone_allocator can only be used with POD types");
    T* p = static_cast<T*>(std::malloc(sizeof(T)));
    std::memcpy(p, &v, sizeof(T));
    return p;
  }

  template <typename T>
  static void deallocate_clone(T const* p)
  {
    static_assert(std::is_pod<T>::value || std::is_void<T>::value
                  , "malloc_clone_allocator can only be used with POD types");
    std::free(const_cast<T*>(p));
  }
};

/**
 * @internal
 */
template <typename A>
struct clone_allocator_deleter
{
  template <typename T>
  void operator()(T* object) const
  {
    A::deallocate_clone(object);
  }
};

/**
 * @}
 */

} }

/**
 * @}
 */

#endif