|
51 | 51 | #include <thrift/compiler/ast/t_union.h>
|
52 | 52 | #include <thrift/compiler/whisker/dsl.h>
|
53 | 53 |
|
54 |
| -#include <fmt/core.h> |
55 |
| - |
56 |
| -#include <boost/core/demangle.hpp> |
57 |
| - |
58 | 54 | #include <filesystem>
|
59 | 55 | #include <functional>
|
60 | 56 | #include <map>
|
61 |
| -#include <stdexcept> |
62 | 57 | #include <string>
|
63 | 58 | #include <string_view>
|
64 |
| -#include <typeindex> |
65 |
| -#include <typeinfo> |
66 |
| -#include <unordered_map> |
67 | 59 |
|
68 | 60 | namespace apache::thrift::compiler {
|
69 | 61 |
|
@@ -186,114 +178,7 @@ class t_whisker_generator : public t_generator {
|
186 | 178 | using prototype = whisker::prototype<T>;
|
187 | 179 | template <typename T = void>
|
188 | 180 | using prototype_ptr = whisker::prototype_ptr<T>;
|
189 |
| - |
190 |
| - /** |
191 |
| - * The prototype database stores and caches prototype indexed by typeid. |
192 |
| - * |
193 |
| - * This is primarily used for the `make_prototype_for*` family of functions |
194 |
| - * below. |
195 |
| - */ |
196 |
| - class prototype_database { |
197 |
| - public: |
198 |
| - template <typename T> |
199 |
| - void define(prototype_ptr<T> prototype) { |
200 |
| - auto [_, inserted] = |
201 |
| - prototypes_.emplace(std::type_index(typeid(T)), std::move(prototype)); |
202 |
| - if (!inserted) { |
203 |
| - throw std::runtime_error(fmt::format( |
204 |
| - "Prototype for type '{}' already exists.", |
205 |
| - boost::core::demangle(typeid(T).name()))); |
206 |
| - } |
207 |
| - } |
208 |
| - |
209 |
| - /** |
210 |
| - * Gets the cached prototype for the given type, or throws an exception if |
211 |
| - * the type is unknown. |
212 |
| - * |
213 |
| - * If allow_lazy is true, then a failed lookup falls back to a "lazy" |
214 |
| - * prototype which is resolved when used. This is helpful when there is a |
215 |
| - * cycle of type references. |
216 |
| - */ |
217 |
| - template <typename T> |
218 |
| - prototype_ptr<T> of(bool allow_lazy = true) const { |
219 |
| - auto found = prototypes_.find(std::type_index(typeid(T))); |
220 |
| - if (found == prototypes_.end()) { |
221 |
| - if (allow_lazy) { |
222 |
| - return this->lazy<T>(); |
223 |
| - } |
224 |
| - throw std::runtime_error(fmt::format( |
225 |
| - "Prototype for type '{}' does not exist.", |
226 |
| - boost::core::demangle(typeid(T).name()))); |
227 |
| - } |
228 |
| - auto casted = |
229 |
| - std::dynamic_pointer_cast<const prototype<T>>(found->second); |
230 |
| - if (casted == nullptr) { |
231 |
| - throw std::runtime_error(fmt::format( |
232 |
| - "Prototype for type '{}' is of an unexpected type.", |
233 |
| - typeid(T).name())); |
234 |
| - } |
235 |
| - return casted; |
236 |
| - } |
237 |
| - |
238 |
| - /** |
239 |
| - * Creates a native_handle for the given reference with a prototype stored |
240 |
| - * in this database. |
241 |
| - * |
242 |
| - * std::remove_reference_t<T> forces the caller to explicitly specify the |
243 |
| - * template argument. This is to prevent accidental use of the wrong type. |
244 |
| - */ |
245 |
| - template <typename T> |
246 |
| - whisker::native_handle<T> create( |
247 |
| - whisker::managed_ptr<std::remove_reference_t<T>> o) const { |
248 |
| - return whisker::native_handle<T>(std::move(o), of<T>()); |
249 |
| - } |
250 |
| - template <typename T> |
251 |
| - whisker::native_handle<T> create( |
252 |
| - const std::remove_reference_t<T>& o) const { |
253 |
| - return this->create<T>(whisker::manage_as_static(o)); |
254 |
| - } |
255 |
| - template <typename T> |
256 |
| - whisker::object create_nullable(const std::remove_reference_t<T>* o) const { |
257 |
| - return o == nullptr ? whisker::make::null |
258 |
| - : whisker::object(this->create<T>(*o)); |
259 |
| - } |
260 |
| - |
261 |
| - /** |
262 |
| - * A "lazy" prototype is one whose definition can be deferred until first |
263 |
| - * use. This allows prototypes to refer to each other in cycles that have |
264 |
| - * cyclic references. |
265 |
| - * |
266 |
| - * Note that cyclical prototypes chains are still disallowed. |
267 |
| - */ |
268 |
| - template <typename T> |
269 |
| - prototype_ptr<T> lazy() const { |
270 |
| - class lazy_prototype final : public prototype<T> { |
271 |
| - public: |
272 |
| - explicit lazy_prototype(const prototype_database& db) : db_(db) {} |
273 |
| - |
274 |
| - const prototype<>::descriptor* find_descriptor( |
275 |
| - std::string_view name) const final { |
276 |
| - return this->resolve()->find_descriptor(name); |
277 |
| - } |
278 |
| - std::set<std::string> keys() const final { |
279 |
| - return this->resolve()->keys(); |
280 |
| - } |
281 |
| - const prototype<>::ptr& parent() const final { |
282 |
| - return this->resolve()->parent(); |
283 |
| - } |
284 |
| - |
285 |
| - private: |
286 |
| - prototype_ptr<T> resolve() const { |
287 |
| - return db_.of<T>(false /* allow_lazy */); |
288 |
| - } |
289 |
| - const prototype_database& db_; |
290 |
| - }; |
291 |
| - return std::make_shared<const lazy_prototype>(*this); |
292 |
| - } |
293 |
| - |
294 |
| - private: |
295 |
| - std::unordered_map<std::type_index, whisker::prototype<>::ptr> prototypes_; |
296 |
| - }; |
| 181 | + using prototype_database = whisker::prototype_database; |
297 | 182 |
|
298 | 183 | /**
|
299 | 184 | * Registers the `make_prototype_for_*` functions with the prototype database.
|
|
0 commit comments