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(signed char);
00154 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(unsigned char);
00155 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(short int);
00156 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(int);
00157 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(long int);
00158 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(unsigned short int);
00159 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(unsigned int);
00160 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(unsigned long int);
00161 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(float);
00162 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(double);
00163 
00164 
00165 template<typename T>
00166 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<T*> {
00167 public:
00168   typedef T* T_ptr;
00169   static std::string name() { return TypeNameTraits<T>::name() + "*"; }
00170   static std::string concreteName(T_ptr) { return name(); }
00171 };
00172 
00173 
00174 template<>
00175 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<std::string> {
00176 public:
00177   static std::string name() { return "string"; }
00178   static std::string concreteName(const std::string&)
00179     { return name(); }
00180 };
00181 
00182 
00183 template<>
00184 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<void*> {
00185 public:
00186   static std::string name() { return "void*"; }
00187   static std::string concreteName(const std::string&) { return name(); }
00188 };
00189 
00190 // mfh 31 Jul 2012: Specialization for "void" will hopefully fix
00191 // compile errors on Windows, such as the following:
00192 //
00193 // http://testing.sandia.gov/cdash/viewBuildError.php?buildid=611137
00194 //
00195 // I'm imitating the specialization of void* above.
00196 template<>
00197 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<void> {
00198 public:
00199   static std::string name() { return "void"; }
00200   static std::string concreteName(const std::string&) { return name(); }
00201 };
00202 
00203 
00204 #ifdef HAVE_TEUCHOS_COMPLEX
00205 
00206 
00207 template<typename T>
00208 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<std::complex<T> > {
00209 public:
00210   static std::string name()
00211     { return "complex<"+TypeNameTraits<T>::name()+">"; }
00212   static std::string concreteName(const std::complex<T>&)
00213     { return name(); }
00214 };
00215 
00216 
00217 #endif // HAVE_TEUCHOS_COMPLEX
00218 
00219 
00220 
00221 } // namespace Teuchos
00222 
00223 
00224 #endif // _TEUCHOS_TYPE_NAME_TRAITS_HPP_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines