Mass replace all std::optional with utils::optional

Also import bad_optional_access into the utils namespace so we can catch the exception when we need to
This commit is contained in:
Celtic Minstrel 2024-07-23 09:19:05 -04:00
parent ffde1cb21c
commit f6ae8ab850
45 changed files with 101 additions and 97 deletions

View File

@ -96,11 +96,11 @@ const credits_data& get_credits_data()
return parsed_credits_data;
}
std::optional<credits_data::const_iterator> get_campaign_credits(const std::string& campaign)
utils::optional<credits_data::const_iterator> get_campaign_credits(const std::string& campaign)
{
const auto res = std::find_if(parsed_credits_data.begin(), parsed_credits_data.end(),
const credits_data::const_iterator res = std::find_if(parsed_credits_data.begin(), parsed_credits_data.end(),
[&campaign](const credits_group& group) { return group.id == campaign; });
return res != parsed_credits_data.end() ? std::optional{res} : std::nullopt;
return res != parsed_credits_data.end() ? utils::make_optional(res) : utils::nullopt;
}
std::vector<std::string> get_background_images(const std::string& campaign)

View File

@ -17,7 +17,7 @@
#include "tstring.hpp"
#include <optional>
#include "utils/optional_fwd.hpp"
#include <string>
#include <utility>
#include <vector>
@ -60,7 +60,7 @@ using credits_data = std::vector<credits_group>;
const credits_data& get_credits_data();
/** Gets credits for a given campaign. Returns a null optional if that campaign has no credits. */
std::optional<credits_data::const_iterator> get_campaign_credits(const std::string& campaign);
utils::optional<credits_data::const_iterator> get_campaign_credits(const std::string& campaign);
/** Gets credit background images for a given campaign. */
std::vector<std::string> get_background_images(const std::string& campaign);

View File

@ -50,7 +50,7 @@
#include "whiteboard/manager.hpp"
#include "wml_exception.hpp"
#include <optional>
#include "utils/optional_fwd.hpp"
static lg::log_domain log_engine("engine");
#define DBG_NG LOG_STREAM(debug, log_engine)

View File

@ -15,7 +15,7 @@
#pragma once
#include <optional>
#include "utils/optional_fwd.hpp"
#include "map/location.hpp"
#include "synced_context.hpp"
#include "config.hpp"
@ -23,7 +23,7 @@
namespace actions {
struct undo_event {
std::optional<int> lua_idx;
utils::optional<int> lua_idx;
config commands, data;
map_location loc1, loc2, filter_loc1, filter_loc2;
std::size_t uid1, uid2;

View File

@ -289,7 +289,7 @@ inline std::shared_ptr<std::vector<target> > lua_object< std::vector<target> >::
lua_pushstring(L, "type"); // st n + 2
lua_rawget(L, -2); // st n + 2
std::optional<ai_target::type> type = ai_target::type::xplicit;
utils::optional<ai_target::type> type = ai_target::type::xplicit;
if(lua_isnumber(L, -1)) {
int target = static_cast<int>(lua_tointeger(L, -1));
type = ai_target::get_enum(target); // st n + 2

View File

@ -328,7 +328,7 @@ auto get_child_impl(Tchildren& children, config_key_type key, int n) -> optional
auto i = children.find(key);
if(i == children.end()) {
DBG_CF << "The config object has no child named »" << key << "«.";
return std::nullopt;
return utils::nullopt;
}
if(n < 0) {
@ -340,7 +340,7 @@ auto get_child_impl(Tchildren& children, config_key_type key, int n) -> optional
} catch(const std::out_of_range&) {
DBG_CF << "The config object has only »" << i->second.size() << "« children named »" << key
<< "«; request for the index »" << n << "« cannot be honored.";
return std::nullopt;
return utils::nullopt;
}
}
@ -421,7 +421,7 @@ optional_config_impl<const config> config::get_deprecated_child(config_key_type
return res;
}
return std::nullopt;
return utils::nullopt;
}
config::const_child_itors config::get_deprecated_child_range(config_key_type old_key, const std::string& in_tag, DEP_LEVEL level, const std::string& message) const
@ -791,7 +791,7 @@ optional_config config::find_child(config_key_type key, const std::string& name,
DBG_CF << "Key »" << name << "« value »" << value << "« pair not found as child of key »" << key << "«.";
return std::nullopt;
return utils::nullopt;
}
const child_list::iterator j = std::find_if(i->second.begin(), i->second.end(),
@ -807,7 +807,7 @@ optional_config config::find_child(config_key_type key, const std::string& name,
DBG_CF << "Key »" << name << "« value »" << value << "« pair not found as child of key »" << key << "«.";
return std::nullopt;
return utils::nullopt;
}
config& config::find_mandatory_child(config_key_type key, const std::string &name, const std::string &value)

View File

@ -62,7 +62,7 @@ public:
{
}
optional_config_impl(std::nullopt_t)
optional_config_impl(utils::nullopt_t)
: opt_()
{
}
@ -73,7 +73,7 @@ public:
return *opt_;
} else {
// We're going to drop this codepath once we can use optional::value anyway, but just
// noting we want this function to ultimately throw std::bad_optional_access.
// noting we want this function to ultimately throw utils::bad_optional_access.
throw std::runtime_error("Optional reference has no value");
}
}

View File

@ -2640,7 +2640,7 @@ void display::draw_invalidated()
DBG_DP << "drawing " << invalidated_.size() << " invalidated hexes with clip " << clip_rect;
// The unit drawer can't function without teams
std::optional<unit_drawer> drawer{};
utils::optional<unit_drawer> drawer{};
if(!dc_->teams().empty()) {
drawer.emplace(*this);
}
@ -2903,7 +2903,7 @@ void display::refresh_report(const std::string& report_name, const config * new_
// Now we will need the config. Generate one if needed.
utils::optional_reference<events::mouse_handler> mhb = std::nullopt;
utils::optional_reference<events::mouse_handler> mhb = utils::nullopt;
if (resources::controller) {
mhb = resources::controller->get_mouse_handler_base();

View File

@ -497,7 +497,7 @@ protected:
private:
std::string addon_id_;
std::optional<config> previous_cfg_;
utils::optional<config> previous_cfg_;
std::string scenario_id_, scenario_name_, scenario_description_;
utils::optional<int> xp_mod_;

View File

@ -15,7 +15,7 @@
#pragma once
#include <array>
#include <optional>
#include "utils/optional_fwd.hpp"
#include <string>
#include <string_view>
#include <tuple>
@ -52,30 +52,30 @@ struct enum_base : public Definition
* Converts a string into its enum equivalent.
*
* @param value The string value to convert.
* @return The equivalent enum or std::nullopt.
* @return The equivalent enum or utils::nullopt.
*/
static constexpr std::optional<enum_type> get_enum(const std::string_view value)
static constexpr utils::optional<enum_type> get_enum(const std::string_view value)
{
for(unsigned int i = 0; i < size(); i++) {
if(value == Definition::values[i]) {
return static_cast<enum_type>(i);
}
}
return std::nullopt;
return utils::nullopt;
}
/**
* Converts an int into its enum equivalent.
*
* @param value The string value to convert.
* @return The equivalent enum or std::nullopt.
* @return The equivalent enum or utils::nullopt.
*/
static constexpr std::optional<enum_type> get_enum(unsigned long value)
static constexpr utils::optional<enum_type> get_enum(unsigned long value)
{
if(value < size()) {
return static_cast<enum_type>(value);
} else {
return std::nullopt;
return utils::nullopt;
}
}

View File

@ -668,7 +668,7 @@ static utils::optional<std::string> get_games_path()
{
PWSTR docs_path = nullptr;
HRESULT res = SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_CREATE, nullptr, &docs_path);
utils::optional<std::string> path = std::nullopt;
utils::optional<std::string> path = utils::nullopt;
if(res == S_OK) {
bfs::path games_path = bfs::path(docs_path) / "My Games";

View File

@ -23,7 +23,7 @@
#include "units/id.hpp"
#include "utils/optional_fwd.hpp"
#include <optional>
#include "utils/optional_fwd.hpp"
#include <set>
#include <vector>

View File

@ -256,7 +256,7 @@ struct filter_attack : public event_filter {
auto temp_other_weapon = event_info.data.optional_child(!first_ ? "first" : "second");
const_attack_ptr second_attack = temp_other_weapon ? std::make_shared<const attack_type>(*temp_other_weapon) : nullptr;
auto ctx = attack->specials_context(u, opp, loc, loc_d, first_, second_attack);
std::optional<decltype(ctx)> opp_ctx;
utils::optional<decltype(ctx)> opp_ctx;
if(second_attack){
opp_ctx.emplace(second_attack->specials_context(opp, u, loc_d, loc, !first_, attack));
}

View File

@ -24,7 +24,7 @@
#include "hotkey/hotkey_command.hpp"
#include "variable.hpp"
#include <optional>
#include "utils/optional_fwd.hpp"
class filter_context;
class game_data;
@ -162,7 +162,7 @@ private:
const std::string hotkey_id_;
/** Controls the lifetime of the associate hotkey's hotkey_command. */
std::optional<hotkey::wml_hotkey_record> hotkey_record_;
utils::optional<hotkey::wml_hotkey_record> hotkey_record_;
/** The image to display in the menu next to this item's description. */
std::string image_;

View File

@ -42,7 +42,7 @@
#include "wesnothd_connection.hpp"
#include <functional>
#include <optional>
#include "utils/optional_fwd.hpp"
#include <thread>
static lg::log_domain log_mp("mp/main");
@ -67,7 +67,7 @@ public:
friend void mp::send_to_server(const config&);
friend mp::lobby_info* mp::get_lobby_info();
mp_manager(const std::optional<std::string> host);
mp_manager(const utils::optional<std::string> host);
~mp_manager()
{
@ -157,7 +157,7 @@ public:
}
};
mp_manager::mp_manager(const std::optional<std::string> host)
mp_manager::mp_manager(const utils::optional<std::string> host)
: network_worker()
, stop(false)
, connection(nullptr)
@ -682,7 +682,7 @@ void start_local_game()
prefs::get().set_message_private(false);
mp_manager(std::nullopt).enter_create_mode();
mp_manager(utils::nullopt).enter_create_mode();
}
void start_local_game_commandline(const commandline_options& cmdline_opts)

View File

@ -26,7 +26,7 @@
#include "tstring.hpp"
#include <cassert>
#include <optional>
#include "utils/optional_fwd.hpp"
namespace gui2
{
@ -127,7 +127,7 @@ private:
*
* If without value, there's no formula.
*/
std::optional<std::string> formula_;
utils::optional<std::string> formula_;
/** If there's no formula it contains the value. */
T value_;

View File

@ -140,7 +140,7 @@ struct builder_grid : public builder_widget
/** Inherited from @ref builder_widget. */
virtual std::unique_ptr<widget> build(const replacements_map& replacements) const override;
void build(grid& grid, optional_replacements replacements = std::nullopt) const;
void build(grid& grid, optional_replacements replacements = utils::nullopt) const;
};
using builder_grid_ptr = std::shared_ptr<builder_grid>;

View File

@ -22,7 +22,7 @@
#include <atomic>
#include <future>
#include <map>
#include <optional>
#include "utils/optional_fwd.hpp"
#include <vector>
namespace cursor
@ -137,7 +137,7 @@ private:
label* progress_stage_label_;
drawing* animation_;
std::optional<decltype(std::chrono::steady_clock::now())> animation_start_;
utils::optional<decltype(std::chrono::steady_clock::now())> animation_start_;
std::atomic<loading_stage> current_stage_;

View File

@ -87,7 +87,7 @@ void network_transmission::pre_show(window& window)
void network_transmission::post_show(window& /*window*/)
{
pump_monitor_.window_ = std::nullopt;
pump_monitor_.window_ = utils::nullopt;
if(get_retval() == retval::CANCEL) {
connection_->cancel();

View File

@ -167,11 +167,11 @@ public:
static const std::string& type();
/** Optionally returns the node definition with the given id, or nullopt if not found. */
std::optional<decltype(node_definitions_)::const_iterator> get_node_definition(const std::string& id) const
utils::optional<decltype(node_definitions_)::const_iterator> get_node_definition(const std::string& id) const
{
const auto def = std::find_if(
node_definitions_.begin(), node_definitions_.end(), [&id](const auto& d) { return d.id == id; });
return def != node_definitions_.end() ? std::optional{def} : std::nullopt;
return def != node_definitions_.end() ? utils::make_optional(def) : utils::nullopt;
}
private:

View File

@ -165,7 +165,7 @@ const topic *help_menu::chosen_topic()
void help_menu::display_visible_items()
{
std::vector<gui::indented_menu_item> menu_items;
std::optional<std::size_t> selected;
utils::optional<std::size_t> selected;
for(std::vector<visible_item>::const_iterator items_it = visible_items_.begin(),
end = visible_items_.end(); items_it != end; ++items_it) {
if (selected_item_ == *items_it)

View File

@ -1055,13 +1055,13 @@ std::pair<int, bool> parse_scale_value(std::string_view arg)
}
/** Common helper function to parse scaling IPF inputs. */
std::optional<std::pair<point, uint8_t>> parse_scale_args(std::string_view args)
utils::optional<std::pair<point, uint8_t>> parse_scale_args(std::string_view args)
{
const auto scale_params = utils::split_view(args, ',', utils::STRIP_SPACES);
const std::size_t num_args = scale_params.size();
if(num_args == 0 || (num_args == 1 && scale_params[0].empty())) {
return std::nullopt;
return utils::nullopt;
}
uint8_t flags = 0;

View File

@ -62,7 +62,7 @@ static std::mutex log_mutex;
static bool log_sanitization = true;
/** whether the current logs directory is writable */
static std::optional<bool> is_log_dir_writable_ = std::nullopt;
static utils::optional<bool> is_log_dir_writable_ = utils::nullopt;
/** alternative stream to write data to */
static std::ostream *output_stream_ = nullptr;
@ -274,7 +274,7 @@ void set_log_to_file()
}
}
std::optional<bool> log_dir_writable()
utils::optional<bool> log_dir_writable()
{
return is_log_dir_writable_;
}

View File

@ -52,7 +52,7 @@
#endif
#include <iosfwd> // needed else all files including log.hpp need to do it.
#include <optional>
#include "utils/optional_fwd.hpp"
#include <string>
#include <utility>
#include <ctime>
@ -162,7 +162,7 @@ void check_log_dir_writable();
*
* @return true if the log directory is writable, false otherwise.
*/
std::optional<bool> log_dir_writable();
utils::optional<bool> log_dir_writable();
/**
* Use the defined prefix and suffix to determine if a filename is a log file.

View File

@ -113,7 +113,7 @@ public:
t_translation::ter_list old_;
t_translation::ter_list new_;
terrain_type_data::merge_mode mode_;
std::optional<t_translation::terrain_code> terrain_;
utils::optional<t_translation::terrain_code> terrain_;
bool use_old_;
bool replace_if_failed_;

View File

@ -131,10 +131,10 @@ T& locator::access_in_cache(cache_type<T>& cache) const
}
template<typename T>
std::optional<T> locator::copy_from_cache(cache_type<T>& cache) const
utils::optional<T> locator::copy_from_cache(cache_type<T>& cache) const
{
const auto& elem = cache.get_element(val_);
return elem.loaded ? std::optional{elem.item} : std::nullopt;
return elem.loaded ? utils::make_optional(elem.item) : utils::nullopt;
}
template<typename T>

View File

@ -18,7 +18,7 @@
#include "map/location.hpp"
#include "terrain/translation.hpp"
#include <optional>
#include "utils/optional_fwd.hpp"
class surface;
class texture;
@ -118,7 +118,7 @@ public:
const T& locate_in_cache(cache_type<T>& cache) const;
template<typename T>
std::optional<T> copy_from_cache(cache_type<T>& cache) const;
utils::optional<T> copy_from_cache(cache_type<T>& cache) const;
template<typename T>
void add_to_cache(cache_type<T>& cache, T data) const;

View File

@ -341,7 +341,7 @@ namespace lua_check_impl
lua_check(lua_State *L, int n)
{
std::string str = lua_check_impl::lua_check<std::string>(L, n);
std::optional<typename T::type> val = T::get_enum(str);
utils::optional<typename T::type> val = T::get_enum(str);
if(!val) {
luaL_argerror(L, n, ("cannot convert " + str + " to enum.").c_str());
}

View File

@ -25,7 +25,7 @@
#include <iosfwd>
#include <map>
#include <string>
#include <optional>
#include "utils/optional_fwd.hpp"
#include <vector>
class config_writer;

View File

@ -500,19 +500,19 @@ void schema_validator::validate(const config& cfg, const std::string& name, int
}
}
std::optional<std::map<std::string, wml_key>> schema_validator::find_mandatory_keys(
utils::optional<std::map<std::string, wml_key>> schema_validator::find_mandatory_keys(
const wml_tag* tag, const config& cfg) const
{
auto visited = std::vector<const wml_tag*>();
return find_mandatory_keys(tag, cfg, visited);
}
std::optional<std::map<std::string, wml_key>> schema_validator::find_mandatory_keys(
utils::optional<std::map<std::string, wml_key>> schema_validator::find_mandatory_keys(
const wml_tag* tag, const config& cfg, std::vector<const wml_tag*>& visited) const
{
// Return an empty optional if a super cycle is detected.
if(std::find(visited.begin(), visited.end(), tag) != visited.end()) {
return std::nullopt;
return utils::nullopt;
}
visited.push_back(tag);
@ -525,7 +525,7 @@ std::optional<std::map<std::string, wml_key>> schema_validator::find_mandatory_k
// Return an empty optional if a super cycle is detected.
if(!super_mandatory_keys) {
return std::nullopt;
return utils::nullopt;
}
super_mandatory_keys->merge(mandatory_keys);

View File

@ -22,7 +22,7 @@
#include "serialization/validator.hpp"
#include <boost/graph/adjacency_list.hpp>
#include <optional>
#include "utils/optional_fwd.hpp"
#include <stack>
#include <string>
@ -181,8 +181,8 @@ private:
*
* The returned map can contain non-mandatory keys if they are overriden, please check the is_mandatory() result.
*/
std::optional<std::map<std::string, wml_key>> find_mandatory_keys(const wml_tag* tag, const config& cfg) const;
std::optional<std::map<std::string, wml_key>> find_mandatory_keys(
utils::optional<std::map<std::string, wml_key>> find_mandatory_keys(const wml_tag* tag, const config& cfg) const;
utils::optional<std::map<std::string, wml_key>> find_mandatory_keys(
const wml_tag* tag, const config& cfg, std::vector<const wml_tag*>& visited) const;
/**

View File

@ -27,7 +27,7 @@
#include "utils/general.hpp"
#include <array>
#include <limits>
#include <optional>
#include "utils/optional_fwd.hpp"
#include <stdexcept>
#include <boost/algorithm/string.hpp>
@ -834,9 +834,9 @@ namespace
* Internal common code for parse_range and parse_range_real.
*
* If str contains two elements and a separator such as "a-b", returns a and b.
* Otherwise, returns the original string and std::nullopt.
* Otherwise, returns the original string and utils::nullopt.
*/
std::pair<std::string, std::optional<std::string>> parse_range_internal_separator(const std::string& str)
std::pair<std::string, utils::optional<std::string>> parse_range_internal_separator(const std::string& str)
{
// If turning this into a list with additional options, ensure that "-" (if present) is last. Otherwise a
// range such as "-2..-1" might be incorrectly split as "-2..", "1".
@ -852,7 +852,7 @@ std::pair<std::string, std::optional<std::string>> parse_range_internal_separato
return {str.substr(0, pos), str.substr(pos + length)};
}
return {str, std::nullopt};
return {str, utils::nullopt};
}
} // namespace

View File

@ -230,12 +230,12 @@ bool have_wml(const utils::optional_reference<const config>& cfg)
* Null WML objects are skipped.
*/
template<typename... Vals>
std::optional<std::vector<std::string>> multi_find_illegal_names(const Vals&... args)
utils::optional<std::vector<std::string>> multi_find_illegal_names(const Vals&... args)
{
std::vector<std::string> names;
((args && check_names_legal(*args, &names)), ...);
return !names.empty() ? std::optional(names) : std::nullopt;
return !names.empty() ? utils::optional(names) : utils::nullopt;
}
/**
@ -244,12 +244,12 @@ std::optional<std::vector<std::string>> multi_find_illegal_names(const Vals&...
* Null WML objects are skipped.
*/
template<typename... Vals>
std::optional<std::vector<std::string>> multi_find_case_conflicts(const Vals&... args)
utils::optional<std::vector<std::string>> multi_find_case_conflicts(const Vals&... args)
{
std::vector<std::string> names;
((args && check_case_insensitive_duplicates(*args, &names)), ...);
return !names.empty() ? std::optional(names) : std::nullopt;
return !names.empty() ? utils::optional(names) : utils::nullopt;
}
/**

View File

@ -192,7 +192,7 @@ public:
struct event_info {
config cmds_;
std::optional<int> lua_;
utils::optional<int> lua_;
game_events::queued_event evt_;
event_info(const config& cmds, game_events::queued_event evt) : cmds_(cmds), evt_(evt) {}
event_info(int lua, game_events::queued_event evt) : lua_(lua), evt_(evt) {}

View File

@ -1997,8 +1997,8 @@ effect::effect(const unit_ability_list& list, int def, const_attack_ptr att, EFF
individual_effect set_effect_max;
individual_effect set_effect_min;
std::optional<int> max_value = std::nullopt;
std::optional<int> min_value = std::nullopt;
utils::optional<int> max_value = utils::nullopt;
utils::optional<int> min_value = utils::nullopt;
for (const unit_ability & ability : list) {
const config& cfg = *ability.ability_cfg;

View File

@ -622,7 +622,7 @@ void unit_attack(display * disp, game_board & board,
int def_hitpoints = defender.hitpoints();
const_attack_ptr weapon = attack.shared_from_this();
auto ctx = weapon->specials_context(attacker.shared_from_this(), defender.shared_from_this(), a, b, attacking, secondary_attack);
std::optional<decltype(ctx)> opp_ctx;
utils::optional<decltype(ctx)> opp_ctx;
if(secondary_attack) {
opp_ctx.emplace(secondary_attack->specials_context(defender.shared_from_this(), attacker.shared_from_this(), b, a, !attacking, weapon));

View File

@ -25,7 +25,7 @@
#include "utils/variant.hpp"
#include <bitset>
#include <optional>
#include "utils/optional_fwd.hpp"
class team;
class unit_animation_component;

View File

@ -74,7 +74,7 @@ bool utils::config_filters::unsigned_matches_if_present(const config& filter, co
return in_ranges<int>(cfg[attribute].to_int(0), utils::parse_ranges_unsigned(filter[attribute].str()));
}
bool utils::config_filters::int_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, std::optional<int> def)
bool utils::config_filters::int_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, utils::optional<int> def)
{
if(!filter.has_attribute(attribute)) {
return true;
@ -92,7 +92,7 @@ bool utils::config_filters::int_matches_if_present(const config& filter, const c
}
bool utils::config_filters::int_matches_if_present_or_negative(
const config& filter, const config& cfg, const std::string& attribute, const std::string& opposite, std::optional<int> def)
const config& filter, const config& cfg, const std::string& attribute, const std::string& opposite, utils::optional<int> def)
{
if(int_matches_if_present(filter, cfg, attribute, def)) {
return true;
@ -115,7 +115,7 @@ bool utils::config_filters::int_matches_if_present_or_negative(
return false;
}
bool utils::config_filters::double_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, std::optional<double> def)
bool utils::config_filters::double_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, utils::optional<double> def)
{
if(!filter.has_attribute(attribute)) {
return true;

View File

@ -40,8 +40,8 @@ bool bool_matches_if_present(const config& filter, const config& cfg, const std:
*
* Always returns true if the filter puts no restriction on the value of @a cfg[@a attribute].
*/
bool double_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, std::optional<double> def = std::nullopt);
bool int_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, std::optional<int> def = std::nullopt);
bool double_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, utils::optional<double> def = utils::nullopt);
bool int_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, utils::optional<int> def = utils::nullopt);
/**
* Restricts filters to only looking for values that are zero or more.
@ -62,7 +62,7 @@ bool unsigned_matches_if_present(const config& filter, const config& cfg, const
* The function is named "negative" in case we later want to add a "reciprocal" for the "multiply"/"divide" pair.
*/
bool int_matches_if_present_or_negative(
const config& filter, const config& cfg, const std::string& attribute, const std::string& opposite, std::optional<int> def = std::nullopt);
const config& filter, const config& cfg, const std::string& attribute, const std::string& opposite, utils::optional<int> def = utils::nullopt);
bool string_matches_if_present(
const config& filter, const config& cfg, const std::string& attribute, const std::string& def);

View File

@ -13,18 +13,18 @@
*/
#include <charconv>
#include <optional>
#include "utils/optional_fwd.hpp"
#include <string_view>
namespace utils
{
template<typename T>
std::optional<T> from_chars(std::string_view str, int base = 10)
utils::optional<T> from_chars(std::string_view str, int base = 10)
{
static_assert(std::is_integral_v<T>, "Float support for charconv incomplete on current build requirements");
T result {};
const auto [_, ec] = std::from_chars(str.data(), str.data() + str.size(), result, base);
return ec == std::errc{} ? std::optional{result} : std::nullopt;
return ec == std::errc{} ? utils::make_optional(result) : utils::nullopt;
}
} // namespace utils

View File

@ -28,11 +28,15 @@ namespace utils
using std::optional;
using std::make_optional;
using std::nullopt;
using std::nullopt_t;
using std::bad_optional_access;
#else
using boost::optional;
using boost::make_optional;
using boost::bad_optional_access;
using nullopt_t = boost::none_t;
// Create a new nullopt object equivalent to boost::none to match the STL interface
static const boost::none_t nullopt{boost::none_t::init_tag{}};

View File

@ -15,15 +15,15 @@
#pragma once
#include <functional>
#include <optional>
#include "utils/optional_fwd.hpp"
namespace utils
{
/**
* A simple wrapper class for optional reference types.
*
* Since std::optional (as of C++17 at least) does not support reference types (see [1]),
* the only way to use those is std::optional<std::reference_wrapper<T>>. However, this makes
* Since utils::optional (as of C++17 at least) does not support reference types (see [1]),
* the only way to use those is utils::optional<std::reference_wrapper<T>>. However, this makes
* the interace messy, as to access the referenced object you need an extra get() call to
* access the value stored in the reference wrapper.
*
@ -45,7 +45,7 @@ public:
{
}
optional_reference(std::nullopt_t)
optional_reference(utils::nullopt_t)
: opt_()
{
}
@ -59,7 +59,7 @@ public:
return opt_->get();
} else {
// We're going to drop this codepath once we can use optional::value anyway, but just
// noting we want this function to ultimately throw std::bad_optional_access.
// noting we want this function to ultimately throw utils::bad_optional_access.
throw std::runtime_error("Optional reference has no value");
}
#endif
@ -97,7 +97,7 @@ public:
}
private:
std::optional<std::reference_wrapper<T>> opt_;
utils::optional<std::reference_wrapper<T>> opt_;
};
} // namespace utils

View File

@ -67,7 +67,7 @@
#include <boost/program_options/errors.hpp> // for error
#include <boost/algorithm/string/predicate.hpp> // for checking cmdline options
#include <optional>
#include "utils/optional_fwd.hpp"
#include <algorithm> // for transform
#include <cerrno> // for ENOMEM

View File

@ -118,7 +118,7 @@ void menu::set_inner_location(const SDL_Rect& /*rect*/)
update_scrollbar_grip_height();
}
void menu::set_items(const std::vector<indented_menu_item>& items, std::optional<std::size_t> selected)
void menu::set_items(const std::vector<indented_menu_item>& items, utils::optional<std::size_t> selected)
{
const bool scrolled_to_max = (has_scrollbar() && get_position() == get_max_position());
items_.clear();

View File

@ -15,7 +15,7 @@
#pragma once
#include <optional>
#include "utils/optional_fwd.hpp"
#include <map>
#include <set>
@ -134,7 +134,7 @@ public:
* to keep the selection at the same position as it were before the items
* were set.
*/
virtual void set_items(const std::vector<indented_menu_item>& items, std::optional<std::size_t> selected);
virtual void set_items(const std::vector<indented_menu_item>& items, utils::optional<std::size_t> selected);
/**
* Set a new max height for this menu. Note that this does not take
@ -201,7 +201,7 @@ private:
/**
* Cached return value of widest_row_width(), calculated on demand when calling that function.
*/
mutable std::optional<int> widest_row_width_;
mutable utils::optional<int> widest_row_width_;
std::size_t selected_;
bool click_selects_;