Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_TypeNameTraits.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #ifndef _TEUCHOS_TYPE_NAME_TRAITS_HPP_
00043 #define _TEUCHOS_TYPE_NAME_TRAITS_HPP_
00044 
00050 #include "Teuchos_ConstTypeTraits.hpp"
00051 
00052 // mfh 30 Jan 2013: Thanks to Jim Willenbring for reporting this, and
00053 // to Mike Glass and Paul Lin for updating the fix for dealing with a
00054 // bug in IBM's XL C++ compiler.  The update was necessary due to a
00055 // relapse of the bug in a newer version of the compiler.
00056 //
00057 // If you don't have this update, you can fix the problem by defining
00058 // the macro TEUCHOS_TYPE_NAME_TRAITS_OLD_IBM when compiling anything
00059 // that includes this header file.  If you have the current version of
00060 // this file, then you don't need to do anything.
00061 #if defined(__IBMCPP__) && ( __IBMCPP__ < 900 || __IBMCPP__ == 1210 )
00062 # define TEUCHOS_TYPE_NAME_TRAITS_OLD_IBM
00063 #endif
00064 
00065 namespace  Teuchos {
00066 
00067 
00075 TEUCHOSCORE_LIB_DLL_EXPORT std::string demangleName( const std::string &mangledName );
00076 
00077 
00082 template<typename T>
00083 class TypeNameTraits {
00084 public:
00086   static std::string name()
00087     {
00088       return demangleName(typeid(T).name());
00089     }
00091 #ifndef TEUCHOS_TYPE_NAME_TRAITS_OLD_IBM
00092   static std::string concreteName( const T& t )
00093 #else
00094   // the IBM compilers on AIX have a problem with const
00095   static std::string concreteName( T t )
00096 #endif
00097     {
00098       return demangleName(typeid(t).name());
00099     }
00100 };
00101 
00102 
00112 template<typename T>
00113 std::string typeName( const T &t )
00114 {
00115   typedef typename ConstTypeTraits<T>::NonConstType ncT;
00116 #ifndef TEUCHOS_TYPE_NAME_TRAITS_OLD_IBM
00117   return TypeNameTraits<ncT>::concreteName(t);
00118 #else
00119   // You can't pass general objects to AIX by value as above.  This means that
00120   // you will not get the concrete name printed on AIX but that is life on
00121   // such compilers.
00122   return TypeNameTraits<ncT>::name();
00123 #endif
00124 }
00125 
00126 
00135 template<typename T>
00136 std::string concreteTypeName( const T &t )
00137 {
00138   typedef typename ConstTypeTraits<T>::NonConstType ncT;
00139   return TypeNameTraits<ncT>::concreteName(t);
00140 }
00141 
00142 
00143 #define TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(TYPE) \
00144 template<> \
00145 class TypeNameTraits<TYPE> { \
00146 public: \
00147   static std::string name() { return (#TYPE); } \
00148   static std::string concreteName(const TYPE&) { return name(); } \
00149 } \
00150 
00151 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(bool);
00152 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(char);
00153 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(int);
00154 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(short int);
00155 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(long int);
00156 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(float);
00157 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(double);
00158 
00159 
00160 template<typename T>
00161 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<T*> {
00162 public:
00163   typedef T* T_ptr;
00164   static std::string name() { return TypeNameTraits<T>::name() + "*"; }
00165   static std::string concreteName(T_ptr) { return name(); }
00166 };
00167 
00168 
00169 template<>
00170 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<std::string> {
00171 public:
00172   static std::string name() { return "string"; }
00173   static std::string concreteName(const std::string&)
00174     { return name(); }
00175 };
00176 
00177 
00178 template<>
00179 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<void*> {
00180 public:
00181   static std::string name() { return "void*"; }
00182   static std::string concreteName(const std::string&) { return name(); }
00183 };
00184 
00185 // mfh 31 Jul 2012: Specialization for "void" will hopefully fix
00186 // compile errors on Windows, such as the following:
00187 //
00188 // http://testing.sandia.gov/cdash/viewBuildError.php?buildid=611137
00189 //
00190 // I'm imitating the specialization of void* above.
00191 template<>
00192 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<void> {
00193 public:
00194   static std::string name() { return "void"; }
00195   static std::string concreteName(const std::string&) { return name(); }
00196 };
00197 
00198 
00199 #ifdef HAVE_TEUCHOS_COMPLEX
00200 
00201 
00202 template<typename T>
00203 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<std::complex<T> > {
00204 public:
00205   static std::string name()
00206     { return "complex<"+TypeNameTraits<T>::name()+">"; }
00207   static std::string concreteName(const std::complex<T>&)
00208     { return name(); }
00209 };
00210 
00211 
00212 #endif // HAVE_TEUCHOS_COMPLEX
00213 
00214 
00215 
00216 } // namespace Teuchos
00217 
00218 
00219 #endif // _TEUCHOS_TYPE_NAME_TRAITS_HPP_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines