2 * Copyright 2013 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // @author Nicholas Ormrod <njormrod@fb.com>
19 #ifndef DYNAMIC_CONVERTER_H
20 #define DYNAMIC_CONVERTER_H
22 #include "folly/dynamic.h"
24 template <typename T> T convertTo(const dynamic&);
25 template <typename T> dynamic toDynamic(const T&);
29 * convertTo returns a well-typed representation of the input dynamic.
33 * dynamic d = { { 1, 2, 3 }, { 4, 5 } }; // a vector of vector of int
34 * auto vvi = convertTo<fbvector<fbvector<int>>>(d);
36 * See docs/DynamicConverter.md for supported types and customization
40 #include <type_traits>
42 #include <boost/iterator/iterator_adaptor.hpp>
43 #include <boost/mpl/has_xxx.hpp>
44 #include "folly/Likely.h"
48 ///////////////////////////////////////////////////////////////////////////////
51 namespace dynamicconverter_detail {
53 BOOST_MPL_HAS_XXX_TRAIT_DEF(value_type);
54 BOOST_MPL_HAS_XXX_TRAIT_DEF(iterator);
55 BOOST_MPL_HAS_XXX_TRAIT_DEF(mapped_type);
57 template <typename T> struct class_is_container {
58 typedef std::reverse_iterator<T*> some_iterator;
59 enum { value = has_value_type<T>::value &&
60 has_iterator<T>::value &&
61 std::is_constructible<T, some_iterator, some_iterator>::value };
64 template <typename T> struct class_is_range {
65 enum { value = has_value_type<T>::value &&
66 has_iterator<T>::value };
70 template <typename T> struct is_container
72 std::is_class<T>::value,
73 class_is_container<T>,
77 template <typename T> struct is_range
79 std::is_class<T>::value,
84 template <typename T> struct is_associative_container
85 : std::integral_constant<
87 is_range<T>::value && has_mapped_type<T>::value
90 } // namespace dynamicconverter_detail
92 ///////////////////////////////////////////////////////////////////////////////
96 * We have iterators that dereference to dynamics, but need iterators
97 * that dereference to typename T.
99 * Implementation details:
100 * 1. We cache the value of the dereference operator. This is necessary
101 * because boost::iterator_adaptor requires *it to return a
103 * 2. For const reasons, we cannot call operator= to refresh the
104 * cache: we must call the destructor then placement new.
107 namespace dynamicconverter_detail {
110 struct Dereferencer {
112 derefToCache(T* mem, const dynamic::const_item_iterator& it) {
113 throw TypeError("array", dynamic::Type::OBJECT);
116 static inline void derefToCache(T* mem, const dynamic::const_iterator& it) {
117 new (mem) T(convertTo<T>(*it));
121 template<typename F, typename S>
122 struct Dereferencer<std::pair<F, S>> {
124 derefToCache(std::pair<F, S>* mem, const dynamic::const_item_iterator& it) {
125 new (mem) std::pair<F, S>(
126 convertTo<F>(it->first), convertTo<S>(it->second)
130 // Intentional duplication of the code in Dereferencer
131 template <typename T>
132 static inline void derefToCache(T* mem, const dynamic::const_iterator& it) {
133 new (mem) T(convertTo<T>(*it));
137 template <typename T, typename It>
138 class Transformer : public boost::iterator_adaptor<
141 typename T::value_type
143 friend class boost::iterator_core_access;
145 typedef typename T::value_type ttype;
147 mutable ttype cache_;
151 ++this->base_reference();
155 ttype& dereference() const {
156 if (LIKELY(!valid_)) {
158 Dereferencer<ttype>::derefToCache(&cache_, this->base_reference());
165 explicit Transformer(const It& it)
166 : Transformer::iterator_adaptor_(it), valid_(false) {}
169 // conversion factory
170 template <typename T, typename It>
171 static inline std::move_iterator<Transformer<T, It>>
172 conversionIterator(const It& it) {
173 return std::make_move_iterator(Transformer<T, It>(it));
176 } // namespace dynamicconverter_detail
178 ///////////////////////////////////////////////////////////////////////////////
179 // DynamicConverter specializations
181 template <typename T, typename Enable = void> struct DynamicConverter;
184 * Each specialization of DynamicConverter has the function
185 * 'static T convert(const dynamic& d);'
190 struct DynamicConverter<bool> {
191 static bool convert(const dynamic& d) {
197 template <typename T>
198 struct DynamicConverter<T,
199 typename std::enable_if<std::is_integral<T>::value &&
200 !std::is_same<T, bool>::value>::type> {
201 static T convert(const dynamic& d) {
202 return static_cast<T>(d.asInt());
207 template <typename T>
208 struct DynamicConverter<T,
209 typename std::enable_if<std::is_floating_point<T>::value>::type> {
210 static T convert(const dynamic& d) {
211 return static_cast<T>(d.asDouble());
217 struct DynamicConverter<folly::fbstring> {
218 static folly::fbstring convert(const dynamic& d) {
225 struct DynamicConverter<std::string> {
226 static std::string convert(const dynamic& d) {
227 return d.asString().toStdString();
232 template <typename F, typename S>
233 struct DynamicConverter<std::pair<F,S>> {
234 static std::pair<F, S> convert(const dynamic& d) {
235 if (d.isArray() && d.size() == 2) {
236 return std::make_pair(convertTo<F>(d[0]), convertTo<S>(d[1]));
237 } else if (d.isObject() && d.size() == 1) {
238 auto it = d.items().begin();
239 return std::make_pair(convertTo<F>(it->first), convertTo<S>(it->second));
241 throw TypeError("array (size 2) or object (size 1)", d.type());
247 template <typename C>
248 struct DynamicConverter<C,
249 typename std::enable_if<
250 dynamicconverter_detail::is_container<C>::value>::type> {
251 static C convert(const dynamic& d) {
253 return C(dynamicconverter_detail::conversionIterator<C>(d.begin()),
254 dynamicconverter_detail::conversionIterator<C>(d.end()));
255 } else if (d.isObject()) {
256 return C(dynamicconverter_detail::conversionIterator<C>
258 dynamicconverter_detail::conversionIterator<C>
261 throw TypeError("object or array", d.type());
267 template <typename C, typename Enable = void>
268 struct DynamicConstructor {
269 static dynamic construct(const C& x) {
275 struct DynamicConstructor<C,
276 typename std::enable_if<
277 dynamicconverter_detail::is_associative_container<C>::value>::type> {
278 static dynamic construct(const C& x) {
279 dynamic d = dynamic::object;
280 for (auto& pair : x) {
281 d.insert(toDynamic(pair.first), toDynamic(pair.second));
288 struct DynamicConstructor<C,
289 typename std::enable_if<
290 !dynamicconverter_detail::is_associative_container<C>::value &&
291 !std::is_constructible<StringPiece, const C&>::value &&
292 dynamicconverter_detail::is_range<C>::value>::type> {
293 static dynamic construct(const C& x) {
295 for (auto& item : x) {
296 d.push_back(toDynamic(item));
302 template<typename A, typename B>
303 struct DynamicConstructor<std::pair<A, B>, void> {
304 static dynamic construct(const std::pair<A, B>& x) {
306 d.push_back(toDynamic(x.first));
307 d.push_back(toDynamic(x.second));
312 ///////////////////////////////////////////////////////////////////////////////
313 // convertTo implementation
315 template <typename T>
316 T convertTo(const dynamic& d) {
317 return DynamicConverter<typename std::remove_cv<T>::type>::convert(d);
321 dynamic toDynamic(const T& x) {
322 return DynamicConstructor<typename std::remove_cv<T>::type>::construct(x);
327 #endif // DYNAMIC_CONVERTER_H