49 struct bad_cast :
public std::logic_error {
bad_cast() : std::logic_error(
"neolib::any_const_iterator::bad_cast") {} };
50 struct is_empty :
public std::logic_error {
is_empty() : std::logic_error(
"neolib::any_const_iterator::is_empty") {} };
56 virtual ~holder_base() =
default;
59 virtual void operator++() = 0;
60 virtual void operator--() = 0;
63 operator const T&()
const
65 if (
typeid(T) != type())
67 return *
static_cast<const T*
>(ptr());
72 if (
typeid(T) != type())
74 return *
static_cast<T*
>(ptr());
77 bool is()
const {
return typeid(T) == type(); }
81 virtual const std::type_info& type()
const = 0;
82 virtual const void* ptr()
const = 0;
83 virtual void* ptr() = 0;
86 class holder :
public holder_base
90 holder(
const T& aObject) : iObject(aObject) {}
93 virtual void operator++() { ++iObject; }
94 virtual void operator--() { --iObject; }
97 return iObject ==
static_cast<const T&
>(aOther);
99 virtual holder_base* clone(
any_const_iterator& aOwner)
const {
return aOwner.create(iObject); }
100 virtual const std::type_info& type()
const {
return typeid(T); }
101 virtual const void* ptr()
const {
return &iObject; }
102 virtual void* ptr() {
return &iObject; }
110 template <
typename T>
118 iHolder = aOther.iHolder->clone(*
this);
159 return aLeft.iHolder->operator==(aRight);
163 return !(aLeft == aRight);
165 template <
typename T>
166 operator const T&()
const {
if (
empty())
throw bad_cast();
return *iHolder; }
167 template <
typename T>
169 template <
typename T>
170 bool is()
const {
return iHolder && iHolder->is<T>(); }
176 template <
typename T>
177 holder_base* create(
const T& aData)
179 if (
sizeof(T) <=
sizeof(iSpace.iBytes))
180 iHolder =
new (iSpace.iBytes) holder<T>(aData);
182 iHolder =
new holder<T>(aData);
187 if (iHolder ==
reinterpret_cast<holder_base*
>(iSpace.iBytes))
188 iHolder->~holder_base();
195 holder_base* iHolder;
207 struct bad_cast :
public std::logic_error {
bad_cast() : std::logic_error(
"neolib::any_iterator::bad_cast") {} };
208 struct is_empty :
public std::logic_error {
is_empty() : std::logic_error(
"neolib::any_iterator::is_empty") {} };
214 virtual ~holder_base() =
default;
217 virtual void operator++() = 0;
218 virtual void operator--() = 0;
220 template <
typename T>
221 operator const T&()
const
223 if (
typeid(T) != type())
225 return *
static_cast<const T*
>(ptr());
227 template <
typename T>
230 if (
typeid(T) != type())
232 return *
static_cast<T*
>(ptr());
234 template <
typename T>
235 bool is()
const {
return typeid(T) == type(); }
236 virtual holder_base* clone(
any_iterator& aOwner)
const = 0;
239 virtual const std::type_info& type()
const = 0;
240 virtual const void* ptr()
const = 0;
241 virtual void* ptr() = 0;
243 template <
typename T>
244 class holder :
public holder_base
248 holder(
const T& aObject) : iObject(aObject) {}
251 virtual void operator++() { ++iObject; }
252 virtual void operator--() { --iObject; }
255 return iObject ==
static_cast<const T&
>(aOther);
257 virtual holder_base* clone(
any_iterator& aOwner)
const {
return aOwner.create(iObject); }
258 virtual const std::type_info& type()
const {
return typeid(T); }
259 virtual const void* ptr()
const {
return &iObject; }
260 virtual void* ptr() {
return &iObject; }
268 template <
typename T>
276 iHolder = aOther.iHolder->clone(*
this);
315 return aLeft.iHolder->operator==(aRight);
319 return !(aLeft == aRight);
321 template <
typename T>
322 operator const T&()
const {
if (
empty())
throw bad_cast();
return *iHolder; }
323 template <
typename T>
325 template <
typename T>
326 bool is()
const {
return iHolder && iHolder->is<T>(); }
332 template <
typename T>
333 holder_base* create(
const T& aData)
335 if (
sizeof(T) <=
sizeof(iSpace.iBytes))
336 iHolder =
new (iSpace.iBytes) holder<T>(aData);
338 iHolder =
new holder<T>(aData);
343 if (iHolder ==
reinterpret_cast<holder_base*
>(iSpace.iBytes))
344 iHolder->~holder_base();
351 holder_base* iHolder;
359 template <
typename Source,
typename Target>
362 return static_cast<Target
>(
static_cast<const Source&
>(aSource));
any_const_iterator operator++(int)
any_const_iterator & operator=(const any_const_iterator &aOther)
any_const_iterator & operator++()
friend bool operator==(const any_const_iterator &aLeft, const any_const_iterator &aRight)
any_const_iterator & operator--()
any_const_iterator operator--(int)
friend bool operator!=(const any_const_iterator &aLeft, const any_const_iterator &aRight)
any_const_iterator(const T &aObject)
any_const_iterator(const any_const_iterator &aOther)
any_iterator & operator--()
any_iterator operator++(int)
friend bool operator!=(const any_iterator &aLeft, const any_iterator &aRight)
friend bool operator==(const any_iterator &aLeft, const any_iterator &aRight)
any_iterator(const T &aObject)
any_iterator(const any_iterator &aOther)
any_iterator & operator=(const any_iterator &aOther)
any_iterator & operator++()
any_iterator operator--(int)
any_const_iterator any_const_iterator_cast(any_iterator aSource)