2018-05-07 06:31:32 +02:00
|
|
|
// 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
|
|
|
|
#define HAVE_IRCD_JSON_TUPLE_KEYS_H
|
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace json {
|
|
|
|
|
2018-05-20 02:32:31 +02:00
|
|
|
template<class tuple> struct keys;
|
|
|
|
|
|
|
|
} // namespace json
|
|
|
|
} // namespace ircd
|
|
|
|
|
|
|
|
/// Array of string literals (in string_views) representing just the keys of a
|
|
|
|
/// tuple. By default construction all keys are included in the array. A
|
|
|
|
/// selection construction will only include keys that are selected. Note that
|
|
|
|
/// the selection construction packs all the chosen keys at the front of the
|
|
|
|
/// array so you cannot rely on this for a key's index into the tuple.
|
|
|
|
template<class tuple>
|
|
|
|
struct ircd::json::keys
|
|
|
|
:std::array<ircd::string_view, tuple::size()>
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
|
|
|
struct selection;
|
|
|
|
struct include;
|
|
|
|
struct exclude;
|
|
|
|
|
2018-05-20 02:32:31 +02:00
|
|
|
size_t count() const;
|
|
|
|
|
|
|
|
keys(const selection &);
|
|
|
|
keys();
|
2018-05-07 06:31:32 +02:00
|
|
|
};
|
|
|
|
|
2018-05-20 02:32:31 +02:00
|
|
|
/// Selection of keys in a tuple represented by a bitset. Users generally
|
|
|
|
/// do not construct this class directly. Instead, construct one of the
|
|
|
|
/// `include` or `exclude` classes which will set these bits appropriately.
|
|
|
|
template<class tuple>
|
|
|
|
struct ircd::json::keys<tuple>::selection
|
|
|
|
:std::bitset<tuple::size()>
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
|
|
|
template<class closure>
|
2018-05-20 02:32:31 +02:00
|
|
|
bool until(closure &&function) const;
|
2018-05-07 06:31:32 +02:00
|
|
|
|
|
|
|
template<class closure>
|
2018-05-20 02:32:31 +02:00
|
|
|
void for_each(closure &&function) const;
|
2018-05-07 06:31:32 +02:00
|
|
|
|
|
|
|
template<class it_a,
|
|
|
|
class it_b>
|
2018-05-20 02:32:31 +02:00
|
|
|
it_a transform(it_a it, const it_b end) const;
|
2018-05-07 06:31:32 +02:00
|
|
|
};
|
|
|
|
|
2018-05-20 02:32:31 +02:00
|
|
|
/// Construct this class with a list of keys you want to select for a given
|
|
|
|
/// tuple. This constructs a bitset representing the keys of the tuple and
|
|
|
|
/// lights the bits for your selections.
|
|
|
|
template<class tuple>
|
|
|
|
struct ircd::json::keys<tuple>::include
|
2018-05-07 06:31:32 +02:00
|
|
|
:selection
|
|
|
|
{
|
2018-05-20 02:32:31 +02:00
|
|
|
include(const std::initializer_list<string_view> &list)
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
|
|
|
for(const auto &key : list)
|
2018-05-20 02:32:31 +02:00
|
|
|
this->set(indexof<tuple>(key), true);
|
2018-05-07 06:31:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-20 02:32:31 +02:00
|
|
|
/// Construct this class with a list of keys you want to deselect for a given
|
|
|
|
/// tuple. This constructs a bitset representing the keys of the tuple and
|
|
|
|
/// lights the bits which are not in the list.
|
|
|
|
template<class tuple>
|
|
|
|
struct ircd::json::keys<tuple>::exclude
|
2018-05-07 06:31:32 +02:00
|
|
|
:selection
|
|
|
|
{
|
2018-05-20 02:32:31 +02:00
|
|
|
exclude(const std::initializer_list<string_view> &list)
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
|
|
|
this->set();
|
|
|
|
for(const auto &key : list)
|
2018-05-20 02:32:31 +02:00
|
|
|
this->set(indexof<tuple>(key), false);
|
2018-05-07 06:31:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-20 02:32:31 +02:00
|
|
|
//
|
|
|
|
// selection
|
|
|
|
//
|
|
|
|
|
|
|
|
template<class tuple>
|
|
|
|
template<class it_a,
|
|
|
|
class it_b>
|
|
|
|
it_a
|
|
|
|
ircd::json::keys<tuple>::selection::transform(it_a it,
|
|
|
|
const it_b end)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
this->until([&it, &end](auto&& key)
|
|
|
|
{
|
|
|
|
if(it == end)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*it = key;
|
|
|
|
++it;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple>
|
|
|
|
template<class closure>
|
|
|
|
void
|
|
|
|
ircd::json::keys<tuple>::selection::for_each(closure &&function)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
this->until([&function](auto&& key)
|
|
|
|
{
|
|
|
|
function(key);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple>
|
|
|
|
template<class closure>
|
|
|
|
bool
|
|
|
|
ircd::json::keys<tuple>::selection::until(closure &&function)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for(size_t i(0); i < tuple::size(); ++i)
|
|
|
|
if(this->test(i))
|
|
|
|
if(!function(key<tuple>(i)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// keys
|
|
|
|
//
|
|
|
|
|
|
|
|
template<class tuple>
|
|
|
|
ircd::json::keys<tuple>::keys()
|
|
|
|
{
|
|
|
|
_key_transform<tuple>(this->begin(), this->end());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple>
|
|
|
|
ircd::json::keys<tuple>::keys(const selection &selection)
|
|
|
|
{
|
|
|
|
selection.transform(this->begin(), this->end());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple>
|
|
|
|
size_t
|
|
|
|
ircd::json::keys<tuple>::count()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t i(0);
|
|
|
|
for(; i < this->size(); ++i)
|
|
|
|
if(!(*this)[i])
|
|
|
|
break;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|