Ilya Biryukov | 657159c | 2017-12-12 11:16:45 +0000 | [diff] [blame] | 1 | //===--- Context.h - Mechanism for passing implicit data --------*- C++-*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Context for storing and retrieving implicit data. Useful for passing implicit |
| 11 | // parameters on a per-request basis. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CONTEXT_H_ |
| 16 | #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CONTEXT_H_ |
| 17 | |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include <memory> |
| 20 | #include <type_traits> |
| 21 | |
| 22 | namespace clang { |
| 23 | namespace clangd { |
| 24 | |
| 25 | /// A key for a value of type \p Type, stored inside a context. Keys are |
| 26 | /// non-movable and non-copyable. See documentation of the Context class for |
| 27 | /// more details and usage examples. |
| 28 | template <class Type> class Key { |
| 29 | public: |
| 30 | static_assert(!std::is_reference<Type>::value, |
| 31 | "Reference arguments to Key<> are not allowed"); |
| 32 | |
| 33 | Key() = default; |
| 34 | |
| 35 | Key(Key const &) = delete; |
| 36 | Key &operator=(Key const &) = delete; |
| 37 | Key(Key &&) = delete; |
| 38 | Key &operator=(Key &&) = delete; |
| 39 | }; |
| 40 | |
| 41 | /// A context is an immutable container for per-request data that must be |
| 42 | /// propagated through layers that don't care about it. An example is a request |
| 43 | /// ID that we may want to use when logging. |
| 44 | /// |
| 45 | /// Conceptually, a context is a heterogeneous map<Key<T>, T>. Each key has |
| 46 | /// an associated value type, which allows the map to be typesafe. |
| 47 | /// |
| 48 | /// You can't add data to an existing context, instead you create a new |
| 49 | /// immutable context derived from it with extra data added. When you retrieve |
| 50 | /// data, the context will walk up the parent chain until the key is found. |
| 51 | /// |
| 52 | /// Contexts should be: |
| 53 | /// - passed by reference when calling synchronous functions |
| 54 | /// - passed by value (move) when calling asynchronous functions. The result |
| 55 | /// callback of async operations will receive the context again. |
| 56 | /// - cloned only when 'forking' an asynchronous computation that we don't wait |
| 57 | /// for. |
| 58 | /// |
| 59 | /// Copy operations for this class are deleted, use an explicit clone() method |
| 60 | /// when you need a copy of the context instead. |
| 61 | /// |
| 62 | /// To derive a child context use derive() function, e.g. |
| 63 | /// Context ChildCtx = ParentCtx.derive(RequestIdKey, 123); |
| 64 | /// |
| 65 | /// To create a new root context, derive() from empty Context. |
| 66 | /// e.g.: |
| 67 | /// Context Ctx = Context::empty().derive(RequestIdKey, 123); |
| 68 | /// |
| 69 | /// Values in the context are indexed by typed keys (instances of Key<T> class). |
| 70 | /// Key<T> serves two purposes: |
| 71 | /// - it provides a lookup key for the context (each instance of a key is |
| 72 | /// unique), |
| 73 | /// - it keeps the type information about the value stored in the context map |
| 74 | /// in the template arguments. |
| 75 | /// This provides a type-safe interface to store and access values of multiple |
| 76 | /// types inside a single context. |
| 77 | /// For example, |
| 78 | /// Key<int> RequestID; |
| 79 | /// Key<int> Version; |
| 80 | /// |
| 81 | /// Context Ctx = Context::empty().derive(RequestID, 10).derive(Version, 3); |
| 82 | /// assert(*Ctx.get(RequestID) == 10); |
| 83 | /// assert(*Ctx.get(Version) == 3); |
| 84 | /// |
| 85 | /// Keys are typically used across multiple functions, so most of the time you |
| 86 | /// would want to make them static class members or global variables. |
| 87 | class Context { |
| 88 | public: |
| 89 | /// Returns an empty context that contains no data. Useful for calling |
| 90 | /// functions that require a context when no explicit context is available. |
| 91 | static Context empty(); |
| 92 | |
| 93 | private: |
| 94 | struct Data; |
| 95 | Context(std::shared_ptr<const Data> DataPtr); |
| 96 | |
| 97 | public: |
Ilya Biryukov | a1d324d | 2017-12-13 13:43:47 +0000 | [diff] [blame^] | 98 | /// Same as Context::empty(), please use Context::empty() instead. |
| 99 | /// Constructor is defined to workaround a bug in MSVC's version of STL. |
| 100 | /// (arguments of std::future<> must be default-construcitble in MSVC). |
| 101 | Context() = default; |
| 102 | |
Ilya Biryukov | 657159c | 2017-12-12 11:16:45 +0000 | [diff] [blame] | 103 | /// Move-only. |
| 104 | Context(Context const &) = delete; |
| 105 | Context &operator=(const Context &) = delete; |
| 106 | |
| 107 | Context(Context &&) = default; |
| 108 | Context &operator=(Context &&) = default; |
| 109 | |
| 110 | /// Get data stored for a typed \p Key. If values are not found |
| 111 | /// \returns Pointer to the data associated with \p Key. If no data is |
| 112 | /// specified for \p Key, return null. |
| 113 | template <class Type> const Type *get(const Key<Type> &Key) const { |
| 114 | for (const Data *DataPtr = this->DataPtr.get(); DataPtr != nullptr; |
| 115 | DataPtr = DataPtr->Parent.get()) { |
| 116 | if (DataPtr->KeyPtr == &Key) |
| 117 | return static_cast<const Type *>(DataPtr->Value->getValuePtr()); |
| 118 | } |
| 119 | return nullptr; |
| 120 | } |
| 121 | |
| 122 | /// A helper to get a reference to a \p Key that must exist in the map. |
| 123 | /// Must not be called for keys that are not in the map. |
| 124 | template <class Type> const Type &getExisting(const Key<Type> &Key) const { |
| 125 | auto Val = get(Key); |
| 126 | assert(Val && "Key does not exist"); |
| 127 | return *Val; |
| 128 | } |
| 129 | |
| 130 | /// Derives a child context |
| 131 | /// It is safe to move or destroy a parent context after calling derive() from |
| 132 | /// it. The child context will continue to have access to the data stored in |
| 133 | /// the parent context. |
| 134 | template <class Type> |
| 135 | Context derive(const Key<Type> &Key, |
| 136 | typename std::decay<Type>::type Value) const & { |
| 137 | return Context(std::make_shared<Data>(Data{ |
| 138 | /*Parent=*/DataPtr, &Key, |
| 139 | llvm::make_unique<TypedAnyStorage<typename std::decay<Type>::type>>( |
| 140 | std::move(Value))})); |
| 141 | } |
| 142 | |
| 143 | template <class Type> |
| 144 | Context |
| 145 | derive(const Key<Type> &Key, |
| 146 | typename std::decay<Type>::type Value) && /* takes ownership */ { |
| 147 | return Context(std::make_shared<Data>(Data{ |
| 148 | /*Parent=*/std::move(DataPtr), &Key, |
| 149 | llvm::make_unique<TypedAnyStorage<typename std::decay<Type>::type>>( |
| 150 | std::move(Value))})); |
| 151 | } |
| 152 | |
| 153 | /// Clone this context object. |
| 154 | Context clone() const; |
| 155 | |
| 156 | private: |
| 157 | class AnyStorage { |
| 158 | public: |
| 159 | virtual ~AnyStorage() = default; |
| 160 | virtual void *getValuePtr() = 0; |
| 161 | }; |
| 162 | |
| 163 | template <class T> class TypedAnyStorage : public Context::AnyStorage { |
| 164 | static_assert(std::is_same<typename std::decay<T>::type, T>::value, |
| 165 | "Argument to TypedAnyStorage must be decayed"); |
| 166 | |
| 167 | public: |
| 168 | TypedAnyStorage(T &&Value) : Value(std::move(Value)) {} |
| 169 | |
| 170 | void *getValuePtr() override { return &Value; } |
| 171 | |
| 172 | private: |
| 173 | T Value; |
| 174 | }; |
| 175 | |
| 176 | struct Data { |
| 177 | // We need to make sure Parent outlives the Value, so the order of members |
| 178 | // is important. We do that to allow classes stored in Context's child |
| 179 | // layers to store references to the data in the parent layers. |
| 180 | std::shared_ptr<const Data> Parent; |
| 181 | const void *KeyPtr; |
| 182 | std::unique_ptr<AnyStorage> Value; |
| 183 | }; |
| 184 | |
| 185 | std::shared_ptr<const Data> DataPtr; |
| 186 | }; // namespace clangd |
| 187 | |
| 188 | } // namespace clangd |
| 189 | } // namespace clang |
| 190 | |
| 191 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_CONTEXT_H_ |