50 struct bad_cast :
public std::logic_error {
bad_cast() : std::logic_error(
"neolib::any_predicate::bad_cast") {} };
57 virtual ~holder_base() =
default;
62 operator const T&()
const
64 if (
typeid(T) != type())
66 return *
static_cast<const T*
>(ptr());
71 if (
typeid(T) != type())
73 return *
static_cast<T*
>(ptr());
76 bool is()
const {
return typeid(T) == type(); }
77 virtual holder_base* clone()
const = 0;
80 virtual const std::type_info& type()
const = 0;
81 virtual const void* ptr()
const = 0;
82 virtual void* ptr() = 0;
85 class holder :
public holder_base
89 holder(
const T& aObject) : iObject(aObject) {}
92 virtual bool operator()(
const value_type& aLeft,
const value_type& aRight)
const {
return iObject(aLeft, aRight); }
93 virtual holder_base* clone()
const {
return new holder(iObject); }
94 virtual const std::type_info& type()
const {
return typeid(T); }
95 virtual const void* ptr()
const {
return &iObject; }
96 virtual void* ptr() {
return &iObject; }
104 template <
typename T>
112 iHolder = aOther.iHolder->clone();
121 return (*iHolder)(aLeft, aRight);
123 template <
typename T>
124 operator const T&()
const {
if (
empty())
throw bad_cast();
return *iHolder; }
125 template <
typename T>
127 template <
typename T>
128 bool is()
const {
return iHolder && iHolder->is<T>(); }
134 void destroy() {
delete iHolder; iHolder =
nullptr; }
137 holder_base* iHolder;