2 * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 // Created by m on 7/27/17.
23 #include "json_helper.hpp"
29 using json = nlohmann::json;
31 layer::layer(nlohmann::json const &j) {
33 if (j["type"] == "range") {
34 this->id_min = get<int>(j["first_surface_id"]);
35 this->id_max = get<int>(j["last_surface_id"]);
37 this->id_min = this->id_max = get<int>(j["surface_id"]);
39 this->name = j["name"].get<std::string>();
40 this->layer_id = get<int>(j["layer_id"]);
41 this->rect = genivi::full_rect;
42 if (j["area"]["type"] == "rect") {
43 auto jr = j["area"]["rect"];
44 this->rect = genivi::rect{
45 get<int32_t>(jr["width"]), get<int32_t>(jr["height"]),
46 get<int32_t>(jr["x"]), get<int32_t>(jr["y"]),
51 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
55 auto m = j["mappings"];
56 stl.layers.reserve(m.size());
57 std::transform(std::cbegin(m), std::cend(m),
58 std::inserter(stl.mapping, stl.mapping.end()),
59 [&stl](nlohmann::json const &j) {
61 stl.layers.push_back(unsigned(k.layer_id));
64 // XXX need to sort layers?
65 for (auto i : stl.mapping) {
67 return Err<struct layer_map>("Found mapping w/o name");
69 if (i.layer_id == -1 || i.id_min == -1 || i.id_max == -1) {
70 return Err<struct layer_map>("Found invalid/unset IDs in mapping");
75 auto jtests = j.value("tests", json());
77 if (!jtests.empty()) {
78 DB("Embedded tests...");
79 std::vector<std::pair<int, int>> tests;
80 tests.reserve(jtests.size());
81 std::transform(std::cbegin(jtests), std::cend(jtests),
82 std::back_inserter(tests), [](json const &j) {
83 return std::make_pair(
84 get<int>(j["surface_id"]),
85 get<int>(j["expect_layer_id"]));
88 for (auto sid : tests) {
89 int lid = stl.get_layer_id(sid.first).value_or(-1);
90 DB("this=" << sid.first << ", that=" << lid
91 << ", expect=" << sid.second);
92 if (lid != sid.second) {
93 return Err<layer_map>("ID Map embedded test failed!");
99 } catch (std::exception &e) {
100 return Err<struct layer_map>(e.what());
104 // Helper to allow std::lower_bound with a int key only
106 operator<(struct layer const &a, int b) {
111 optional<layer> get_surface_id_to_layer(struct layer_map const *s2l,
113 auto i = std::lower_bound(std::cbegin(s2l->mapping), std::cend(s2l->mapping),
116 if (i != s2l->mapping.end()) {
117 // std::less only checks for layer::id_max, so check
118 // that we are actually inside of an interval here.
119 if (i->id_min <= surface_id) {
120 return optional<layer>(*i);
128 optional<int> layer_map::get_layer_id(int surface_id) {
129 auto e = get_surface_id_to_layer(this, surface_id);
130 return e ? optional<int>(e->layer_id) : nullopt;
133 optional<genivi::rect> layer_map::get_layer_rect(int surface_id) {
134 auto e = get_surface_id_to_layer(this, surface_id);
135 return e ? optional<genivi::rect>(e->rect) : nullopt;
138 json layer::to_json() const {
139 auto is_full = this->rect == genivi::full_rect;
143 r = {{"type", "full"}};
145 r = {{"type", "rect"},
147 {{"x", this->rect.x},
149 {"width", this->rect.w},
150 {"height", this->rect.h}}}};
154 {"id_min", this->id_min},
155 {"id_max", this->id_max},
156 {"name", this->name},
157 {"layer_id", this->layer_id},
162 json layer_map::to_json() const {
164 for (auto const &i: this->mapping) {
165 j.push_back(i.to_json());