/*
- * 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.
//////////////////////////////////////////////////////////////////////
+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 {
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<void*>() != 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_;
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) {