Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. //===- Any.h - Generic type erased holder of any type -----------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. ///  This file provides Any, a non-template class modeled in the spirit of
  11. ///  std::any.  The idea is to provide a type-safe replacement for C's void*.
  12. ///  It can hold a value of any copy-constructible copy-assignable type
  13. ///
  14. //===----------------------------------------------------------------------===//
  15.  
  16. #ifndef LLVM_ADT_ANY_H
  17. #define LLVM_ADT_ANY_H
  18.  
  19. #include "llvm/ADT/STLForwardCompat.h"
  20. #include "llvm/Support/Compiler.h"
  21.  
  22. #include <cassert>
  23. #include <memory>
  24. #include <type_traits>
  25.  
  26. namespace llvm {
  27.  
  28. class LLVM_EXTERNAL_VISIBILITY Any {
  29.  
  30.   // The `Typeid<T>::Id` static data member below is a globally unique
  31.   // identifier for the type `T`. It is explicitly marked with default
  32.   // visibility so that when `-fvisibility=hidden` is used, the loader still
  33.   // merges duplicate definitions across DSO boundaries.
  34.   // We also cannot mark it as `const`, otherwise msvc merges all definitions
  35.   // when lto is enabled, making any comparison return true.
  36.   template <typename T> struct TypeId { static char Id; };
  37.  
  38.   struct StorageBase {
  39.     virtual ~StorageBase() = default;
  40.     virtual std::unique_ptr<StorageBase> clone() const = 0;
  41.     virtual const void *id() const = 0;
  42.   };
  43.  
  44.   template <typename T> struct StorageImpl : public StorageBase {
  45.     explicit StorageImpl(const T &Value) : Value(Value) {}
  46.  
  47.     explicit StorageImpl(T &&Value) : Value(std::move(Value)) {}
  48.  
  49.     std::unique_ptr<StorageBase> clone() const override {
  50.       return std::make_unique<StorageImpl<T>>(Value);
  51.     }
  52.  
  53.     const void *id() const override { return &TypeId<T>::Id; }
  54.  
  55.     T Value;
  56.  
  57.   private:
  58.     StorageImpl &operator=(const StorageImpl &Other) = delete;
  59.     StorageImpl(const StorageImpl &Other) = delete;
  60.   };
  61.  
  62. public:
  63.   Any() = default;
  64.  
  65.   Any(const Any &Other)
  66.       : Storage(Other.Storage ? Other.Storage->clone() : nullptr) {}
  67.  
  68.   // When T is Any or T is not copy-constructible we need to explicitly disable
  69.   // the forwarding constructor so that the copy constructor gets selected
  70.   // instead.
  71.   template <typename T,
  72.             std::enable_if_t<
  73.                 std::conjunction<
  74.                     std::negation<std::is_same<std::decay_t<T>, Any>>,
  75.                     // We also disable this overload when an `Any` object can be
  76.                     // converted to the parameter type because in that case,
  77.                     // this constructor may combine with that conversion during
  78.                     // overload resolution for determining copy
  79.                     // constructibility, and then when we try to determine copy
  80.                     // constructibility below we may infinitely recurse. This is
  81.                     // being evaluated by the standards committee as a potential
  82.                     // DR in `std::any` as well, but we're going ahead and
  83.                     // adopting it to work-around usage of `Any` with types that
  84.                     // need to be implicitly convertible from an `Any`.
  85.                     std::negation<std::is_convertible<Any, std::decay_t<T>>>,
  86.                     std::is_copy_constructible<std::decay_t<T>>>::value,
  87.                 int> = 0>
  88.   Any(T &&Value) {
  89.     Storage =
  90.         std::make_unique<StorageImpl<std::decay_t<T>>>(std::forward<T>(Value));
  91.   }
  92.  
  93.   Any(Any &&Other) : Storage(std::move(Other.Storage)) {}
  94.  
  95.   Any &swap(Any &Other) {
  96.     std::swap(Storage, Other.Storage);
  97.     return *this;
  98.   }
  99.  
  100.   Any &operator=(Any Other) {
  101.     Storage = std::move(Other.Storage);
  102.     return *this;
  103.   }
  104.  
  105.   bool has_value() const { return !!Storage; }
  106.  
  107.   void reset() { Storage.reset(); }
  108.  
  109. private:
  110.   // Only used for the internal llvm::Any implementation
  111.   template <typename T> bool isa() const {
  112.     if (!Storage)
  113.       return false;
  114.     return Storage->id() == &Any::TypeId<remove_cvref_t<T>>::Id;
  115.   }
  116.  
  117.   template <class T> friend T any_cast(const Any &Value);
  118.   template <class T> friend T any_cast(Any &Value);
  119.   template <class T> friend T any_cast(Any &&Value);
  120.   template <class T> friend const T *any_cast(const Any *Value);
  121.   template <class T> friend T *any_cast(Any *Value);
  122.   template <typename T> friend bool any_isa(const Any &Value);
  123.  
  124.   std::unique_ptr<StorageBase> Storage;
  125. };
  126.  
  127. template <typename T> char Any::TypeId<T>::Id = 0;
  128.  
  129. template <typename T>
  130. LLVM_DEPRECATED("Use any_cast(Any*) != nullptr instead", "any_cast")
  131. bool any_isa(const Any &Value) {
  132.   return Value.isa<T>();
  133. }
  134.  
  135. template <class T> T any_cast(const Any &Value) {
  136.   assert(Value.isa<T>() && "Bad any cast!");
  137.   return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
  138. }
  139.  
  140. template <class T> T any_cast(Any &Value) {
  141.   assert(Value.isa<T>() && "Bad any cast!");
  142.   return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
  143. }
  144.  
  145. template <class T> T any_cast(Any &&Value) {
  146.   assert(Value.isa<T>() && "Bad any cast!");
  147.   return static_cast<T>(std::move(*any_cast<remove_cvref_t<T>>(&Value)));
  148. }
  149.  
  150. template <class T> const T *any_cast(const Any *Value) {
  151.   using U = remove_cvref_t<T>;
  152.   if (!Value || !Value->isa<U>())
  153.     return nullptr;
  154.   return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
  155. }
  156.  
  157. template <class T> T *any_cast(Any *Value) {
  158.   using U = std::decay_t<T>;
  159.   if (!Value || !Value->isa<U>())
  160.     return nullptr;
  161.   return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
  162. }
  163.  
  164. } // end namespace llvm
  165.  
  166. #endif // LLVM_ADT_ANY_H
  167.