/*
- * Copyright 2016 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.
do { \
switch ((type)) { \
case NULLT: \
- apply(void*); \
+ apply(std::nullptr_t); \
break; \
case ARRAY: \
apply(Array); \
explicit TypeError(const std::string& expected, dynamic::Type actual);
explicit TypeError(const std::string& expected,
dynamic::Type actual1, dynamic::Type actual2);
- ~TypeError();
+ ~TypeError() override;
};
//////////////////////////////////////////////////////////////////////
+struct dynamic::item_iterator : boost::iterator_adaptor<
+ dynamic::item_iterator,
+ dynamic::ObjectImpl::iterator> {
+ /* implicit */ item_iterator(base_type b) : iterator_adaptor_(b) {}
+
+ using object_type = dynamic::ObjectImpl;
+
+ private:
+ friend class boost::iterator_core_access;
+};
+
+struct dynamic::value_iterator : boost::iterator_adaptor<
+ dynamic::value_iterator,
+ dynamic::ObjectImpl::iterator,
+ dynamic> {
+ /* implicit */ value_iterator(base_type b) : iterator_adaptor_(b) {}
+
+ using object_type = dynamic::ObjectImpl;
+
+ private:
+ dynamic& dereference() const {
+ return base_reference()->second;
+ }
+ friend class boost::iterator_core_access;
+};
+
struct dynamic::const_item_iterator
: boost::iterator_adaptor<dynamic::const_item_iterator,
dynamic::ObjectImpl::const_iterator> {
/* implicit */ const_item_iterator(base_type b) : iterator_adaptor_(b) { }
+ /* implicit */ const_item_iterator(item_iterator i)
+ : iterator_adaptor_(i.base()) {}
+ /* implicit */ const_item_iterator(dynamic::ObjectImpl::iterator i)
+ : iterator_adaptor_(i) {}
+
+ using object_type = dynamic::ObjectImpl const;
private:
friend class boost::iterator_core_access;
dynamic const> {
/* implicit */ const_key_iterator(base_type b) : iterator_adaptor_(b) { }
+ using object_type = dynamic::ObjectImpl const;
+
private:
dynamic const& dereference() const {
return base_reference()->first;
dynamic::ObjectImpl::const_iterator,
dynamic const> {
/* implicit */ const_value_iterator(base_type b) : iterator_adaptor_(b) { }
+ /* implicit */ const_value_iterator(value_iterator i)
+ : iterator_adaptor_(i.base()) {}
+ /* implicit */ const_value_iterator(dynamic::ObjectImpl::iterator i)
+ : iterator_adaptor_(i) {}
+
+ using object_type = dynamic::ObjectImpl const;
private:
dynamic const& dereference() const {
template <class T>
struct dynamic::NumericTypeHelper<
T, typename std::enable_if<std::is_integral<T>::value>::type> {
+ static_assert(
+ !kIsObjC || sizeof(T) > sizeof(char),
+ "char-sized types are ambiguous in objc; cast to bool or wider type");
using type = int64_t;
};
template <>
return get<Array>().end();
}
+inline dynamic::iterator dynamic::begin() {
+ return get<Array>().begin();
+}
+inline dynamic::iterator dynamic::end() {
+ return get<Array>().end();
+}
+
template <class It>
struct dynamic::IterableProxy {
- typedef It const_iterator;
+ typedef It iterator;
typedef typename It::value_type value_type;
+ typedef typename It::object_type object_type;
- /* implicit */ IterableProxy(const dynamic::ObjectImpl* o) : o_(o) { }
+ /* implicit */ IterableProxy(object_type* o) : o_(o) {}
It begin() const {
return o_->begin();
}
private:
- const dynamic::ObjectImpl* o_;
+ object_type* o_;
};
inline dynamic::IterableProxy<dynamic::const_key_iterator> dynamic::keys()
return &(get<ObjectImpl>());
}
+inline dynamic::IterableProxy<dynamic::value_iterator> dynamic::values() {
+ return &(get<ObjectImpl>());
+}
+
+inline dynamic::IterableProxy<dynamic::item_iterator> dynamic::items() {
+ return &(get<ObjectImpl>());
+}
+
inline bool dynamic::isString() const {
- return get_nothrow<std::string>();
+ return get_nothrow<std::string>() != nullptr;
+}
+inline bool dynamic::isObject() const {
+ return get_nothrow<ObjectImpl>() != nullptr;
+}
+inline bool dynamic::isBool() const {
+ return get_nothrow<bool>() != nullptr;
+}
+inline bool dynamic::isArray() const {
+ return get_nothrow<Array>() != nullptr;
+}
+inline bool dynamic::isDouble() const {
+ return get_nothrow<double>() != nullptr;
+}
+inline bool dynamic::isInt() const {
+ return get_nothrow<int64_t>() != nullptr;
+}
+inline bool dynamic::isNull() const {
+ return get_nothrow<std::nullptr_t>() != nullptr;
+}
+inline bool dynamic::isNumber() const {
+ return isInt() || isDouble();
}
-inline bool dynamic::isObject() const { return get_nothrow<ObjectImpl>(); }
-inline bool dynamic::isBool() const { return get_nothrow<bool>(); }
-inline bool dynamic::isArray() const { return get_nothrow<Array>(); }
-inline bool dynamic::isDouble() const { return get_nothrow<double>(); }
-inline bool dynamic::isInt() const { return get_nothrow<int64_t>(); }
-inline bool dynamic::isNull() const { return get_nothrow<void*>(); }
-inline bool dynamic::isNumber() const { return isInt() || isDouble(); }
inline dynamic::Type dynamic::type() const {
return type_;
return false;
}
};
+template<>
+struct dynamic::CompareOp<std::nullptr_t> {
+ static bool comp(std::nullptr_t const&, std::nullptr_t const&) {
+ return true;
+ }
+};
inline dynamic& dynamic::operator+=(dynamic const& o) {
if (type() == STRING && o.type() == STRING) {
inline dynamic::const_item_iterator dynamic::find(dynamic const& key) const {
return get<ObjectImpl>().find(key);
}
+inline dynamic::item_iterator dynamic::find(dynamic const& key) {
+ return get<ObjectImpl>().find(key);
+}
template<class K, class V> inline void dynamic::insert(K&& key, V&& val) {
auto& obj = get<ObjectImpl>();
return obj.erase(key);
}
-inline dynamic::const_iterator dynamic::erase(const_iterator it) {
+inline dynamic::iterator dynamic::erase(const_iterator it) {
auto& arr = get<Array>();
// std::vector doesn't have an erase method that works on const iterators,
// even though the standard says it should, so this hack converts to a
return const_key_iterator(get<ObjectImpl>().erase(it.base()));
}
-inline dynamic::const_key_iterator dynamic::erase(const_key_iterator first,
- const_key_iterator last) {
+inline dynamic::const_key_iterator dynamic::erase(
+ const_key_iterator first,
+ const_key_iterator last) {
return const_key_iterator(get<ObjectImpl>().erase(first.base(),
last.base()));
}
-inline dynamic::const_value_iterator dynamic::erase(const_value_iterator it) {
- return const_value_iterator(get<ObjectImpl>().erase(it.base()));
+inline dynamic::value_iterator dynamic::erase(const_value_iterator it) {
+ return value_iterator(get<ObjectImpl>().erase(it.base()));
}
-inline dynamic::const_value_iterator dynamic::erase(const_value_iterator first,
- const_value_iterator last) {
- return const_value_iterator(get<ObjectImpl>().erase(first.base(),
- last.base()));
+inline dynamic::value_iterator dynamic::erase(
+ const_value_iterator first,
+ const_value_iterator last) {
+ return value_iterator(get<ObjectImpl>().erase(first.base(), last.base()));
}
-inline dynamic::const_item_iterator dynamic::erase(const_item_iterator it) {
- return const_item_iterator(get<ObjectImpl>().erase(it.base()));
+inline dynamic::item_iterator dynamic::erase(const_item_iterator it) {
+ return item_iterator(get<ObjectImpl>().erase(it.base()));
}
-inline dynamic::const_item_iterator dynamic::erase(const_item_iterator first,
- const_item_iterator last) {
- return const_item_iterator(get<ObjectImpl>().erase(first.base(),
- last.base()));
+inline dynamic::item_iterator dynamic::erase(
+ const_item_iterator first,
+ const_item_iterator last) {
+ return item_iterator(get<ObjectImpl>().erase(first.base(), last.base()));
}
inline void dynamic::resize(std::size_t sz, dynamic const& c) {
}; \
//
-FOLLY_DYNAMIC_DEC_TYPEINFO(void*, "null", dynamic::NULLT)
+FOLLY_DYNAMIC_DEC_TYPEINFO(std::nullptr_t, "null", dynamic::NULLT)
FOLLY_DYNAMIC_DEC_TYPEINFO(bool, "boolean", dynamic::BOOL)
FOLLY_DYNAMIC_DEC_TYPEINFO(std::string, "string", dynamic::STRING)
FOLLY_DYNAMIC_DEC_TYPEINFO(dynamic::Array, "array", dynamic::ARRAY)
}
template<class T> struct dynamic::GetAddrImpl {};
-template<> struct dynamic::GetAddrImpl<void*> {
- static void** get(Data& d) noexcept { return &d.nul; }
+template<> struct dynamic::GetAddrImpl<std::nullptr_t> {
+ static std::nullptr_t* get(Data& d) noexcept { return &d.nul; }
};
template<> struct dynamic::GetAddrImpl<dynamic::Array> {
static Array* get(Data& d) noexcept { return &d.array; }
};
// Otherwise, null, being (void*)0, would print as 0.
template <>
-struct dynamic::PrintImpl<void*> {
+struct dynamic::PrintImpl<std::nullptr_t> {
static void print(dynamic const& /* d */,
std::ostream& out,
- void* const& nul) {
- DCHECK_EQ((void*)0, nul);
+ std::nullptr_t const&) {
out << "null";
}
};