/*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#ifndef DYNAMIC_CONVERTER_H
#define DYNAMIC_CONVERTER_H
-#include "folly/dynamic.h"
+#include <folly/dynamic.h>
namespace folly {
template <typename T> T convertTo(const dynamic&);
template <typename T> dynamic toDynamic(const T&);
#include <iterator>
#include <boost/iterator/iterator_adaptor.hpp>
#include <boost/mpl/has_xxx.hpp>
-#include "folly/Likely.h"
+#include <folly/Likely.h>
namespace folly {
std::false_type
>::type {};
-template <typename T> struct is_associative_container
+template <typename T> struct is_map
: std::integral_constant<
bool,
is_range<T>::value && has_mapped_type<T>::value
///////////////////////////////////////////////////////////////////////////////
// DynamicConverter specializations
-template <typename T, typename Enable = void> struct DynamicConverter;
-
/**
* Each specialization of DynamicConverter has the function
- * 'static T convert(const dynamic& d);'
+ * 'static T convert(const dynamic&);'
*/
+// default - intentionally unimplemented
+template <typename T, typename Enable = void> struct DynamicConverter;
+
// boolean
template <>
struct DynamicConverter<bool> {
typename std::enable_if<std::is_integral<T>::value &&
!std::is_same<T, bool>::value>::type> {
static T convert(const dynamic& d) {
- return static_cast<T>(d.asInt());
+ return folly::to<T>(d.asInt());
}
};
struct DynamicConverter<T,
typename std::enable_if<std::is_floating_point<T>::value>::type> {
static T convert(const dynamic& d) {
- return static_cast<T>(d.asDouble());
+ return folly::to<T>(d.asDouble());
}
};
throw TypeError("object or array", d.type());
}
}
-
};
+///////////////////////////////////////////////////////////////////////////////
+// DynamicConstructor specializations
+
+/**
+ * Each specialization of DynamicConstructor has the function
+ * 'static dynamic construct(const C&);'
+ */
+
+// default
template <typename C, typename Enable = void>
struct DynamicConstructor {
static dynamic construct(const C& x) {
}
};
+// maps
template<typename C>
struct DynamicConstructor<C,
typename std::enable_if<
- dynamicconverter_detail::is_associative_container<C>::value>::type> {
+ dynamicconverter_detail::is_map<C>::value>::type> {
static dynamic construct(const C& x) {
dynamic d = dynamic::object;
for (auto& pair : x) {
}
};
+// other ranges
template<typename C>
struct DynamicConstructor<C,
typename std::enable_if<
- !dynamicconverter_detail::is_associative_container<C>::value &&
+ !dynamicconverter_detail::is_map<C>::value &&
!std::is_constructible<StringPiece, const C&>::value &&
dynamicconverter_detail::is_range<C>::value>::type> {
static dynamic construct(const C& x) {
}
};
+// pair
template<typename A, typename B>
struct DynamicConstructor<std::pair<A, B>, void> {
static dynamic construct(const std::pair<A, B>& x) {
};
///////////////////////////////////////////////////////////////////////////////
-// convertTo implementation
+// implementation
template <typename T>
T convertTo(const dynamic& d) {