Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
Teuchos_TypeNameTraits.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef _TEUCHOS_TYPE_NAME_TRAITS_HPP_
43 #define _TEUCHOS_TYPE_NAME_TRAITS_HPP_
44 
51 
52 // mfh 30 Jan 2013: Thanks to Jim Willenbring for reporting this, and
53 // to Mike Glass and Paul Lin for updating the fix for dealing with a
54 // bug in IBM's XL C++ compiler. The update was necessary due to a
55 // relapse of the bug in a newer version of the compiler.
56 //
57 // If you don't have this update, you can fix the problem by defining
58 // the macro TEUCHOS_TYPE_NAME_TRAITS_OLD_IBM when compiling anything
59 // that includes this header file. If you have the current version of
60 // this file, then you don't need to do anything.
61 #if defined(__IBMCPP__) && ( __IBMCPP__ < 900 || __IBMCPP__ == 1210 )
62 # define TEUCHOS_TYPE_NAME_TRAITS_OLD_IBM
63 #endif
64 
65 namespace Teuchos {
66 
67 
75 TEUCHOSCORE_LIB_DLL_EXPORT std::string demangleName( const std::string &mangledName );
76 
77 
82 template<typename T>
84 public:
86  static std::string name()
87  {
88  return demangleName(typeid(T).name());
89  }
91 #ifndef TEUCHOS_TYPE_NAME_TRAITS_OLD_IBM
92  static std::string concreteName( const T& t )
93 #else
94  // the IBM compilers on AIX have a problem with const
95  static std::string concreteName( T t )
96 #endif
97  {
98  return demangleName(typeid(t).name());
99  }
100 };
101 
102 
112 template<typename T>
113 std::string typeName( const T &t )
114 {
115  typedef typename ConstTypeTraits<T>::NonConstType ncT;
116 #ifndef TEUCHOS_TYPE_NAME_TRAITS_OLD_IBM
118 #else
119  // You can't pass general objects to AIX by value as above. This means that
120  // you will not get the concrete name printed on AIX but that is life on
121  // such compilers.
122  return TypeNameTraits<ncT>::name();
123 #endif
124 }
125 
126 
135 template<typename T>
136 std::string concreteTypeName( const T &t )
137 {
138  typedef typename ConstTypeTraits<T>::NonConstType ncT;
140 }
141 
142 
143 #define TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(TYPE) \
144 template<> \
145 class TypeNameTraits<TYPE> { \
146 public: \
147  static std::string name() { return (#TYPE); } \
148  static std::string concreteName(const TYPE&) { return name(); } \
149 } \
150 
163 
164 #ifdef HAVE_TEUCHOSCORE_QUADMATH
166 #endif // HAVE_TEUCHOSCORE_QUADMATH
167 
168 template<typename T>
170 public:
171  typedef T* T_ptr;
172  static std::string name() { return TypeNameTraits<T>::name() + "*"; }
173  static std::string concreteName(T_ptr) { return name(); }
174 };
175 
176 
177 template<>
179 public:
180  static std::string name() { return "string"; }
181  static std::string concreteName(const std::string&)
182  { return name(); }
183 };
184 
185 
186 template<>
188 public:
189  static std::string name() { return "void*"; }
190  static std::string concreteName(const std::string&) { return name(); }
191 };
192 
193 // mfh 31 Jul 2012: Specialization for "void" will hopefully fix
194 // compile errors on Windows, such as the following:
195 //
196 // http://testing.sandia.gov/cdash/viewBuildError.php?buildid=611137
197 //
198 // I'm imitating the specialization of void* above.
199 template<>
201 public:
202  static std::string name() { return "void"; }
203  static std::string concreteName(const std::string&) { return name(); }
204 };
205 
206 
207 #ifdef HAVE_TEUCHOS_COMPLEX
208 
209 
210 template<typename T>
211 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<std::complex<T> > {
212 public:
213  static std::string name()
214  { return "complex<"+TypeNameTraits<T>::name()+">"; }
215  static std::string concreteName(const std::complex<T>&)
216  { return name(); }
217 };
218 
219 
220 #endif // HAVE_TEUCHOS_COMPLEX
221 
222 
223 
224 } // namespace Teuchos
225 
226 
227 #endif // _TEUCHOS_TYPE_NAME_TRAITS_HPP_
static std::string concreteName(const T &t)
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
TEUCHOSCORE_LIB_DLL_EXPORT std::string demangleName(const std::string &mangledName)
Demangle a C++ name if valid.
static std::string concreteName(const std::string &)
std::string concreteTypeName(const T &t)
Template function for returning the type name of the actual concrete name of a passed-in object...
static std::string concreteName(const std::string &)
#define TEUCHOSCORE_LIB_DLL_EXPORT
static std::string concreteName(T_ptr)
TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(bool)
Default traits class that just returns typeid(T).name().
static std::string concreteName(const std::string &)