summaryrefslogtreecommitdiff
path: root/ACE/contrib/utility/Utility/Hetero/Container.hpp
blob: d28d5fff0c4f2f15b2040223756c289285509120 (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
// file      : Utility/Hetero/Container.hpp
// author    : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2002-2003 Boris Kolpackov
// license   : http://kolpackov.net/license.html

#ifndef UTILITY_HETERO_CONTAINER_HPP
#define UTILITY_HETERO_CONTAINER_HPP

#include <typeinfo>

namespace Utility
{
  namespace Hetero
  {
    //
    //
    //
    class Typing {};

    //
    //
    //
    class Container
    {
    public:
      ~Container ()
      {
        delete holder_;
      }

    public:
      template <typename T>
      Container (T const& t)
          : holder_ (new TypedHolder<T> (t))
      {
      }

      Container (Container const& c)
          : holder_ (c.holder_->clone ())
      {
      }

    public:
      template <typename T>
      Container&
      operator= (T const& t)
      {
        delete holder_;
        holder_ = 0;
        holder_ = new TypedHolder<T> (t);
        return *this;
      }

      Container&
      operator= (Container const& c)
      {
        delete holder_;
        holder_ = 0;
        holder_ = c.holder_->clone ();
        return *this;
      }

    public:
      template <typename T>
      operator T& ()
      {
        return value<T> ();
      }

      template <typename T>
      operator T const& () const
      {
        return value<T> ();
      }

    public:
      template <typename T>
      T&
      value ()
      {
        if (holder_->type () == typeid (T))
        {
          return dynamic_cast<TypedHolder<T>*>(holder_)->value ();
        }
        else
        {
          throw Typing ();
        }
      }

      template <typename T>
      T const&
      value () const
      {
        if (holder_->type () == typeid (T))
        {
          return dynamic_cast<TypedHolder<T>*>(holder_)->value ();
        }
        else
        {
          throw Typing ();
        }
      }

    public:
      std::type_info const&
      type () const
      {
        return holder_->type ();
      }

    public:
      template <typename T>
      friend T
      operator+ (Container const& a, T const& b)
      {
        return a.value<T> () + b;
      }

      template <typename T>
      friend T
      operator+ (T const& a, Container const& b)
      {
        return a + b.value<T> ();
      }

    private:
      struct Holder
      {
        virtual
        ~Holder () {}

        virtual Holder*
        clone () const = 0;

        virtual std::type_info const&
        type () const = 0;
      };

      template <typename T>
      struct TypedHolder : public Holder
      {
        TypedHolder (T const& value)
            : value_ (value)
        {
        }

        virtual Holder*
        clone () const
        {
          return new TypedHolder<T> (value_);
        }

        virtual std::type_info const&
        type () const
        {
          return typeid (T);
        }

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

        T&
        value ()
        {
          return value_;
        }

      private:
        T value_;
      };

      Holder* holder_;
    };
  }
}

#endif  // UTILITY_HETERO_CONTAINER_HPP
//$Id$