refactor: Remove last usage of boost::optional

Since it's the only thing boost was used for at the moment
we drop it to be able to get rid of a dependency.
This commit is contained in:
Michael Carlberg 2016-12-15 17:13:15 +01:00
parent a0d485f79d
commit 9f9f438fae
11 changed files with 78 additions and 94 deletions

View File

@ -48,7 +48,6 @@ def DirectoryOfThisScript():
flags.append('-I'+ DirectoryOfThisScript() +'/src')
flags.append('-I'+ DirectoryOfThisScript() +'/include')
flags.append('-I'+ DirectoryOfThisScript() +'/lib/boost/include')
flags.append('-I'+ DirectoryOfThisScript() +'/lib/concurrentqueue/include')
flags.append('-I'+ DirectoryOfThisScript() +'/lib/i3ipcpp/include')
flags.append('-I'+ DirectoryOfThisScript() +'/lib/xpp/include')

View File

@ -1,6 +1,4 @@
#pragma once
#include <boost/optional.hpp>
#include <unordered_map>
#include "common.hpp"
@ -12,9 +10,6 @@
POLYBAR_NS
using boost::optional;
using boost::none;
#define GET_CONFIG_VALUE(section, var, name) var = m_conf.get<decltype(var)>(section, name, var)
#define REQ_CONFIG_VALUE(section, var, name) var = m_conf.get<decltype(var)>(section, name)
#define DEPR_CONFIG_VALUE(section, var, old, name) var = m_conf.deprecated<decltype(var)>(section, old, name, var)
@ -58,19 +53,11 @@ class config {
*/
template <typename T>
T get(const string& section, const string& key) const {
optional<T> value{opt<T>(section, key)};
if (value == none) {
auto it = m_sections.find(section);
if (it == m_sections.end() || it->second.find(key) == it->second.end()) {
throw key_error("Missing parameter [" + section + "." + key + "]");
}
string string_value{opt<string>(section, key).get()};
if (!string_value.empty()) {
return dereference<T>(section, key, opt<string>(section, key).get(), value.get());
}
return move(value.get());
return dereference<T>(section, key, it->second.at(key), convert<T>(string{it->second.at(key)}));
}
/**
@ -79,19 +66,13 @@ class config {
*/
template <typename T>
T get(const string& section, const string& key, const T& default_value) const {
optional<T> value{opt<T>(section, key)};
if (value == none) {
try {
T result{get<T>(section, key)};
string string_value{get<string>(section, key)};
return dereference<T>(move(section), move(key), move(string_value), move(result));
} catch (const key_error& err) {
return default_value;
}
string string_value{opt<string>(section, key).get()};
if (!string_value.empty()) {
return dereference<T>(section, key, opt<string>(section, key).get(), value.get());
}
return move(value.get());
}
/**
@ -107,23 +88,28 @@ class config {
*/
template <typename T>
vector<T> get_list(const string& section, const string& key) const {
vector<T> vec;
optional<T> value;
vector<T> results;
while ((value = opt<T>(section, key + "-" + to_string(vec.size()))) != none) {
string string_value{get<string>(section, key + "-" + to_string(vec.size()))};
while (true) {
try {
string string_value{get<string>(section, key + "-" + to_string(results.size()))};
T value{get<T>(section, key + "-" + to_string(results.size()))};
if (!string_value.empty()) {
vec.emplace_back(dereference<T>(section, key, move(string_value), move(value.get())));
} else {
vec.emplace_back(move(value.get()));
if (!string_value.empty()) {
results.emplace_back(dereference<T>(section, key, move(string_value), move(value)));
} else {
results.emplace_back(move(value));
}
} catch (const key_error& err) {
break;
}
}
if (vec.empty())
if (results.empty()) {
throw key_error("Missing parameter [" + section + "." + key + "-0]");
}
return vec;
return results;
}
/**
@ -132,23 +118,29 @@ class config {
*/
template <typename T>
vector<T> get_list(const string& section, const string& key, const vector<T>& default_value) const {
vector<T> vec;
optional<T> value;
vector<T> results;
while ((value = opt<T>(section, key + "-" + to_string(vec.size()))) != none) {
string string_value{get<string>(section, key + "-" + to_string(vec.size()))};
while (true) {
try {
string string_value{get<string>(section, key + "-" + to_string(results.size()))};
T value{get<T>(section, key + "-" + to_string(results.size()))};
if (!string_value.empty()) {
vec.emplace_back(dereference<T>(section, key, move(string_value), move(value.get())));
} else {
vec.emplace_back(move(value.get()));
if (!string_value.empty()) {
results.emplace_back(dereference<T>(section, key, move(string_value), move(value)));
} else {
results.emplace_back(move(value));
}
} catch (const key_error& err) {
break;
}
}
if (vec.empty())
return default_value;
else
return vec;
if (!results.empty()) {
return results;
;
}
return default_value;
}
/**
@ -188,18 +180,6 @@ class config {
template <typename T>
T convert(string&& value) const;
template <typename T>
const optional<T> opt(const string& section, const string& key) const {
sectionmap_t::const_iterator s;
valuemap_t::const_iterator v;
if ((s = m_sections.find(section)) != m_sections.end() && (v = s->second.find(key)) != s->second.end()) {
return {convert<T>(string{v->second})};
}
return none;
}
/**
* Dereference value reference
*/
@ -232,20 +212,19 @@ class config {
template <typename T>
T dereference_local(string section, const string& key, const string& current_section) const {
if (section == "BAR") {
m_logger.warn("${BAR.key} is deprecated. Use ${root.key} instead");
m_log.warn("${BAR.key} is deprecated. Use ${root.key} instead");
}
section = string_util::replace(section, "BAR", this->section(), 0, 3);
section = string_util::replace(section, "root", this->section(), 0, 4);
section = string_util::replace(section, "self", current_section, 0, 4);
optional<T> result{opt<T>(section, key)};
if (result == none) {
try {
T result{get<T>(section, key)};
return dereference<T>(string(section), move(key), get<string>(section, key), move(result));
} catch (const key_error& err) {
throw value_error("Unexisting reference defined [" + section + "." + key + "]");
}
return dereference<T>(section, key, opt<string>(section, key).get(), result.get());
}
/**
@ -265,14 +244,13 @@ class config {
if (env_util::has(var.c_str())) {
string env_value{env_util::get(var.c_str())};
m_logger.info("Found matching environment variable ${" + var + "} with the value \"" + env_value + "\"");
m_log.info("Found matching environment variable ${" + var + "} with the value \"" + env_value + "\"");
return convert<T>(move(env_value));
}
if (!env_default.empty()) {
m_logger.info("The environment variable ${" + var + "} is undefined or empty, using defined fallback value \"" + env_default + "\"");
} else if (!env_default.empty()) {
m_log.info("The environment variable ${" + var + "} is undefined or empty, using defined fallback value \"" +
env_default + "\"");
} else {
m_logger.info("The environment variable ${" + var + "} is undefined or empty");
m_log.info("The environment variable ${" + var + "} is undefined or empty");
}
return convert<T>(move(env_default));
@ -298,7 +276,7 @@ class config {
private:
static constexpr const char* KEY_INHERIT{"inherit"};
const logger& m_logger;
const logger& m_log;
const xresource_manager& m_xrm;
string m_file;
string m_barname;

View File

@ -105,6 +105,7 @@ class renderer
xcb_rectangle_t m_cleared{0, 0, 0U, 0U};
reserve_area m_cleararea{};
uint8_t m_depth{32};
xcb_window_t m_window;
xcb_colormap_t m_colormap;
xcb_visualtype_t* m_visual;

View File

@ -96,7 +96,6 @@ struct bar_settings {
bar_settings(const bar_settings& other) = default;
xcb_window_t window{XCB_NONE};
monitor_t monitor{};
edge origin{edge::TOP};
struct size size {

View File

@ -3,7 +3,6 @@
#include <X11/X.h>
#include <X11/Xlib-xcb.h>
#include <xcb/xcb.h>
#include <boost/optional.hpp>
#include <iomanip>
#include "common.hpp"
@ -74,7 +73,7 @@ class connection : public xpp_connection {
void send_dummy_event(xcb_window_t target, uint32_t event = XCB_EVENT_MASK_STRUCTURE_NOTIFY) const;
boost::optional<xcb_visualtype_t*> visual_type(xcb_screen_t* screen, int match_depth = 32);
xcb_visualtype_t* visual_type(xcb_screen_t* screen, int match_depth = 32);
static string error_str(int error_code);

View File

@ -42,7 +42,6 @@ endif()
# Locate dependencies {{{
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Boost REQUIRED)
find_package(Threads REQUIRED)
find_package(X11 REQUIRED COMPONENTS Xft Xutil)
find_package(X11_XCB REQUIRED)
@ -51,9 +50,8 @@ pkg_check_modules(FONTCONFIG REQUIRED fontconfig)
set(APP_LIBRARIES ${APP_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
set(APP_LIBRARIES ${APP_LIBRARIES} ${X11_Xft_LIB})
set(APP_LIBRARIES ${APP_LIBRARIES} ${FONTCONFIG_LIBRARIES})
#set(APP_LIBRARIES ${APP_LIBRARIES} ${FONTCONFIG_LIBRARIES})
set(APP_INCLUDE_DIRS ${APP_INCLUDE_DIRS} ${Boost_INCLUDE_DIR})
set(APP_INCLUDE_DIRS ${APP_INCLUDE_DIRS} ${FONTCONFIG_INCLUDE_DIRS})
set(APP_INCLUDE_DIRS ${APP_INCLUDE_DIRS} ${PROJECT_SOURCE_DIR}/include)
set(APP_INCLUDE_DIRS ${APP_INCLUDE_DIRS} ${PROJECT_SOURCE_DIR}/lib/concurrentqueue/include)

View File

@ -26,7 +26,7 @@ config::make_type config::make(string path, string bar) {
* Construct config object
*/
config::config(const logger& logger, const xresource_manager& xrm, string&& path, string&& bar)
: m_logger(logger), m_xrm(xrm), m_file(forward<string>(path)), m_barname(forward<string>(bar)) {
: m_log(logger), m_xrm(xrm), m_file(forward<string>(path)), m_barname(forward<string>(bar)) {
if (!file_util::exists(m_file)) {
throw application_error("Could not find config file: " + m_file);
}
@ -45,8 +45,8 @@ config::config(const logger& logger, const xresource_manager& xrm, string&& path
throw application_error("Undefined bar: " + m_barname);
}
m_logger.trace("config: Loaded %s", m_file);
m_logger.trace("config: Current bar section: [%s]", section());
m_log.trace("config: Loaded %s", m_file);
m_log.trace("config: Current bar section: [%s]", section());
}
/**
@ -69,7 +69,7 @@ string config::section() const {
void config::warn_deprecated(const string& section, const string& key, string replacement) const {
try {
auto value = get<string>(section, key);
m_logger.warn("The config parameter `%s.%s` is deprecated, use `%s` instead.", section, key, move(replacement));
m_log.warn("The config parameter `%s.%s` is deprecated, use `%s` instead.", section, key, move(replacement));
} catch (const key_error& err) {
}
}
@ -149,7 +149,7 @@ void config::copy_inherited() {
throw value_error("[" + section.first + "." + KEY_INHERIT + "] invalid reference \"" + inherit + "\"");
}
m_logger.trace("config: Copying missing params (sub=\"%s\", base=\"%s\")", section.first, inherit);
m_log.trace("config: Copying missing params (sub=\"%s\", base=\"%s\")", section.first, inherit);
// Iterate the base and copy the parameters
// that hasn't been defined for the sub-section
@ -191,13 +191,13 @@ template <>
bool config::convert(string&& value) const {
string lower{string_util::lower(forward<string>(value))};
if (lower.compare(0, 4, "true") == 0) {
if (lower == "true") {
return true;
} else if (lower.compare(0, 3, "yes") == 0) {
} else if (lower == "yes") {
return true;
} else if (lower.compare(0, 2, "on") == 0) {
} else if (lower == "on") {
return true;
} else if (lower.compare(0, 1, "1") == 0) {
} else if (lower == "1") {
return true;
} else {
return false;

View File

@ -44,7 +44,17 @@ renderer::renderer(connection& conn, signal_emitter& emitter, const logger& logg
m_sig.attach(this);
m_log.trace("renderer: Get TrueColor visual");
m_visual = m_connection.visual_type(m_connection.screen(), 32).get();
if ((m_visual = m_connection.visual_type(m_connection.screen(), 32)) == nullptr) {
m_log.err("No 32-bit TrueColor visual found...");
m_depth = 24;
}
if ((m_visual = m_connection.visual_type(m_connection.screen(), 24)) == nullptr) {
m_log.err("No 24-bit TrueColor visual found, aborting...");
}
if (m_visual == nullptr) {
throw application_error("No matching TrueColor visual found...");
}
m_log.trace("renderer: Allocate colormap");
m_colormap = m_connection.generate_id();

View File

@ -1,4 +1,3 @@
#include <boost/algorithm/string/replace.hpp>
#include <sstream>
#include <utility>

View File

@ -179,7 +179,7 @@ void connection::send_dummy_event(xcb_window_t target, uint32_t event) const {
* Try to get a visual type for the given screen that
* matches the given depth
*/
boost::optional<xcb_visualtype_t*> connection::visual_type(xcb_screen_t* screen, int match_depth) {
xcb_visualtype_t* connection::visual_type(xcb_screen_t* screen, int match_depth) {
xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(screen);
if (depth_iter.data) {
for (; depth_iter.rem; xcb_depth_next(&depth_iter)) {
@ -193,7 +193,7 @@ boost::optional<xcb_visualtype_t*> connection::visual_type(xcb_screen_t* screen,
return visual_type(screen, 0);
}
}
return {};
return nullptr;
}
/**

View File

@ -1,4 +1,5 @@
#include <utility>
#include <algorithm>
#include "components/types.hpp"
#include "utils/string.hpp"