Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
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
67namespace Teuchos {
68
69
77TEUCHOSCORE_LIB_DLL_EXPORT std::string demangleName( const std::string &mangledName );
78
79
84template<typename T>
86public:
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
114template<typename T>
115std::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.
125#endif
126}
127
128
137template<typename T>
138std::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) \
146template<> \
147class TypeNameTraits<TYPE> { \
148public: \
149 static std::string name() { return (#TYPE); } \
150 static std::string concreteName(const TYPE&) { return name(); } \
151} \
152
153TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(bool);
154TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(char);
155TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(signed char);
156TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(unsigned char);
157TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(short int);
158TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(int);
159TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(long int);
160TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(unsigned short int);
161TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(unsigned int);
162TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(unsigned long int);
163TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(float);
164TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(double);
165
166#ifdef HAVE_TEUCHOSCORE_QUADMATH
167TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(__float128);
168#endif // HAVE_TEUCHOSCORE_QUADMATH
169
170#ifdef HAVE_TEUCHOS_LONG_DOUBLE
171TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(long double);
172#endif // HAVE_TEUCHOS_LONG_DOUBLE
173
174template<typename T>
175class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<T*> {
176public:
177 typedef T* T_ptr;
178 static std::string name() { return TypeNameTraits<T>::name() + "*"; }
179 static std::string concreteName(T_ptr) { return name(); }
180};
181
182
183template<>
184class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<std::string> {
185public:
186 static std::string name() { return "string"; }
187 static std::string concreteName(const std::string&)
188 { return name(); }
189};
190
191
192template<>
193class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<void*> {
194public:
195 static std::string name() { return "void*"; }
196 static std::string concreteName(const std::string&) { return name(); }
197};
198
199// mfh 31 Jul 2012: Specialization for "void" will hopefully fix
200// compile errors on Windows, such as the following:
201//
202// http://testing.sandia.gov/cdash/viewBuildError.php?buildid=611137
203//
204// I'm imitating the specialization of void* above.
205template<>
206class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<void> {
207public:
208 static std::string name() { return "void"; }
209 static std::string concreteName(const std::string&) { return name(); }
210};
211
212
213#ifdef HAVE_TEUCHOS_COMPLEX
214
215
216template<typename T>
217class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<std::complex<T> > {
218public:
219 static std::string name()
220 { return "complex<"+TypeNameTraits<T>::name()+">"; }
221 static std::string concreteName(const std::complex<T>&)
222 { return name(); }
223};
224
225
226#endif // HAVE_TEUCHOS_COMPLEX
227
228
229
230} // namespace Teuchos
231
232
233#endif // _TEUCHOS_TYPE_NAME_TRAITS_HPP_
Smart reference counting pointer class for automatic garbage collection.
Default traits class that just returns typeid(T).name().
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.
std::string concreteTypeName(const T &t)
Template function for returning the type name of the actual concrete name of a passed-in object.
TEUCHOSCORE_LIB_DLL_EXPORT std::string demangleName(const std::string &mangledName)
Demangle a C++ name if valid.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...