mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2024-11-23 00:33:44 +00:00
Extract string conversion functions from SettingsBag.
It's useful to have this functionality elsewhere, especially the string to bool one which I'm going to use in the next commit.
This commit is contained in:
parent
9fc9cf851c
commit
8d19c794ed
5 changed files with 101 additions and 41 deletions
|
@ -19,6 +19,7 @@ add_library(${PROJECT_NAME} STATIC
|
|||
error_response.cpp
|
||||
quick_submit_paste_response.cpp
|
||||
storage.cpp
|
||||
string_conv.cpp
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
#include <ciso646>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace kamokan {
|
||||
|
@ -60,41 +59,4 @@ namespace kamokan {
|
|||
assert(m_defaults.find(parKey) == m_defaults.end());
|
||||
m_defaults[parKey] = parValue;
|
||||
}
|
||||
|
||||
template <>
|
||||
std::string SettingsBag::as (boost::string_view parIndex) const {
|
||||
auto& setting = this->at(parIndex);
|
||||
return std::string(setting);
|
||||
}
|
||||
|
||||
template <>
|
||||
bool SettingsBag::as (boost::string_view parIndex) const {
|
||||
auto& setting = this->at(parIndex);
|
||||
if (setting == "true" or setting == "yes" or setting == "1" or setting == "on") {
|
||||
return true;
|
||||
}
|
||||
else if (setting == "false" or setting == "no" or setting == "0" or setting == "off") {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
std::ostringstream oss;
|
||||
oss << "Bad conversion: can't convert \"" << setting << "\" to bool";
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
uint16_t SettingsBag::as (boost::string_view parIndex) const {
|
||||
return dhandy::lexical_cast<uint16_t>(this->at(parIndex));
|
||||
}
|
||||
|
||||
template <>
|
||||
uint32_t SettingsBag::as (boost::string_view parIndex) const {
|
||||
return dhandy::lexical_cast<uint32_t>(this->at(parIndex));
|
||||
}
|
||||
|
||||
template std::string SettingsBag::as<std::string> (boost::string_view parIndex) const;
|
||||
template bool SettingsBag::as<bool> (boost::string_view parIndex) const;
|
||||
template uint16_t SettingsBag::as<uint16_t> (boost::string_view parIndex) const;
|
||||
template uint32_t SettingsBag::as<uint32_t> (boost::string_view parIndex) const;
|
||||
} //namespace kamokan
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#if defined(SPDLOG_DEBUG_ON)
|
||||
# include "spdlog.hpp"
|
||||
#endif
|
||||
#include "string_conv.hpp"
|
||||
#include <map>
|
||||
#include <boost/utility/string_view.hpp>
|
||||
#include <functional>
|
||||
|
@ -45,9 +46,9 @@ namespace kamokan {
|
|||
const IniFile::KeyValueMapType* m_values;
|
||||
};
|
||||
|
||||
template <>
|
||||
inline boost::string_view SettingsBag::as (boost::string_view parIndex) const {
|
||||
return (*this)[parIndex];
|
||||
template <typename T>
|
||||
inline T SettingsBag::as (boost::string_view parIndex) const {
|
||||
return string_conv<T>(this->at(parIndex));
|
||||
}
|
||||
|
||||
inline const boost::string_view& SettingsBag::at (boost::string_view parIndex) const {
|
||||
|
|
38
src/kamokan_impl/string_conv.cpp
Normal file
38
src/kamokan_impl/string_conv.cpp
Normal file
|
@ -0,0 +1,38 @@
|
|||
/* Copyright 2017, Michele Santullo
|
||||
* This file is part of "kamokan".
|
||||
*
|
||||
* "kamokan" is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* "kamokan" is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with "kamokan". If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "string_conv.hpp"
|
||||
#include <sstream>
|
||||
|
||||
namespace kamokan {
|
||||
template <>
|
||||
bool string_conv (boost::string_view parStr) {
|
||||
if (parStr == "true" or parStr == "yes" or parStr == "1" or parStr == "on") {
|
||||
return true;
|
||||
}
|
||||
else if (parStr == "false" or parStr == "no" or parStr == "0" or parStr == "off") {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
std::ostringstream oss;
|
||||
oss << "Bad conversion: can't convert \"" << parStr << "\" to bool";
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
template bool string_conv<bool> (boost::string_view parStr);
|
||||
} //namespace kamokan
|
58
src/kamokan_impl/string_conv.hpp
Normal file
58
src/kamokan_impl/string_conv.hpp
Normal file
|
@ -0,0 +1,58 @@
|
|||
/* Copyright 2017, Michele Santullo
|
||||
* This file is part of "kamokan".
|
||||
*
|
||||
* "kamokan" is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* "kamokan" is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with "kamokan". If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "duckhandy/lexical_cast.hpp"
|
||||
#include <boost/utility/string_view.hpp>
|
||||
#include <type_traits>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace kamokan {
|
||||
template <typename T>
|
||||
[[gnu::pure]]
|
||||
T string_conv (boost::string_view parStr);
|
||||
|
||||
template <>
|
||||
[[gnu::pure]]
|
||||
bool string_conv (boost::string_view parStr);
|
||||
|
||||
template <>
|
||||
[[gnu::always_inline]] inline
|
||||
boost::string_view string_conv (boost::string_view parStr) {
|
||||
return parStr;
|
||||
}
|
||||
|
||||
template <>
|
||||
[[gnu::pure,gnu::always_inline]] inline
|
||||
uint16_t string_conv (boost::string_view parStr) {
|
||||
return dhandy::lexical_cast<uint16_t>(parStr);
|
||||
}
|
||||
|
||||
template <>
|
||||
[[gnu::pure,gnu::always_inline]] inline
|
||||
uint32_t string_conv (boost::string_view parStr) {
|
||||
return dhandy::lexical_cast<uint32_t>(parStr);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline
|
||||
std::string string_conv (boost::string_view parStr) {
|
||||
return std::string(parStr.data(), parStr.size());
|
||||
}
|
||||
} //namespace kamokan
|
Loading…
Reference in a new issue