1 /*
2  * Copyright (C) 2018 Red Hat, Inc.
3  *
4  * Licensed under the GNU Lesser General Public License Version 2.1
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "OptionBinds.hpp"
22 
23 #include "bgettext/bgettext-lib.h"
24 #include "tinyformat/tinyformat.hpp"
25 
26 #include <utility>
27 
28 namespace libdnf {
29 
30 // ========== OptionBinds::Item class ===============
31 
Item(Option & option,const NewStringFunc & newString,const GetValueStringFunc & getValueString,bool addValue)32 OptionBinds::Item::Item(Option & option, const NewStringFunc & newString,
33     const GetValueStringFunc & getValueString, bool addValue)
34 : option(&option), newStr(newString), getValueStr(getValueString), addValue(addValue) {}
35 
Item(Option & option,NewStringFunc && newString,GetValueStringFunc && getValueString,bool addValue)36 OptionBinds::Item::Item(Option & option, NewStringFunc && newString,
37     GetValueStringFunc && getValueString, bool addValue)
38 : option(&option), newStr(std::move(newString)), getValueStr(std::move(getValueString)), addValue(addValue) {}
39 
Item(Option & option)40 OptionBinds::Item::Item(Option & option)
41 : option(&option) {}
42 
getPriority() const43 Option::Priority OptionBinds::Item::getPriority() const
44 {
45     return option->getPriority();
46 }
47 
newString(Option::Priority priority,const std::string & value)48 void OptionBinds::Item::newString(Option::Priority priority, const std::string & value)
49 {
50     if (newStr)
51         newStr(priority, value);
52     else
53         option->set(priority, value);
54 }
55 
getValueString() const56 std::string OptionBinds::Item::getValueString() const
57 {
58     if (getValueStr)
59         return getValueStr();
60     else
61         return option->getValueString();
62 }
63 
getAddValue() const64 bool OptionBinds::Item::getAddValue() const
65 {
66     return addValue;
67 }
68 
getOption() const69 const Option & OptionBinds::Item::getOption() const
70 {
71     return *option;
72 }
73 
getOption()74 Option & OptionBinds::Item::getOption()
75 {
76     return *option;
77 }
78 
79 // =========== OptionBinds class ===============
80 
what() const81 const char * OptionBinds::OutOfRange::what() const noexcept
82 {
83     try {
84         if (tmpMsg.empty())
85             tmpMsg = tfm::format(_("Configuration: OptionBinding with id \"%s\" does not exist"),
86                 Exception::what());
87         return tmpMsg.c_str();
88     } catch (...) {
89         return Exception::what();
90     }
91 }
92 
what() const93 const char * OptionBinds::AlreadyExists::what() const noexcept
94 {
95     try {
96         if (tmpMsg.empty())
97             tmpMsg = tfm::format(_("Configuration: OptionBinding with id \"%s\" already exists"),
98                 Exception::what());
99         return tmpMsg.c_str();
100     } catch (...) {
101         return Exception::what();
102     }
103 }
104 
at(const std::string & id)105 OptionBinds::Item & OptionBinds::at(const std::string & id)
106 {
107     auto item = items.find(id);
108     if (item == items.end())
109         throw OutOfRange(id);
110     return item->second;
111 }
112 
at(const std::string & id) const113 const OptionBinds::Item & OptionBinds::at(const std::string & id) const
114 {
115     auto item = items.find(id);
116     if (item == items.end())
117         throw OutOfRange(id);
118     return item->second;
119 }
120 
add(const std::string & id,Option & option,const Item::NewStringFunc & newString,const Item::GetValueStringFunc & getValueString,bool addValue)121 OptionBinds::Item & OptionBinds::add(const std::string & id, Option & option,
122     const Item::NewStringFunc & newString, const Item::GetValueStringFunc & getValueString, bool addValue)
123 {
124     auto item = items.find(id);
125     if (item != items.end())
126         throw AlreadyExists(id);
127     auto res = items.emplace(id, Item(option, newString, getValueString, addValue));
128     return res.first->second;
129 }
130 
add(const std::string & id,Option & option,Item::NewStringFunc && newString,Item::GetValueStringFunc && getValueString,bool addValue)131 OptionBinds::Item & OptionBinds::add(const std::string & id, Option & option,
132     Item::NewStringFunc && newString, Item::GetValueStringFunc && getValueString, bool addValue)
133 {
134     auto item = items.find(id);
135     if (item != items.end())
136         throw AlreadyExists(id);
137     auto res = items.emplace(id, Item(option, std::move(newString), std::move(getValueString), addValue));
138     return res.first->second;
139 }
140 
add(const std::string & id,Option & option)141 OptionBinds::Item & OptionBinds::add(const std::string & id, Option & option)
142 {
143     auto item = items.find(id);
144     if (item != items.end())
145         throw AlreadyExists(id);
146     auto res = items.emplace(id, Item(option));
147     return res.first->second;
148 }
149 
150 }
151