summaryrefslogtreecommitdiff
path: root/libcult/cult/containers/any.hxx
blob: 10f771f2026a13913b65e61ab3346d5586db3f03 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// file      : cult/containers/any.hxx
// author    : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2005-2010 Boris Kolpackov
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef CULT_CONTAINERS_ANY_HXX
#define CULT_CONTAINERS_ANY_HXX

#include <cult/types.hxx>

#include <cult/rtti/type-id.hxx>

namespace Cult
{
  namespace Containers
  {
    //@@ eager clonning: do I need it?
    //
    //
    class Any
    {
    public:
      struct Typing {};

    public:
      template <typename X>
      Any (X const& x)
          : holder_ (new HolderTemplate<X> (x))
      {
      }

      Any (Any const& any)
          : holder_ (any.holder_->clone ())
      {
      }

    public:
      template <typename X>
      Any&
      operator= (X const& x)
      {
        holder_ = Evptr<Holder> (new HolderTemplate<X> (x));
        return *this;
      }

      Any&
      operator= (Any const& any)
      {
        holder_ = any.holder_->clone ();
        return *this;
      }

    public:
      /*
      template <typename X>
      operator X& ()
      {
        return value<X> ();
      }

      template <typename X>
      operator X const& () const
      {
        return value<X> ();
      }
      */

    public:
      template <typename X>
      X&
      value ()
      {
        //@@ too strict
        //
        if (holder_->type_id () == typeid (X))
        {
          return dynamic_cast<HolderTemplate<X>*>(holder_.get ())->value ();
        }
        else
        {
          throw Typing ();
        }
      }

      template <typename X>
      X const&
      value () const
      {
        if (holder_->type_id () == typeid (X))
        {
          return dynamic_cast<HolderTemplate<X>*>(holder_.get ())->value ();
        }
        else
        {
          throw Typing ();
        }
      }

    public:
      RTTI::TypeId
      type_id () const
      {
        return holder_->type_id ();
      }

    public:
      /*
        template <typename x>
        friend x
        operator+ (any const& a, x const& b)
        {
        return a.value<x> () + b;
        }

        template <typename x>
        friend x
        operator+ (x const& a, any const& b)
        {
        return a + b.value<x> ();
        }
      */

    private:
      class Holder
      {
      public:
        virtual
        ~Holder ()
        {
        }

        Evptr<Holder>
        clone () const
        {
          return clone_ ();
        }

        virtual RTTI::TypeId
        type_id () const = 0;

      protected:
        virtual Evptr<Holder>
        clone_ () const = 0;
      };

      template <typename X>
      class HolderTemplate : public Holder
      {
      public:
        HolderTemplate (X const& value)
            : value_ (value)
        {
        }

        virtual RTTI::TypeId
        type_id () const
        {
          return RTTI::TypeId (typeid (value_));
        }

        X const&
        value () const
        {
          return value_;
        }

        X&
        value ()
        {
          return value_;
        }

      protected:
        virtual Evptr<Holder>
        clone_ () const
        {
          return new HolderTemplate<X> (value_);
        }

      private:
        X value_;
      };

    private:
      Evptr<Holder> holder_;
    };
  }
}

#include <cult/containers/any.txx>

#endif  // CULT_CONTAINERS_ANY_HXX