Tidy up the code a little (including removing some no longer needed code).

Signed-off-by: Adrian Conlon <adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon 2021-10-13 23:33:08 +01:00
parent 4892ea95d3
commit 500e65b895
13 changed files with 32 additions and 139 deletions

View File

@ -161,7 +161,6 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="json_t.cpp" />
<ClCompile Include="opcode_test_suite_t.cpp" />
<ClCompile Include="simdjson\simdjson.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
@ -181,7 +180,6 @@
<ClCompile Include="test_t.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="json_t.h" />
<ClInclude Include="nlohmann\json.hpp" />
<ClInclude Include="opcode_test_suite_t.h" />
<ClInclude Include="simdjson\simdjson.h" />

View File

@ -23,9 +23,6 @@
<ClCompile Include="stdafx.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="json_t.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="state_t.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@ -46,9 +43,6 @@
<ClInclude Include="stdafx.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="json_t.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="state_t.h">
<Filter>Header Files</Filter>
</ClInclude>

View File

@ -40,7 +40,6 @@ private:
bool check(std::string what, uint16_t address, uint8_t expected, uint8_t actual);
void addActualEvent(test_t::action action, uint16_t address, uint8_t value);
void dumpEvents(std::string which, const test_t::events_t& events);

View File

@ -1,58 +0,0 @@
#include "stdafx.h"
#include "json_t.h"
#ifdef USE_BOOST_JSON
#include <cassert>
const boost::json::value& json_t::get_value(const boost::json::object& object, std::string key) {
auto* value = object.if_contains(key);
assert(value != nullptr);
return *value;
}
int64_t json_t::get_int64(const boost::json::value& value) {
assert(value.is_number());
assert(value.is_int64());
return value.get_int64();
}
uint16_t json_t::get_uint16(const boost::json::value& value) {
return static_cast<uint16_t>(get_int64(value));
}
uint8_t json_t::get_uint8(const boost::json::value& value) {
return static_cast<uint8_t>(get_int64(value));
}
int64_t json_t::get_int64(const boost::json::object& object, std::string key) {
return get_int64(get_value(object, key));
}
uint16_t json_t::get_uint16(const boost::json::object& object, std::string key) {
return static_cast<uint16_t>(get_int64(object, key));
}
uint8_t json_t::get_uint8(const boost::json::object& object, std::string key) {
return static_cast<uint8_t>(get_int64(object, key));
}
const boost::json::array& json_t::get_array(const boost::json::value& value) {
assert(value.is_array());
return value.get_array();
}
const boost::json::array& json_t::get_array(const boost::json::object& object, std::string key) {
return get_array(get_value(object, key));
}
const boost::json::string& json_t::get_string(const boost::json::value& value) {
assert(value.is_string());
return value.get_string();
}
const boost::json::string& json_t::get_string(const boost::json::object& object, std::string key) {
return get_string(get_value(object, key));
}
#endif

View File

@ -1,28 +0,0 @@
#pragma once
#ifdef USE_BOOST_JSON
# include <cstdint>
# include <string>
# include <boost/json.hpp>
#endif
class json_t {
#ifdef USE_BOOST_JSON
protected:
[[nodiscard]] static const boost::json::value& get_value(const boost::json::object& object, std::string key);
[[nodiscard]] static int64_t get_int64(const boost::json::value& value);
[[nodiscard]] static uint16_t get_uint16(const boost::json::value& value);
[[nodiscard]] static uint8_t get_uint8(const boost::json::value& value);
[[nodiscard]] static int64_t get_int64(const boost::json::object& object, std::string key);
[[nodiscard]] static uint16_t get_uint16(const boost::json::object& object, std::string key);
[[nodiscard]] static uint8_t get_uint8(const boost::json::object& object, std::string key);
[[nodiscard]] static const boost::json::array& get_array(const boost::json::value& value);
[[nodiscard]] static const boost::json::array& get_array(const boost::json::object& object, std::string key);
[[nodiscard]] static const boost::json::string& get_string(const boost::json::value& value);
[[nodiscard]] static const boost::json::string& get_string(const boost::json::object& object, std::string key);
#endif
};

View File

@ -32,11 +32,6 @@ void opcode_test_suite_t::load() {
#ifdef USE_BOOST_JSON
const boost::json::array& opcode_test_suite_t::get_array() const noexcept {
assert(raw().is_array());
return raw().get_array();
}
void opcode_test_suite_t::parse() {
m_raw = boost::json::parse(m_contents);
m_contents.clear();

View File

@ -65,10 +65,6 @@ public:
[[nodiscard]] constexpr const auto& raw() const noexcept { return m_raw; }
#endif
#ifdef USE_BOOST_JSON
[[nodiscard]] const boost::json::array& get_array() const noexcept;
#endif
void load(); // Reads into contents
void parse(); // Parse the contents
};

View File

@ -12,8 +12,7 @@ state_t::state_t(const boost::json::object& serialised) {
}
state_t::state_t(const boost::json::value& serialised) {
assert(serialised.is_object());
initialise(serialised.get_object());
initialise(serialised.as_object());
assert(initialised());
}
@ -21,20 +20,19 @@ void state_t::initialise(const boost::json::object& serialised) {
assert(!initialised());
m_pc = get_uint16(serialised, "pc");
m_s = get_uint8(serialised, "s");
m_a = get_uint8(serialised, "a");
m_x = get_uint8(serialised, "x");
m_y = get_uint8(serialised, "y");
m_p = get_uint8(serialised, "p");
m_pc = (uint16_t)serialised.at("pc").as_int64();
m_s = (uint8_t)serialised.at("s").as_int64();
m_a = (uint8_t)serialised.at("a").as_int64();
m_x = (uint8_t)serialised.at("x").as_int64();
m_y = (uint8_t)serialised.at("y").as_int64();
m_p = (uint8_t)serialised.at("p").as_int64();
const auto& ram_entries = get_array(serialised, "ram");
const auto& ram_entries = serialised.at("ram").as_array();
for (const auto& ram_entry : ram_entries) {
assert(ram_entry.is_array());
const auto& ram_entry_array = ram_entry.as_array();
assert(ram_entry_array.size() == 2);
const auto address = get_uint16(ram_entry_array[0]);
const auto value = get_uint8(ram_entry_array[1]);
const auto address = (uint16_t)ram_entry_array[0].as_int64();
const auto value = (uint8_t)ram_entry_array[1].as_int64();
m_ram[address] = value;
}

View File

@ -1,7 +1,7 @@
#pragma once
#include <cstdint>
#include <unordered_map>
#include <map>
#ifdef USE_BOOST_JSON
# include <boost/json.hpp>
@ -19,9 +19,7 @@
# include "simdjson/simdjson.h"
#endif
#include "json_t.h"
class state_t final : public json_t {
class state_t final {
private:
bool m_initialised = false;
@ -31,7 +29,7 @@ private:
uint8_t m_x = 0xff;
uint8_t m_y = 0xff;
uint8_t m_p = 0xff;
std::unordered_map<uint16_t, uint8_t> m_ram;
std::map<uint16_t, uint8_t> m_ram;
[[nodiscard]] constexpr auto initialised() const noexcept { return m_initialised; }

View File

@ -8,15 +8,15 @@
#include <stdexcept>
#include <string>
#include <tuple>
#include <unordered_map>
#include <map>
#include <vector>
//#define TEST_JSON_PERFORMANCE
#define USE_SIMDJSON_JSON // 15 seconds
//#define USE_BOOST_JSON // 32 seconds
//#define USE_NLOHMANN_JSON // 58 seconds
//#define USE_JSONCPP_JSON // 88 seconds
#define USE_SIMDJSON_JSON // 16 seconds
//#define USE_BOOST_JSON // 31 seconds
//#define USE_NLOHMANN_JSON // 73 seconds
//#define USE_JSONCPP_JSON // 105 seconds
#ifdef USE_BOOST_JSON
# include <boost/json.hpp>

View File

@ -26,25 +26,24 @@ test_t::test_t(const boost::json::object& serialised) {
}
test_t::test_t(const boost::json::value& serialised) {
assert(serialised.is_object());
initialise(serialised.get_object());
initialise(serialised.as_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"));
m_name = serialised.at("name").as_string();
m_initial_state = state_t(serialised.at("initial"));
m_final_state = state_t(serialised.at("final"));
const auto& cycles_array = get_array(serialised, "cycles");
const auto& cycles_array = serialised.at("cycles").as_array();
m_cycles.reserve(cycles_array.size());
for (const auto& cycles_entry : cycles_array) {
const auto& cycle_array = get_array(cycles_entry);
const auto& cycle_array = cycles_entry.as_array();
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]));
const auto address = (uint16_t)cycle_array[0].as_int64();
const auto contents = (uint8_t)cycle_array[1].as_int64();
const auto action = to_action((std::string)cycle_array[2].as_string());
m_cycles.push_back({ address, contents, action });
}
}

View File

@ -22,9 +22,8 @@
#endif
#include "state_t.h"
#include "json_t.h"
class test_t final : public json_t {
class test_t final {
public:
enum class action { read, write };

View File

@ -24,12 +24,15 @@ int main() {
opcode.parse();
#ifdef USE_BOOST_JSON
const auto& opcode_test_array = opcode.get_array();
assert(opcode.raw().is_array());
const auto& opcode_test_array = opcode.raw().as_array();
#endif
#ifdef USE_NLOHMANN_JSON
assert(opcode.raw().is_array());
const auto& opcode_test_array = opcode.raw();
#endif
#ifdef USE_JSONCPP_JSON
assert(opcode.raw().is_array());
const auto& opcode_test_array = opcode.raw();
#endif
#ifdef USE_SIMDJSON_JSON