Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make glz::manage format agnostic #1646

Merged
merged 2 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions include/glaze/json/manage.hpp → include/glaze/core/manage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

#pragma once

#include "glaze/json/json_ptr.hpp"
#include "glaze/json/read.hpp"
#include "glaze/json/write.hpp"
#include "glaze/core/common.hpp"

// Calls a read function after reading and calls a write function before writing.
// glz::manage is useful for transforming state from a user facing format
// into a more complex or esoteric internal format.

namespace glz
{
Expand All @@ -25,17 +27,17 @@ namespace glz
template <class T, class Member, class From, class To>
manage_t(T&, Member, From, To) -> manage_t<T, Member, From, To>;

template <class T>
template <uint32_t Format, class T>
requires(is_specialization_v<T, manage_t>)
struct from<JSON, T>
struct from<Format, T>
{
template <auto Opts>
static void op(auto&& value, is_context auto&& ctx, auto&& it, auto&& end)
{
using V = std::decay_t<decltype(value)>;
using From = typename V::from_t;

parse<JSON>::op<Opts>(get_member(value.val, value.member), ctx, it, end);
parse<Format>::template op<Opts>(get_member(value.val, value.member), ctx, it, end);

if constexpr (std::is_member_pointer_v<From>) {
if constexpr (std::is_member_function_pointer_v<From>) {
Expand Down Expand Up @@ -70,9 +72,9 @@ namespace glz
}
};

template <class T>
template <uint32_t Format, class T>
requires(is_specialization_v<T, manage_t>)
struct to<JSON, T>
struct to<Format, T>
{
template <auto Opts>
static void op(auto&& value, is_context auto&& ctx, auto&&... args)
Expand Down Expand Up @@ -112,7 +114,7 @@ namespace glz
}

using Value = core_t<decltype(get_member(value.val, value.member))>;
to<JSON, Value>::template op<Opts>(get_member(value.val, value.member), ctx, args...);
to<Format, Value>::template op<Opts>(get_member(value.val, value.member), ctx, args...);
}
};

Expand Down
2 changes: 1 addition & 1 deletion include/glaze/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "glaze/json/json_concepts.hpp"
#include "glaze/json/json_ptr.hpp"
#include "glaze/json/json_t.hpp"
#include "glaze/json/manage.hpp"
#include "glaze/core/manage.hpp"
#include "glaze/json/max_write_precision.hpp"
#include "glaze/json/minify.hpp"
#include "glaze/json/ndjson.hpp"
Expand Down
105 changes: 105 additions & 0 deletions tests/roundtrip/roundtrip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,109 @@ suite tests = [] {
};
};

struct manage_x
{
std::vector<int> x{};
std::vector<int> y{};

bool read_x()
{
y = x;
return true;
}

bool write_x()
{
x = y;
return true;
}
};

template <>
struct glz::meta<manage_x>
{
using T = manage_x;
static constexpr auto value = object("x", manage<&T::x, &T::read_x, &T::write_x>);
};

struct manage_x_lambda
{
std::vector<int> x{};
std::vector<int> y{};
};

template <>
struct glz::meta<manage_x_lambda>
{
using T = manage_x_lambda;
static constexpr auto read_x = [](auto& s) {
s.y = s.x;
return true;
};
static constexpr auto write_x = [](auto& s) {
s.x = s.y;
return true;
};
[[maybe_unused]] static constexpr auto value = object("x", manage<&T::x, read_x, write_x>);
};

struct manage_test_struct
{
std::string a{};
std::string b{};

bool read_a() { return true; }
bool write_a() { return false; }
};

template <>
struct glz::meta<manage_test_struct>
{
using T = manage_test_struct;
static constexpr auto value = object("a", manage<&T::a, &T::read_a, &T::write_a>, "b", &T::b);
};

suite manage_test = [] {
"manage"_test = [] {
manage_x obj{.x = {1,2,3}, .y = {1,2,3}};
std::string s{};
expect(not glz::write<default_opts>(obj, s));
obj = {};
expect(not glz::read<default_opts>(obj, s));
expect(obj.y[0] == 1);
expect(obj.y[1] == 2);
expect(obj.y[2] == 3);
obj.x.clear();
s.clear();
expect(not glz::write<default_opts>(obj, s));
expect(obj.x[0] == 1);
expect(obj.x[1] == 2);
expect(obj.x[2] == 3);
};

"manage_lambdas"_test = [] {
manage_x_lambda obj{.x = {1,2,3}, .y = {1,2,3}};
std::string s{};
expect(not glz::write<default_opts>(obj, s));
obj = {};
expect(not glz::read<default_opts>(obj, s));
expect(obj.y[0] == 1);
expect(obj.y[1] == 2);
expect(obj.y[2] == 3);
obj.x.clear();
s.clear();
expect(not glz::write<default_opts>(obj, s));
expect(obj.x[0] == 1);
expect(obj.x[1] == 2);
expect(obj.x[2] == 3);
};

"manage_test_struct"_test = [] {
manage_test_struct obj{.a = "aaa", .b = "bbb"};
std::string s{};
const auto ec = glz::write<default_opts>(obj, s);
expect(bool(ec));
};
};

int main() { return 0; }