Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_any.hpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Teuchos: Common Tools Package
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
44 #ifndef TEUCHOS_ANY_HPP
45 #define TEUCHOS_ANY_HPP
46 
51 #include <utility>
52 #include <type_traits>
53 #include <exception>
54 
55 #include "Teuchos_Assert.hpp"
57 
58 //
59 // This file was taken from the boost library which contained the
60 // following notice:
61 //
62 // *************************************************************
63 //
64 // what: variant type boost::any
65 // who: contributed by Kevlin Henney,
66 // with features contributed and bugs found by
67 // Ed Brey, Mark Rodgers, Peter Dimov, and James Curran
68 // when: July 2001
69 // where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95
70 //
71 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
72 //
73 // Permission to use, copy, modify, and distribute this software for any
74 // purpose is hereby granted without fee, provided that this copyright and
75 // permissions notice appear in all copies and derivatives.
76 //
77 // This software is provided "as is" without express or implied warranty.
78 //
79 // *************************************************************
80 //
81 // RAB modified the file for use in Teuchos. I changed the nature of
82 // the any_cast<> to be easier to use.
83 //
84 
85 namespace Teuchos {
86 
87 template<class T>
88 struct is_comparable
89 {
90  template<class X>
91  static auto test(int) -> decltype(std::declval<X>() == std::declval<X>(),
92  void(), std::true_type());
93  template<class X>
94  static auto test(...) -> std::false_type;
95  using type = decltype(test<T>(0));
96 };
97 
98 template<class T>
99 struct is_printable
100 {
101  template<class X>
102  static auto test(int) -> decltype(std::declval<std::ostream&>() << std::declval<X>(),
103  void(), std::true_type());
104  template<class X>
105  static auto test(...) -> std::false_type;
106  using type = decltype(test<T>(0));
107 };
108 
109 template <class T, class ok = typename is_comparable<T>::type>
110 struct compare;
111 
112 template <class T>
113 struct compare<T, std::false_type> {
114  bool operator()(T const&, T const&) const {
115  TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error,
116  "Trying to compare type " << typeid(T).name() << " which is not comparable");
117 #ifndef __CUDACC__
118  return false;
119 #endif
120  }
121 };
122 
123 template <class T>
124 struct compare<T, std::true_type> {
125  bool operator()(T const& a, T const& b) const {
126  return a == b;
127  }
128 };
129 
130 template <class T, class ok = typename is_printable<T>::type>
131 struct print;
132 
133 template <class T>
134 struct print<T, std::false_type> {
135  std::ostream& operator()(std::ostream& s, T const&) const {
136  TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error,
137  "Trying to print type " << typeid(T).name() << " which is not printable");
138 #ifndef __CUDACC__
139  return s;
140 #endif
141  }
142 };
143 
144 template <class T>
145 struct print<T, std::true_type> {
146  std::ostream& operator()(std::ostream& a, T const& b) const {
147  return a << b;
148  }
149 };
150 
154 class TEUCHOSCORE_LIB_DLL_EXPORT any
155 {
156 public:
158  any()
159  : content(0)
160  {}
161 
163  template<typename ValueType>
164  explicit any(const ValueType & value)
165  : content(new holder<ValueType>(value))
166  {}
167 
169  any(const any & other)
170  : content(other.content ? other.content->clone() : 0)
171  {}
172 
175  {
176  delete content;
177  }
178 
180  any & swap(any & rhs)
181  {
182  std::swap(content, rhs.content);
183  return *this;
184  }
185 
187  template<typename ValueType>
188  any & operator=(const ValueType & rhs)
189  {
190  any(rhs).swap(*this);
191  return *this;
192  }
193 
195  any & operator=(const any & rhs)
196  {
197  any(rhs).swap(*this);
198  return *this;
199  }
200 
202  bool empty() const
203  {
204  return !content;
205  }
206 
208  const std::type_info & type() const
209  {
210  return content ? content->type() : typeid(void);
211  }
212 
214  std::string typeName() const
215  {
216  return content ? content->typeName() : "NONE";
217  }
218 
223  bool same( const any &other ) const
224  {
225  if( this->empty() && other.empty() )
226  return true;
227  else if( this->empty() && !other.empty() )
228  return false;
229  else if( !this->empty() && other.empty() )
230  return false;
231  // !this->empty() && !other.empty()
232  return content->same(*other.content);
233  }
234 
239  void print(std::ostream& os) const
240  {
241  if (content) content->print(os);
242  }
243 
244 #ifndef DOXYGEN_SHOULD_SKIP_THIS
245 
247 
249  class placeholder
250  {
251  public:
253  virtual ~placeholder() {}
255  virtual const std::type_info & type() const = 0;
257  virtual std::string typeName() const = 0;
259  virtual placeholder * clone() const = 0;
261  virtual bool same( const placeholder &other ) const = 0;
263  virtual void print(std::ostream & os) const = 0;
264  };
265 
267  template<typename ValueType>
268  class holder : public placeholder
269  {
270  public:
272  holder(const ValueType & value)
273  : held(value)
274  {}
276  const std::type_info & type() const
277  { return typeid(ValueType); }
279  std::string typeName() const
280  { return TypeNameTraits<ValueType>::name(); }
282  placeholder * clone() const
283  { return new holder(held); }
285  bool same( const placeholder &other ) const
286  {
287  if( type() != other.type() ) {
288  return false;
289  }
290  // type() == other.type()
291  const ValueType
292  &other_held = dynamic_cast<const holder<ValueType>&>(other).held;
293  return ::Teuchos::compare<ValueType>{}(held, other_held);
294  }
296  void print(std::ostream & os) const
297  { ::Teuchos::print<ValueType>{}(os, held); }
299  ValueType held;
300  };
301 
303 
304 public:
305  // Danger: This is made public to allow any_cast to be non-friend
306  placeholder* access_content()
307  { return content; }
308  const placeholder* access_content() const
309  { return content; }
310 #endif
311 
312 private:
313 
314  // /////////////////////////
315  // Private data members
316 
317  placeholder * content;
318 
319 };
320 
324 class bad_any_cast : public std::runtime_error
325 {
326 public:
327  bad_any_cast( const std::string msg ) : std::runtime_error(msg) {}
328 };
329 
338 template<typename ValueType>
339 ValueType& any_cast(any &operand)
340 {
341  const std::string ValueTypeName = TypeNameTraits<ValueType>::name();
343  operand.type() != typeid(ValueType), bad_any_cast,
344  "any_cast<"<<ValueTypeName<<">(operand): Error, cast to type "
345  << "any::holder<"<<ValueTypeName<<"> failed since the actual underlying type is \'"
346  << typeName(*operand.access_content()) << "!"
347  );
349  !operand.access_content(), bad_any_cast
350  ,"any_cast<"<<ValueTypeName<<">(operand): Error, cast to type "
351  << "any::holder<"<<ValueTypeName<<"> failed because the content is NULL"
352  );
353  any::holder<ValueType>
354  *dyn_cast_content = dynamic_cast<any::holder<ValueType>*>(operand.access_content());
356  !dyn_cast_content, std::logic_error
357  ,"any_cast<"<<ValueTypeName <<">(operand): Error, cast to type "
358  << "any::holder<"<<ValueTypeName<<"> failed but should not have and the actual underlying type is \'"
359  << typeName(*operand.access_content()) << "!"
360  << " The problem might be related to incompatible RTTI systems in static and shared libraries!"
361  );
362  return dyn_cast_content->held;
363 }
364 
374 template<typename ValueType>
375 const ValueType& any_cast(const any &operand)
376 {
377  return any_cast<ValueType>(const_cast<any&>(operand));
378 }
379 
387 template<typename ValueType>
388 ValueType& any_ref_cast(any &operand)
389 {
390  return Teuchos::any_cast<ValueType>(operand);
391 }
392 
398 inline std::string toString(const any &rhs)
399 {
400  std::ostringstream oss;
401  rhs.print(oss);
402  return oss.str();
403 }
404 
410 inline bool operator==( const any &a, const any &b )
411 {
412  return a.same(b);
413 }
414 
420 inline bool operator!=( const any &a, const any &b )
421 {
422  return !a.same(b);
423 }
424 
430 inline std::ostream & operator<<(std::ostream & os, const any &rhs)
431 {
432  rhs.print(os);
433  return os;
434 }
435 
439 inline void swap(Teuchos::any& a, Teuchos::any& b) {
440  a.swap(b);
441 }
442 
446 template <typename T>
447 T & make_any_ref(any &rhs)
448 {
449  any(T()).swap(rhs);
450  return any_cast<T>(rhs);
451 }
452 
453 } // namespace Teuchos
454 
455 #endif // TEUCHOS_ANY_HPP
void print(std::ostream &os) const
Print this value to the output stream os
T & make_any_ref(any &rhs)
Default constructs a new T value and returns a reference to it.
bool same(const any &other) const
Return if two any objects are the same or not.
ValueType & any_cast(any &operand)
Used to extract the templated value held in Teuchos::any to a given value type.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
const ValueType & any_cast(const any &operand)
Used to extract the const templated value held in Teuchos::any to a given const value type...
any()
Empty constructor.
Modified boost::any class, which is a container for a templated value.
any & swap(any &rhs)
Method for swapping the contents of two any classes.
std::string toString(const any &rhs)
Converts the value in any to a std::string.
Thrown if any_cast is attempted between two incompatable types.
ValueType & any_ref_cast(any &operand)
Keep the convenient behavior of Teuchos::any_cast w.r.t. references, but don&#39;t confuse it with the be...
any & operator=(const ValueType &rhs)
Copy the value rhs
~any()
Destructor.
bool operator==(const any &a, const any &b)
Returns true if two any objects have the same value.
std::string typeName() const
Return the name of the type.
const std::type_info & type() const
Return the type of value being stored.
bool operator!=(const any &a, const any &b)
Returns true if two any objects do not have the same value.
void swap(Teuchos::any &a, Teuchos::any &b)
Special swap for other code to find via Argument Dependent Lookup.
Defines basic traits returning the name of a type in a portable and readable way. ...
any & operator=(const any &rhs)
Copy the value held in rhs
any(const ValueType &value)
Templated constructor.
any(const any &other)
Copy constructor.
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
std::ostream & operator<<(std::ostream &os, const any &rhs)
Writes &quot;any&quot; input rhs to the output stream os.
bool empty() const
Return true if nothing is being stored.