From 08f7973d0cc2b30f1cadd2ecee0386c9f47713b9 Mon Sep 17 00:00:00 2001 From: Philip Rideout Date: Wed, 30 Sep 2020 09:31:25 -0700 Subject: [PATCH] Refactor Automation API. --- libs/viewer/include/viewer/Automation.h | 38 ++++++++++++++------ libs/viewer/src/Automation.cpp | 46 ++++++++++++++++++++----- libs/viewer/tests/test_settings.cpp | 14 ++++---- 3 files changed, 72 insertions(+), 26 deletions(-) diff --git a/libs/viewer/include/viewer/Automation.h b/libs/viewer/include/viewer/Automation.h index 5eff6e5c09..9aeeb1836d 100644 --- a/libs/viewer/include/viewer/Automation.h +++ b/libs/viewer/include/viewer/Automation.h @@ -17,30 +17,46 @@ #ifndef VIEWER_AUTOMATION_H #define VIEWER_AUTOMATION_H -#include -#include - #include namespace filament { namespace viewer { -// Named list of settings permutations. +// Immutable list of settings permutations that are created and owned by AutomationList. struct AutomationSpec { - std::string name; - std::vector cases; + char const* name; + size_t count; + Settings const* settings; }; -// Consumes a JSON string and produces a list of automation specs using a counting algorithm. +// List of automation specs constructed from a JSON string. // // Each top-level item in the JSON is an object with "name", "base" and "permute". // The "base" object specifies a single set of changes to apply to default settings. // The optional "permute" object specifies a cross product of changes to apply to the base. // See the unit test for an example. -// -// - Returns true if successful. -// - This function writes warnings and error messages into the utils log. -bool generate(const char* jsonChunk, size_t size, std::vector* out); +class AutomationList { +public: + + // Parses a JSON spec, then generates a set of Settings lists. + // Returns null on failure (see utils log for warnings and errors). + // Clients should release memory using "delete". + static AutomationList* generate(const char* jsonChunk, size_t size); + + // Returns the number of generated Settings lists. + size_t size() const; + + // Returns a view onto a generated Settings list. + AutomationSpec get(size_t index) const; + + // Frees all Settings objects and name strings. + ~AutomationList(); + +private: + struct Impl; + AutomationList(Impl*); + Impl* mImpl; +}; } // namespace viewer } // namespace filament diff --git a/libs/viewer/src/Automation.cpp b/libs/viewer/src/Automation.cpp index e444660112..5c001230cd 100644 --- a/libs/viewer/src/Automation.cpp +++ b/libs/viewer/src/Automation.cpp @@ -23,6 +23,8 @@ #include #include +#include +#include #include @@ -35,6 +37,15 @@ static const bool VERBOSE = false; namespace filament { namespace viewer { +struct SpecImpl { + std::string name; + std::vector cases; +}; + +struct AutomationList::Impl { + std::vector specs; +}; + static int parse(jsmntok_t const* tokens, int i, const char* jsonChunk, std::string* val) { CHECK_TOKTYPE(tokens[i], JSMN_STRING); *val = STR(tokens[i], jsonChunk); @@ -100,7 +111,7 @@ static int parsePermutationsSpec(jsmntok_t const* tokens, int i, const char* jso } static int parseAutomationSpec(jsmntok_t const* tokens, int i, const char* jsonChunk, - AutomationSpec* out) { + SpecImpl* out) { CHECK_TOKTYPE(tokens[i], JSMN_OBJECT); int size = tokens[i++].size; Settings base; @@ -193,8 +204,7 @@ static int parseAutomationSpec(jsmntok_t const* tokens, int i, const char* jsonC return i; } -static int parse(jsmntok_t const* tokens, int i, const char* jsonChunk, - vector* out) { +static int parse(jsmntok_t const* tokens, int i, const char* jsonChunk, vector* out) { CHECK_TOKTYPE(tokens[i], JSMN_ARRAY); int size = tokens[i++].size; out->resize(size); @@ -204,12 +214,12 @@ static int parse(jsmntok_t const* tokens, int i, const char* jsonChunk, return i; } -bool generate(const char* jsonChunk, size_t size, vector* out) { +AutomationList* AutomationList::generate(const char* jsonChunk, size_t size) { jsmn_parser parser = { 0, 0, 0 }; int tokenCount = jsmn_parse(&parser, jsonChunk, size, nullptr, 0); if (tokenCount <= 0) { - return false; + return nullptr; } jsmntok_t* tokens = (jsmntok_t*) malloc(sizeof(jsmntok_t) * tokenCount); @@ -220,13 +230,33 @@ bool generate(const char* jsonChunk, size_t size, vector* out) { if (tokenCount <= 0) { free(tokens); - return false; + return nullptr; } - int i = parse(tokens, 0, jsonChunk, out); + AutomationList::Impl* impl = new AutomationList::Impl(); + int i = parse(tokens, 0, jsonChunk, &impl->specs); free(tokens); - return i >= 0; + + if (i < 0) { + delete impl; + return nullptr; + } + + return new AutomationList(impl); } +AutomationSpec AutomationList::get(size_t index) const { + const SpecImpl& spec = mImpl->specs[index]; + return { + .name = spec.name.c_str(), + .count = spec.cases.size(), + .settings = spec.cases.data() + }; +} + +size_t AutomationList::size() const { return mImpl->specs.size(); } +AutomationList::AutomationList(Impl* impl) : mImpl(impl) {} +AutomationList::~AutomationList() { delete mImpl; } + } // namespace viewer } // namespace filament diff --git a/libs/viewer/tests/test_settings.cpp b/libs/viewer/tests/test_settings.cpp index d5cee3696e..e8f3df4239 100644 --- a/libs/viewer/tests/test_settings.cpp +++ b/libs/viewer/tests/test_settings.cpp @@ -21,8 +21,6 @@ using namespace filament::viewer; -using std::vector; - class ViewSettingsTest : public testing::Test {}; static const char* JSON_TEST_DEFAULTS = R"TXT( @@ -169,11 +167,13 @@ TEST_F(ViewSettingsTest, JsonTestDefaults) { } TEST_F(ViewSettingsTest, AutomationSpec) { - vector specs; - ASSERT_TRUE(generate(JSON_AUTOMATION_TEST, strlen(JSON_AUTOMATION_TEST), &specs)); - ASSERT_EQ(specs.size(), 2); - ASSERT_EQ(specs[0].cases.size(), 1); - ASSERT_EQ(specs[1].cases.size(), 1 << 6); + AutomationList* specs = AutomationList::generate(JSON_AUTOMATION_TEST, + strlen(JSON_AUTOMATION_TEST)); + ASSERT_TRUE(specs); + ASSERT_EQ(specs->size(), 2); + ASSERT_EQ(specs->get(0).count, 1); + ASSERT_EQ(specs->get(1).count, 1 << 6); + delete specs; } int main(int argc, char** argv) {