Refactor Automation API.

This commit is contained in:
Philip Rideout
2020-09-30 16:17:23 -07:00
parent a736b8b018
commit 08f7973d0c
3 changed files with 72 additions and 26 deletions
+27 -11
View File
@@ -17,30 +17,46 @@
#ifndef VIEWER_AUTOMATION_H
#define VIEWER_AUTOMATION_H
#include <string>
#include <vector>
#include <viewer/Settings.h>
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<Settings> 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<AutomationSpec>* 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
+38 -8
View File
@@ -23,6 +23,8 @@
#include <assert.h>
#include <sstream>
#include <string>
#include <vector>
#include <utils/Log.h>
@@ -35,6 +37,15 @@ static const bool VERBOSE = false;
namespace filament {
namespace viewer {
struct SpecImpl {
std::string name;
std::vector<Settings> cases;
};
struct AutomationList::Impl {
std::vector<SpecImpl> 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<AutomationSpec>* out) {
static int parse(jsmntok_t const* tokens, int i, const char* jsonChunk, vector<SpecImpl>* 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<AutomationSpec>* 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<AutomationSpec>* 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
+7 -7
View File
@@ -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<AutomationSpec> 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) {