From cb9ea47e7825b5073d4d645afb94f6326cb7cf4d Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 6 Sep 2009 12:52:53 +0200 Subject: Start the libcutl repository --- cutl/container/any.hxx | 133 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 cutl/container/any.hxx (limited to 'cutl/container/any.hxx') diff --git a/cutl/container/any.hxx b/cutl/container/any.hxx new file mode 100644 index 0000000..0c89a38 --- /dev/null +++ b/cutl/container/any.hxx @@ -0,0 +1,133 @@ +// file : cutl/container/any.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef CUTL_CONTAINER_ANY_HXX +#define CUTL_CONTAINER_ANY_HXX + +#include // std::auto_ptr +#include // std::type_info + +namespace cutl +{ + namespace container + { + class any + { + public: + struct typing {}; + + public: + template + any (X const& x) + : holder_ (new holder_impl (x)) + { + } + + any (any const& x) + : holder_ (x.holder_->clone ()) + { + } + + template + any& + operator= (X const& x) + { + holder_.reset (new holder_impl (x)); + return *this; + } + + any& + operator= (any const& x) + { + holder_.reset (x.holder_->clone ()); + return *this; + } + + public: + template + X& + value () + { + if (holder_impl* p = dynamic_cast*> (holder_.get ())) + return p->value (); + else + throw typing (); + } + + template + X const& + value () const + { + if (holder_impl* p = dynamic_cast*> (holder_.get ())) + return p->value (); + else + throw typing (); + } + + public: + std::type_info const& + type_info () const + { + return holder_->type_info (); + } + + private: + class holder + { + public: + virtual + ~holder () {} + + virtual holder* + clone () const = 0; + + virtual std::type_info& + type_info () const = 0; + }; + + template + 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& + type_info () const + { + return typeid (x_); + } + + X const& + value () const + { + return x_; + } + + X& + value () + { + return x_; + } + + private: + X x_; + }; + + private: + std::auto_ptr holder_; + }; + } +} + +#endif // CUTL_CONTAINER_ANY_HXX -- cgit v1.1