mirror of
https://github.com/matrix-construct/construct
synced 2024-11-29 10:12:39 +01:00
ircd::json: Various tuple cleanup; split tuple transform suites into files.
This commit is contained in:
parent
4dd8f5ab3a
commit
6576406123
4 changed files with 168 additions and 133 deletions
85
include/ircd/json/tuple/_key_transform.h
Normal file
85
include/ircd/json/tuple/_key_transform.h
Normal file
|
@ -0,0 +1,85 @@
|
|||
// 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__KEY_TRANSFORM_H
|
||||
|
||||
namespace ircd {
|
||||
namespace json {
|
||||
|
||||
template<class tuple,
|
||||
class it_a,
|
||||
class it_b,
|
||||
size_t i,
|
||||
class closure>
|
||||
constexpr typename std::enable_if<i == tuple::size(), it_a>::type
|
||||
_key_transform(it_a it,
|
||||
const it_b &end,
|
||||
closure&& lambda)
|
||||
{
|
||||
return it;
|
||||
}
|
||||
|
||||
template<class tuple,
|
||||
class it_a,
|
||||
class it_b,
|
||||
size_t i = 0,
|
||||
class closure>
|
||||
constexpr typename std::enable_if<i < tuple::size(), it_a>::type
|
||||
_key_transform(it_a it,
|
||||
const it_b &end,
|
||||
closure&& lambda)
|
||||
{
|
||||
if(it != end)
|
||||
{
|
||||
*it = lambda(key<tuple, i>());
|
||||
++it;
|
||||
}
|
||||
|
||||
return _key_transform<tuple, it_a, it_b, i + 1>(it, end, std::move(lambda));
|
||||
}
|
||||
|
||||
template<class tuple,
|
||||
class it_a,
|
||||
class it_b>
|
||||
constexpr auto
|
||||
_key_transform(it_a it,
|
||||
const it_b &end)
|
||||
{
|
||||
return _key_transform<tuple>(it, end, []
|
||||
(auto&& key)
|
||||
{
|
||||
return key;
|
||||
});
|
||||
}
|
||||
|
||||
template<class it_a,
|
||||
class it_b,
|
||||
class... T>
|
||||
auto
|
||||
_key_transform(const tuple<T...> &tuple,
|
||||
it_a it,
|
||||
const it_b &end)
|
||||
{
|
||||
for_each(tuple, [&it, &end]
|
||||
(const auto &key, const auto &val)
|
||||
{
|
||||
if(it != end)
|
||||
{
|
||||
*it = key;
|
||||
++it;
|
||||
}
|
||||
});
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
} // namespace ircd
|
76
include/ircd/json/tuple/_member_transform.h
Normal file
76
include/ircd/json/tuple/_member_transform.h
Normal file
|
@ -0,0 +1,76 @@
|
|||
// 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__MEMBER_TRANSFORM_H
|
||||
|
||||
namespace ircd {
|
||||
namespace json {
|
||||
|
||||
template<class it_a,
|
||||
class it_b,
|
||||
class closure,
|
||||
class... T>
|
||||
auto
|
||||
_member_transform_if(const tuple<T...> &tuple,
|
||||
it_a it,
|
||||
const it_b end,
|
||||
closure&& lambda)
|
||||
{
|
||||
until(tuple, [&it, &end, &lambda]
|
||||
(const auto &key, auto&& val)
|
||||
{
|
||||
if(it == end)
|
||||
return false;
|
||||
|
||||
if(lambda(*it, key, val))
|
||||
++it;
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
template<class it_a,
|
||||
class it_b,
|
||||
class closure,
|
||||
class... T>
|
||||
auto
|
||||
_member_transform(const tuple<T...> &tuple,
|
||||
it_a it,
|
||||
const it_b end,
|
||||
closure&& lambda)
|
||||
{
|
||||
return _member_transform_if(tuple, it, end, [&lambda]
|
||||
(auto&& ret, const auto &key, auto&& val)
|
||||
{
|
||||
ret = lambda(key, val);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
template<class it_a,
|
||||
class it_b,
|
||||
class... T>
|
||||
auto
|
||||
_member_transform(const tuple<T...> &tuple,
|
||||
it_a it,
|
||||
const it_b end)
|
||||
{
|
||||
return _member_transform(tuple, it, end, []
|
||||
(auto&& ret, const auto &key, auto&& val) -> member
|
||||
{
|
||||
return { key, val };
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
} // namespace ircd
|
|
@ -11,13 +11,10 @@
|
|||
#pragma once
|
||||
#define HAVE_IRCD_JSON_TUPLE_KEYS_H
|
||||
|
||||
namespace ircd {
|
||||
namespace json {
|
||||
|
||||
template<class tuple> struct keys;
|
||||
|
||||
} // namespace json
|
||||
} // namespace ircd
|
||||
namespace ircd::json
|
||||
{
|
||||
template<class tuple> struct keys;
|
||||
}
|
||||
|
||||
/// 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
|
||||
|
|
|
@ -213,139 +213,16 @@ tuple<T...>::size()
|
|||
return std::tuple_size<tuple_type>();
|
||||
}
|
||||
|
||||
template<class tuple,
|
||||
class it_a,
|
||||
class it_b,
|
||||
size_t i,
|
||||
class closure>
|
||||
constexpr typename std::enable_if<i == tuple::size(), it_a>::type
|
||||
_key_transform(it_a it,
|
||||
const it_b &end,
|
||||
closure&& lambda)
|
||||
{
|
||||
return it;
|
||||
}
|
||||
|
||||
template<class tuple,
|
||||
class it_a,
|
||||
class it_b,
|
||||
size_t i = 0,
|
||||
class closure>
|
||||
constexpr typename std::enable_if<i < tuple::size(), it_a>::type
|
||||
_key_transform(it_a it,
|
||||
const it_b &end,
|
||||
closure&& lambda)
|
||||
{
|
||||
if(it != end)
|
||||
{
|
||||
*it = lambda(key<tuple, i>());
|
||||
++it;
|
||||
}
|
||||
|
||||
return _key_transform<tuple, it_a, it_b, i + 1>(it, end, std::move(lambda));
|
||||
}
|
||||
|
||||
template<class tuple,
|
||||
class it_a,
|
||||
class it_b>
|
||||
constexpr auto
|
||||
_key_transform(it_a it,
|
||||
const it_b &end)
|
||||
{
|
||||
return _key_transform<tuple>(it, end, []
|
||||
(auto&& key)
|
||||
{
|
||||
return key;
|
||||
});
|
||||
}
|
||||
|
||||
template<class it_a,
|
||||
class it_b,
|
||||
class... T>
|
||||
auto
|
||||
_key_transform(const tuple<T...> &tuple,
|
||||
it_a it,
|
||||
const it_b &end)
|
||||
{
|
||||
for_each(tuple, [&it, &end]
|
||||
(const auto &key, const auto &val)
|
||||
{
|
||||
if(it != end)
|
||||
{
|
||||
*it = key;
|
||||
++it;
|
||||
}
|
||||
});
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
} // namespace ircd
|
||||
|
||||
#include "_key_transform.h"
|
||||
#include "keys.h"
|
||||
#include "_member_transform.h"
|
||||
|
||||
namespace ircd {
|
||||
namespace json {
|
||||
|
||||
template<class it_a,
|
||||
class it_b,
|
||||
class closure,
|
||||
class... T>
|
||||
auto
|
||||
_member_transform_if(const tuple<T...> &tuple,
|
||||
it_a it,
|
||||
const it_b end,
|
||||
closure&& lambda)
|
||||
{
|
||||
until(tuple, [&it, &end, &lambda]
|
||||
(const auto &key, auto&& val)
|
||||
{
|
||||
if(it == end)
|
||||
return false;
|
||||
|
||||
if(lambda(*it, key, val))
|
||||
++it;
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
template<class it_a,
|
||||
class it_b,
|
||||
class closure,
|
||||
class... T>
|
||||
auto
|
||||
_member_transform(const tuple<T...> &tuple,
|
||||
it_a it,
|
||||
const it_b end,
|
||||
closure&& lambda)
|
||||
{
|
||||
return _member_transform_if(tuple, it, end, [&lambda]
|
||||
(auto&& ret, const auto &key, auto&& val)
|
||||
{
|
||||
ret = lambda(key, val);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
template<class it_a,
|
||||
class it_b,
|
||||
class... T>
|
||||
auto
|
||||
_member_transform(const tuple<T...> &tuple,
|
||||
it_a it,
|
||||
const it_b end)
|
||||
{
|
||||
return _member_transform(tuple, it, end, []
|
||||
(auto&& ret, const auto &key, auto&& val) -> member
|
||||
{
|
||||
return { key, val };
|
||||
});
|
||||
}
|
||||
|
||||
template<class... T>
|
||||
size_t
|
||||
serialized(const tuple<T...> &t)
|
||||
|
@ -471,8 +348,8 @@ const
|
|||
{
|
||||
stringify(buffer, *this);
|
||||
});
|
||||
return ret;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
|
|
Loading…
Reference in a new issue