Unit: used optional instead of unique_ptr where appropriate

This commit is contained in:
Charles Dang 2020-12-03 13:19:48 +11:00
parent bf3cdeb179
commit 6aae355fcf
2 changed files with 50 additions and 62 deletions

View File

@ -24,35 +24,35 @@
#include "display.hpp"
#include "formatter.hpp"
#include "formula/string_utils.hpp" // for VGETTEXT
#include "game_board.hpp" // for game_board
#include "game_config.hpp" // for add_color_info, etc
#include "game_board.hpp" // for game_board
#include "game_config.hpp" // for add_color_info, etc
#include "game_data.hpp"
#include "game_errors.hpp" // for game_error
#include "game_errors.hpp" // for game_error
#include "game_events/manager.hpp" // for add_events
#include "preferences/game.hpp" // for encountered_units
#include "gettext.hpp" // for N_
#include "game_version.hpp"
#include "gettext.hpp" // for N_
#include "lexical_cast.hpp"
#include "log.hpp" // for LOG_STREAM, logger, etc
#include "map/map.hpp" // for gamemap
#include "random.hpp" // for generator, rng
#include "resources.hpp" // for units, gameboard, teams, etc
#include "log.hpp" // for LOG_STREAM, logger, etc
#include "map/map.hpp" // for gamemap
#include "preferences/game.hpp" // for encountered_units
#include "random.hpp" // for generator, rng
#include "resources.hpp" // for units, gameboard, teams, etc
#include "scripting/game_lua_kernel.hpp" // for game_lua_kernel
#include "side_filter.hpp" // for side_filter
#include "side_filter.hpp" // for side_filter
#include "synced_context.hpp"
#include "team.hpp" // for team, get_teams, etc
#include "terrain/filter.hpp" // for terrain_filter
#include "units/abilities.hpp" // for effect, filter_base_matches
#include "units/animation.hpp" // for unit_animation
#include "team.hpp" // for team, get_teams, etc
#include "terrain/filter.hpp" // for terrain_filter
#include "units/abilities.hpp" // for effect, filter_base_matches
#include "units/animation.hpp" // for unit_animation
#include "units/animation_component.hpp" // for unit_animation_component
#include "units/filter.hpp"
#include "units/formula_manager.hpp" // for unit_formula_manager
#include "units/id.hpp"
#include "units/map.hpp" // for unit_map, etc
#include "units/types.hpp"
#include "units/map.hpp" // for unit_map, etc
#include "variable.hpp" // for vconfig, etc
#include "game_version.hpp"
#include "utils/functional.hpp"
#include "variable.hpp" // for vconfig, etc
#include <boost/dynamic_bitset.hpp>
#include <boost/function_output_iterator.hpp>
@ -200,12 +200,6 @@ namespace
++cur;
}
}
template<typename T>
T* copy_or_null(const std::unique_ptr<T>& ptr)
{
return ptr ? new T(*ptr) : nullptr;
}
} // end anon namespace
/**
@ -329,9 +323,9 @@ unit::unit(const unit& o)
, advancements_(o.advancements_)
, description_(o.description_)
, special_notes_(o.special_notes_)
, usage_(copy_or_null(o.usage_))
, halo_(copy_or_null(o.halo_))
, ellipse_(copy_or_null(o.ellipse_))
, usage_(o.usage_)
, halo_(o.halo_)
, ellipse_(o.ellipse_)
, random_traits_(o.random_traits_)
, generate_name_(o.generate_name_)
, upkeep_(o.upkeep_)
@ -1457,16 +1451,16 @@ void unit::write(config& cfg, bool write_all) const
}
}
if(halo_.get()) {
cfg["halo"] = *halo_;
if(halo_) {
cfg["halo"] = halo_.value();
}
if(ellipse_.get()) {
cfg["ellipse"] = *ellipse_;
if(ellipse_) {
cfg["ellipse"] = ellipse_.value();
}
if(usage_.get()) {
cfg["usage"] = *usage_;
if(usage_) {
cfg["usage"] = usage_.value();
}
write_upkeep(cfg["upkeep"]);
@ -2640,7 +2634,7 @@ void unit::set_image_halo(const std::string& halo)
{
appearance_changed_ = true;
anim_comp_->clear_haloes();
halo_.reset(new std::string(halo));
halo_ = halo;
}
void unit::parse_upkeep(const config::attribute_value& upkeep)

View File

@ -21,6 +21,7 @@
#include "units/attack_type.hpp"
#include "units/race.hpp"
#include "units/alignment.hpp"
#include "utils/optional_fwd.hpp"
#include <bitset>
#include <boost/dynamic_bitset_fwd.hpp>
@ -34,21 +35,7 @@ class unit_formula_manager;
class vconfig;
struct color_t;
namespace unit_detail
{
template<typename T>
const T& get_or_default(const std::unique_ptr<T>& v)
{
if(v) {
return *v;
} else {
static const T def;
return def;
}
}
}
/// Data typedef for unit_ability_list.
/** Data typedef for unit_ability_list. */
struct unit_ability
{
unit_ability(const config* ability_cfg, map_location student_loc, map_location teacher_loc)
@ -56,15 +43,15 @@ struct unit_ability
, teacher_loc(teacher_loc)
, ability_cfg(ability_cfg)
{
}
/// Used by the formula in the ability.
/// The REAL location of the student (not the 'we are assiming the student is at this position' location)
/// The REAL location of the student (not the 'we are assuming the student is at this position' location)
/// once unit_ability_list can contain abilities from different 'students', as it contains abilities from
/// a unit aswell from its opponents (abilities with apply_to= opponent)
map_location student_loc;
/// The location of the teacher, that is the unit who owns the ability tags
/// (differnt from student because of [afect_adjacent])
/// (different from student because of [affect_adjacent])
map_location teacher_loc;
/// The contents of the ability tag, never nullptr.
const config* ability_cfg;
@ -137,6 +124,7 @@ public:
/** The path to the leader crown overlay. */
static const std::string& leader_crown();
private:
void init(const config& cfg, bool use_traits = false, const vconfig* vcfg = nullptr);
@ -145,12 +133,13 @@ private:
// Copy constructor
unit(const unit& u);
struct unit_ctor_t {};
public:
//private default ctor, butusing constructor to allow calling make_shared<unit> in create().
unit(unit_ctor_t);
unit() = delete;
private:
enum UNIT_ATTRIBUTE
{
@ -176,16 +165,21 @@ private:
UA_UPKEEP,
UA_COUNT
};
void set_attr_changed(UNIT_ATTRIBUTE attr)
{
changed_attributes_[int(attr)] = true;
}
bool get_attacks_changed() const;
bool get_attr_changed(UNIT_ATTRIBUTE attr) const
{
return changed_attributes_[int(attr)];
}
void clear_changed_attributes();
public:
/** Initializes a unit from a config */
static unit_ptr create(const config& cfg, bool use_traits = false, const vconfig* vcfg = nullptr)
@ -686,13 +680,13 @@ public:
*/
std::string usage() const
{
return unit_detail::get_or_default(usage_);
return usage_.value_or("");
}
/** Sets this unit's usage. */
void set_usage(const std::string& usage)
{
usage_.reset(new std::string(usage));
usage_ = usage;
}
/**
@ -1355,7 +1349,7 @@ public:
loc_ = loc;
}
/** The current directin this unit is facing within its hex. */
/** The current direction this unit is facing within its hex. */
map_location::DIRECTION facing() const
{
return facing_;
@ -1541,7 +1535,7 @@ public:
/** Get the unit's halo image. */
std::string image_halo() const
{
return unit_detail::get_or_default(halo_);
return halo_.value_or("");
}
/** Set the unit's halo image. */
@ -1550,14 +1544,14 @@ public:
/** Get the unit's ellipse image. */
std::string image_ellipse() const
{
return unit_detail::get_or_default(ellipse_);
return ellipse_.value_or("");
}
/** Set the unit's ellipse image. */
void set_image_ellipse(const std::string& ellipse)
{
appearance_changed_ = true;
ellipse_.reset(new std::string(ellipse));
ellipse_ = ellipse;
}
/**
@ -1893,9 +1887,9 @@ private:
t_string description_;
std::vector<t_string> special_notes_;
std::unique_ptr<std::string> usage_;
std::unique_ptr<std::string> halo_;
std::unique_ptr<std::string> ellipse_;
utils::optional<std::string> usage_;
utils::optional<std::string> halo_;
utils::optional<std::string> ellipse_;
bool random_traits_;
bool generate_name_;