diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2020-12-16 20:29:05 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2021-02-24 16:40:04 +0300 |
commit | 8e761289a2446367267c6c0d9a26e734f0f78306 (patch) | |
tree | fb495d8c18801f271d124ee48731f10df396ca89 /libcutl/container/any.hxx | |
parent | 4c8104756b92b9fa16b3a725e8a6aa620dfd606e (diff) |
Get rid of legacy build systems and rename cutl/ to libcutl/
Diffstat (limited to 'libcutl/container/any.hxx')
-rw-r--r-- | libcutl/container/any.hxx | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/libcutl/container/any.hxx b/libcutl/container/any.hxx new file mode 100644 index 0000000..a7b1194 --- /dev/null +++ b/libcutl/container/any.hxx @@ -0,0 +1,149 @@ +// file : libcutl/container/any.hxx +// license : MIT; see accompanying LICENSE file + +#ifndef LIBCUTL_CONTAINER_ANY_HXX +#define LIBCUTL_CONTAINER_ANY_HXX + +#include <memory> // std::unique_ptr/auto_ptr +#include <typeinfo> // std::type_info + +#include <libcutl/exception.hxx> + +namespace cutl +{ + namespace container + { + class any + { + public: + struct typing: exception {}; + + public: + any () + { + } + + template <typename X> + any (X const& x) + : holder_ (new holder_impl<X> (x)) + { + } + + any (any const& x) + : holder_ (x.holder_->clone ()) + { + } + + template <typename X> + any& + operator= (X const& x) + { + holder_.reset (new holder_impl<X> (x)); + return *this; + } + + any& + operator= (any const& x) + { + holder_.reset (x.holder_->clone ()); + return *this; + } + + public: + template <typename X> + X& + value () + { + if (holder_impl<X>* p = dynamic_cast<holder_impl<X>*> (holder_.get ())) + return p->value (); + else + throw typing (); + } + + template <typename X> + X const& + value () const + { + if (holder_impl<X>* p = dynamic_cast<holder_impl<X>*> (holder_.get ())) + return p->value (); + else + throw typing (); + } + + std::type_info const& + type_info () const + { + return holder_->type_info (); + } + + public: + bool + empty () const + { + return holder_.get () == 0; + } + + void + reset () + { + return holder_.reset (); + } + + private: + class LIBCUTL_EXPORT holder + { + public: + virtual + ~holder () {} + + virtual holder* + clone () const = 0; + + virtual std::type_info const& + type_info () const = 0; + }; + + template <typename X> + class holder_impl: public holder + { + public: + holder_impl (X const& x) + : x_ (x) + { + } + + virtual holder_impl* + clone () const + { + return new holder_impl (x_); + } + + virtual std::type_info const& + type_info () const + { + return typeid (x_); + } + + X const& + value () const + { + return x_; + } + + X& + value () + { + return x_; + } + + private: + X x_; + }; + + private: + std::unique_ptr<holder> holder_; + }; + } +} + +#endif // LIBCUTL_CONTAINER_ANY_HXX |