Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
50 #include "Teuchos_ConstTypeTraits.hpp"
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 #include <typeinfo>
66 
67 namespace Teuchos {
68 
69 
77 TEUCHOSCORE_LIB_DLL_EXPORT std::string demangleName( const std::string &mangledName );
78 
79 
84 template<typename T>
86 public:
88  static std::string name()
89  {
90  return demangleName(typeid(T).name());
91  }
93 #ifndef TEUCHOS_TYPE_NAME_TRAITS_OLD_IBM
94  static std::string concreteName( const T& t )
95 #else
96  // the IBM compilers on AIX have a problem with const
97  static std::string concreteName( T t )
98 #endif
99  {
100  return demangleName(typeid(t).name());
101  }
102 };
103 
104 
114 template<typename T>
115 std::string typeName( const T &t )
116 {
117  typedef typename ConstTypeTraits<T>::NonConstType ncT;
118 #ifndef TEUCHOS_TYPE_NAME_TRAITS_OLD_IBM
120 #else
121  // You can't pass general objects to AIX by value as above. This means that
122  // you will not get the concrete name printed on AIX but that is life on
123  // such compilers.
124  return TypeNameTraits<ncT>::name();
125 #endif
126 }
127 
128 
137 template<typename T>
138 std::string concreteTypeName( const T &t )
139 {
140  typedef typename ConstTypeTraits<T>::NonConstType ncT;
142 }
143 
144 
145 #define TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(TYPE) \
146 template<> \
147 class TypeNameTraits<TYPE> { \
148 public: \
149  static std::string name() { return (#TYPE); } \
150  static std::string concreteName(const TYPE&) { return name(); } \
151 } \
152 
153 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(bool);
154 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(char);
155 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(signed char);
156 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(unsigned char);
157 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(short int);
158 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(int);
159 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(long int);
160 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(unsigned short int);
161 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(unsigned int);
162 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(unsigned long int);
163 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(float);
164 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(double);
165 
166 #ifdef HAVE_TEUCHOSCORE_QUADMATH
167 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(__float128);
168 #endif // HAVE_TEUCHOSCORE_QUADMATH
169 
170 template<typename T>
171 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<T*> {
172 public:
173  typedef T* T_ptr;
174  static std::string name() { return TypeNameTraits<T>::name() + "*"; }
175  static std::string concreteName(T_ptr) { return name(); }
176 };
177 
178 
179 template<>
180 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<std::string> {
181 public:
182  static std::string name() { return "string"; }
183  static std::string concreteName(const std::string&)
184  { return name(); }
185 };
186 
187 
188 template<>
189 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<void*> {
190 public:
191  static std::string name() { return "void*"; }
192  static std::string concreteName(const std::string&) { return name(); }
193 };
194 
195 // mfh 31 Jul 2012: Specialization for "void" will hopefully fix
196 // compile errors on Windows, such as the following:
197 //
198 // http://testing.sandia.gov/cdash/viewBuildError.php?buildid=611137
199 //
200 // I'm imitating the specialization of void* above.
201 template<>
202 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<void> {
203 public:
204  static std::string name() { return "void"; }
205  static std::string concreteName(const std::string&) { return name(); }
206 };
207 
208 
209 #ifdef HAVE_TEUCHOS_COMPLEX
210 
211 
212 template<typename T>
213 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<std::complex<T> > {
214 public:
215  static std::string name()
216  { return "complex<"+TypeNameTraits<T>::name()+">"; }
217  static std::string concreteName(const std::complex<T>&)
218  { return name(); }
219 };
220 
221 
222 #endif // HAVE_TEUCHOS_COMPLEX
223 
224 
225 
226 } // namespace Teuchos
227 
228 
229 #endif // _TEUCHOS_TYPE_NAME_TRAITS_HPP_
static std::string concreteName(const T &t)
TEUCHOSCORE_LIB_DLL_EXPORT std::string demangleName(const std::string &mangledName)
Demangle a C++ name if valid.
std::string concreteTypeName(const T &t)
Template function for returning the type name of the actual concrete name of a passed-in object...
Default traits class that just returns typeid(T).name().
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.