Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinchowdhury18 committed Dec 2, 2024
1 parent fc0c577 commit 946ebcd
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ static T deserialize_object (nonstd::span<const std::byte>& bytes)
const auto serial_bytes = get_bytes_for_deserialization (bytes);
jassert (serial_bytes.size() == sizeof (T));

JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wclass-memaccess")
T object;
std::memcpy (&object, serial_bytes.data(), serial_bytes.size());
return object;
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
}

template <typename T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void NonParamState::serialize (ChainedArenaAllocator& arena, const NonParamState
serialize_direct (serialize_num_bytes, num_bytes);
}

void NonParamState::deserialize (nonstd::span<const std::byte> serial_data, NonParamState& state, ChainedArenaAllocator& arena)
void NonParamState::deserialize (nonstd::span<const std::byte>& serial_data, NonParamState& state, ChainedArenaAllocator& arena)
{
auto num_bytes = deserialize_direct<bytes_detail::size_type> (serial_data);
if (num_bytes == 0)
Expand Down Expand Up @@ -111,7 +111,7 @@ json NonParamState::serialize_json (const NonParamState& state)
{
auto serial = nlohmann::json::object();
for (const auto& value : state.values)
serial[value->name] = value->serialize();
serial[value->name] = value->serialize_json();
return serial;
}

Expand All @@ -130,7 +130,7 @@ void NonParamState::legacy_deserialize (const json& deserial, const NonParamStat
{
if (name == value->name)
{
value->deserialize (valueDeserial);
value->deserialize_json (valueDeserial);
namesThatHaveBeenDeserialized.push_back (name);
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ void NonParamState::deserialize_json (const json& deserial, const NonParamState&
{
auto iter = deserial.find (value->name);
if (iter != deserial.end())
value->deserialize (*iter);
value->deserialize_json (*iter);
else
value->reset();

Check warning on line 163 in modules/plugin/chowdsp_plugin_state/Backend/chowdsp_NonParamState.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_plugin_state/Backend/chowdsp_NonParamState.cpp#L163

Added line #L163 was not covered by tests
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class NonParamState
static void serialize (ChainedArenaAllocator& arena, const NonParamState& state);

/** Custom deserializer */
static void deserialize (nonstd::span<const std::byte> serial_data, NonParamState& state, ChainedArenaAllocator& arena);
static void deserialize (nonstd::span<const std::byte>& serial_data, NonParamState& state, ChainedArenaAllocator& arena);

/** Custom serializer */
static json serialize_json (const NonParamState& state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ void PluginStateImpl<ParameterState, NonParameterState>::serialize (juce::Memory
serialize_object (int {}, arena);

Check warning on line 31 in modules/plugin/chowdsp_plugin_state/Backend/chowdsp_PluginStateImpl.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_plugin_state/Backend/chowdsp_PluginStateImpl.cpp#L31

Added line #L31 was not covered by tests
#endif

ParamHolder::serialize (arena, params);
NonParamState::serialize (arena, nonParams);
ParamHolder::serialize (arena, params);

dump_serialized_bytes (data, arena, &frame);
}
Expand All @@ -53,8 +53,8 @@ void PluginStateImpl<ParameterState, NonParameterState>::deserialize (juce::Memo
else
{
pluginStateVersion = Version::fromVersionHint (deserialize_object<int> (serial_data));
ParamHolder::deserialize (serial_data, params);
NonParamState::deserialize (serial_data, nonParams, *params.arena);
ParamHolder::deserialize (serial_data, params);
}

params.applyVersionStreaming (pluginStateVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ struct StateValueBase

virtual void reset() {}

[[nodiscard]] virtual nlohmann::json serialize() const { return {}; }
virtual void deserialize (const nlohmann::json&) {}
[[nodiscard]] virtual nlohmann::json serialize_json() const { return {}; }
virtual void deserialize_json (const nlohmann::json&) {}

Check warning on line 16 in modules/plugin/chowdsp_plugin_state/Backend/chowdsp_StateValue.h

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_plugin_state/Backend/chowdsp_StateValue.h#L15-L16

Added lines #L15 - L16 were not covered by tests

[[nodiscard]] virtual size_t serialize (ChainedArenaAllocator&) const { return 0; }
virtual void deserialize (nonstd::span<const std::byte>&) {}

Check warning on line 19 in modules/plugin/chowdsp_plugin_state/Backend/chowdsp_StateValue.h

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_plugin_state/Backend/chowdsp_StateValue.h#L18-L19

Added lines #L18 - L19 were not covered by tests
Expand Down Expand Up @@ -74,13 +74,13 @@ struct StateValue : StateValueBase
void reset() override { set (defaultValue); }

/** JSON Serializer */
[[nodiscard]] nlohmann::json serialize() const override
[[nodiscard]] nlohmann::json serialize_json() const override
{
return get();
}

/** JSON Deserializer */
void deserialize (const nlohmann::json& deserial) override
void deserialize_json (const nlohmann::json& deserial) override
{
set (deserial.get<element_type>());
}
Expand Down
43 changes: 24 additions & 19 deletions modules/plugin/chowdsp_presets_v2/Backend/chowdsp_PresetState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,15 @@ void PresetState::reset()
set ({});
}

#if CHOWDSP_USE_LEGACY_STATE_SERIALIZATION
void PresetState::serialize (JSONSerializer::SerializedType& serial) const
nlohmann::json PresetState::serialize_json() const

Check warning on line 40 in modules/plugin/chowdsp_presets_v2/Backend/chowdsp_PresetState.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_presets_v2/Backend/chowdsp_PresetState.cpp#L40

Added line #L40 was not covered by tests
{
JSONSerializer::addChildElement (serial, name);
if (preset == nullptr)
JSONSerializer::addChildElement (serial, {});
else
JSONSerializer::addChildElement (serial, preset->toJson());
if (preset != nullptr)
return preset->toJson();

Check warning on line 43 in modules/plugin/chowdsp_presets_v2/Backend/chowdsp_PresetState.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_presets_v2/Backend/chowdsp_PresetState.cpp#L43

Added line #L43 was not covered by tests

return {};

Check warning on line 45 in modules/plugin/chowdsp_presets_v2/Backend/chowdsp_PresetState.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_presets_v2/Backend/chowdsp_PresetState.cpp#L45

Added line #L45 was not covered by tests
}

void PresetState::deserialize (JSONSerializer::DeserializedType deserial)
void PresetState::deserialize_json (const nlohmann::json& deserial)

Check warning on line 48 in modules/plugin/chowdsp_presets_v2/Backend/chowdsp_PresetState.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_presets_v2/Backend/chowdsp_PresetState.cpp#L48

Added line #L48 was not covered by tests
{
if (deserial.is_null())
{
Expand All @@ -57,26 +55,33 @@ void PresetState::deserialize (JSONSerializer::DeserializedType deserial)

set (PresetPtr { deserial });
}
#else
nlohmann::json PresetState::serialize() const

[[nodiscard]] size_t PresetState::serialize (ChainedArenaAllocator& arena) const
{
if (preset != nullptr)
return preset->toJson();
size_t num_bytes = 0;
if (preset == nullptr)
{
num_bytes += serialize_string ("", arena);
return num_bytes;
}

return {};
num_bytes += serialize_string (preset->toJson().dump(), arena);
return num_bytes;
}

void PresetState::deserialize (const nlohmann::json& deserial)
void PresetState::deserialize (nonstd::span<const std::byte>& bytes)
{
if (deserial.is_null())
try
{
const auto stateJson = json::parse (deserialize_string (bytes));
set (PresetPtr { stateJson });
}
catch (const std::exception& e)
{
juce::Logger::writeToLog (std::string { "Unable to load preset state: " } + e.what());
reset();
return;
}

set (PresetPtr { deserial });
}
#endif

bool operator== (const PresetState& presetState, std::nullptr_t)
{
Expand Down
12 changes: 5 additions & 7 deletions modules/plugin/chowdsp_presets_v2/Backend/chowdsp_PresetState.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,17 @@ class PresetState : public StateValueBase
/** Internal use only! */
void reset() override;

#if CHOWDSP_USE_LEGACY_STATE_SERIALIZATION
/** Internal use only! */
void serialize (JSONSerializer::SerializedType& serial) const override;
[[nodiscard]] nlohmann::json serialize_json() const override;

/** Internal use only! */
void deserialize (JSONSerializer::DeserializedType deserial) override;
#else
void deserialize_json (const nlohmann::json& deserial) override;

/** Internal use only! */
[[nodiscard]] nlohmann::json serialize() const override;
[[nodiscard]] size_t serialize (ChainedArenaAllocator&) const override;

/** Internal use only! */
void deserialize (const nlohmann::json& deserial) override;
#endif
void deserialize (nonstd::span<const std::byte>&) override;

private:
PresetPtr preset {};
Expand Down
15 changes: 10 additions & 5 deletions tests/plugin_tests/chowdsp_plugin_state_test/NonParamTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ TEST_CASE ("Non-Param Test", "[plugin][state]")

std::vector<std::byte> serial_bytes (chowdsp::get_serial_num_bytes (arena));
chowdsp::dump_serialized_bytes (serial_bytes, arena);
nonstd::span<const std::byte> bytes { serial_bytes };

chowdsp::NonParamState state {};
state.addStateValues ({ &int_val, &atomic_int_val, &bool_vals, &string_val, &string_view_val, &juce_string_val, &json_val });
Expand All @@ -95,7 +96,7 @@ TEST_CASE ("Non-Param Test", "[plugin][state]")
REQUIRE (juce_string_val.get() == "juce");
REQUIRE (json_val.get() == chowdsp::json { { "val1", 100 }, { "val2", "test" } });

chowdsp::NonParamState::deserialize (serial_bytes, state, arena);
chowdsp::NonParamState::deserialize (bytes, state, arena);
REQUIRE (int_val.get() == 101);
REQUIRE (atomic_int_val.get() == 102);
REQUIRE (! bool_vals.get()[0]);
Expand Down Expand Up @@ -126,6 +127,7 @@ TEST_CASE ("Non-Param Test", "[plugin][state]")

std::vector<std::byte> serial_bytes (chowdsp::get_serial_num_bytes (arena));
chowdsp::dump_serialized_bytes (serial_bytes, arena);
nonstd::span<const std::byte> bytes { serial_bytes };

chowdsp::NonParamState state {};
state.addStateValues ({ &string_val, &atomic_int_val, &int_val });
Expand All @@ -134,7 +136,7 @@ TEST_CASE ("Non-Param Test", "[plugin][state]")
REQUIRE (atomic_int_val.get() == 99);
REQUIRE (string_val.get() == "blah");

chowdsp::NonParamState::deserialize (serial_bytes, state, arena);
chowdsp::NonParamState::deserialize (bytes, state, arena);
REQUIRE (int_val.get() == 101);
REQUIRE (atomic_int_val.get() == 102);
REQUIRE (string_val.get() == "blah blah");
Expand All @@ -156,6 +158,7 @@ TEST_CASE ("Non-Param Test", "[plugin][state]")

std::vector<std::byte> serial_bytes (chowdsp::get_serial_num_bytes (arena));
chowdsp::dump_serialized_bytes (serial_bytes, arena);
nonstd::span<const std::byte> bytes { serial_bytes };

chowdsp::StateValue<float> float_val { "float", 90.0f };
chowdsp::NonParamState state {};
Expand All @@ -166,7 +169,7 @@ TEST_CASE ("Non-Param Test", "[plugin][state]")
REQUIRE (float_val.get() == 100.0f);
REQUIRE (string_val.get() == "blah");

chowdsp::NonParamState::deserialize (serial_bytes, state, arena);
chowdsp::NonParamState::deserialize (bytes, state, arena);
REQUIRE (int_val.get() == 101);
REQUIRE (float_val.get() == 90.0f);
REQUIRE (string_val.get() == "blah blah");
Expand All @@ -190,14 +193,15 @@ TEST_CASE ("Non-Param Test", "[plugin][state]")

std::vector<std::byte> serial_bytes (chowdsp::get_serial_num_bytes (arena));
chowdsp::dump_serialized_bytes (serial_bytes, arena);
nonstd::span<const std::byte> bytes { serial_bytes };

chowdsp::NonParamState state {};
state.addStateValues ({ &string_val, &int_val });
state.reset();
REQUIRE (int_val.get() == 42);
REQUIRE (string_val.get() == "blah");

chowdsp::NonParamState::deserialize (serial_bytes, state, arena);
chowdsp::NonParamState::deserialize (bytes, state, arena);
REQUIRE (int_val.get() == 101);
REQUIRE (string_val.get() == "blah blah");
}
Expand All @@ -215,9 +219,10 @@ TEST_CASE ("Non-Param Test", "[plugin][state]")
string_val = "blah blah";

const auto serial = std::array<std::byte, 2> {};
nonstd::span<const std::byte> bytes { serial };

chowdsp::ChainedArenaAllocator arena { 1024 };
chowdsp::NonParamState::deserialize (serial, state, arena);
chowdsp::NonParamState::deserialize (bytes, state, arena);
REQUIRE (int_val.get() == 42);
REQUIRE (atomic_int_val.get() == 99);
REQUIRE (string_val.get() == "blah");
Expand Down

0 comments on commit 946ebcd

Please sign in to comment.