#ifndef CHILON_APPEND_HPP
#define CHILON_APPEND_HPP
#include <chilon/meta/return.hpp>
#include <vector>
#include <unordered_map>
#include <type_traits>
namespace chilon {
namespace detail { struct appendable {}; }
template <class K>
struct key_collision {
key_collision(K const& key) : key_(key) {};
K const key_;
};
template <class K>
void throw_key_collision(K const& key) {
throw key_collision<K>(key);
}
template <class T, class U>
static inline
CHILON_RETURN_REQUIRE(void, std::is_base_of<detail::appendable, T>)
append(T& src, U const& dest) {
T::append_type::exec(src, dest);
}
template <class T, class U>
static inline
CHILON_RETURN_REQUIRE(void, std::is_base_of<detail::appendable, T>)
append(T& src, U&& dest) {
T::append_type::exec(src, std::move(dest));
}
template <class T>
static inline void append(std::vector<T>& dest, std::vector<T> const& src) {
for (auto i = src.begin(); i != src.end(); ++i) dest.push_back(*i);
}
template <class T>
static inline void append(std::vector<T>& dest, std::vector<T>&& src) {
for (auto i = src.begin(); i != src.end(); ++i) dest.push_back(std::move(*i));
}
template <class T, class U>
static inline void append_map(T& t, U const& u) {
for (auto it = u.begin(); it != u.end(); ++it) {
if (! t.insert(*it).second)
throw_key_collision(it->first);
}
}
template <class T, class U>
static inline void append_map(T& t, U&& u) {
for (auto it = u.begin(); it != u.end(); ++it) {
if (! t.emplace(std::move(*it)).second)
throw_key_collision(it->first);
}
}
template <class U, class K, class V, class _H, class A>
static inline void append(std::unordered_map<K, V, _H, A>& t, U const& u) {
append_map(t, u);
}
template <class U, class K, class V, class _H, class A>
static inline void append(std::unordered_map<K, V, _H, A>& t, U&& u) {
append_map(t, u);
}
template <class T>
struct appendable : detail::appendable {
typedef T append_type;
};
}
#endif