2 * Copyright 2012 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(key_type);
54 BOOST_MPL_HAS_XXX_TRAIT_DEF(mapped_type);
55 BOOST_MPL_HAS_XXX_TRAIT_DEF(iterator);
57 template <typename T> struct map_container_has_correct_types
58 : std::is_same<std::pair<typename std::add_const<typename T::key_type>::type,
59 typename T::mapped_type>,
60 typename T::value_type> {};
62 template <typename T> struct class_is_container {
63 typedef std::reverse_iterator<T*> some_iterator;
64 enum { value = has_value_type<T>::value &&
65 has_iterator<T>::value &&
66 std::is_constructible<T, some_iterator, some_iterator>::value };
69 template <typename T> struct container_is_map
71 has_key_type<T>::value && has_mapped_type<T>::value,
72 map_container_has_correct_types<T>,
76 template <typename T> struct is_container
78 std::is_class<T>::value,
79 class_is_container<T>,
83 template <typename T> struct is_map_container
85 is_container<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 {
109 template <typename F, typename S>
111 derefToCache(std::pair<F, S>* mem, const dynamic::const_item_iterator& it) {
112 new (mem) std::pair<F, S>(convertTo<F>(it->first), convertTo<S>(it->second));
115 template <typename T>
116 inline void derefToCache(T* mem, const dynamic::const_iterator& it) {
117 new (mem) T(convertTo<T>(*it));
120 template <typename T, typename It>
121 class Transformer : public boost::iterator_adaptor<
124 typename T::value_type
126 friend class boost::iterator_core_access;
128 typedef typename T::value_type ttype;
130 mutable ttype cache_;
134 ++this->base_reference();
138 ttype& dereference() const {
139 if (LIKELY(!valid_)) {
141 derefToCache(&cache_, this->base_reference());
148 explicit Transformer(const It& it)
149 : Transformer::iterator_adaptor_(it), valid_(false) {}
152 // conversion factory
153 template <typename T, typename It>
154 static inline std::move_iterator<Transformer<T, It>>
155 conversionIterator(const It& it) {
156 return std::make_move_iterator(Transformer<T, It>(it));
159 } // namespace dynamicconverter_detail
161 ///////////////////////////////////////////////////////////////////////////////
162 // DynamicConverter specializations
164 template <typename T, typename Enable = void> struct DynamicConverter;
167 * Each specialization of DynamicConverter has the function
168 * 'static T convert(const dynamic& d);'
173 struct DynamicConverter<bool> {
174 static bool convert(const dynamic& d) {
180 template <typename T>
181 struct DynamicConverter<T,
182 typename std::enable_if<std::is_integral<T>::value &&
183 !std::is_same<T, bool>::value>::type> {
184 static T convert(const dynamic& d) {
185 return static_cast<T>(d.asInt());
190 template <typename T>
191 struct DynamicConverter<T,
192 typename std::enable_if<std::is_floating_point<T>::value>::type> {
193 static T convert(const dynamic& d) {
194 return static_cast<T>(d.asDouble());
200 struct DynamicConverter<folly::fbstring> {
201 static folly::fbstring convert(const dynamic& d) {
208 struct DynamicConverter<std::string> {
209 static std::string convert(const dynamic& d) {
210 return d.asString().toStdString();
215 template <typename F, typename S>
216 struct DynamicConverter<std::pair<F,S>> {
217 static std::pair<F, S> convert(const dynamic& d) {
218 if (d.isArray() && d.size() == 2) {
219 return std::make_pair(convertTo<F>(d[0]), convertTo<S>(d[1]));
220 } else if (d.isObject() && d.size() == 1) {
221 auto it = d.items().begin();
222 return std::make_pair(convertTo<F>(it->first), convertTo<S>(it->second));
224 throw TypeError("array (size 2) or object (size 1)", d.type());
230 template <typename C>
231 struct DynamicConverter<C,
232 typename std::enable_if<
233 dynamicconverter_detail::is_map_container<C>::value>::type> {
234 static C convert(const dynamic& d) {
235 if (LIKELY(d.isObject())) {
236 return C(dynamicconverter_detail::conversionIterator<C>
238 dynamicconverter_detail::conversionIterator<C>
240 } else if (d.isArray()) {
241 return C(dynamicconverter_detail::conversionIterator<C>(d.begin()),
242 dynamicconverter_detail::conversionIterator<C>(d.end()));
244 throw TypeError("object or array", d.type());
249 // non-map containers
250 template <typename C>
251 struct DynamicConverter<C,
252 typename std::enable_if<
253 dynamicconverter_detail::is_container<C>::value &&
254 !dynamicconverter_detail::is_map_container<C>::value
257 static C convert(const dynamic& d) {
258 if (LIKELY(d.isArray())) {
259 return C(dynamicconverter_detail::conversionIterator<C>(d.begin()),
260 dynamicconverter_detail::conversionIterator<C>(d.end()));
262 throw TypeError("array", d.type());
267 ///////////////////////////////////////////////////////////////////////////////
268 // convertTo implementation
270 template <typename T>
271 T convertTo(const dynamic& d) {
272 return DynamicConverter<typename std::remove_cv<T>::type>::convert(d);
277 #endif // DYNAMIC_CONVERTER_H