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&);
28 * convertTo returns a well-typed representation of the input dynamic.
32 * dynamic d = { { 1, 2, 3 }, { 4, 5 } }; // a vector of vector of int
33 * auto vvi = convertTo<fbvector<fbvector<int>>>(d);
35 * See docs/DynamicConverter.md for supported types and customization
39 #include <type_traits>
41 #include <boost/iterator/iterator_adaptor.hpp>
42 #include <boost/mpl/has_xxx.hpp>
43 #include "folly/Likely.h"
47 ///////////////////////////////////////////////////////////////////////////////
50 namespace dynamicconverter_detail {
52 BOOST_MPL_HAS_XXX_TRAIT_DEF(value_type);
53 BOOST_MPL_HAS_XXX_TRAIT_DEF(iterator);
55 template <typename T> struct class_is_container {
56 typedef std::reverse_iterator<T*> some_iterator;
57 enum { value = has_value_type<T>::value &&
58 has_iterator<T>::value &&
59 std::is_constructible<T, some_iterator, some_iterator>::value };
62 template <typename T> struct is_container
64 std::is_class<T>::value,
65 class_is_container<T>,
69 } // namespace dynamicconverter_detail
71 ///////////////////////////////////////////////////////////////////////////////
75 * We have iterators that dereference to dynamics, but need iterators
76 * that dereference to typename T.
78 * Implementation details:
79 * 1. We cache the value of the dereference operator. This is necessary
80 * because boost::iterator_adaptor requires *it to return a
82 * 2. For const reasons, we cannot call operator= to refresh the
83 * cache: we must call the destructor then placement new.
86 namespace dynamicconverter_detail {
91 derefToCache(T* mem, const dynamic::const_item_iterator& it) {
92 throw TypeError("array", dynamic::Type::OBJECT);
95 static inline void derefToCache(T* mem, const dynamic::const_iterator& it) {
96 new (mem) T(convertTo<T>(*it));
100 template<typename F, typename S>
101 struct Dereferencer<std::pair<F, S>> {
103 derefToCache(std::pair<F, S>* mem, const dynamic::const_item_iterator& it) {
104 new (mem) std::pair<F, S>(
105 convertTo<F>(it->first), convertTo<S>(it->second)
109 // Intentional duplication of the code in Dereferencer
110 template <typename T>
111 static inline void derefToCache(T* mem, const dynamic::const_iterator& it) {
112 new (mem) T(convertTo<T>(*it));
116 template <typename T, typename It>
117 class Transformer : public boost::iterator_adaptor<
120 typename T::value_type
122 friend class boost::iterator_core_access;
124 typedef typename T::value_type ttype;
126 mutable ttype cache_;
130 ++this->base_reference();
134 ttype& dereference() const {
135 if (LIKELY(!valid_)) {
137 Dereferencer<ttype>::derefToCache(&cache_, this->base_reference());
144 explicit Transformer(const It& it)
145 : Transformer::iterator_adaptor_(it), valid_(false) {}
148 // conversion factory
149 template <typename T, typename It>
150 static inline std::move_iterator<Transformer<T, It>>
151 conversionIterator(const It& it) {
152 return std::make_move_iterator(Transformer<T, It>(it));
155 } // namespace dynamicconverter_detail
157 ///////////////////////////////////////////////////////////////////////////////
158 // DynamicConverter specializations
160 template <typename T, typename Enable = void> struct DynamicConverter;
163 * Each specialization of DynamicConverter has the function
164 * 'static T convert(const dynamic& d);'
169 struct DynamicConverter<bool> {
170 static bool convert(const dynamic& d) {
176 template <typename T>
177 struct DynamicConverter<T,
178 typename std::enable_if<std::is_integral<T>::value &&
179 !std::is_same<T, bool>::value>::type> {
180 static T convert(const dynamic& d) {
181 return static_cast<T>(d.asInt());
186 template <typename T>
187 struct DynamicConverter<T,
188 typename std::enable_if<std::is_floating_point<T>::value>::type> {
189 static T convert(const dynamic& d) {
190 return static_cast<T>(d.asDouble());
196 struct DynamicConverter<folly::fbstring> {
197 static folly::fbstring convert(const dynamic& d) {
204 struct DynamicConverter<std::string> {
205 static std::string convert(const dynamic& d) {
206 return d.asString().toStdString();
211 template <typename F, typename S>
212 struct DynamicConverter<std::pair<F,S>> {
213 static std::pair<F, S> convert(const dynamic& d) {
214 if (d.isArray() && d.size() == 2) {
215 return std::make_pair(convertTo<F>(d[0]), convertTo<S>(d[1]));
216 } else if (d.isObject() && d.size() == 1) {
217 auto it = d.items().begin();
218 return std::make_pair(convertTo<F>(it->first), convertTo<S>(it->second));
220 throw TypeError("array (size 2) or object (size 1)", d.type());
226 template <typename C>
227 struct DynamicConverter<C,
228 typename std::enable_if<
229 dynamicconverter_detail::is_container<C>::value>::type> {
230 static C convert(const dynamic& d) {
232 return C(dynamicconverter_detail::conversionIterator<C>(d.begin()),
233 dynamicconverter_detail::conversionIterator<C>(d.end()));
234 } else if (d.isObject()) {
235 return C(dynamicconverter_detail::conversionIterator<C>
237 dynamicconverter_detail::conversionIterator<C>
240 throw TypeError("object or array", d.type());
245 ///////////////////////////////////////////////////////////////////////////////
246 // convertTo implementation
248 template <typename T>
249 T convertTo(const dynamic& d) {
250 return DynamicConverter<typename std::remove_cv<T>::type>::convert(d);
255 #endif // DYNAMIC_CONVERTER_H