0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-04 20:52:45 +01:00
construct/include/ircd/json/tuple/tuple.h

407 lines
8.4 KiB
C
Raw Normal View History

// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#pragma once
2017-08-26 06:39:36 +02:00
#define HAVE_IRCD_JSON_TUPLE_H
#include "property.h"
namespace ircd {
namespace json {
//TODO: sort
template<class tuple> struct keys;
/// All tuple templates inherit from this non-template type for tagging.
struct tuple_base
{
// EBO tag
};
/// A compile-time construct to describe a JSON object's members and types.
///
/// Member access by name is O(1) because of recursive constexpr function
/// inlining when translating a name to the index number which is then used
/// as the template argument to std::get() for the value.
///
/// Here we represent a JSON object with a named tuple, allowing the programmer
/// to create a structure specifying all of the potentially valid members of the
/// object. Thus at runtime, the tuple carries around its values like a
/// `struct`. Unlike a `struct`, the tuple is abstractly iterable and we have
/// implemented logic operating on all JSON tuples regardless of their makeup
/// without any effort from a developer when creating a new tuple.
///
/// The member structure for the tuple is called `property` because json::member
/// is already used to pair together runtime oriented json::values.
///
/// Create and use a tuple to efficiently extract members from a json::object.
/// The tuple will populate its own members during a single-pass iteration of
/// the JSON input.
///
/// But remember, the tuple carries very little information for you at runtime
/// which may make it difficult to represent all JS phenomena like "undefined"
/// and "null".
///
template<class... T>
2017-08-26 06:39:36 +02:00
struct tuple
:std::tuple<T...>
,tuple_base
{
using tuple_type = std::tuple<T...>;
using super_type = tuple<T...>;
static constexpr size_t size() noexcept;
operator json::value() const;
operator crh::sha256::buf() const;
/// For json::object constructions, the source JSON (string_view) is
/// carried with the instance. This is important to convey additional
/// keys not enumerated in the tuple. This will be default-initialized
/// for other constructions when no source JSON buffer is available.
json::object source;
template<class name> constexpr decltype(auto) get(name&&) const noexcept;
template<class name> constexpr decltype(auto) get(name&&) noexcept;
template<class name> constexpr decltype(auto) at(name&&) const;
template<class name> constexpr decltype(auto) at(name&&);
template<class R, class name> R get(name&&, R def = {}) const noexcept;
template<class R, class name> const R &at(name&&) const;
template<class R, class name> R &at(name&&);
template<class... U> explicit tuple(const tuple<U...> &);
template<class U> explicit tuple(const json::object &, const json::keys<U> &);
template<class U> explicit tuple(const tuple &, const json::keys<U> &);
tuple(const json::object &);
2017-09-09 21:20:00 +02:00
tuple(const json::iov &);
tuple(const json::members &);
tuple() = default;
};
template<class tuple>
constexpr bool
is_tuple()
noexcept
{
return std::is_base_of<tuple_base, tuple>::value;
}
template<class tuple,
class R>
using enable_if_tuple = typename std::enable_if<is_tuple<tuple>(), R>::type;
template<class tuple,
class test,
class R>
using enable_if_tuple_and = typename std::enable_if<is_tuple<tuple>() && test(), R>::type;
2017-08-26 06:39:36 +02:00
template<class tuple>
using tuple_type = typename tuple::tuple_type;
2017-08-26 06:39:36 +02:00
template<class tuple>
using tuple_size = std::tuple_size<tuple_type<tuple>>;
template<class tuple,
size_t i>
2017-08-26 06:39:36 +02:00
using tuple_element = typename std::tuple_element<i, tuple_type<tuple>>::type;
template<class tuple,
size_t i>
using tuple_value_type = typename tuple_element<tuple, i>::value_type;
2017-08-26 06:39:36 +02:00
template<class tuple>
inline auto &
2017-08-26 06:39:36 +02:00
stdcast(const tuple &o)
{
2017-08-26 06:39:36 +02:00
return static_cast<const typename tuple::tuple_type &>(o);
}
2017-08-26 06:39:36 +02:00
template<class tuple>
inline auto &
2017-08-26 06:39:36 +02:00
stdcast(tuple &o)
{
2017-08-26 06:39:36 +02:00
return static_cast<typename tuple::tuple_type &>(o);
}
template<class tuple>
constexpr enable_if_tuple<tuple, size_t>
size()
noexcept
{
return tuple_size<tuple>::value;
}
} // namespace json
} // namespace ircd
2018-05-20 02:33:28 +02:00
#include "key.h"
#include "indexof.h"
namespace ircd {
namespace json {
template<size_t i,
class tuple>
inline enable_if_tuple<tuple, tuple_value_type<tuple, i> &>
val(tuple &t)
noexcept
{
return static_cast<tuple_value_type<tuple, i> &>(std::get<i>(t));
}
template<size_t i,
class tuple>
inline enable_if_tuple<tuple, const tuple_value_type<tuple, i> &>
val(const tuple &t)
noexcept
{
return static_cast<const tuple_value_type<tuple, i> &>(std::get<i>(t));
}
template<class tuple>
constexpr bool
key_exists(const string_view &key)
2017-09-21 04:33:50 +02:00
{
return indexof<tuple>(key) < size<tuple>();
2017-09-21 04:33:50 +02:00
}
} // namespace json
} // namespace ircd
2017-09-21 04:33:50 +02:00
#include "for_each.h"
#include "until.h"
#include "get.h"
#include "at.h"
#include "set.h"
2017-09-21 04:33:50 +02:00
namespace ircd {
namespace json {
template<class... T>
template<class U>
tuple<T...>::tuple(const json::object &object,
const json::keys<U> &keys)
:source
{
object
}
{
std::for_each(std::begin(object), std::end(object), [this, &keys]
(const auto &member)
{
if(keys.has(member.first))
set(*this, member.first, member.second);
});
}
template<class... T>
tuple<T...>::tuple(const json::object &object)
:source
{
object
}
2017-08-26 06:39:36 +02:00
{
std::for_each(std::begin(object), std::end(object), [this]
(const auto &member)
2017-08-26 06:39:36 +02:00
{
2017-09-21 04:33:50 +02:00
set(*this, member.first, member.second);
2017-08-26 06:39:36 +02:00
});
}
template<class... T>
2017-09-09 21:20:00 +02:00
tuple<T...>::tuple(const json::iov &iov)
{
std::for_each(std::begin(iov), std::end(iov), [this]
(const auto &member)
{
2017-09-21 04:33:50 +02:00
set(*this, member.first, member.second);
});
}
template<class... T>
tuple<T...>::tuple(const json::members &members)
{
std::for_each(std::begin(members), std::end(members), [this]
(const auto &member)
{
2017-09-21 04:33:50 +02:00
set(*this, member.first, member.second);
});
}
template<class... T>
template<class U>
tuple<T...>::tuple(const tuple &t,
const keys<U> &keys)
:source
{
t.source
}
{
for_each(t, [this, &keys]
(const auto &key, const auto &val)
{
if(keys.has(key))
set(*this, key, val);
});
}
template<class... T>
template<class... U>
tuple<T...>::tuple(const tuple<U...> &t)
:source
{
t.source
}
{
for_each(t, [this]
(const auto &key, const auto &val)
{
set(*this, key, val);
});
}
template<class... T>
template<class name>
constexpr decltype(auto)
tuple<T...>::at(name&& n)
{
constexpr const size_t hash
{
name_hash(n)
};
return json::at<hash>(*this);
}
template<class... T>
template<class name>
constexpr decltype(auto)
tuple<T...>::at(name&& n)
const
{
constexpr const size_t hash
{
name_hash(n)
};
return json::at<hash>(*this);
}
template<class... T>
template<class name>
constexpr decltype(auto)
tuple<T...>::get(name&& n)
noexcept
{
constexpr const size_t hash
2019-06-24 07:09:41 +02:00
{
name_hash(n)
2019-06-24 07:09:41 +02:00
};
return json::get<hash>(*this);
}
template<class... T>
template<class name>
constexpr decltype(auto)
tuple<T...>::get(name&& n)
const noexcept
{
constexpr const size_t hash
{
name_hash(n)
};
return json::get<hash>(*this);
}
template<class... T>
template<class R,
class name>
inline R
tuple<T...>::get(name&& n,
R ret)
const noexcept
{
return json::get<R>(*this, n, ret);
}
template<class... T>
template<class R,
class name>
inline const R &
tuple<T...>::at(name&& n)
const
{
return json::at<R>(*this, n);
}
template<class... T>
template<class R,
class name>
inline R &
tuple<T...>::at(name&& n)
{
return json::at<R>(*this, n);
}
template<class... T>
constexpr size_t
tuple<T...>::size()
noexcept
{
return std::tuple_size<tuple_type>();
}
} // namespace json
} // namespace ircd
#include "_key_transform.h"
#include "keys.h"
#include "_member_transform.h"
#include "tool.h"
template<class... T>
ircd::json::tuple<T...>::operator
crh::sha256::buf()
const
{
//TODO: XXX
const auto preimage
{
json::strung(*this)
};
return crh::sha256::buf
{
[&preimage](auto &buf)
{
sha256{buf, const_buffer{preimage}};
}
};
}
template<class... T>
ircd::json::tuple<T...>::operator
json::value()
const
{
json::value ret;
ret.type = OBJECT;
ret.create_string(serialized(*this), [this]
(mutable_buffer buffer)
{
stringify(buffer, *this);
});
return ret;
}