Skip to content

Commit

Permalink
Add methods for serializing/deserializing the serial type (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinchowdhury18 authored Dec 15, 2023
1 parent 8d4c752 commit 9b1c6c9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ class BaseSerializer
return serial;
}

/** Serializer for serial type */
template <typename Serializer, typename T>
static std::enable_if_t<std::is_same_v<T, SerialType<Serializer>>, SerialType<Serializer>>
serialize (T x)
{
return x;
}

/** Deserializer for serial types */
template <typename Serializer, typename T>
static std::enable_if_t<std::is_same_v<T, SerialType<Serializer>>, void>
deserialize (DeserialType<Serializer> serial, T& x)
{
x = serial;
}

/** Serializer for arithmetic types */
template <typename Serializer, typename T>
static std::enable_if_t<std::is_arithmetic_v<T>, SerialType<Serializer>>
Expand Down Expand Up @@ -164,7 +180,7 @@ class BaseSerializer

/** Serializer for container types */
template <typename Serializer, typename T>
static std::enable_if_t<IsContainerNotMapOrString<T>, SerialType<Serializer>>
static std::enable_if_t<IsContainerNotMapOrString<T> && ! std::is_same_v<T, SerialType<Serializer>>, SerialType<Serializer>>
serialize (const T& container)
{
auto serial = Serializer::createBaseElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct PluginNonParameterState : chowdsp::NonParamState

PluginNonParameterState()
{
addStateValues ({ &editorWidth, &editorHeight, &atomicThing });
addStateValues ({ &editorWidth, &editorHeight, &atomicThing, &jsonThing });

for (size_t i = 0; i < 4; ++i)
{
Expand All @@ -52,6 +52,14 @@ struct PluginNonParameterState : chowdsp::NonParamState
chowdsp::StateValue<int> editorWidth { "editor_width", 300 };
chowdsp::StateValue<int> editorHeight { "editor_height", 500 };
chowdsp::StateValue<std::atomic_int, int> atomicThing { "something_atomic", 12 };
chowdsp::StateValue<nlohmann::json> jsonThing { "json_thing",
nlohmann::json { { "pi", 3.141 },
{ "happy", true },
{ "name", "Niels" },
{ "nothing", nullptr },
{ "answer", { { "everything", 42 } } },
{ "list", { 1, 0, 2 } },
{ "object", { { "currency", "USD" }, { "value", 42.99 } } } } };

std::array<chowdsp::StringLiteral<8>, 8> yesNoNames {};
chowdsp::SmallVector<chowdsp::StateValue<YesNo>, 8> yesNoVals;
Expand Down Expand Up @@ -187,13 +195,15 @@ TEST_CASE ("State Serialization Test", "[plugin][state]")
static constexpr int width = 200;
static constexpr int height = 150;
static constexpr int atomic = 24;
const auto testJSON = nlohmann::json { { "new", 20 } };

juce::MemoryBlock block;
{
State state;
state.nonParams.editorWidth = width;
state.nonParams.editorHeight = height;
state.nonParams.atomicThing = atomic;
state.nonParams.jsonThing = testJSON;
state.serialize (block);
}

Expand All @@ -202,6 +212,7 @@ TEST_CASE ("State Serialization Test", "[plugin][state]")
REQUIRE_MESSAGE (state.nonParams.editorWidth.get() == width, "Editor width is incorrect");
REQUIRE_MESSAGE (state.nonParams.editorHeight.get() == height, "Editor height is incorrect");
REQUIRE_MESSAGE (state.nonParams.atomicThing.get() == atomic, "Atomic thing is incorrect");
REQUIRE_MESSAGE (state.nonParams.jsonThing.get() == testJSON, "JSON thing is incorrect");
}

SECTION ("Added Parameter Test")
Expand Down

0 comments on commit 9b1c6c9

Please sign in to comment.