Added a member function t_token::default_value<char >()...

...to create static initialization safe default values for parameters.
This commit is contained in:
Thonsew 2011-09-09 07:39:08 +00:00
parent 42b5b0391f
commit 1828d8f1d9
11 changed files with 40 additions and 56 deletions

View file

@ -79,13 +79,6 @@ private:
std::vector<cache_item<T> > content_;
};
//static initialization fiasco fix don't inline
n_token::t_token const & locator::z_empty_default() {
static const n_token::t_token * x = new n_token::t_token(z_empty);
return *x;
}
template <typename T>
bool locator::in_cache(cache_type<T> &cache) const
{

View file

@ -79,9 +79,6 @@ namespace image {
// (the second will work, of course, but will be slower)
locator();
//temporary fix //todo move somewhere like inside t_token?
static n_token::t_token const & z_empty_default();
locator(const locator &a, const n_token::t_token &mods = n_token::t_token::z_empty());
// locator(const locator &a, const std::string &mods ="");

View file

@ -198,7 +198,6 @@ std::vector< n_token::t_token > parenthetical_split_token(n_token::t_token const
const char separator, n_token::t_token const &left,
n_token::t_token const &right, int flags) {
static t_paren_cache my_cache(t_calc_paren_cache_item(), PAREN_CACHE_SIZE);
return my_cache.check( t_paren_split_inputs(val, separator, left, right, flags ) );
}

View file

@ -37,14 +37,6 @@ typedef std::string utf8_string;
class t_string;
namespace {
//Static tokens are replacements for string literals in code
//They allow for fast comparison, copying and hashing operations.
static const n_token::t_token z_lparen("(", false);
static const n_token::t_token z_rparen(")", false);
}
namespace utils {
extern const std::string unicode_minus;
@ -89,9 +81,9 @@ std::vector< std::string > parenthetical_split(std::string const &val,
std::string const &right=")",int flags = REMOVE_EMPTY | STRIP_SPACES);
std::vector< n_token::t_token > parenthetical_split_token(n_token::t_token const &val,
const char separator = 0 , n_token::t_token const &left=z_lparen,
n_token::t_token const &right=z_rparen,int flags = REMOVE_EMPTY | STRIP_SPACES);
std::vector< n_token::t_token > parenthetical_split_token(n_token::t_token const &val
, const char separator = 0, n_token::t_token const &left=n_token::t_token::default_value<'('>()
, n_token::t_token const &right = n_token::t_token::default_value<')'>(), int flags = REMOVE_EMPTY | STRIP_SPACES);
template <typename T>
std::string join(T const &v, const std::string& s = ",")

View file

@ -29,7 +29,6 @@ t_token const & t_token::z_empty() {
static t_token *z_empty = new t_token("", false);
return *z_empty;
}
}//end namepace

View file

@ -57,8 +57,12 @@ public:
explicit t_token(std::string const & a , bool is_ref_counted = true) : t_base(a, is_ref_counted) {}
t_token(t_token const & a) : t_base(a) {}
t_token & operator=(t_token const & a) { this->t_base::operator=(a); return *this;}
///Empty string token in a form suitable for use as a default value safe from static initialization errors
static const t_token & z_empty();
///Return a default interned object suitable as a default value which won't cause a static initialization error
template <char T_defchar>
static t_token const & default_value();
inline bool empty() const {return *this == z_empty() ;}
@ -74,6 +78,12 @@ public:
return out << static_cast<std::string const &>(a); }
};
/// Do not inline this enforces static initialization order
template <char T_defval>
t_token const & t_token::default_value() {
static t_token *z_defval = new t_token(std::string(1, T_defval), false);
return *z_defval;
}
inline std::string operator+(const n_token::t_token &a, const std::string &b) { return static_cast<std::string const &>(a) + b; }
inline std::string operator+(const std::string &a, const n_token::t_token &b) { return a + static_cast<std::string const &>(b); }

View file

@ -287,7 +287,7 @@ public:
STATE_FORGET, /** animation will be automatically replaced by a standing anim when finished */
STATE_ANIM}; /** normal anims */
void start_animation(int start_time, const unit_animation *animation,
bool with_bars, const n_token::t_token &text = z_empty,
bool with_bars, const n_token::t_token &text = n_token::t_token::z_empty(),
Uint32 text_color = 0, STATE state = STATE_ANIM);
/** The name of the file to game_display (used in menus). */

View file

@ -41,7 +41,7 @@ class unit_animation
static void fill_initial_animations( std::vector<unit_animation> & animations, const config & cfg);
static void add_anims( std::vector<unit_animation> & animations, const config & cfg);
int matches(const game_display &disp,const map_location& loc,const map_location& second_loc,const unit* my_unit,const n_token::t_token & event=z_empty,const int value=0,hit_type hit=INVALID,const attack_type* attack=NULL,const attack_type* second_attack = NULL, int value2 =0) const;
int matches(const game_display &disp,const map_location& loc,const map_location& second_loc,const unit* my_unit,const n_token::t_token & event= n_token::t_token::z_empty(),const int value=0,hit_type hit=INVALID,const attack_type* attack=NULL,const attack_type* second_attack = NULL, int value2 =0) const;
const unit_frame& get_last_frame() const{ return unit_anim_.get_last_frame() ; };
@ -60,7 +60,7 @@ class unit_animation
void start_animation(int start_time
, const map_location &src = map_location::null_location
, const map_location &dst = map_location::null_location
, const n_token::t_token& text = z_empty
, const n_token::t_token& text = n_token::t_token::z_empty()
, const Uint32 text_color = 0
, const bool accelerate = true);
void update_parameters(const map_location &src, const map_location &dst);
@ -73,7 +73,7 @@ class unit_animation
friend class unit;
explicit unit_animation(const config &cfg, const n_token::t_token &frame_string = z_empty);
explicit unit_animation(const config &cfg, const n_token::t_token &frame_string = n_token::t_token::z_empty());
protected:
// reserved to class unit, for the special case of redrawing the unit base frame
@ -81,7 +81,7 @@ class unit_animation
private:
explicit unit_animation(int start_time
, const unit_frame &frame
, const n_token::t_token& event = z_empty
, const n_token::t_token& event = n_token::t_token::z_empty()
, const int variation=DEFAULT_ANIM
, const frame_builder & builder = frame_builder());
@ -105,12 +105,12 @@ class unit_animation
void override(int start_time
, int duration
, const cycle_state cycles
, const n_token::t_token& highlight = z_empty
, const n_token::t_token& blend_ratio =z_empty
, const n_token::t_token& highlight = n_token::t_token::z_empty()
, const n_token::t_token& blend_ratio = n_token::t_token::z_empty()
, Uint32 blend_color = 0
, const n_token::t_token& offset = z_empty
, const n_token::t_token& layer = z_empty
, const n_token::t_token& modifiers = z_empty);
, const n_token::t_token& offset = n_token::t_token::z_empty()
, const n_token::t_token& layer = n_token::t_token::z_empty()
, const n_token::t_token& modifiers = n_token::t_token::z_empty());
void redraw( const frame_parameters& value,const map_location &src, const map_location &dst);
std::set<map_location> get_overlaped_hex(const frame_parameters& value,const map_location &src, const map_location &dst);
void start_animation(int start_time);
@ -166,7 +166,7 @@ class unit_animator
, const unit_animation * animation
, const map_location &src = map_location::null_location
, bool with_bars = false
, const n_token::t_token& text = z_empty
, const n_token::t_token& text = n_token::t_token::z_empty()
, const Uint32 text_color = 0);
void add_animation(unit* animated_unit
, const n_token::t_token& event
@ -174,7 +174,7 @@ class unit_animator
, const map_location &dst = map_location::null_location
, const int value = 0
, bool with_bars = false
, const n_token::t_token& text = z_empty
, const n_token::t_token& text = n_token::t_token::z_empty()
, const Uint32 text_color = 0
, const unit_animation::hit_type hit_type =
unit_animation::INVALID
@ -187,10 +187,9 @@ class unit_animator
, const map_location &dst = map_location::null_location
, const int value = 0
, bool with_bars = false
, const n_token::t_token& text = z_empty
, const n_token::t_token& text = n_token::t_token::z_empty()
, const Uint32 text_color = 0
, const unit_animation::hit_type hit_type =
unit_animation::INVALID
, const unit_animation::hit_type hit_type = unit_animation::INVALID
, const attack_type* attack = NULL
, const attack_type* second_attack = NULL
, int value2 = 0);

View file

@ -29,11 +29,9 @@ class config;
template <class T>
class progressive_discrete {
public:
progressive_discrete(const n_token::t_token& data = z_empty, int duration = 0) : data_(), input_(data) {
progressive_discrete(const n_token::t_token& data = n_token::t_token::z_empty(), int duration = 0) : data_(), input_(data) {
progressive_discrete_core(data, duration); }
// progressive_discrete(const std::string& data = z_empty, int duration = 0): data_(), input_(data) {
// progressive_discrete_core(n_token::t_token(data), duration); }
void progressive_discrete_core(const n_token::t_token& data = z_empty, int duration = 0);
void progressive_discrete_core(const n_token::t_token& data = n_token::t_token::z_empty(), int duration = 0);
progressive_discrete(progressive_discrete const & );
int duration() const;
const T & get_current_element(int time) const;
@ -61,9 +59,9 @@ class progressive_continuous {
static T const & default_default_value();
public:
progressive_continuous(const n_token::t_token& data = z_empty, int duration = 0) : data_(), input_(data) {
progressive_continuous(const n_token::t_token& data = n_token::t_token::z_empty(), int duration = 0) : data_(), input_(data) {
progressive_continuous_core(data, duration); }
void progressive_continuous_core(const n_token::t_token& data = z_empty, int duration = 0);
void progressive_continuous_core(const n_token::t_token& data = n_token::t_token::z_empty(), int duration = 0);
progressive_continuous(progressive_continuous const &);
int duration() const;
const T get_current_element(int time,T const & default_val=progressive_continuous<T>::default_default_value() ) const;
@ -117,11 +115,11 @@ class frame_parsed_parameters;
class frame_builder {
public:
frame_builder();
frame_builder(const config& cfg,const n_token::t_token &frame_string = z_empty);
frame_builder(const config& cfg,const n_token::t_token &frame_string = n_token::t_token::z_empty());
/** allow easy chained modifications will raised assert if used after initialization */
frame_builder & duration(const int duration);
frame_builder & image(const image::locator& image ,const n_token::t_token & image_mod=z_empty);
frame_builder & image_diagonal(const image::locator& image_diagonal,const n_token::t_token & image_mod=z_empty);
frame_builder & image(const image::locator& image ,const n_token::t_token & image_mod= n_token::t_token::z_empty());
frame_builder & image_diagonal(const image::locator& image_diagonal,const n_token::t_token & image_mod= n_token::t_token::z_empty());
frame_builder & sound(const n_token::t_token& sound);
frame_builder & text(const n_token::t_token& text,const Uint32 text_color);
frame_builder & halo(const n_token::t_token &halo, const n_token::t_token &halo_x, const n_token::t_token& halo_y,const n_token::t_token& halo_mod);
@ -175,12 +173,12 @@ class frame_parsed_parameters {
frame_parsed_parameters(const frame_builder& builder=frame_builder(),int override_duration = 0);
/** allow easy chained modifications will raised assert if used after initialization */
void override( int duration
, const n_token::t_token& highlight = z_empty
, const n_token::t_token& blend_ratio =z_empty
, const n_token::t_token& highlight = n_token::t_token::z_empty()
, const n_token::t_token& blend_ratio = n_token::t_token::z_empty()
, Uint32 blend_color = 0
, const n_token::t_token& offset = z_empty
, const n_token::t_token& layer = z_empty
, const n_token::t_token& modifiers = z_empty);
, const n_token::t_token& offset = n_token::t_token::z_empty()
, const n_token::t_token& layer = n_token::t_token::z_empty()
, const n_token::t_token& modifiers = n_token::t_token::z_empty());
/** getters for the different parameters */
const frame_parameters parameters(int current_time) const ;

View file

@ -22,7 +22,6 @@
namespace n_interned {
}//end namepace

View file

@ -57,7 +57,6 @@ class t_interned_token {
public:
inline ~t_interned_token();
///default constructor.
t_interned_token();
@ -165,7 +164,6 @@ typename t_interned_token<T, T_hasher>::t_stash & t_interned_token<T, T_hasher>:
return *the_stash_;
}
template <typename T, typename T_hasher >
typename t_interned_token<T, T_hasher>::t_stash::value_type * t_interned_token<T, T_hasher>::first_default_constructed() {
t_stash & the_stash_ = the_stash();