Teuchos - Trilinos Tools Package 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 #if defined(__IBMCPP__) && __IBMCPP__ < 900
00053 # define TEUCHOS_TYPE_NAME_TRAITS_OLD_IBM
00054 #endif
00055 
00056 namespace  Teuchos {
00057 
00058 
00066 TEUCHOS_LIB_DLL_EXPORT std::string demangleName( const std::string &mangledName );
00067 
00068 
00073 template<typename T>
00074 class TypeNameTraits {
00075 public:
00077   static std::string name()
00078     {
00079       return demangleName(typeid(T).name());
00080     }
00082 #ifndef TEUCHOS_TYPE_NAME_TRAITS_OLD_IBM
00083   static std::string concreteName( const T& t )
00084 #else
00085   // the IBM compilers on AIX have a problem with const
00086   static std::string concreteName( T t )
00087 #endif
00088     {
00089       return demangleName(typeid(t).name());
00090     }
00091 };
00092 
00093 
00103 template<typename T>
00104 std::string typeName( const T &t )
00105 {
00106   typedef typename ConstTypeTraits<T>::NonConstType ncT;
00107 #ifndef TEUCHOS_TYPE_NAME_TRAITS_OLD_IBM
00108   return TypeNameTraits<ncT>::concreteName(t);
00109 #else
00110   // You can't pass general objects to AIX by value as above.  This means that
00111   // you will not get the concrete name printed on AIX but that is life on
00112   // such compilers.
00113   return TypeNameTraits<ncT>::name();
00114 #endif
00115 }
00116 
00117 
00126 template<typename T>
00127 std::string concreteTypeName( const T &t )
00128 {
00129   typedef typename ConstTypeTraits<T>::NonConstType ncT;
00130   return TypeNameTraits<ncT>::concreteName(t);
00131 }
00132 
00133 
00134 #define TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(TYPE) \
00135 template<> \
00136 class TEUCHOS_LIB_DLL_EXPORT TypeNameTraits<TYPE> { \
00137 public: \
00138   static std::string name() { return (#TYPE); } \
00139   static std::string concreteName(const TYPE&) { return name(); } \
00140 } \
00141 
00142 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(bool);
00143 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(char);
00144 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(int);
00145 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(short int);
00146 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(long int);
00147 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(float);
00148 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(double);
00149 
00150 
00151 template<typename T>
00152 class TEUCHOS_LIB_DLL_EXPORT TypeNameTraits<T*> {
00153 public:
00154   typedef T* T_ptr;
00155   static std::string name() { return TypeNameTraits<T>::name() + "*"; }
00156   static std::string concreteName(T_ptr) { return name(); }
00157 };
00158 
00159 
00160 template<>
00161 class TEUCHOS_LIB_DLL_EXPORT TypeNameTraits<std::string> {
00162 public:
00163   static std::string name() { return "string"; }
00164   static std::string concreteName(const std::string&)
00165     { return name(); }
00166 };
00167 
00168 
00169 template<>
00170 class TEUCHOS_LIB_DLL_EXPORT TypeNameTraits<void*> {
00171 public:
00172   static std::string name() { return "void*"; }
00173   static std::string concreteName(const std::string&) { return name(); }
00174 };
00175 
00176 // mfh 31 Jul 2012: Specialization for "void" will hopefully fix
00177 // compile errors on Windows, such as the following:
00178 //
00179 // http://testing.sandia.gov/cdash/viewBuildError.php?buildid=611137
00180 //
00181 // I'm imitating the specialization of void* above.
00182 template<>
00183 class TEUCHOS_LIB_DLL_EXPORT TypeNameTraits<void> {
00184 public:
00185   static std::string name() { return "void"; }
00186   static std::string concreteName(const std::string&) { return name(); }
00187 };
00188 
00189 
00190 #ifdef HAVE_TEUCHOS_COMPLEX
00191 
00192 
00193 template<typename T>
00194 class TEUCHOS_LIB_DLL_EXPORT TypeNameTraits<std::complex<T> > {
00195 public:
00196   static std::string name()
00197     { return "complex<"+TypeNameTraits<T>::name()+">"; }
00198   static std::string concreteName(const std::complex<T>&)
00199     { return name(); }
00200 };
00201 
00202 
00203 #endif // HAVE_TEUCHOS_COMPLEX
00204 
00205 
00206 
00207 } // namespace Teuchos
00208 
00209 
00210 #endif // _TEUCHOS_TYPE_NAME_TRAITS_HPP_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines