any.h 7.1 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
16 17 18 19 20 21
#include <algorithm>
#include <cstring>
#include <type_traits>
#include <typeinfo>
#include <utility>

Y
Yan Chunwei 已提交
22 23 24 25 26 27 28
#include "lite/utils/cp_logging.h"

namespace paddle {
namespace lite {

class Any {
 public:
29 30 31
  inline Any() = default;
  inline explicit Any(Any&& other);
  inline explicit Any(const Any& other);
32

Y
Yan Chunwei 已提交
33
  template <typename T>
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  void set();

  template <typename T>
  void set(T&& other);

  template <typename T>
  const T& get() const;

  template <typename T>
  T* get_mutable();

  template <typename T>
  inline explicit Any(T&& other);

  inline ~Any();

  inline Any& operator=(Any&& other);
  inline Any& operator=(const Any& other);

  template <typename T>
  inline Any& operator=(T&& other);

  inline bool empty() const;
  inline bool valid() const;
  inline void clear();
  inline void swap(Any& other);
  inline const std::type_info& type() const;

  template <typename T, typename... Args>
  inline void construct(Args&&... args);

 private:
  template <typename T>
  class TypeOnHeap;

  template <typename T>
  class TypeOnStack;

  template <typename T>
  class TypeInfo;

  static const size_t kStack = sizeof(void*) * 3;
  static const size_t kAlign = sizeof(void*);

  union Data {
    std::aligned_storage<kStack, kAlign>::type stack;
    void* pheap;
  };

  struct Type {
    void (*destroy)(Data* data);
    void (*create_from_data)(Data* dst, const Data& src);
    const std::type_info* ptype_info;
  };
Y
Yan Chunwei 已提交
88 89

  template <typename T>
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  struct data_on_stack {
    static const bool value = ((alignof(T) <= kAlign) && (sizeof(T) <= kStack));
  };

  inline void construct(Any&& other);
  inline void construct(const Any& other);

  template <typename T>
  inline void check_type() const;

  template <typename T>
  inline void check_type_by_name() const;

  const Type* type_{nullptr};
  Data data_;
};

template <typename T>
inline Any::Any(T&& other) {
  typedef typename std::decay<T>::type DT;
  if (std::is_same<DT, Any>::value) {
    this->construct(std::forward<T>(other));
  } else {
    static_assert(std::is_copy_constructible<DT>::value,
                  "Any can only hold value that is copy constructable");
    type_ = TypeInfo<DT>::get_type();
    if (data_on_stack<DT>::value) {
#pragma GCC diagnostic push
#if 6 <= __GNUC__
#pragma GCC diagnostic ignored "-Wplacement-new"
#endif
      new (&(data_.stack)) DT(std::forward<T>(other));
#pragma GCC diagnostic pop
Y
Yan Chunwei 已提交
123
    } else {
124
      data_.pheap = new DT(std::forward<T>(other));
Y
Yan Chunwei 已提交
125 126
    }
  }
127
}
Y
Yan Chunwei 已提交
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142
inline Any::Any(Any&& other) { this->construct(std::move(other)); }

inline Any::Any(const Any& other) { this->construct(other); }

inline void Any::construct(Any&& other) {
  type_ = other.type_;
  data_ = other.data_;
  other.type_ = nullptr;
}

inline void Any::construct(const Any& other) {
  type_ = other.type_;
  if (type_ != nullptr) {
    type_->create_from_data(&data_, other.data_);
Y
Yan Chunwei 已提交
143
  }
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
}

template <typename T, typename... Args>
inline void Any::construct(Args&&... args) {
  clear();
  typedef typename std::decay<T>::type DT;
  type_ = TypeInfo<DT>::get_type();
  if (data_on_stack<DT>::value) {
#pragma GCC diagnostic push
#if 6 <= __GNUC__
#pragma GCC diagnostic ignored "-Wplacement-new"
#endif
    new (&(data_.stack)) DT(std::forward<Args>(args)...);
#pragma GCC diagnostic pop
  } else {
    data_.pheap = new DT(std::forward<Args>(args)...);
Y
Yan Chunwei 已提交
160
  }
161 162 163 164 165 166 167 168 169 170 171
}

template <typename T>
void Any::set() {
  this->construct<T>();
}

template <typename T>
void Any::set(T&& other) {
  this->construct<T>(std::forward<T>(other));
}
Y
Yan Chunwei 已提交
172

173
inline Any::~Any() { this->clear(); }
Y
Yan Chunwei 已提交
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
inline Any& Any::operator=(Any&& other) {
  Any(std::move(other)).swap(*this);
  return *this;
}

inline Any& Any::operator=(const Any& other) {
  Any(other).swap(*this);
  return *this;
}

template <typename T>
inline Any& Any::operator=(T&& other) {
  Any(std::forward<T>(other)).swap(*this);
  return *this;
}

inline void Any::swap(Any& other) {
  std::swap(type_, other.type_);
  std::swap(data_, other.data_);
}

inline void Any::clear() {
  if (type_ != nullptr) {
    if (type_->destroy != nullptr) {
      type_->destroy(&data_);
200
    }
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    type_ = nullptr;
  }
}

inline bool Any::empty() const { return type_ == nullptr; }

inline bool Any::valid() const { return empty() == false; }

inline const std::type_info& Any::type() const {
  if (type_ != nullptr) {
    return *(type_->ptype_info);
  } else {
    return typeid(void);
  }
}

template <typename T>
inline void Any::check_type() const {
  CHECK_EQ((type_ == nullptr), false);
  CHECK_EQ((*(type_->ptype_info) == typeid(T)), true);
}

template <typename T>
inline void Any::check_type_by_name() const {
  CHECK_EQ((type_ == nullptr), false);
  CHECK_EQ(strcmp(type_->ptype_info->name(), typeid(T).name()), 0);
}

template <typename T>
inline const T& Any::get() const {
  this->check_type<T>();
  return *Any::TypeInfo<T>::get_ptr(&(this->data_));
}

template <typename T>
T* Any::get_mutable() {
  return Any::TypeInfo<T>::get_ptr(&(this->data_));
}

template <typename T>
class Any::TypeOnHeap {
 public:
  inline static T* get_ptr(Any::Data* data) {
    return static_cast<T*>(data->pheap);
  }
  inline static const T* get_ptr(const Any::Data* data) {
    return static_cast<const T*>(data->pheap);
  }
  inline static void create_from_data(Any::Data* dst, const Any::Data& data) {
    dst->pheap = new T(*get_ptr(&data));
  }
  inline static void destroy(Data* data) {
    delete static_cast<T*>(data->pheap);
  }
};

template <typename T>
class Any::TypeOnStack {
 public:
  inline static T* get_ptr(Any::Data* data) {
    return reinterpret_cast<T*>(&(data->stack));
  }
  inline static const T* get_ptr(const Any::Data* data) {
    return reinterpret_cast<const T*>(&(data->stack));
  }
  inline static void create_from_data(Any::Data* dst, const Any::Data& data) {
    new (&(dst->stack)) T(*get_ptr(&data));
  }
  inline static void destroy(Data* data) {
    T* dptr = reinterpret_cast<T*>(&(data->stack));
    dptr->~T();
  }
};

template <typename T>
class Any::TypeInfo : public std::conditional<Any::data_on_stack<T>::value,
                                              Any::TypeOnStack<T>,
                                              Any::TypeOnHeap<T>>::type {
 public:
  inline static const Type* get_type() {
    static TypeInfo<T> tp;
    return &(tp.type_);
283
  }
Y
Yan Chunwei 已提交
284 285

 private:
286 287 288 289 290 291 292 293 294 295
  Type type_;
  TypeInfo() {
    if (std::is_pod<T>::value && data_on_stack<T>::value) {
      type_.destroy = nullptr;
    } else {
      type_.destroy = TypeInfo<T>::destroy;
    }
    type_.create_from_data = TypeInfo<T>::create_from_data;
    type_.ptype_info = &typeid(T);
  }
Y
Yan Chunwei 已提交
296 297 298 299
};

}  // namespace lite
}  // namespace paddle