Home | History | Annotate | Download | only in brillo
      1 // Copyright 2014 The Chromium OS Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef LIBBRILLO_BRILLO_TYPE_NAME_UNDECORATE_H_
      6 #define LIBBRILLO_BRILLO_TYPE_NAME_UNDECORATE_H_
      7 
      8 #include <string>
      9 #include <typeinfo>
     10 
     11 #include <brillo/brillo_export.h>
     12 
     13 #if !defined(USE_RTTI_FOR_TYPE_TAGS) && !defined(__clang__)
     14 // When type information is used with RTTI disabled, we rely on
     15 // __PRETTY_FUNCTION__ macro for type tags. Unfortunately gcc and clang produce
     16 // different signatures for types that have optional template parameters, such
     17 // as std::vector and std::map. The problem arises when inter-operating between
     18 // libraries that are compiled with different compilers.
     19 // Since most of Brillo is compiled with clang, we choose clang here exclusively
     20 // and prevent this code from compiling with GCC to avoid hidden runtime errors.
     21 #error TypeInfo/Any with RTTI disabled is supported on clang compiler only.
     22 #endif
     23 
     24 namespace brillo {
     25 
     26 template<typename T>
     27 const char* GetTypeTag() {
     28 #if defined(USE_RTTI_FOR_TYPE_TAGS) && \
     29     (defined(__cpp_rtti) || defined(__GXX_RTTI))
     30   return typeid(T).name();
     31 #else
     32   // __PRETTY_FUNCTION__ would include the type T signature and therefore each
     33   // instance of brillo::internal_details::GetTypeTag<T>() will have a different
     34   // tag string.
     35   return __PRETTY_FUNCTION__;
     36 #endif
     37 }
     38 
     39 // Explicitly instantiate GetTypeTag<T>() for common types to minimize static
     40 // data segment pollution.
     41 extern template BRILLO_EXPORT const char* GetTypeTag<int8_t>();
     42 extern template BRILLO_EXPORT const char* GetTypeTag<uint8_t>();
     43 extern template BRILLO_EXPORT const char* GetTypeTag<int16_t>();
     44 extern template BRILLO_EXPORT const char* GetTypeTag<uint16_t>();
     45 extern template BRILLO_EXPORT const char* GetTypeTag<int32_t>();
     46 extern template BRILLO_EXPORT const char* GetTypeTag<uint32_t>();
     47 extern template BRILLO_EXPORT const char* GetTypeTag<int64_t>();
     48 extern template BRILLO_EXPORT const char* GetTypeTag<uint64_t>();
     49 extern template BRILLO_EXPORT const char* GetTypeTag<bool>();
     50 extern template BRILLO_EXPORT const char* GetTypeTag<double>();
     51 extern template BRILLO_EXPORT const char* GetTypeTag<std::string>();
     52 
     53 // Use brillo::UndecorateTypeName() to obtain human-readable type from
     54 // the decorated/mangled type name returned by std::type_info::name().
     55 BRILLO_EXPORT std::string UndecorateTypeName(const char* type_name);
     56 
     57 // Returns undecorated type name for the given type tag. This will extract the
     58 // actual type name from the type tag string.
     59 BRILLO_EXPORT std::string GetUndecoratedTypeNameForTag(const char* type_tag);
     60 
     61 // A template helper function that returns the undecorated type name for type T.
     62 template<typename T>
     63 inline std::string GetUndecoratedTypeName() {
     64   return GetUndecoratedTypeNameForTag(GetTypeTag<T>());
     65 }
     66 
     67 }  // namespace brillo
     68 
     69 #endif  // LIBBRILLO_BRILLO_TYPE_NAME_UNDECORATE_H_
     70