tylerliu | bcd8348 | 2020-10-07 14:45:28 -0700 | [diff] [blame] | 1 | // |
| 2 | // Created by Tyler on 10/6/20. |
| 3 | // |
| 4 | |
| 5 | #include <iosfwd> |
| 6 | #include "assignment-or.hpp" |
| 7 | |
| 8 | namespace ndn { |
| 9 | namespace ndncert { |
| 10 | |
tylerliu | bcd8348 | 2020-10-07 14:45:28 -0700 | [diff] [blame] | 11 | NDNCERT_REGISTER_FUNCFACTORY(AssignmentOr, "or"); |
| 12 | |
| 13 | AssignmentOr::AssignmentOr() |
| 14 | : NameAssignmentFuncFactory("or") |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | NameAssignmentFunc |
| 19 | AssignmentOr::getFunction(std::list<NameAssignmentFunc> funcs){ |
| 20 | if (funcs.size() == 1) return *funcs.begin(); |
| 21 | return OrAssignmentFunc(funcs); |
| 22 | } |
| 23 | |
| 24 | NameAssignmentFunc |
| 25 | AssignmentOr::getFunction(const std::string &factoryParam) { |
| 26 | std::list<NameAssignmentFunc> paramList; |
| 27 | std::stringstream ss; |
| 28 | ss << factoryParam; |
| 29 | JsonSection section; |
| 30 | try { |
| 31 | boost::property_tree::read_json(ss, section); |
| 32 | } |
| 33 | catch (const std::exception& error) { |
| 34 | BOOST_THROW_EXCEPTION(std::runtime_error(std::string("Failed to parse configuration for name assignment function or, ") + error.what())); |
| 35 | } |
| 36 | if (section.begin() == section.end()) { |
| 37 | BOOST_THROW_EXCEPTION(std::runtime_error("No JSON configuration found for name assignment function")); |
| 38 | } |
| 39 | for (const auto& item: section) { |
| 40 | auto factory = NameAssignmentFuncFactory::createNameAssignmentFuncFactory(item.first); |
| 41 | if (!factory) { |
| 42 | BOOST_THROW_EXCEPTION(std::runtime_error("Invalid assignment factory type")); |
| 43 | } |
| 44 | try { |
| 45 | paramList.push_back(factory->getFunction(item.second.data())); |
| 46 | } catch (const std::exception& e) { |
| 47 | BOOST_THROW_EXCEPTION(std::runtime_error("Error on creating function")); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return getFunction(paramList); |
| 52 | } |
| 53 | |
| 54 | AssignmentOr::OrAssignmentFunc::OrAssignmentFunc(std::list<NameAssignmentFunc> funcList) |
| 55 | : m_funcList(std::move(funcList)) |
| 56 | {} |
| 57 | |
| 58 | std::vector<PartialName> |
| 59 | AssignmentOr::OrAssignmentFunc::operator() (const std::vector<std::tuple<std::string, std::string>> params) |
| 60 | { |
| 61 | std::vector<PartialName> nameList; |
| 62 | for (const auto& func : m_funcList) { |
| 63 | auto result = func(params); |
| 64 | nameList.insert(nameList.end(), result.begin(), result.end()); |
| 65 | } |
| 66 | |
| 67 | return nameList; |
| 68 | } |
| 69 | |
| 70 | } |
| 71 | } |