clipsmm - C++ CLIPS Interface Library

clipsmm logo
any.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2006 by Rick L. Vinyard, Jr. *
3  * rvinyard@cs.nmsu.edu *
4  * *
5  * This file is part of the clipsmm library. *
6  * *
7  * The clipsmm library is free software; you can redistribute it and/or *
8  * modify it under the terms of the GNU General Public License *
9  * version 3 as published by the Free Software Foundation. *
10  * *
11  * The clipsmm library is distributed in the hope that it will be *
12  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this software. If not see <http://www.gnu.org/licenses/>. *
18  ***************************************************************************/
19 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
20 //
21 // Permission to use, copy, modify, and distribute this software for any
22 // purpose is hereby granted without fee, provided that this copyright and
23 // permissions notice appear in all copies and derivatives.
24 //
25 // This software is provided "as is" without express or implied warranty.
26 
27 // See http://www.boost.org/libs/any for Documentation.
28 
29 #ifndef CLIPSANY_H
30 #define CLIPSANY_H
31 
32 // what: variant type boost::any
33 // who: contributed by Kevlin Henney,
34 // with features contributed and bugs found by
35 // Ed Brey, Mark Rodgers, Peter Dimov, and James Curran
36 // when: July 2001
37 // where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95
38 
39 #include <algorithm>
40 #include <typeinfo>
41 
42 // #include "boost/config.hpp"
43 
44 namespace CLIPS
45 {
46  class any
47  {
48  public: // structors
49 
50  any()
51  : content(0)
52  {
53  }
54 
55  template<typename ValueType>
56  any(const ValueType & value)
57  : content(new holder<ValueType>(value))
58  {
59  }
60 
61  any(const any & other)
62  : content(other.content ? other.content->clone() : 0)
63  {
64  }
65 
66  ~any()
67  {
68  delete content;
69  }
70 
71  public: // modifiers
72 
73  any & swap(any & rhs)
74  {
76  return *this;
77  }
78 
79  template<typename ValueType>
80  any & operator=(const ValueType & rhs)
81  {
82  any(rhs).swap(*this);
83  return *this;
84  }
85 
86  any & operator=(const any & rhs)
87  {
88  any(rhs).swap(*this);
89  return *this;
90  }
91 
92  public: // queries
93 
94  bool empty() const
95  {
96  return !content;
97  }
98 
99  const std::type_info & type() const
100  {
101  return content ? content->type() : typeid(void);
102  }
103 
104  private: // types
105 
107  {
108  public: // structors
109 
110  virtual ~placeholder()
111  {
112  }
113 
114  public: // queries
115 
116  virtual const std::type_info & type() const = 0;
117 
118  virtual placeholder * clone() const = 0;
119 
120  };
121 
122  template<typename ValueType>
123  class holder : public placeholder
124  {
125  public: // structors
126 
127  holder(const ValueType & value)
128  : held(value)
129  {
130  }
131 
132  public: // queries
133 
134  virtual const std::type_info & type() const
135  {
136  return typeid(ValueType);
137  }
138 
139  virtual placeholder * clone() const
140  {
141  return new holder(held);
142  }
143 
144  public: // representation
145 
146  ValueType held;
147 
148  };
149 
150  private: // representation
151 
152  template<typename ValueType>
153  friend ValueType * any_cast(any *);
154 
156 
157  };
158 
159  class bad_any_cast : public std::bad_cast
160  {
161  public:
162  virtual const char * what() const throw()
163  {
164  return "boost::bad_any_cast: "
165  "failed conversion using boost::any_cast";
166  }
167  };
168 
169  template<typename ValueType>
170  ValueType * any_cast(any * operand)
171  {
172  return operand && operand->type() == typeid(ValueType)
173  ? &static_cast<any::holder<ValueType> *>(operand->content)->held
174  : 0;
175  }
176 
177  template<typename ValueType>
178  const ValueType * any_cast(const any * operand)
179  {
180  return any_cast<ValueType>(const_cast<any *>(operand));
181  }
182 
183  template<typename ValueType>
184  ValueType any_cast(const any & operand)
185  {
186  const ValueType * result = any_cast<ValueType>(&operand);
187  if(!result)
188  throw bad_any_cast();
189  return *result;
190  }
191 
192 }
193 
194 #endif

Generated on Thu Sep 27 2012 17:57:05 for clipsmm by doxygen 1.8.1.2