summaryrefslogtreecommitdiff
path: root/src/bindings/cxx/eina_cxx/eina_variant.hh
blob: 4885c8023676d6f73f4989c33181445152db6512 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#ifndef EINA_VARIANT_HH_
#define EINA_VARIANT_HH_

#include <cstddef>
#include <algorithm>
#include <utility>
#include <type_traits>
#include <tuple>
#include <iosfwd>

#include <eina_aligned_union.hh>

namespace efl { namespace eina {

namespace _impl {

template <typename T, typename U, typename...Others>
struct is_one_of : std::conditional<std::is_same<T, U>::value
                                    , std::is_same<T, U>
                                    , is_one_of<T, Others...> >::type::type
{};

template <typename T, typename U>
struct is_one_of<T, U> : std::is_same<T, U>
{};


template <std::size_t size, typename T, typename U, typename...Args>
struct find_impl : find_impl<size+1, T, Args...>
{};

template <std::size_t size, typename T, typename...Args>
struct find_impl<size, T, T, Args...> : std::integral_constant<std::size_t, size> {};
  
template <typename T, typename U, typename...Args>
struct find : find_impl<0u, T, U, Args...>
{};

}
    
template <std::size_t N, std::size_t L, typename Tuple>
struct call_visitor
{
   template <typename F>
   static typename F::result_type call(int type, void const* buffer, F f)
   {
      if(type == N)
        {
          using std::tuple_element;
          typedef typename tuple_element<N, Tuple>::type type;
          type const* o = static_cast<type const*>(buffer);
          return f(*o);
        }
      else
        return call_visitor<N+1, L, Tuple>::call(type, buffer, f);
   }
   template <typename F>
   static typename F::result_type call(int type, void* buffer, F f)
   {
      if(type == N)
        {
          using std::tuple_element;
          typedef typename tuple_element<N, Tuple>::type type;
          type* o = static_cast<type*>(buffer);
          return f(*o);
        }
      else
        return call_visitor<N+1, L, Tuple>::call(type, buffer, f);
   }
};

template <std::size_t L, typename Tuple>
struct call_visitor<L, L, Tuple>
{
    template <typename F>
    static typename F::result_type call(int, void const*, F)
    {
       std::abort();
    }
};

struct compare_equal_visitor
{
   void const* buffer;
   typedef bool result_type;
   template <typename T>
   bool operator()(T const& other) const
   {
      return *static_cast<T const*>(buffer) == other;
   }
};
        
struct copy_visitor
{
   typedef void result_type;
   void* buffer;
   template <typename T>
   void operator()(T const& other) const
   {
      new (buffer) T(other);
   }
};
    
struct move_visitor
{
   typedef void result_type;
   void* buffer;
   template <typename T>
   void operator()(T& other) const
   {
      typedef typename std::remove_cv<typename std::remove_reference<T>::type>::type type;
      new (buffer) type(std::move(other));
   }
};

struct assign_visitor
{
   typedef void result_type;
   void* buffer;
   template <typename T>
   void operator()(T const& other) const
   {
      typedef typename std::remove_cv<typename std::remove_reference<T>::type>::type type;
      type* assigned = static_cast<type*>(buffer);
      *assigned = other;
   }
};

struct move_assign_visitor
{
   typedef void result_type;
   void* buffer;
   template <typename T>
   void operator()(T& other) const
   {
      typedef typename std::remove_cv<typename std::remove_reference<T>::type>::type type;
      type* assigned = static_cast<type*>(buffer);
      *assigned = std::move(other);
   }
};
    
struct destroy_visitor
{
   typedef void result_type;
   template <typename T>
   void operator()(T&& other) const noexcept
   {
      typedef typename std::remove_cv<typename std::remove_reference<T>::type>::type type;
      other.~type();
   }
};

struct ostream_visitor
{
   std::ostream* s;
   typedef std::ostream& result_type;
   template <typename T>
   std::ostream& operator()(T const& other) const
   {
     return *s << other;
   }
};

template <typename T>
struct get_visitor
{
   typedef T* result_type;
   T* operator()(T& object) const
   {
     return &object;
   }
   template <typename U>
   T* operator()(U&) const { return nullptr; }
};
        
template <typename... Args>
struct variant
{
   typedef variant<Args...> _self_type; /**< Type for the optional class itself. */

   constexpr variant()
     : type(-1)
   {}

   template <typename T>
   variant(T object,
           typename std::enable_if<_impl::is_one_of
           <typename std::remove_cv<typename std::remove_reference<T>::type>::type, Args...>::value>::type* = 0)
     : type(_impl::find<typename std::remove_cv<typename std::remove_reference<T>::type>::type, Args...>::value)
   {
      construct(object);
   }

   variant(variant const& other)
     : type(other.type)
   {
     if(other.type != -1)
       other.visit(copy_visitor{static_cast<void*>(&buffer)});
   }
   variant& operator=(variant const& other)
   {
     if(type == other.type && type != -1)
       {
         other.visit(assign_visitor{static_cast<void*>(&buffer)});
       }
     else if(type != other.type)
       {
         if(type != -1)
           destroy_unsafe();
         type = other.type;
         other.visit(copy_visitor{static_cast<void*>(&buffer)});
       }
     return *this;
   }
   variant(variant&& other)
     : type(other.type)
   {
     if(other.type != -1)
       other.visit(move_visitor{static_cast<void*>(&buffer)});
   }
   variant& operator=(variant&& other)
   {
     if(type == other.type && type != -1)
       {
         other.visit(move_assign_visitor{static_cast<void*>(&buffer)});
       }
     else if(type != other.type)
       {
         if(type != -1)
           destroy_unsafe();
         type = other.type;
         other.visit(move_visitor{static_cast<void*>(&buffer)});
       }
     return *this;
   }
   ~variant()
   {
     if(type != -1)
       destroy_unsafe();
   }

   void destroy()
   {
     if(type != -1)
       {
         destroy_unsafe();
         type = -1;
       }
   }

   void destroy_unsafe()
   {
     visit_unsafe(destroy_visitor());
   }

   bool empty() const
   {
     return type == -1;
   }
  
   template <typename F>
   typename F::result_type visit(F f) const
   {
      if(type == -1)
        {
           throw std::runtime_error("variant is empty");
        }
      else
        return call_visitor<0u, sizeof...(Args), std::tuple<Args...>>::call(type, static_cast<const void*>(&buffer), f);
   }

   template <typename F>
   typename F::result_type visit(F f)
   {
      if(type == -1)
        {
           throw std::runtime_error("variant is empty");
        }
      else
        return call_visitor<0u, sizeof...(Args), std::tuple<Args...>>::call(type, static_cast<void*>(&buffer), f);
   }

   template <typename F>
   typename F::result_type visit_unsafe(F f) const
   {
     return call_visitor<0u, sizeof...(Args), std::tuple<Args...>>::call(type, static_cast<const void*>(&buffer), f);
   }

   template <typename F>
   typename F::result_type visit_unsafe(F f)
   {
     return call_visitor<0u, sizeof...(Args), std::tuple<Args...>>::call(type, static_cast<void*>(&buffer), f);
   }
  
private:
   template <typename T>
   void construct(T object)
   {
     new (&buffer) T(std::move(object));
   }
  
   typedef typename eina::aligned_union<1, Args...>::type buffer_type;

   friend bool operator==(variant<Args...> const& lhs, variant<Args...> const& rhs)
   {
     return rhs.type == lhs.type
       && (rhs.type == -1
           || rhs.visit(compare_equal_visitor{&lhs.buffer}));
   }
   friend std::ostream& operator<<(std::ostream& s, variant<Args...> const& rhs)
   {
     return rhs.visit(ostream_visitor{&s});
   }
  
   int type;
   /**
    * Member variable for holding the contained value.
    */
   buffer_type buffer;
};

template <typename...Args>
inline bool operator!=(variant<Args...>const& lhs, variant<Args...> const& rhs)
{
   return !(lhs == rhs);
}

template <typename T, typename...Args>
T* get(variant<Args...>* variant, typename std::enable_if<_impl::is_one_of
       <typename std::remove_cv<typename std::remove_reference<T>::type>::type, Args...>::value>::type* = 0)
{
   return variant->visit(get_visitor<T>{});
}
template <typename T, typename...Args>
T const* get(variant<Args...>const* variant, typename std::enable_if<_impl::is_one_of
       <typename std::remove_cv<typename std::remove_reference<T>::type>::type, Args...>::value>::type* = 0)
{
   return variant->visit(get_visitor<T const>{});
}
template <typename T, typename...Args>
T& get(variant<Args...>& variant, typename std::enable_if<_impl::is_one_of
       <typename std::remove_cv<typename std::remove_reference<T>::type>::type, Args...>::value>::type* = 0)
{
   T* p = variant.visit(get_visitor<T>{});
   if(p)
     return *p;
   else
     throw std::logic_error("");
}
template <typename T, typename...Args>
T const& get(variant<Args...>const& variant, typename std::enable_if<_impl::is_one_of
       <typename std::remove_cv<typename std::remove_reference<T>::type>::type, Args...>::value>::type* = 0)
{
   T const* p = variant.visit(get_visitor<T const>{});
   if(p)
     return *p;
   else
     throw std::logic_error("");
}
        
} }

#endif