X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FDynamicConverter.h;h=e569f641b04357ede7dad796de156e8c95cf9b8a;hb=00ff5917775f9b58a04f74835585cbb32e306289;hp=198c0ae1bad31e5e8d5931da8173a9478360616e;hpb=ff878dc12e3f7d7234f21b0b4b3251595c031b4c;p=folly.git diff --git a/folly/DynamicConverter.h b/folly/DynamicConverter.h index 198c0ae1..e569f641 100644 --- a/folly/DynamicConverter.h +++ b/folly/DynamicConverter.h @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,7 @@ // @author Nicholas Ormrod -#ifndef DYNAMIC_CONVERTER_H -#define DYNAMIC_CONVERTER_H +#pragma once #include namespace folly { @@ -30,7 +29,9 @@ namespace folly { * * Example: * - * dynamic d = { { 1, 2, 3 }, { 4, 5 } }; // a vector of vector of int + * dynamic d = dynamic::array( + * dynamic::array(1, 2, 3), + * dynamic::array(4, 5)); // a vector of vector of int * auto vvi = convertTo>>(d); * * See docs/DynamicConverter.md for supported types and customization @@ -54,13 +55,20 @@ BOOST_MPL_HAS_XXX_TRAIT_DEF(value_type); BOOST_MPL_HAS_XXX_TRAIT_DEF(iterator); BOOST_MPL_HAS_XXX_TRAIT_DEF(mapped_type); -template struct class_is_container { - typedef std::reverse_iterator some_iterator; +template struct iterator_class_is_container { + typedef std::reverse_iterator some_iterator; enum { value = has_value_type::value && - has_iterator::value && std::is_constructible::value }; }; +template +using class_is_container = typename + std::conditional< + has_iterator::value, + iterator_class_is_container, + std::false_type + >::type; + template struct class_is_range { enum { value = has_value_type::value && has_iterator::value }; @@ -108,8 +116,8 @@ namespace dynamicconverter_detail { template struct Dereferencer { - static inline void - derefToCache(T* mem, const dynamic::const_item_iterator& it) { + static inline void derefToCache( + T* /* mem */, const dynamic::const_item_iterator& /* it */) { throw TypeError("array", dynamic::Type::OBJECT); } @@ -168,7 +176,7 @@ public: // conversion factory template -static inline std::move_iterator> +inline std::move_iterator> conversionIterator(const It& it) { return std::make_move_iterator(Transformer(it)); } @@ -204,6 +212,16 @@ struct DynamicConverter +struct DynamicConverter::value>::type> { + static T convert(const dynamic& d) { + using type = typename std::underlying_type::type; + return static_cast(DynamicConverter::convert(d)); + } +}; + // floating point template struct DynamicConverter { template <> struct DynamicConverter { static std::string convert(const dynamic& d) { - return d.asString().toStdString(); + return d.asString(); } }; @@ -280,14 +298,26 @@ struct DynamicConstructor { } }; +// identity +template +struct DynamicConstructor< + C, + typename std::enable_if::value>::type> { + static dynamic construct(const C& x) { + return x; + } +}; + // maps -template -struct DynamicConstructor +struct DynamicConstructor< + C, typename std::enable_if< - dynamicconverter_detail::is_map::value>::type> { + !std::is_same::value && + dynamicconverter_detail::is_map::value>::type> { static dynamic construct(const C& x) { dynamic d = dynamic::object; - for (auto& pair : x) { + for (const auto& pair : x) { d.insert(toDynamic(pair.first), toDynamic(pair.second)); } return d; @@ -295,15 +325,17 @@ struct DynamicConstructor -struct DynamicConstructor +struct DynamicConstructor< + C, typename std::enable_if< - !dynamicconverter_detail::is_map::value && - !std::is_constructible::value && - dynamicconverter_detail::is_range::value>::type> { + !std::is_same::value && + !dynamicconverter_detail::is_map::value && + !std::is_constructible::value && + dynamicconverter_detail::is_range::value>::type> { static dynamic construct(const C& x) { - dynamic d = {}; - for (auto& item : x) { + dynamic d = dynamic::array; + for (const auto& item : x) { d.push_back(toDynamic(item)); } return d; @@ -314,7 +346,7 @@ struct DynamicConstructor struct DynamicConstructor, void> { static dynamic construct(const std::pair& x) { - dynamic d = {}; + dynamic d = dynamic::array; d.push_back(toDynamic(x.first)); d.push_back(toDynamic(x.second)); return d; @@ -335,5 +367,3 @@ dynamic toDynamic(const T& x) { } } // namespace folly - -#endif // DYNAMIC_CONVERTER_H