2 // Created by m on 7/27/17.
7 #include "json_helper.hpp"
13 using json = nlohmann::json;
15 layer::layer(nlohmann::json const &j) {
17 if (j["type"] == "range") {
18 this->id_min = get<int>(j["first_surface_id"]);
19 this->id_max = get<int>(j["last_surface_id"]);
21 this->id_min = this->id_max = get<int>(j["surface_id"]);
23 this->name = j["name"].get<std::string>();
24 this->layer_id = get<int>(j["layer_id"]);
25 this->rect = genivi::full_rect;
26 if (j["area"]["type"] == "rect") {
27 auto jr = j["area"]["rect"];
28 this->rect = genivi::rect{
29 get<int32_t>(jr["width"]), get<int32_t>(jr["height"]),
30 get<int32_t>(jr["x"]), get<int32_t>(jr["y"]),
35 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
39 auto m = j["mappings"];
40 stl.layers.reserve(m.size());
41 std::transform(std::cbegin(m), std::cend(m),
42 std::inserter(stl.mapping, stl.mapping.end()),
43 [&stl](nlohmann::json const &j) {
45 stl.layers.push_back(unsigned(k.layer_id));
48 // XXX need to sort layers?
49 for (auto i : stl.mapping) {
51 return Err<struct layer_map>("Found mapping w/o name");
53 if (i.layer_id == -1 || i.id_min == -1 || i.id_max == -1) {
54 return Err<struct layer_map>("Found invalid/unset IDs in mapping");
59 auto jtests = j.value("tests", json());
61 if (!jtests.empty()) {
62 DB("Embedded tests...");
63 std::vector<std::pair<int, int>> tests;
64 tests.reserve(jtests.size());
65 std::transform(std::cbegin(jtests), std::cend(jtests),
66 std::back_inserter(tests), [](json const &j) {
67 return std::make_pair(
68 get<int>(j["surface_id"]),
69 get<int>(j["expect_layer_id"]));
72 for (auto sid : tests) {
73 int lid = stl.get_layer_id(sid.first).value_or(-1);
74 DB("this=" << sid.first << ", that=" << lid
75 << ", expect=" << sid.second);
76 if (lid != sid.second) {
77 return Err<layer_map>("ID Map embedded test failed!");
83 } catch (std::exception &e) {
84 return Err<struct layer_map>(e.what());
88 // Helper to allow std::lower_bound with a int key only
90 operator<(struct layer const &a, int b) {
95 optional<layer> get_surface_id_to_layer(struct layer_map const *s2l,
97 auto i = std::lower_bound(std::cbegin(s2l->mapping), std::cend(s2l->mapping),
100 if (i != s2l->mapping.end()) {
101 // std::less only checks for layer::id_max, so check
102 // that we are actually inside of an interval here.
103 if (i->id_min <= surface_id) {
104 return optional<layer>(*i);
112 optional<int> layer_map::get_layer_id(int surface_id) {
113 auto e = get_surface_id_to_layer(this, surface_id);
114 return e ? optional<int>(e->layer_id) : nullopt;
117 optional<genivi::rect> layer_map::get_layer_rect(int surface_id) {
118 auto e = get_surface_id_to_layer(this, surface_id);
119 return e ? optional<genivi::rect>(e->rect) : nullopt;
122 json layer::to_json() const {
123 auto is_full = this->rect == genivi::full_rect;
127 r = {{"type", "full"}};
129 r = {{"type", "rect"},
131 {{"x", this->rect.x},
133 {"width", this->rect.w},
134 {"height", this->rect.h}}}};
138 {"id_min", this->id_min},
139 {"id_max", this->id_max},
140 {"name", this->name},
141 {"layer_id", this->layer_id},
146 json layer_map::to_json() const {
148 for (auto const &i: this->mapping) {
149 j.push_back(i.to_json());