mirror of
https://github.com/MoleskiCoder/EightBit.git
synced 2025-01-22 12:30:44 +00:00
f3f6452119
1/2 speed, compared to boost.json Signed-off-by: Adrian Conlon <adrian.conlon@gmail.com>
80 lines
2.3 KiB
C++
80 lines
2.3 KiB
C++
#include "stdafx.h"
|
|
#include "test_t.h"
|
|
#include <cassert>
|
|
#include <stdexcept>
|
|
|
|
test_t::action test_t::to_action(std::string value) {
|
|
if (value == "read")
|
|
return action::read;
|
|
if (value == "write")
|
|
return action::write;
|
|
throw new std::out_of_range("Unknown action");
|
|
}
|
|
|
|
std::string test_t::to_string(action value) {
|
|
if (value == action::read)
|
|
return "read";
|
|
if (value == action::write)
|
|
return "write";
|
|
throw new std::out_of_range("Unknown action");
|
|
}
|
|
|
|
#ifdef USE_BOOST_JSON
|
|
|
|
test_t::test_t(const boost::json::object& serialised) {
|
|
initialise(serialised);
|
|
}
|
|
|
|
test_t::test_t(const boost::json::value& serialised) {
|
|
assert(serialised.is_object());
|
|
initialise(serialised.get_object());
|
|
}
|
|
|
|
void test_t::initialise(const boost::json::object& serialised) {
|
|
|
|
m_name = get_string(serialised, "name");
|
|
m_initial_state = state_t(get_value(serialised, "initial"));
|
|
m_final_state = state_t(get_value(serialised, "final"));
|
|
|
|
const auto& cycles_array = get_array(serialised, "cycles");
|
|
m_cycles.reserve(cycles_array.size());
|
|
|
|
for (const auto& cycles_entry : cycles_array) {
|
|
const auto& cycle_array = get_array(cycles_entry);
|
|
assert(cycle_array.size() == 3);
|
|
const auto address = get_uint16(cycle_array[0]);
|
|
const auto contents = get_uint8(cycle_array[1]);
|
|
const auto action = to_action((std::string)get_string(cycle_array[2]));
|
|
m_cycles.push_back({ address, contents, action });
|
|
}
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef USE_NLOHMANN_JSON
|
|
|
|
test_t::test_t(const nlohmann::json& serialised) {
|
|
assert(serialised.is_object());
|
|
initialise(serialised);
|
|
}
|
|
|
|
void test_t::initialise(const nlohmann::json& serialised) {
|
|
|
|
m_name = serialised["name"].get<std::string>();
|
|
m_initial_state = state_t(serialised["initial"]);
|
|
m_final_state = state_t(serialised["final"]);
|
|
|
|
const auto& cycles_array = serialised["cycles"];
|
|
m_cycles.reserve(cycles_array.size());
|
|
|
|
for (const auto& cycles_entry : cycles_array) {
|
|
assert(cycles_entry.size() == 3);
|
|
const auto address = cycles_entry[0].get<uint16_t>();
|
|
const auto contents = cycles_entry[1].get<uint8_t>();
|
|
const auto action = to_action(cycles_entry[2].get<std::string>());
|
|
m_cycles.push_back({ address, contents, action });
|
|
}
|
|
}
|
|
|
|
#endif
|