blitzdg
an open-source project aiming to implement parallel discontinuous Galerkin (dg) solvers for common partial differential equations systems using blitz++ for array and tensor manipulations and MPI for distributed parallelism.
stringize.h
1 
2 // Copyright Joakim Karlsson & Kim Gräsman 2010-2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #ifndef IGLOO_STRINGIZE_H
8 #define IGLOO_STRINGIZE_H
9 
10 namespace snowhouse {
11  namespace detail {
12 
13  // This type soaks up any implicit conversions and makes the following operator<<
14  // less preferred than any other such operator found via ADL.
15  struct any
16  {
17  // Conversion constructor for any type.
18  template <class T>
19  any(T const&);
20  };
21 
22  // A tag type returned by operator<< for the any struct in this namespace
23  // when T does not support <<.
24  struct tag {};
25 
26  // Fallback operator<< for types T that don't support <<.
27  tag operator<<(std::ostream&, any const&);
28 
29  // Two overloads to distinguish whether T supports a certain operator expression.
30  // The first overload returns a reference to a two-element character array and is chosen if
31  // T does not support the expression, such as <<, whereas the second overload returns a char
32  // directly and is chosen if T supports the expression. So using sizeof(check(<expression>))
33  // returns 2 for the first overload and 1 for the second overload.
34  typedef char yes;
35  typedef char (&no)[2];
36 
37  no check(tag);
38 
39  template <class T>
40  yes check(T const&);
41 
42  template <class T>
44  {
45  static const T& x;
46  static const bool value = sizeof(check(std::cout << x)) == sizeof(yes);
47  };
48 
49  template<typename T, bool type_is_streamable>
51  {
52  static std::string ToString(const T& value)
53  {
54  std::ostringstream buf;
55  buf << value;
56  return buf.str();
57  }
58  };
59 
60  template<typename T>
61  struct DefaultStringizer<T, false>
62  {
63  static std::string ToString(const T&)
64  {
65  return "[unsupported type]";
66  }
67  };
68  }
69 
70  template<typename T>
71  struct Stringizer;
72 
73  template<typename T>
74  std::string Stringize(const T& value)
75  {
76  return Stringizer<T>::ToString(value);
77  }
78 
79  // NOTE: Specialize snowhouse::Stringizer to customize assertion messages
80  template<typename T>
81  struct Stringizer
82  {
83  static std::string ToString(const T& value)
84  {
85  using namespace detail;
86 
87  return DefaultStringizer< T, is_output_streamable<T>::value >::ToString(value);
88  }
89  };
90 }
91 
92 #endif
Definition: assert.h:13
Definition: stringize.h:15
Definition: stringize.h:50
Definition: stringize.h:24
Definition: stringize.h:71
Definition: stringize.h:43