Skip to content

Commit

Permalink
Migrate from cata::optional<T> to std::optional<T> (CleverRaven#64333)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Granade <[email protected]>
  • Loading branch information
BrettDong and kevingranade authored Mar 21, 2023
1 parent 9d1bb28 commit 32774b3
Show file tree
Hide file tree
Showing 305 changed files with 2,809 additions and 3,054 deletions.
1 change: 1 addition & 0 deletions msvc-full-features/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <mutex>
#include <new>
#include <numeric>
#include <optional>
#include <ostream>
#include <queue>
#include <random>
Expand Down
2 changes: 1 addition & 1 deletion object_creator/spell_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ creator::spell_window::spell_window( QWidget *parent, Qt::WindowFlags flags )
std::string selected = field_id_box.currentText().toStdString();
field_type_id field;
if( selected == "NONE" ) {
editable_spell.field = cata::nullopt;
editable_spell.field = std::nullopt;
} else {
editable_spell.field = field_type_id( selected );
}
Expand Down
11 changes: 9 additions & 2 deletions pch/main-pch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <chrono>
#include <climits>
Expand All @@ -29,10 +29,10 @@
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <filesystem>
#include <forward_list>
#include <fstream>
#include <functional>
Expand All @@ -47,16 +47,23 @@
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <optional>
#include <ostream>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <string_view>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeinfo>
Expand Down
16 changes: 8 additions & 8 deletions src/achievement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ struct achievement_requirement {
achievement_comparison comparison;
cata_variant target;
requirement_visibility visibility = requirement_visibility::always;
cata::optional<translation> description;
std::optional<translation> description;

bool becomes_false = false; // NOLINT(cata-serialize)

Expand Down Expand Up @@ -477,14 +477,14 @@ void achievement::check() const
}
}

static cata::optional<std::string> text_for_requirement(
static std::optional<std::string> text_for_requirement(
const achievement_requirement &req,
const cata_variant &current_value,
achievement_completion ach_completed )
{
bool is_satisfied = req.satisfied_by( current_value );
if( !req.is_visible( ach_completed, is_satisfied ) ) {
return cata::nullopt;
return std::nullopt;
}
nc_color c = is_satisfied ? c_green : c_yellow;
std::string result;
Expand All @@ -506,12 +506,12 @@ static cata::optional<std::string> text_for_requirement(
return colorize( result, c );
}

static std::string format_requirements( const std::vector<cata::optional<std::string>> &req_texts,
static std::string format_requirements( const std::vector<std::optional<std::string>> &req_texts,
nc_color c )
{
bool some_missing = false;
std::string result;
for( const cata::optional<std::string> &req_text : req_texts ) {
for( const std::optional<std::string> &req_text : req_texts ) {
if( req_text ) {
result += " " + *req_text + "\n";
} else {
Expand Down Expand Up @@ -548,7 +548,7 @@ class requirement_watcher : stat_watcher
return requirement_->satisfied_by( current_value_ );
}

cata::optional<std::string> ui_text() const {
std::optional<std::string> ui_text() const {
return text_for_requirement( *requirement_, current_value_,
achievement_completion::pending );
}
Expand Down Expand Up @@ -617,7 +617,7 @@ std::string achievement_state::ui_text( const achievement *ach ) const
// If these two vectors are of different sizes then the definition must
// have changed since it was completed / failed, so we don't print any
// requirements info.
std::vector<cata::optional<std::string>> req_texts;
std::vector<std::optional<std::string>> req_texts;
if( final_values.size() == reqs.size() ) {
for( size_t i = 0; i < final_values.size(); ++i ) {
req_texts.push_back( text_for_requirement( reqs[i], final_values[i], completion ) );
Expand Down Expand Up @@ -750,7 +750,7 @@ std::string achievement_tracker::ui_text() const
}

// Next: the requirements
std::vector<cata::optional<std::string>> req_texts;
std::vector<std::optional<std::string>> req_texts;
for( const std::unique_ptr<requirement_watcher> &watcher : watchers_ ) {
req_texts.push_back( watcher->ui_text() );
}
Expand Down
6 changes: 3 additions & 3 deletions src/achievement.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#include <iosfwd>
#include <memory>
#include <new>
#include <optional>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include "calendar.h"
#include "cata_variant.h"
#include "event_subscriber.h"
#include "optional.h"
#include "translations.h"
#include "type_id.h"

Expand Down Expand Up @@ -110,7 +110,7 @@ class achievement
time_duration period_;
};

const cata::optional<time_bound> &time_constraint() const {
const std::optional<time_bound> &time_constraint() const {
return time_constraint_;
}

Expand All @@ -129,7 +129,7 @@ class achievement
// if the achievement is given by an EOC
bool manually_given_ = false;
std::vector<achievement_id> hidden_by_;
cata::optional<time_bound> time_constraint_;
std::optional<time_bound> time_constraint_;
std::vector<achievement_requirement> requirements_;
};

Expand Down
28 changes: 14 additions & 14 deletions src/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <iterator>
#include <memory>
#include <new>
#include <optional>
#include <string>
#include <utility>

Expand All @@ -27,7 +28,6 @@
#include "mapdata.h"
#include "memory_fast.h"
#include "messages.h"
#include "optional.h"
#include "options.h"
#include "output.h"
#include "path_info.h"
Expand Down Expand Up @@ -539,13 +539,13 @@ std::string press_x( action_id act, const std::string &key_bound_pre,
input_context ctxt = get_default_mode_input_context();
return ctxt.press_x( action_ident( act ), key_bound_pre, key_bound_suf, key_unbound );
}
cata::optional<std::string> press_x_if_bound( action_id act )
std::optional<std::string> press_x_if_bound( action_id act )
{
input_context ctxt = get_default_mode_input_context();
std::string description = action_ident( act );
if( ctxt.keys_bound_to( description, /*maximum_modifier_count=*/ -1,
/*restrict_to_printable=*/false ).empty() ) {
return cata::nullopt;
return std::nullopt;
}
return press_x( act );
}
Expand Down Expand Up @@ -603,14 +603,14 @@ point get_delta_from_movement_action( const action_id act, const iso_rotate rot
}
}

cata::optional<input_event> hotkey_for_action( const action_id action,
std::optional<input_event> hotkey_for_action( const action_id action,
const int maximum_modifier_count, const bool restrict_to_printable )
{
const std::vector<input_event> keys = keys_bound_to( action,
maximum_modifier_count,
restrict_to_printable );
if( keys.empty() ) {
return cata::nullopt;
return std::nullopt;
} else {
return keys.front();
}
Expand Down Expand Up @@ -1048,7 +1048,7 @@ action_id handle_main_menu()
REGISTER_ACTION( ACTION_HELP );

// The hotkey is reserved for the uilist keybindings menu
entries.emplace_back( ACTION_KEYBINDINGS, true, cata::nullopt,
entries.emplace_back( ACTION_KEYBINDINGS, true, std::nullopt,
ctxt.get_action_name( action_ident( ACTION_KEYBINDINGS ) ) );

REGISTER_ACTION( ACTION_OPTIONS );
Expand Down Expand Up @@ -1076,7 +1076,7 @@ action_id handle_main_menu()
}
}

cata::optional<tripoint> choose_direction( const std::string &message, const bool allow_vertical )
std::optional<tripoint> choose_direction( const std::string &message, const bool allow_vertical )
{
input_context ctxt( "DEFAULTMODE", keyboard_mode::keycode );
ctxt.set_iso( true );
Expand All @@ -1097,7 +1097,7 @@ cata::optional<tripoint> choose_direction( const std::string &message, const boo
do {
ui_manager::redraw();
action = ctxt.handle_input();
if( cata::optional<tripoint> vec = ctxt.get_direction( action ) ) {
if( std::optional<tripoint> vec = ctxt.get_direction( action ) ) {
FacingDirection &facing = get_player_character().facing;
// Make player's sprite face left/right if interacting with something to the left or right
if( vec->x > 0 ) {
Expand All @@ -1116,16 +1116,16 @@ cata::optional<tripoint> choose_direction( const std::string &message, const boo
} while( action != "QUIT" );

add_msg( _( "Never mind." ) );
return cata::nullopt;
return std::nullopt;
}

cata::optional<tripoint> choose_adjacent( const std::string &message, const bool allow_vertical )
std::optional<tripoint> choose_adjacent( const std::string &message, const bool allow_vertical )
{
const cata::optional<tripoint> dir = choose_direction( message, allow_vertical );
const std::optional<tripoint> dir = choose_direction( message, allow_vertical );
return dir ? *dir + get_player_character().pos() : dir;
}

cata::optional<tripoint> choose_adjacent_highlight( const std::string &message,
std::optional<tripoint> choose_adjacent_highlight( const std::string &message,
const std::string &failure_message, const action_id action, bool allow_vertical )
{
const std::function<bool( const tripoint & )> f = [&action]( const tripoint & p ) {
Expand All @@ -1134,7 +1134,7 @@ cata::optional<tripoint> choose_adjacent_highlight( const std::string &message,
return choose_adjacent_highlight( message, failure_message, f, allow_vertical );
}

cata::optional<tripoint> choose_adjacent_highlight( const std::string &message,
std::optional<tripoint> choose_adjacent_highlight( const std::string &message,
const std::string &failure_message, const std::function<bool ( const tripoint & )> &allowed,
const bool allow_vertical )
{
Expand All @@ -1152,7 +1152,7 @@ cata::optional<tripoint> choose_adjacent_highlight( const std::string &message,
const bool auto_select = get_option<bool>( "AUTOSELECT_SINGLE_VALID_TARGET" );
if( valid.empty() && auto_select ) {
add_msg( failure_message );
return cata::nullopt;
return std::nullopt;
} else if( valid.size() == 1 && auto_select ) {
return valid.back();
}
Expand Down
15 changes: 8 additions & 7 deletions src/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <functional>
#include <iosfwd>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <vector>
Expand Down Expand Up @@ -409,9 +410,9 @@ std::vector<input_event> keys_bound_to( action_id act,
* keys only if they are printable (space counts as non-printable
* here). If `false`, all keys (whether they are printable or not)
* are returned.
* @returns the input event for the hotkey or cata::nullopt if no key is associated with the given action.
* @returns the input event for the hotkey or std::nullopt if no key is associated with the given action.
*/
cata::optional<input_event> hotkey_for_action( action_id action,
std::optional<input_event> hotkey_for_action( action_id action,
int maximum_modifier_count = -1, bool restrict_to_printable = true );

/**
Expand Down Expand Up @@ -465,7 +466,7 @@ bool can_action_change_worldstate( action_id act );
* @param[in] message Message used in assembling the prompt to the player
* @param[in] allow_vertical Allows player to select tiles above/below them if true
*/
cata::optional<tripoint> choose_adjacent( const std::string &message, bool allow_vertical = false );
std::optional<tripoint> choose_adjacent( const std::string &message, bool allow_vertical = false );

/**
* Request player input of a direction, possibly including vertical component
Expand All @@ -478,7 +479,7 @@ cata::optional<tripoint> choose_adjacent( const std::string &message, bool allow
* @param[in] message Message used in assembling the prompt to the player
* @param[in] allow_vertical Allows direction vector to have vertical component if true
*/
cata::optional<tripoint> choose_direction( const std::string &message,
std::optional<tripoint> choose_direction( const std::string &message,
bool allow_vertical = false );

/**
Expand All @@ -496,7 +497,7 @@ cata::optional<tripoint> choose_direction( const std::string &message,
* @param[in] action An action ID to drive the highlighting output
* @param[in] allow_vertical Allows direction vector to have vertical component if true
*/
cata::optional<tripoint> choose_adjacent_highlight( const std::string &message,
std::optional<tripoint> choose_adjacent_highlight( const std::string &message,
const std::string &failure_message, action_id action, bool allow_vertical = false );

/**
Expand All @@ -515,7 +516,7 @@ cata::optional<tripoint> choose_adjacent_highlight( const std::string &message,
* @param[in] allowed A function that will be called to determine if a given location is allowed for selection
* @param[in] allow_vertical Allows direction vector to have vertical component if true
*/
cata::optional<tripoint> choose_adjacent_highlight( const std::string &message,
std::optional<tripoint> choose_adjacent_highlight( const std::string &message,
const std::string &failure_message, const std::function<bool( const tripoint & )> &allowed,
bool allow_vertical = false );

Expand All @@ -528,7 +529,7 @@ std::string press_x( action_id act, const std::string &key_bound_pre,
// ('Z'ing|zing) (X( or Y)))
std::string press_x( action_id act, const std::string &act_desc );
// Return "Press X" or nullopt if not bound
cata::optional<std::string> press_x_if_bound( action_id act );
std::optional<std::string> press_x_if_bound( action_id act );

// only has effect in iso mode
enum class iso_rotate : int {
Expand Down
Loading

0 comments on commit 32774b3

Please sign in to comment.