Move the template class tformula in its own header.
This commit is contained in:
parent
cb6154d62a
commit
91df132eba
4 changed files with 165 additions and 137 deletions
|
@ -19,17 +19,15 @@
|
|||
|
||||
#include "config.hpp"
|
||||
#include "font.hpp"
|
||||
#include "formula.hpp"
|
||||
#include "sdl_utils.hpp"
|
||||
#include "image.hpp"
|
||||
#include "gettext.hpp"
|
||||
#include "gui/widgets/formula.hpp"
|
||||
#include "gui/widgets/helper.hpp"
|
||||
#include "log.hpp"
|
||||
#include "sdl_utils.hpp"
|
||||
#include "serialization/parser.hpp"
|
||||
#include "wml_exception.hpp"
|
||||
|
||||
#include <boost/static_assert.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
|
@ -53,100 +51,8 @@
|
|||
#define WRN_G_P LOG_STREAM_INDENT(warn, gui_parse)
|
||||
#define ERR_G_P LOG_STREAM_INDENT(err, gui_parse)
|
||||
|
||||
|
||||
namespace gui2{
|
||||
|
||||
template<class T>
|
||||
tformula<T>::tformula(const std::string& str) :
|
||||
formula_(),
|
||||
value_()
|
||||
{
|
||||
if(str.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(str[0] == '(') {
|
||||
formula_ = str;
|
||||
} else {
|
||||
convert(str);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T tformula<T>::operator() (const game_logic::map_formula_callable& variables) const
|
||||
{
|
||||
if(has_formula()) {
|
||||
DBG_G_D << "Formula: execute '" << formula_ << "'.\n";
|
||||
return execute(variables);
|
||||
} else {
|
||||
return value_;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
bool tformula<bool>::execute(const game_logic::map_formula_callable& variables) const
|
||||
{
|
||||
return game_logic::formula(formula_).execute(variables).as_bool();
|
||||
}
|
||||
|
||||
template<>
|
||||
int tformula<int>::execute(const game_logic::map_formula_callable& variables) const
|
||||
{
|
||||
return game_logic::formula(formula_).execute(variables).as_int();
|
||||
}
|
||||
|
||||
template<>
|
||||
unsigned tformula<unsigned>::execute(const game_logic::map_formula_callable& variables) const
|
||||
{
|
||||
return game_logic::formula(formula_).execute(variables).as_int();
|
||||
}
|
||||
|
||||
template<>
|
||||
std::string tformula<std::string>::execute(const game_logic::map_formula_callable& variables) const
|
||||
{
|
||||
return game_logic::formula(formula_).execute(variables).as_string();
|
||||
}
|
||||
|
||||
template<>
|
||||
t_string tformula<t_string>::execute(const game_logic::map_formula_callable& variables) const
|
||||
{
|
||||
return game_logic::formula(formula_).execute(variables).as_string();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T tformula<T>::execute(const game_logic::map_formula_callable& variables) const
|
||||
{
|
||||
// Every type needs it's own execute function avoid instantiation of the
|
||||
// default execute.
|
||||
BOOST_STATIC_ASSERT(sizeof(T) == 0);
|
||||
return T();
|
||||
}
|
||||
|
||||
template<>
|
||||
void tformula<bool>::convert(const std::string& str)
|
||||
{
|
||||
value_ = utils::string_bool(str);
|
||||
}
|
||||
|
||||
template<>
|
||||
void tformula<std::string>::convert(const std::string& str)
|
||||
{
|
||||
value_ = str;
|
||||
}
|
||||
|
||||
template<>
|
||||
void tformula<t_string>::convert(const std::string& str)
|
||||
{
|
||||
value_ = str;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void tformula<T>::convert(const std::string& str)
|
||||
{
|
||||
value_ = lexical_cast_default<T>(str);
|
||||
}
|
||||
|
||||
//! Definition of a line shape.
|
||||
class tline : public tcanvas::tshape
|
||||
{
|
||||
|
|
|
@ -104,47 +104,6 @@ private:
|
|||
bool dirty_;
|
||||
};
|
||||
|
||||
//! Template class can hold a value or a formula calculating the value.
|
||||
//!
|
||||
//! Note since it's also instanciated in canvas.cpp declared the class here.
|
||||
template <class T>
|
||||
class tformula
|
||||
{
|
||||
public:
|
||||
tformula<T>(const std::string& str);
|
||||
|
||||
//! Returns the value, can only be used it the data is no formula.
|
||||
//!
|
||||
//! Another option would be to cache the output of the formula in value_
|
||||
//! and always allow this function. But for now decided that the caller
|
||||
//! needs to do the caching. It might be changed later.
|
||||
T operator()() const
|
||||
{
|
||||
assert(!has_formula());
|
||||
return value_;
|
||||
}
|
||||
|
||||
//! Returns the value, can always be used.
|
||||
T operator() (const game_logic::map_formula_callable& variables) const;
|
||||
|
||||
//! Determine whether the class contains a formula.
|
||||
bool has_formula() const { return !formula_.empty(); }
|
||||
|
||||
private:
|
||||
|
||||
//! Converts the string ot the template value.
|
||||
void convert(const std::string& str);
|
||||
|
||||
T execute(const game_logic::map_formula_callable& variables) const;
|
||||
|
||||
//! If there is a formula it's stored in this string, empty if no formula.
|
||||
std::string formula_;
|
||||
|
||||
//! If no formula it contains the value.
|
||||
T value_;
|
||||
|
||||
};
|
||||
|
||||
} // namespace gui2
|
||||
|
||||
|
||||
|
|
162
src/gui/widgets/formula.hpp
Normal file
162
src/gui/widgets/formula.hpp
Normal file
|
@ -0,0 +1,162 @@
|
|||
/* $Id$ */
|
||||
/*
|
||||
Copyright (C) 2008 by Mark de Wever <koraq@xs4all.nl>
|
||||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
or at your option any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#ifndef __GUI_WIDGETS_FORMULA_HPP_INCLUDED__
|
||||
#define __GUI_WIDGETS_FORMULA_HPP_INCLUDED__
|
||||
|
||||
#include "formula_callable.hpp"
|
||||
#include "../../formula.hpp"
|
||||
#include "log.hpp"
|
||||
|
||||
#include <boost/static_assert.hpp>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace gui2{
|
||||
|
||||
/** Template class can hold a value or a formula calculating the value. */
|
||||
template <class T>
|
||||
class tformula
|
||||
{
|
||||
public:
|
||||
tformula<T>(const std::string& str);
|
||||
|
||||
/**
|
||||
* Returns the value, can only be used it the data is no formula.
|
||||
*
|
||||
* Another option would be to cache the output of the formula in value_
|
||||
* and always allow this function. But for now decided that the caller
|
||||
* needs to do the caching. It might be changed later.
|
||||
*/
|
||||
T operator()() const
|
||||
{
|
||||
assert(!has_formula());
|
||||
return value_;
|
||||
}
|
||||
|
||||
/** Returns the value, can always be used. */
|
||||
T operator() (const game_logic::map_formula_callable& variables) const;
|
||||
|
||||
/** Determine whether the class contains a formula. */
|
||||
bool has_formula() const { return !formula_.empty(); }
|
||||
|
||||
private:
|
||||
|
||||
/** Converts the string ot the template value. */
|
||||
void convert(const std::string& str);
|
||||
|
||||
T execute(const game_logic::map_formula_callable& variables) const;
|
||||
|
||||
/** If there is a formula it's stored in this string, empty if no formula. */
|
||||
std::string formula_;
|
||||
|
||||
/** If no formula it contains the value. */
|
||||
T value_;
|
||||
|
||||
};
|
||||
|
||||
template<class T>
|
||||
tformula<T>::tformula(const std::string& str) :
|
||||
formula_(),
|
||||
value_()
|
||||
{
|
||||
if(str.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(str[0] == '(') {
|
||||
formula_ = str;
|
||||
} else {
|
||||
convert(str);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline T tformula<T>::operator() (const game_logic::map_formula_callable& variables) const
|
||||
{
|
||||
if(has_formula()) {
|
||||
LOG_STREAM_INDENT(debug, gui_draw) << "Formula: execute '" << formula_ << "'.\n";
|
||||
return execute(variables);
|
||||
} else {
|
||||
return value_;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
inline bool tformula<bool>::execute(const game_logic::map_formula_callable& variables) const
|
||||
{
|
||||
return game_logic::formula(formula_).execute(variables).as_bool();
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int tformula<int>::execute(const game_logic::map_formula_callable& variables) const
|
||||
{
|
||||
return game_logic::formula(formula_).execute(variables).as_int();
|
||||
}
|
||||
|
||||
template<>
|
||||
inline unsigned tformula<unsigned>::execute(const game_logic::map_formula_callable& variables) const
|
||||
{
|
||||
return game_logic::formula(formula_).execute(variables).as_int();
|
||||
}
|
||||
|
||||
template<>
|
||||
inline std::string tformula<std::string>::execute(const game_logic::map_formula_callable& variables) const
|
||||
{
|
||||
return game_logic::formula(formula_).execute(variables).as_string();
|
||||
}
|
||||
|
||||
template<>
|
||||
inline t_string tformula<t_string>::execute(const game_logic::map_formula_callable& variables) const
|
||||
{
|
||||
return game_logic::formula(formula_).execute(variables).as_string();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline T tformula<T>::execute(const game_logic::map_formula_callable& variables) const
|
||||
{
|
||||
// Every type needs it's own execute function avoid instantiation of the
|
||||
// default execute.
|
||||
BOOST_STATIC_ASSERT(sizeof(T) == 0);
|
||||
return T();
|
||||
}
|
||||
|
||||
template<>
|
||||
inline void tformula<bool>::convert(const std::string& str)
|
||||
{
|
||||
value_ = utils::string_bool(str);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline void tformula<std::string>::convert(const std::string& str)
|
||||
{
|
||||
value_ = str;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline void tformula<t_string>::convert(const std::string& str)
|
||||
{
|
||||
value_ = str;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void tformula<T>::convert(const std::string& str)
|
||||
{
|
||||
value_ = lexical_cast_default<T>(str);
|
||||
}
|
||||
|
||||
} // namespace gui2
|
||||
|
||||
#endif
|
|
@ -20,6 +20,7 @@
|
|||
|
||||
#include "config.hpp"
|
||||
#include "gui/widgets/canvas.hpp"
|
||||
#include "gui/widgets/formula.hpp"
|
||||
#include "gui/widgets/window_builder.hpp"
|
||||
#include "tstring.hpp"
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue