forked from pytorch/ELF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptionMap.cc
49 lines (39 loc) · 1.26 KB
/
OptionMap.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Copyright (c) 2018-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
#include "OptionMap.h"
#include <stdexcept>
using nlohmann::json;
namespace elf {
namespace options {
void OptionMap::registerPy(pybind11::module& m) {
namespace py = pybind11;
py::class_<OptionMap>(m, "OptionMap")
.def(py::init<OptionSpec>())
.def(py::init<const OptionMap&>())
.def("getOptionSpec", &OptionMap::getOptionSpec)
.def("getJSONString", &OptionMap::getJSONString)
.def("loadJSONString", &OptionMap::loadJSONString)
.def("getAsJSONString", &OptionMap::getAsJSONString)
.def("setAsJSONString", &OptionMap::setAsJSONString);
}
void OptionMap::loadJSON(const json& data) {
for (auto it = data.begin(); it != data.end(); ++it) {
data_[it.key()] = it.value();
}
}
const json& OptionMap::getAsJSON(const std::string& optionName) const {
const auto& elem = data_.find(optionName);
if (elem == data_.end()) {
throw std::runtime_error(optionName + " has not been set!");
}
return elem.value();
}
} // namespace options
} // namespace elf