Home | History | Annotate | Download | only in framework
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #ifndef TENSORFLOW_FRAMEWORK_TYPE_INDEX_H_
     17 #define TENSORFLOW_FRAMEWORK_TYPE_INDEX_H_
     18 
     19 #include <string>
     20 #if defined(__GXX_RTTI) || defined(_CPPRTTI)
     21 #include <typeindex>
     22 #include <typeinfo>
     23 #endif  // __GXX_RTTI
     24 
     25 #include "tensorflow/core/platform/types.h"
     26 
     27 namespace tensorflow {
     28 
     29 // On some platforms, we would like to avoid using RTTI in order to have smaller
     30 // binary sizes. The following #ifdef section provides a non-RTTI
     31 // replacement for std::type_index (with a minimal set of functions needed by
     32 // the TensorFlow framework, and more can be added if necessary).
     33 #if !defined(__GXX_RTTI) && !defined(_CPPRTTI)
     34 
     35 // A thin TypeIndex class that mimics std::type_index but does not use RTTI. As
     36 // a result, it does not provide the actual name of the type, and only returns a
     37 // pre-baked string specifying that RTTI is disabled.
     38 // The hash code provided in this class is unique for each class. However, it is
     39 // generated at runtime so this hash code should not be serialized - the value
     40 // for the same type can change from run to run.
     41 class TypeIndex {
     42  public:
     43   TypeIndex(const TypeIndex& src) : hash_(src.hash_) {}
     44   TypeIndex& operator=(const TypeIndex& src) {
     45     hash_ = src.hash_;
     46     return *this;
     47   }
     48   bool operator==(const TypeIndex& rhs) const { return (hash_ == rhs.hash_); }
     49   bool operator!=(const TypeIndex& rhs) const { return (hash_ != rhs.hash_); }
     50   ~TypeIndex() {}
     51 
     52   const char* name() const { return "[RTTI disabled for Android]"; }
     53   uint64 hash_code() const { return hash_; }
     54 
     55   // Returns a TypeIndex object that corresponds to a typename.
     56   template <typename T>
     57   static TypeIndex Make() {
     58     static bool hash_bit[1];
     59     return TypeIndex(static_cast<uint64>(reinterpret_cast<intptr_t>(hash_bit)));
     60   }
     61 
     62  private:
     63   // We hide the constructor of the TypeIndex class. Use the templated
     64   // Make<T>() function to create a TypeIndex object.
     65   TypeIndex(const uint64 hash) : hash_(hash) {}
     66   uint64 hash_;
     67 };
     68 
     69 template <typename T>
     70 inline TypeIndex MakeTypeIndex() {
     71   return TypeIndex::Make<T>();
     72 }
     73 
     74 #else  // __GXX_RTTI
     75 
     76 // In the presence of RTTI, we will simply delegate to std::type_index for
     77 // runtime type inference.
     78 typedef std::type_index TypeIndex;
     79 template <typename T>
     80 inline TypeIndex MakeTypeIndex() {
     81   return TypeIndex(typeid(T));
     82 }
     83 
     84 #endif  // __GXX_RTTI
     85 }  // namespace tensorflow
     86 
     87 #endif  // TENSORFLOW_FRAMEWORK_TYPE_INDEX_H_
     88