X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FDynamicConverter.h;h=e63bf842666c58ad6d19db25b262cfd736e13a6a;hb=4fd5e900005f59337d3b22f3ec8ef6b182486d81;hp=5da62711a498f796fa7929be00e11944f55344cb;hpb=96a65f544209fab619ab26226aaf39abcc5d1cdf;p=folly.git diff --git a/folly/DynamicConverter.h b/folly/DynamicConverter.h index 5da62711..e63bf842 100644 --- a/folly/DynamicConverter.h +++ b/folly/DynamicConverter.h @@ -18,8 +18,16 @@ #pragma once -#include +#include +#include + +#include +#include + +#include +#include #include +#include namespace folly { template T convertTo(const dynamic&); @@ -39,13 +47,6 @@ namespace folly { * See docs/DynamicConverter.md for supported types and customization */ - -#include -#include -#include -#include -#include - namespace folly { /////////////////////////////////////////////////////////////////////////////// @@ -99,31 +100,35 @@ using is_associative = StrictConjunction, has_key_type>; namespace dynamicconverter_detail { -template +template struct Dereferencer { static inline void derefToCache( - T* /* mem */, const dynamic::const_item_iterator& /* it */) { + Optional* /* mem */, + const dynamic::const_item_iterator& /* it */) { throw TypeError("array", dynamic::Type::OBJECT); } - static inline void derefToCache(T* mem, const dynamic::const_iterator& it) { - new (mem) T(convertTo(*it)); + static inline void derefToCache( + Optional* mem, + const dynamic::const_iterator& it) { + mem->emplace(convertTo(*it)); } }; -template +template struct Dereferencer> { - static inline void - derefToCache(std::pair* mem, const dynamic::const_item_iterator& it) { - new (mem) std::pair( - convertTo(it->first), convertTo(it->second) - ); + static inline void derefToCache( + Optional>* mem, + const dynamic::const_item_iterator& it) { + mem->emplace(convertTo(it->first), convertTo(it->second)); } // Intentional duplication of the code in Dereferencer template - static inline void derefToCache(T* mem, const dynamic::const_iterator& it) { - new (mem) T(convertTo(*it)); + static inline void derefToCache( + Optional* mem, + const dynamic::const_iterator& it) { + mem->emplace(convertTo(*it)); } }; @@ -135,26 +140,22 @@ class Transformer typedef typename T::value_type ttype; - mutable ttype cache_; - mutable bool valid_; + mutable Optional cache_; void increment() { ++this->base_reference(); - valid_ = false; + cache_ = none; } ttype& dereference() const { - if (LIKELY(!valid_)) { - cache_.~ttype(); + if (!cache_) { Dereferencer::derefToCache(&cache_, this->base_reference()); - valid_ = true; } - return cache_; + return cache_.value(); } public: - explicit Transformer(const It& it) - : Transformer::iterator_adaptor_(it), valid_(false) {} + explicit Transformer(const It& it) : Transformer::iterator_adaptor_(it) {} }; // conversion factory @@ -355,7 +356,7 @@ struct DynamicConstructor< }; // pair -template +template struct DynamicConstructor, void> { static dynamic construct(const std::pair& x) { dynamic d = dynamic::array; @@ -373,7 +374,7 @@ T convertTo(const dynamic& d) { return DynamicConverter::type>::convert(d); } -template +template dynamic toDynamic(const T& x) { return DynamicConstructor::type>::construct(x); }