aboutsummaryrefslogtreecommitdiff
path: root/cutl/container/any.hxx
blob: 8999fbb7b2c85247b76a5e14c483863b16f94664 (plain)
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
// file      : cutl/container/any.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#ifndef CUTL_CONTAINER_ANY_HXX
#define CUTL_CONTAINER_ANY_HXX

#include <memory>   // std::auto_ptr
#include <typeinfo> // std::type_info

#include <cutl/exception.hxx>

namespace cutl
{
  namespace container
  {
    class any
    {
    public:
      struct typing: exception {};

    public:
      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 ();
      }

    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 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::auto_ptr<holder> holder_;
    };
  }
}

#endif // CUTL_CONTAINER_ANY_HXX