2 // Created by m on 7/27/17.
7 #include "json_helper.hpp"
13 surface_id_to_layer::surface_id_to_layer(nlohmann::json const &j) {
15 if (j["type"] == "range") {
16 this->id_min = get<int>(j["first_surface_id"]);
17 this->id_max = get<int>(j["last_surface_id"]);
19 this->id_min = this->id_max = get<int>(j["surface_id"]);
21 this->name = j["name"].get<std::string>();
22 this->layer_id = get<int>(j["layer_id"]);
25 struct result<struct surface_id_to_layer_map> to_surface_id_to_layer_map(
26 nlohmann::json const &j) {
29 surface_id_to_layer_map stl{};
31 std::cbegin(j), std::cend(j),
32 std::inserter(stl.mapping, stl.mapping.end()),
33 [](nlohmann::json const &j) { return surface_id_to_layer(j); });
34 for (auto i : stl.mapping) {
36 return Err<struct surface_id_to_layer_map>(
37 "Found mapping w/o name");
39 if (i.layer_id == -1 || i.id_min == -1 || i.id_max == -1) {
40 return Err<struct surface_id_to_layer_map>(
41 "Found invalid/unset IDs in mapping");
45 } catch (std::exception &e) {
46 return Err<struct surface_id_to_layer_map>(e.what());
50 // Helper to allow std::lower_bound with a int key only
52 operator<(struct surface_id_to_layer const &a, int b) {
56 optional<int> surface_id_to_layer_map::get_layer_for_surface(int surface_id) {
57 auto i = std::lower_bound(std::cbegin(this->mapping),
58 std::cend(this->mapping), surface_id);
60 if (i != this->mapping.end()) {
61 // std::less only checks for surface_id_to_layer::id_max, so check
62 // that we are actually inside of an interval here.
63 if (i->id_min <= surface_id) {
64 return optional<int>(i->layer_id);