Uniformize (and document) the use of a dummy race...

...to guarantee certain pointers are never NULL.

(The pointers cannot be references because they sometimes need to
change what they point to.)
This commit is contained in:
J. Tyne 2013-01-02 05:46:10 +00:00
parent 8064f893c0
commit 6c01ba329d
7 changed files with 23 additions and 19 deletions

View File

@ -906,9 +906,7 @@ const unit_preview_pane::details units_list_preview_pane::get_details() const
det.name = u.name();
det.type_name = u.type_name();
if(u.race() != NULL) {
det.race = u.race()->name(u.gender());
}
det.race = u.race()->name(u.gender());
det.level = u.level();
det.alignment = unit_type::alignment_description(u.alignment(), u.gender());
det.traits = utils::join(u.trait_names(), ", ");

View File

@ -26,6 +26,11 @@
#include "random.hpp"
#include "simple_rng.hpp"
/// Dummy race used when a race is not yet known.
const unit_race unit_race::null_race;
static const config &empty_traits() {
static config cfg;
return cfg;

View File

@ -31,8 +31,7 @@ class unit_race
public:
enum GENDER { MALE, FEMALE, NUM_GENDERS };
unit_race();
unit_race(const config& cfg);
explicit unit_race(const config& cfg);
const config& get_cfg() const { return cfg_; };
const std::string& id() const { return id_; };
@ -49,7 +48,13 @@ public:
unsigned int num_traits() const;
const std::string& undead_variation() const { return undead_variation_; };
/// Dummy race used when a race is not yet known.
static const unit_race null_race;
private:
/// Only used to construct null_race.
unit_race();
const config cfg_;
std::string id_;

View File

@ -202,7 +202,7 @@ unit::unit(const config &cfg, bool use_traits, game_state* state, const vconfig*
loc_(cfg["x"] - 1, cfg["y"] - 1),
advances_to_(),
type_(cfg["type"]),
race_(NULL),
race_(&unit_race::null_race),
id_(cfg["id"]),
name_(cfg["name"].t_str()),
underlying_id_(0),
@ -327,8 +327,7 @@ unit::unit(const config &cfg, bool use_traits, game_state* state, const vconfig*
if (const unit_race *r = unit_types.find_race(*v)) {
race_ = r;
} else {
static const unit_race dummy_race;
race_ = &dummy_race;
race_ = &unit_race::null_race;
}
}
level_ = cfg["level"].to_int(level_);
@ -583,7 +582,7 @@ unit::unit(const unit_type *t, int side, bool real_unit,
loc_(),
advances_to_(),
type_(),
race_(NULL),
race_(&unit_race::null_race),
id_(),
name_(),
underlying_id_(real_unit? 0: n_unit::id_manager::instance().next_fake_id()),

View File

@ -313,6 +313,7 @@ public:
std::string usage() const { return cfg_["usage"]; }
unit_type::ALIGNMENT alignment() const { return alignment_; }
/// Never returns NULL, but may point to the null race.
const unit_race* race() const { return race_; }
const unit_animation* choose_animation(const display& disp,
@ -409,7 +410,7 @@ private:
std::vector<std::string> advances_to_;
std::string type_;
const unit_race* race_;
const unit_race* race_; /// Never NULL, but may point to the null race.
std::string id_;
t_string name_;
size_t underlying_id_;

View File

@ -599,11 +599,6 @@ int defense_modifier_internal(defense_cache &defense_mods,
return (std::max)(def.max_, def.min_);
}
static const unit_race& dummy_race(){
static unit_race ur;
return ur;
}
#ifdef _MSC_VER
#pragma warning(push)
@ -686,7 +681,7 @@ unit_type::unit_type(config &cfg) :
num_traits_(0),
gender_types_(),
variations_(),
race_(&dummy_race()),
race_(&unit_race::null_race),
alpha_(),
abilities_(),
adv_abilities_(),
@ -746,7 +741,7 @@ void unit_type::build_full(const movement_type_map &mv_types,
alignment_ = NEUTRAL;
}
if (race_ != &dummy_race())
if ( race_ != &unit_race::null_race )
{
if (!race_->uses_global_traits()) {
possibleTraits_.clear();
@ -836,7 +831,7 @@ void unit_type::build_help_index(const movement_type_map &mv_types,
if(race_it != races.end()) {
race_ = &race_it->second;
} else {
race_ = &dummy_race();
race_ = &unit_race::null_race;
}
// if num_traits is not defined, we use the num_traits from race

View File

@ -298,6 +298,7 @@ public:
std::vector<std::string> variations() const;
const std::string race() const { return cfg_["race"]; } //race_->id(); }
/// Never returns NULL, but may point to the null race.
const unit_race* race_ptr() const { return race_; }
bool hide_help() const;
@ -340,7 +341,7 @@ private:
typedef std::map<std::string,unit_type*> variations_map;
variations_map variations_;
const unit_race* race_;
const unit_race* race_; /// Never returns NULL, but may point to the null race.
fixed_t alpha_;