/
any.h
983 lines (871 loc) · 23 KB
/
any.h
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
/*
* Copyright (c) 2016, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Written (W) 2016 Sergey Lisitsyn
* Written (W) 2016 Sanuj Sharma
*/
#ifndef _ANY_H_
#define _ANY_H_
#include <shogun/base/init.h>
#include <algorithm>
#include <limits>
#include <stdexcept>
#include <string.h>
#include <string>
#include <typeinfo>
#ifdef HAVE_CXA_DEMANGLE
#include <cxxabi.h>
#endif
namespace shogun
{
/** Converts compiler-dependent name of class to
* something human readable.
* @return human readable name of class
*/
template <typename T>
std::string demangled_type()
{
#ifdef HAVE_CXA_DEMANGLE
size_t length;
int status;
char* demangled =
abi::__cxa_demangle(typeid(T).name(), nullptr, &length, &status);
std::string demangled_string(demangled);
free(demangled);
#else
std::string demangled_string(typeid(T).name());
#endif
return demangled_string;
}
enum class PolicyType
{
OWNING,
NON_OWNING
};
class CSGObject;
template <class T>
class SGVector;
template <class T>
class SGMatrix;
class TypeMismatchException : public std::exception
{
public:
TypeMismatchException(
const std::string& expected, const std::string& actual)
: m_expected(expected), m_actual(actual)
{
}
TypeMismatchException(const TypeMismatchException& other)
: m_expected(other.m_expected), m_actual(other.m_actual)
{
}
std::string expected() const
{
return m_expected;
}
std::string actual() const
{
return m_actual;
}
private:
std::string m_expected;
std::string m_actual;
};
template <class T, class S>
class ArrayReference
{
public:
ArrayReference(T** ptr, S* length) : m_ptr(ptr), m_length(length)
{
}
ArrayReference(const ArrayReference<T, S>& other)
: m_ptr(other.m_ptr), m_length(other.m_length)
{
}
ArrayReference<T, S> operator=(const ArrayReference<T, S>& other)
{
throw std::logic_error("Assignment not supported");
}
bool equals(const ArrayReference<T, S>& other) const;
void reset(const ArrayReference<T, S>& other);
private:
T** m_ptr;
S* m_length;
};
template <class T, class S>
class Array2DReference
{
public:
Array2DReference(T** ptr, S* rows, S* cols)
: m_ptr(ptr), m_rows(rows), m_cols(cols)
{
}
Array2DReference(const Array2DReference<T, S>& other)
: m_ptr(other.m_ptr), m_rows(other.m_rows), m_cols(other.m_cols)
{
}
Array2DReference<T, S> operator=(const Array2DReference<T, S>& other)
{
throw std::logic_error("Assignment not supported");
}
bool equals(const Array2DReference<T, S>& other) const;
void reset(const Array2DReference<T, S>& other);
private:
T** m_ptr;
S* m_rows;
S* m_cols;
};
/** Used to denote an empty Any object */
struct Empty
{
/** Equality operator */
bool operator==(const Empty& other) const
{
return true;
}
};
class AnyVisitor
{
public:
virtual ~AnyVisitor() = default;
virtual void on(bool*) = 0;
virtual void on(int32_t*) = 0;
virtual void on(int64_t*) = 0;
virtual void on(float*) = 0;
virtual void on(double*) = 0;
virtual void on(CSGObject**) = 0;
virtual void on(SGVector<int>*) = 0;
virtual void on(SGVector<float>*) = 0;
virtual void on(SGVector<double>*) = 0;
virtual void on(SGMatrix<int>*) = 0;
virtual void on(SGMatrix<float>*) = 0;
virtual void on(SGMatrix<double>*) = 0;
template<class T, std::enable_if_t<std::is_base_of<CSGObject, T>::value, T>* = nullptr>
void on(T** v)
{
on((CSGObject**)v);
}
void on(Empty*)
{
}
void on(...)
{
}
};
namespace any_detail
{
struct by_default
{
};
struct general : by_default
{
};
struct more_important : general
{
};
struct maybe_most_important : more_important
{
};
template <class T>
auto compare_impl(by_default, const T& lhs, const T& rhs) = delete;
template <class T>
bool compare_impl_eq(const T& lhs, const T& rhs)
{
return lhs == rhs;
}
template <>
bool compare_impl_eq(const float32_t& lhs, const float32_t& rhs);
template <>
bool compare_impl_eq(const float64_t& lhs, const float64_t& rhs);
template <>
bool compare_impl_eq(const floatmax_t& lhs, const floatmax_t& rhs);
template <>
bool compare_impl_eq(const complex128_t& lhs, const complex128_t& rhs);
template <class T>
auto compare_impl(general, const T& lhs, const T& rhs)
-> decltype(lhs == rhs)
{
return compare_impl_eq(lhs, rhs);
}
template <class T>
auto compare_impl(more_important, const T& lhs, const T& rhs)
-> decltype(lhs.equals(rhs))
{
return lhs.equals(rhs);
}
template <class T>
auto compare_impl(maybe_most_important, T* lhs, T* rhs)
-> decltype(lhs->equals(rhs))
{
if (lhs && rhs)
return lhs->equals(rhs);
else if (!lhs && !rhs)
return true;
else
return false;
}
template <class T>
inline bool compare(const T& lhs, const T& rhs)
{
return compare_impl(maybe_most_important(), lhs, rhs);
}
template <class T>
inline T clone_impl(general, T& value)
{
return T(value);
}
template <class T>
inline auto clone_impl(more_important, const T& value)
-> decltype(value.clone())
{
return value.clone();
}
template <class T>
inline auto clone_impl(maybe_most_important, T* value)
-> decltype(static_cast<void*>(value->clone()))
{
if (!value)
return nullptr;
return static_cast<void*>(value->clone());
}
template <class T>
inline T& mutable_value_of(void** ptr)
{
return *static_cast<T*>(*ptr);
}
template <class T>
inline T const* typed_pointer(const void* ptr)
{
return static_cast<T const*>(ptr);
}
template <class T>
inline auto clone(void** storage, T& value)
-> decltype(clone_impl(maybe_most_important(), value))
{
auto cloned = clone_impl(maybe_most_important(), value);
mutable_value_of<decltype(cloned)>(storage) = cloned;
return cloned;
}
template <class T, class S>
inline auto clone(void** storage, const ArrayReference<T, S>& value)
{
auto existing = mutable_value_of<ArrayReference<T, S>>(storage);
existing.reset(value);
}
template <class T, class S>
inline auto clone(void** storage, const Array2DReference<T, S>& value)
{
auto existing = mutable_value_of<Array2DReference<T, S>>(storage);
existing.reset(value);
}
template <class T>
inline const T& value_of(T const* ptr)
{
return *ptr;
}
template <class T, class S>
inline auto free_array(T* ptr, S size)
{
if (!ptr)
{
return 0;
}
SG_FREE(ptr);
return 0;
}
void free_object(CSGObject* obj);
template <class T, class S>
inline auto free_array(T** ptr, S size) -> decltype(ptr[0]->unref())
{
if (!ptr)
{
return 0;
}
for (S i = 0; i < size; ++i)
{
free_object(ptr[i]);
}
SG_FREE(ptr);
return 0;
}
template <class T>
inline void copy_array(T* begin, T* end, T* dst)
{
std::transform(begin, end, dst, [](const T& value) {
return static_cast<T>(
clone_impl(maybe_most_important(), value));
});
}
}
using any_detail::typed_pointer;
using any_detail::value_of;
using any_detail::mutable_value_of;
using any_detail::compare;
template <class T, class S>
bool ArrayReference<T, S>::equals(const ArrayReference<T, S>& other) const
{
if (*(m_length) != *(other.m_length))
{
return false;
}
if (*(m_ptr) == *(other.m_ptr))
{
return true;
}
return std::equal(
*(m_ptr), *(m_ptr) + *(m_length), *(other.m_ptr),
[](T lhs, T rhs) -> bool { return any_detail::compare(lhs, rhs); });
}
template <class T, class S>
void ArrayReference<T, S>::reset(const ArrayReference<T, S>& other)
{
auto src = *(other.m_ptr);
auto len = *(other.m_length);
auto& dst = *(this->m_ptr);
auto own_len = *(this->m_length);
any_detail::free_array(dst, own_len);
dst = new T[len];
*(this->m_length) = len;
any_detail::copy_array(src, src + len, dst);
}
template <class T, class S>
bool
Array2DReference<T, S>::equals(const Array2DReference<T, S>& other) const
{
if ((*(m_rows) != *(other.m_rows)) || (*(m_cols) != *(other.m_cols)))
{
return false;
}
if (*(m_ptr) == *(other.m_ptr))
{
return true;
}
int64_t size = int64_t(*(m_rows)) * (*(m_cols));
return std::equal(
*(m_ptr), *(m_ptr) + size, *(other.m_ptr),
[](T lhs, T rhs) -> bool { return any_detail::compare(lhs, rhs); });
}
template <class T, class S>
void Array2DReference<T, S>::reset(const Array2DReference<T, S>& other)
{
auto src = *(other.m_ptr);
auto rows = *(other.m_rows);
auto cols = *(other.m_cols);
auto& dst = *(this->m_ptr);
auto own_rows = *(this->m_rows);
auto own_cols = *(this->m_cols);
any_detail::free_array(dst, ((long)own_rows * own_cols));
dst = new T[(long)rows * cols];
*(this->m_rows) = rows;
*(this->m_cols) = cols;
any_detail::copy_array(src, src + ((long)rows * cols), dst);
}
/** @brief An interface for a policy to store a value.
* Value can be any data like primitive data-types, shogun objects, etc.
* Policy defines how to handle this data. It works with a
* provided memory region and is able to set value, clear it
* and return the type-name as string.
*/
class BaseAnyPolicy
{
public:
/** Puts provided value pointed by v (untyped to be generic) to storage.
* @param storage pointer to a pointer to storage
* @param v pointer to value
*/
virtual void set(void** storage, const void* v) const = 0;
/** Clones value provided by from into storage
* @param storage pointer to a pointer to storage
* @param from pointer to value to clone
*/
virtual void clone(void** storage, const void* from) const = 0;
/** Clears storage.
* @param storage pointer to a pointer to storage
*/
virtual void clear(void** storage) const = 0;
/** Returns type-name as string.
* @return name of type class
*/
virtual std::string type() const = 0;
/** Returns type info
* @return type info of value's type
*/
virtual const std::type_info& type_info() const = 0;
/** Compares type.
* @param ti type information
* @return true if type matches
*/
virtual bool matches(const std::type_info& ti) const = 0;
/** Checks if policies are compatible.
* @param other other policy
* @return true if policies do match
*/
virtual bool matches(BaseAnyPolicy* other) const = 0;
/** Compares two storages.
* @param storage pointer to a pointer to storage
* @param other_storage pointer to a pointer to another storage
* @return true if both storages have same value
*/
virtual bool
equals(const void* storage, const void* other_storage) const = 0;
/** Returns the type of policy.
* @return type of policy
*/
virtual PolicyType policy_type() const = 0;
/** Visitor pattern. Calls the appropriate 'on' method of AnyVisitor.
*
* @param storage pointer to storage
* @param visitor abstract visitor to use
*/
virtual void visit(void* storage, AnyVisitor* visitor) const = 0;
};
/** @brief This is one concrete implementation of policy that
* uses void pointers to store values.
*/
template <typename T>
class PointerValueAnyPolicy : public BaseAnyPolicy
{
public:
/** Puts provided value pointed by v (untyped to be generic) to storage.
* @param storage pointer to a pointer to storage
* @param v pointer to value
*/
virtual void set(void** storage, const void* v) const
{
*(storage) = new T(value_of(typed_pointer<T>(v)));
}
/** Clones value provided by from into storage
* @param storage pointer to a pointer to storage
* @param from pointer to value to clone
*/
virtual void clone(void** storage, const void* from) const
{
any_detail::clone(storage, value_of(typed_pointer<T>(from)));
}
/** Clears storage.
* @param storage pointer to a pointer to storage
*/
virtual void clear(void** storage) const
{
delete typed_pointer<T>(*storage);
}
/** Returns type-name as string.
* @return name of type class
*/
virtual std::string type() const
{
return demangled_type<T>();
}
/** Returns type info
* @return type info of value's type
*/
virtual const std::type_info& type_info() const
{
return typeid(T);
}
/** Compares type.
* @param ti type information
* @return true if type matches
*/
virtual bool matches(const std::type_info& ti) const
{
return typeid(T) == ti;
}
/** Checks if policies are compatible.
* @param other other policy
* @return true if policies do match
*/
virtual bool matches(BaseAnyPolicy* other) const;
/** Compares two storages.
* @param storage pointer to a pointer to storage
* @param other_storage pointer to a pointer to another storage
* @return true if both storages have same value
*/
bool equals(const void* storage, const void* other_storage) const
{
const T& typed_storage = value_of(typed_pointer<T>(storage));
const T& typed_other_storage =
value_of(typed_pointer<T>(other_storage));
return compare(typed_storage, typed_other_storage);
}
virtual PolicyType policy_type() const
{
return PolicyType::OWNING;
}
/** Visitor pattern. Calls the appropriate 'on' method of AnyVisitor.
*
* @param storage pointer to a pointer to storage
* @param visitor abstract visitor to use
*/
virtual void visit(void* storage, AnyVisitor* visitor) const
{
visitor->on(static_cast<T*>(storage));
}
};
template <typename T>
class NonOwningAnyPolicy : public BaseAnyPolicy
{
public:
/** Puts provided value pointed by v (untyped to be generic) to storage.
* @param storage pointer to a pointer to storage
* @param v pointer to value
*/
virtual void set(void** storage, const void* v) const
{
mutable_value_of<T>(storage) = value_of(typed_pointer<T>(v));
}
/** Clones value provided by from into storage
* @param storage pointer to a pointer to storage
* @param from pointer to value to clone
*/
virtual void clone(void** storage, const void* from) const
{
any_detail::clone(storage, value_of(typed_pointer<T>(from)));
}
/** Clears storage.
* @param storage pointer to a pointer to storage
*/
virtual void clear(void** storage) const
{
}
/** Returns type-name as string.
* @return name of type class
*/
virtual std::string type() const
{
return demangled_type<T>();
}
/** Returns type info
* @return type info of value's type
*/
virtual const std::type_info& type_info() const
{
return typeid(T);
}
/** Compares type.
* @param ti type information
* @return true if type matches
*/
virtual bool matches(const std::type_info& ti) const
{
return typeid(T) == ti;
}
/** Checks if policies are compatible.
* @param other other policy
* @return true if policies do match
*/
virtual bool matches(BaseAnyPolicy* other) const;
/** Compares two storages.
* @param storage pointer to a pointer to storage
* @param other_storage pointer to a pointer to another storage
* @return true if both storages have same value
*/
bool equals(const void* storage, const void* other_storage) const
{
const T& typed_storage = value_of(typed_pointer<T>(storage));
const T& typed_other_storage =
value_of(typed_pointer<T>(other_storage));
return compare(typed_storage, typed_other_storage);
}
virtual PolicyType policy_type() const
{
return PolicyType::NON_OWNING;
}
/** Visitor pattern. Calls the appropriate 'on' method of AnyVisitor.
*
* @param storage pointer to storage
* @param visitor abstract visitor to use
*/
virtual void visit(void* storage, AnyVisitor* visitor) const
{
visitor->on(static_cast<T*>(storage));
}
};
template <typename T>
static BaseAnyPolicy* owning_policy()
{
typedef PointerValueAnyPolicy<T> Policy;
static Policy policy;
return &policy;
}
template <typename T>
static BaseAnyPolicy* non_owning_policy()
{
typedef NonOwningAnyPolicy<T> Policy;
static Policy policy;
return &policy;
}
template <class T>
bool NonOwningAnyPolicy<T>::matches(BaseAnyPolicy* other) const
{
if (this == other)
{
return true;
}
if (other == owning_policy<T>())
{
return true;
}
return matches(other->type_info());
}
template <class T>
bool PointerValueAnyPolicy<T>::matches(BaseAnyPolicy* other) const
{
if (this == other)
{
return true;
}
if (other == non_owning_policy<T>())
{
return true;
}
return matches(other->type_info());
}
/** @brief Allows to store objects of arbitrary types
* by using a BaseAnyPolicy and provides a type agnostic API.
* See its usage in CSGObject::Self, CSGObject::set(), CSGObject::get()
* and CSGObject::has().
* .
*/
class Any
{
public:
/** Empty value constructor */
Any() : Any(owning_policy<Empty>(), nullptr)
{
}
/** Base constructor */
Any(BaseAnyPolicy* the_policy, void* the_storage)
: policy(the_policy), storage(the_storage)
{
}
/** Constructor to copy value */
template <typename T>
explicit Any(const T& v) : Any(owning_policy<T>(), nullptr)
{
policy->set(&storage, &v);
}
/** Copy constructor */
Any(const Any& other) : Any(other.policy, nullptr)
{
set_or_inherit(other);
}
/** Move constructor */
Any(Any&& other) : Any(other.policy, nullptr)
{
set_or_inherit(other);
}
/** Assignment operator
* @param other another Any object
* @return Any object
*/
Any& operator=(const Any& other)
{
if (empty())
{
policy = other.policy;
set_or_inherit(other);
return *(this);
}
if (!policy->matches(other.policy))
{
throw TypeMismatchException(
other.policy->type(), policy->type());
}
policy->clear(&storage);
if (other.policy->policy_type() == PolicyType::NON_OWNING)
{
policy = other.policy;
storage = other.storage;
}
else
{
policy->set(&storage, other.storage);
}
return *(this);
}
Any& clone_from(const Any& other)
{
if (!policy->matches(other.policy))
{
throw TypeMismatchException(
other.policy->type(), policy->type());
}
policy->clone(&storage, other.storage);
return *(this);
}
/** Equality operator
* @param lhs Any object on left hand side
* @param rhs Any object on right hand side
* @return true if both are equal
*/
friend inline bool operator==(const Any& lhs, const Any& rhs);
/** Inequality operator
* @param lhs Any object on left hand side
* @param rhs Any object on right hand side
* @return false if both are equal
*/
friend inline bool operator!=(const Any& lhs, const Any& rhs);
/** Destructor */
~Any()
{
policy->clear(&storage);
}
/** Casts hidden value to provided type, fails otherwise.
* @return type-casted value
*/
template <typename T>
const T& as() const
{
if (same_type<T>())
{
return value_of(typed_pointer<T>(storage));
}
else
{
throw TypeMismatchException(
demangled_type<T>(), policy->type());
}
}
/** @return true if type is same. */
template <typename T>
inline bool same_type() const
{
return (policy == owning_policy<T>()) ||
(policy == non_owning_policy<T>()) ||
same_type_fallback<T>();
}
/** @return true if type-id is same. */
template <typename T>
bool same_type_fallback() const
{
return policy->matches(typeid(T));
}
/** @return true if Any object is empty. */
bool empty() const
{
return same_type<Empty>();
}
const std::type_info& type_info() const
{
return policy->type_info();
}
/** Returns type-name of policy as string.
* @return name of type class
*/
std::string type() const
{
return policy->type();
}
/** Visitor pattern. Calls the appropriate 'on' method of AnyVisitor.
*
* @param visitor visitor object to use
*/
void visit(AnyVisitor* visitor) const
{
policy->visit(storage, visitor);
}
private:
void set_or_inherit(const Any& other)
{
if (other.policy->policy_type() == PolicyType::NON_OWNING)
{
storage = other.storage;
}
else
{
policy->set(&storage, other.storage);
}
}
private:
BaseAnyPolicy* policy;
void* storage;
};
inline bool operator==(const Any& lhs, const Any& rhs)
{
if (lhs.empty() || rhs.empty())
{
return lhs.empty() && rhs.empty();
}
if (!lhs.policy->matches(rhs.policy))
{
return false;
}
void* lhs_storage = lhs.storage;
void* rhs_storage = rhs.storage;
return lhs.policy->equals(lhs_storage, rhs_storage);
}
inline bool operator!=(const Any& lhs, const Any& rhs)
{
return !(lhs == rhs);
}
/** Erases value type i.e. converts it to Any
* For input object of any type, it returns an Any object
* which stores the input object's raw value. It saves the type
* information internally to be cast back later by using any_cast().
*
* @param v value
* @return Any object with the input value
*/
template <typename T>
inline Any make_any(const T& v)
{
return Any(v);
}
template <typename T>
inline Any make_any_ref(T* v)
{
return Any(non_owning_policy<T>(), v);
}
template <typename T, typename S>
inline Any make_any_ref(T** ptr, S* length)
{
return make_any(ArrayReference<T, S>(ptr, length));
}
template <typename T, typename S>
inline Any make_any_ref(T** ptr, S* rows, S* cols)
{
return make_any(Array2DReference<T, S>(ptr, rows, cols));
}
/** Tries to recall Any type, fails when type is wrong.
* Any stores type information of an object internally in a BaseAnyPolicy.
* This function returns type-casted value if the internal type information
* matches with the provided typename, otherwise throws std::logic_error.
*
* @param any object of Any
* @return type-casted value
*/
template <typename T>
inline T any_cast(const Any& any)
{
return any.as<T>();
}
}
#endif //_ANY_H_