Reduce use of const_cast

This commit is contained in:
Celtic Minstrel 2016-08-12 13:57:29 -04:00
parent bb43fa29c4
commit 30b7ab8b2d
25 changed files with 79 additions and 152 deletions

View file

@ -281,7 +281,6 @@
91A2156B1CAD6DA400927AEA /* lua_race.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ECC2FFF91A51A00900023AF4 /* lua_race.cpp */; };
91A2156C1CAD6DA500927AEA /* lua_rng.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ECA4A67A1A1EC319006BCCF2 /* lua_rng.cpp */; };
91A2156D1CAD6DA500927AEA /* lua_team.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC5430211A4E6024006D206C /* lua_team.cpp */; };
91A2156E1CAD6DA500927AEA /* lua_types.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C02A0E182F1F64008525C6 /* lua_types.cpp */; };
91A2156F1CAD6DA500927AEA /* lua_unit_type.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC5430221A4E6024006D206C /* lua_unit_type.cpp */; };
91A215701CAD6E7500927AEA /* context.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC59F25F1A4529D2001910CB /* context.cpp */; };
91A215711CAD6E7500927AEA /* manager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC59F2601A4529D2001910CB /* manager.cpp */; };
@ -1262,7 +1261,6 @@
F480CD4B14035038007175D6 /* sourceparser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F480CD3C14035038007175D6 /* sourceparser.cpp */; };
F480CD4C14035038007175D6 /* tag.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F480CD3E14035038007175D6 /* tag.cpp */; };
F49F134A15BC627C00B64B0B /* edit_label.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F49F134815BC627C00B64B0B /* edit_label.cpp */; };
F4C02A11182F1F64008525C6 /* lua_types.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C02A0E182F1F64008525C6 /* lua_types.cpp */; };
F4C5DD68158CFD5E0044F754 /* point.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C5DD66158CFD5E0044F754 /* point.cpp */; };
F4D2A99614DAED0E00CAFF31 /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4D2A99514DAED0E00CAFF31 /* CoreFoundation.framework */; };
F4D2A9D514DAED4200CAFF31 /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4D2A99514DAED0E00CAFF31 /* CoreFoundation.framework */; };
@ -4150,8 +4148,6 @@
91B621EC1B76BB2200B00E0F /* lua_rng.hpp */,
EC5430211A4E6024006D206C /* lua_team.cpp */,
91B621ED1B76BB2600B00E0F /* lua_team.hpp */,
F4C02A0E182F1F64008525C6 /* lua_types.cpp */,
F4C02A0F182F1F64008525C6 /* lua_types.hpp */,
9193FC801D5C2CF7004F6C07 /* lua_unit.cpp */,
9193FC811D5C2CF8004F6C07 /* lua_unit.hpp */,
9193FC841D5D7452004F6C07 /* lua_unit_attacks.cpp */,
@ -4959,7 +4955,6 @@
ECC2FFFA1A51A00900023AF4 /* lua_race.cpp in Sources */,
ECA4A67D1A1EC319006BCCF2 /* lua_rng.cpp in Sources */,
EC5430231A4E6024006D206C /* lua_team.cpp in Sources */,
F4C02A11182F1F64008525C6 /* lua_types.cpp in Sources */,
EC5430241A4E6024006D206C /* lua_unit_type.cpp in Sources */,
EC89A1481879D17D00A3B0B1 /* lundump.cpp in Sources */,
EC89A1491879D17D00A3B0B1 /* lvm.cpp in Sources */,
@ -5619,7 +5614,6 @@
91A2156B1CAD6DA400927AEA /* lua_race.cpp in Sources */,
91A2156C1CAD6DA500927AEA /* lua_rng.cpp in Sources */,
91A2156D1CAD6DA500927AEA /* lua_team.cpp in Sources */,
91A2156E1CAD6DA500927AEA /* lua_types.cpp in Sources */,
91A2156F1CAD6DA500927AEA /* lua_unit_type.cpp in Sources */,
91A215701CAD6E7500927AEA /* context.cpp in Sources */,
91A215711CAD6E7500927AEA /* manager.cpp in Sources */,

View file

@ -383,8 +383,7 @@ void readonly_context_impl::calculate_moves(const unit_map& units, std::map<map_
continue;
}
// If it's an enemy unit, reset its moves while we do the calculations.
unit* held_unit = const_cast<unit *>(&*un_it);
const unit_movement_resetter move_resetter(*held_unit,enemy || assume_full_movement);
const unit_movement_resetter move_resetter(*un_it,enemy || assume_full_movement);
// Insert the trivial moves of staying on the same map location.
if (un_it->movement_left() > 0) {

View file

@ -50,7 +50,8 @@ static lg::log_domain log_ai_engine_lua("ai/engine/lua");
#define LOG_LUA LOG_STREAM(info, log_ai_engine_lua)
#define ERR_LUA LOG_STREAM(err, log_ai_engine_lua)
static char const aisKey = 0;
// This is an array so we can use sizeof()
static char const aisKey[] = "ai contexts";
namespace ai {
@ -59,7 +60,7 @@ static void push_attack_analysis(lua_State *L, const attack_analysis&);
void lua_ai_context::init(lua_State *L)
{
// Create the ai elements table.
lua_pushlightuserdata(L, static_cast<void *>(const_cast<char *>(&aisKey)));
lua_pushlstring(L, aisKey, sizeof(aisKey));
lua_newtable(L);
lua_rawset(L, LUA_REGISTRYINDEX);
}
@ -68,7 +69,7 @@ void lua_ai_context::get_arguments(config &cfg) const
{
int top = lua_gettop(L);
lua_pushlightuserdata(L, static_cast<void *>(const_cast<char *>(&aisKey)));
lua_pushlstring(L, aisKey, sizeof(aisKey));
lua_rawget(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, -1, num_);
@ -82,7 +83,7 @@ void lua_ai_context::set_arguments(const config &cfg)
{
int top = lua_gettop(L);
lua_pushlightuserdata(L, static_cast<void *>(const_cast<char *>(&aisKey)));
lua_pushlstring(L, aisKey, sizeof(aisKey));
lua_rawget(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, -1, num_);
@ -96,7 +97,7 @@ void lua_ai_context::get_persistent_data(config &cfg) const
{
int top = lua_gettop(L);
lua_pushlightuserdata(L, static_cast<void *>(const_cast<char *>(&aisKey)));
lua_pushlstring(L, aisKey, sizeof(aisKey));
lua_rawget(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, -1, num_);
@ -110,7 +111,7 @@ void lua_ai_context::set_persistent_data(const config &cfg)
{
int top = lua_gettop(L);
lua_pushlightuserdata(L, static_cast<void *>(const_cast<char *>(&aisKey)));
lua_pushlstring(L, aisKey, sizeof(aisKey));
lua_rawget(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, -1, num_);
@ -955,7 +956,7 @@ static void generate_and_push_ai_table(lua_State* L, ai::engine_lua* engine) {
static size_t generate_and_push_ai_state(lua_State* L, ai::engine_lua* engine)
{
// Retrieve the ai elements table from the registry.
lua_pushlightuserdata(L, static_cast<void *>(const_cast<char *>(&aisKey)));
lua_pushlstring(L, aisKey, sizeof(aisKey));
lua_rawget(L, LUA_REGISTRYINDEX); // [-1: AIs registry table]
size_t length_ai = lua_rawlen(L, -1); // length of table
lua_newtable(L); // [-1: AI state table -2: AIs registry table]
@ -1022,7 +1023,7 @@ lua_ai_action_handler* lua_ai_action_handler::create(lua_State *L, char const *c
}
// Retrieve the ai elements table from the registry.
lua_pushlightuserdata(L, static_cast<void *>(const_cast<char *>(&aisKey)));
lua_pushlstring(L, aisKey, sizeof(aisKey));
lua_rawget(L, LUA_REGISTRYINDEX); //stack size is now 2 [-1: ais_table -2: f]
// Push the function in the table so that it is not collected.
size_t length = lua_rawlen(L, -1);//length of ais_table
@ -1054,7 +1055,7 @@ lua_ai_load::lua_ai_load(lua_ai_context& ctx, bool read_only) : L(ctx.L), was_re
return; // Leave the AI table on the stack, as requested
}
lua_pop(L, 1); // Pop the nil value off the stack
lua_pushlightuserdata(L, static_cast<void *>(const_cast<char *>(&aisKey))); // [-1: key]
lua_pushlstring(L, aisKey, sizeof(aisKey)); // [-1: key]
lua_rawget(L, LUA_REGISTRYINDEX); // [-1: AI registry]
lua_rawgeti(L, -1, ctx.num_); // [-1: AI state -2: AI registry]
lua_remove(L,-2); // [-1: AI state]
@ -1087,7 +1088,7 @@ lua_ai_load::~lua_ai_load()
lua_ai_context::~lua_ai_context()
{
// Remove the ai context from the registry, so that it can be collected.
lua_pushlightuserdata(L, static_cast<void *>(const_cast<char *>(&aisKey)));
lua_pushlstring(L, aisKey, sizeof(aisKey));
lua_rawget(L, LUA_REGISTRYINDEX);
lua_pushnil(L);
lua_rawseti(L, -2, num_);
@ -1102,7 +1103,7 @@ void lua_ai_action_handler::handle(const config &cfg, bool read_only, lua_object
lua_ai_load ctx(context_, read_only); // [-1: AI state table]
// Load the user function from the registry.
lua_pushlightuserdata(L, static_cast<void *>(const_cast<char *>(&aisKey))); // [-1: key -2: AI state]
lua_pushlstring(L, aisKey, sizeof(aisKey)); // [-1: key -2: AI state]
lua_rawget(L, LUA_REGISTRYINDEX); // [-1: AI registry -2: AI state]
lua_rawgeti(L, -1, num_); // [-1: AI action -2: AI registry -3: AI state]
lua_remove(L, -2); // [-1: AI action -2: AI state]
@ -1125,7 +1126,7 @@ void lua_ai_action_handler::handle(const config &cfg, bool read_only, lua_object
lua_ai_action_handler::~lua_ai_action_handler()
{
// Remove the function from the registry, so that it can be collected.
lua_pushlightuserdata(L, static_cast<void *>(const_cast<char *>(&aisKey)));
lua_pushlstring(L, aisKey, sizeof(aisKey));
lua_rawget(L, LUA_REGISTRYINDEX);
lua_pushnil(L);
lua_rawseti(L, -2, num_);

View file

@ -21,7 +21,6 @@
#include "ai/lua/lua_object.hpp"
#include "ai/lua/engine_lua.hpp"
#include "ai/default/aspect_attacks.hpp"
#include "scripting/lua_types.hpp"
#include "scripting/lua_common.hpp"
#include "resources.hpp"

View file

@ -863,8 +863,10 @@ void config::recursive_clear_value(const std::string& key)
values.erase(key);
for (const any_child &value : all_children_range()) {
const_cast<config *>(&value.cfg)->recursive_clear_value(key);
for(std::pair<const std::string, child_list>& p : children) {
for(config* cfg : p.second) {
cfg->recursive_clear_value(key);
}
}
}
@ -1339,8 +1341,10 @@ void config::clear_diff_track(const config& diff)
itor->second[index]->clear_diff_track(item.cfg);
}
}
for (const any_child &value : all_children_range()) {
const_cast<config *>(&value.cfg)->remove_attribute(diff_track_attribute);
for(std::pair<const std::string, child_list>& p : children) {
for(config* cfg : p.second) {
cfg->remove_attribute(diff_track_attribute);
}
}
}

View file

@ -213,9 +213,9 @@ const config &manager::get_values()
config& manager::get_value_cfg(const std::string& id)
{
{
const config& value_cfg = get_value_cfg_or_empty(id);
if (!value_cfg.empty()) {
return const_cast<config&>(value_cfg);
config& value_cfg = values_.find_child("option", "id", id);
if(value_cfg) {
return value_cfg;
}
}
@ -237,16 +237,8 @@ config& manager::get_value_cfg(const std::string& id)
const config& manager::get_value_cfg_or_empty(const std::string& id) const
{
static const config empty;
for (const config::any_child& i : values_.all_children_range()) {
for (const config& j : i.cfg.child_range("option")) {
if (j["id"] == id) {
return j;
}
}
}
return empty;
const config& cfg = values_.find_child("option", "id", id);
return cfg ? cfg : empty;
}
config::any_child manager::get_option_parent(const std::string& id) const

View file

@ -94,7 +94,7 @@ void map_labels::read(const config &cfg)
recalculate_labels();
}
const terrain_label* map_labels::get_label(const map_location& loc, const std::string& team_name) const
terrain_label* map_labels::get_label_private(const map_location& loc, const std::string& team_name)
{
team_label_map::const_iterator label_map = labels_.find(team_name);
if (label_map != labels_.end()) {

View file

@ -41,7 +41,9 @@ public:
void write(config& res) const;
void read(const config &cfg);
const terrain_label* get_label(const map_location& loc, const std::string& team_name) const;
const terrain_label* get_label(const map_location& loc, const std::string& team_name) const {
return const_cast<map_labels*>(this)->get_label_private(loc, team_name);
}
// search a team-only label, if fails then try public labels
const terrain_label* get_label(const map_location& loc) const;
const terrain_label* set_label(const map_location& loc,
@ -81,10 +83,7 @@ private:
void add_label(const map_location &, terrain_label *);
void clear_map(label_map &, bool);
/// For our private use, a wrapper for get_label() that can return a pointer
/// to a non-const terrain_label.
terrain_label* get_label_private(const map_location& loc, const std::string& team_name)
{ return const_cast<terrain_label*>(get_label(loc, team_name)); }
terrain_label* get_label_private(const map_location& loc, const std::string& team_name);
// Note: this is not an overload of get_label() so that we do not block
// outsiders from calling get_label for a non-const map_labels object.

View file

@ -458,7 +458,7 @@ static void find_routes(
}
static paths::dest_vect::iterator lower_bound(paths::dest_vect &v, const map_location &loc)
static paths::dest_vect::const_iterator lower_bound(const paths::dest_vect &v, const map_location &loc)
{
size_t sz = v.size(), pos = 0;
while (sz)
@ -473,14 +473,14 @@ static paths::dest_vect::iterator lower_bound(paths::dest_vect &v, const map_loc
paths::dest_vect::const_iterator paths::dest_vect::find(const map_location &loc) const
{
const_iterator i = lower_bound(const_cast<dest_vect &>(*this), loc), i_end = end();
const_iterator i = lower_bound(*this, loc), i_end = end();
if (i != i_end && i->curr != loc) i = i_end;
return i;
}
void paths::dest_vect::insert(const map_location &loc)
{
iterator i = lower_bound(*this, loc), i_end = end();
const_iterator i = lower_bound(*this, loc), i_end = end();
if (i != i_end && i->curr == loc) return;
paths::step s = { loc, map_location(), 0 };
std::vector<step>::insert(i, s);

View file

@ -739,7 +739,7 @@ const mp_game_settings& play_controller::get_mp_settings()
return saved_game_.mp_settings();
}
const game_classification& play_controller::get_classification()
game_classification& play_controller::get_classification()
{
return saved_game_.classification();
}

View file

@ -198,7 +198,7 @@ public:
std::shared_ptr<wb::manager> get_whiteboard();
const mp_game_settings& get_mp_settings();
const game_classification& get_classification();
game_classification& get_classification();
int get_server_request_number() const { return gamestate().server_request_number_; }
void increase_server_request_number() { ++gamestate().server_request_number_; }

View file

@ -34,7 +34,6 @@
#include "scripting/lua_cpp_function.hpp"
#include "scripting/lua_fileops.hpp"
#include "scripting/lua_kernel_base.hpp"
#include "scripting/lua_types.hpp"
#include "scripting/plugins/context.hpp"
#include "scripting/plugins/manager.hpp"

View file

@ -80,7 +80,6 @@
#include "scripting/lua_pathfind_cost_calculator.hpp"
#include "scripting/lua_race.hpp"
#include "scripting/lua_team.hpp"
#include "scripting/lua_types.hpp" // for getunitKey, dlgclbkKey, etc
#include "scripting/lua_unit_type.hpp"
#include "scripting/push_check.hpp"
#include "sdl/utils.hpp" // for surface
@ -714,14 +713,14 @@ int game_lua_kernel::intf_clear_menu_item(lua_State *L)
int game_lua_kernel::intf_set_end_campaign_credits(lua_State *L)
{
game_classification &classification = const_cast<game_classification &> (play_controller_.get_classification());
game_classification &classification = play_controller_.get_classification();
classification.end_credits = luaW_toboolean(L, 1);
return 0;
}
int game_lua_kernel::intf_set_end_campaign_text(lua_State *L)
{
game_classification &classification = const_cast<game_classification &> (play_controller_.get_classification());
game_classification &classification = play_controller_.get_classification();
classification.end_text = luaW_checktstring(L, 1);
if (lua_isnumber(L, 2)) {
classification.end_text_duration = static_cast<int> (lua_tonumber(L, 2));

View file

@ -27,7 +27,6 @@
#include "config.hpp"
#include "scripting/lua_unit.hpp"
#include "scripting/lua_types.hpp" // for gettextKey, tstringKey, etc
#include "tstring.hpp" // for t_string
#include "variable.hpp" // for vconfig
#include "log.hpp"
@ -44,11 +43,13 @@
#include "lua/lauxlib.h"
#include "lua/lua.h"
static const char * gettextKey = "gettext";
static const char * vconfigKey = "vconfig";
static const char * vconfigpairsKey = "vconfig pairs";
static const char * vconfigipairsKey = "vconfig ipairs";
const char * tstringKey = "translatable string";
// These are arrays so we can use sizeof
static const char gettextKey[] = "gettext";
static const char vconfigKey[] = "vconfig";
static const char vconfigpairsKey[] = "vconfig pairs";
static const char vconfigipairsKey[] = "vconfig ipairs";
static const char tstringKey[] = "translatable string";
static const char executeKey[] = "err";
static lg::log_domain log_scripting_lua("scripting/lua");
#define LOG_LUA LOG_STREAM(info, log_scripting_lua)
@ -536,13 +537,11 @@ bool luaW_toscalar(lua_State *L, int index, config::attribute_value& v)
return true;
}
bool luaW_hasmetatable(lua_State *L
, int index
, luatypekey key)
bool luaW_hasmetatable(lua_State *L, int index, const char* key)
{
if (!lua_getmetatable(L, index))
return false;
lua_pushlightuserdata(L, key);
lua_pushstring(L, key);
lua_rawget(L, LUA_REGISTRYINDEX);
bool ok = lua_rawequal(L, -1, -2) == 1;
lua_pop(L, 2);
@ -913,7 +912,7 @@ int luaW_pcall_internal(lua_State *L, int nArgs, int nRets);
void push_error_handler(lua_State *L)
{
lua_pushlightuserdata(L, executeKey);
lua_pushlstring(L, executeKey, sizeof(executeKey));
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
lua_remove(L, -2);
@ -924,7 +923,7 @@ void push_error_handler(lua_State *L)
int luaW_pcall_internal(lua_State *L, int nArgs, int nRets)
{
// Load the error handler before the function and its arguments.
lua_pushlightuserdata(L, executeKey);
lua_pushlstring(L, executeKey, sizeof(executeKey));
lua_rawget(L, LUA_REGISTRYINDEX);
lua_insert(L, -2 - nArgs);

View file

@ -25,7 +25,6 @@ class t_string;
class vconfig;
#include "config.hpp"
#include "scripting/lua_types.hpp"
#include "variable_info.hpp"
#include "map/location.hpp"
#include <vector>
@ -40,7 +39,6 @@ namespace lua_common {
std::string register_vconfig_metatable(lua_State *L);
}
extern const char * tstringKey;
void* operator new(size_t sz, lua_State *L);
void operator delete(void* p, lua_State *L);
@ -69,7 +67,7 @@ bool luaW_toscalar(lua_State *L, int index, config::attribute_value& v);
/**
* Returns true if the metatable of the object is the one found in the registry.
*/
bool luaW_hasmetatable(lua_State *L, int index, luatypekey key);
bool luaW_hasmetatable(lua_State *L, int index, const char* key);
/**
* Converts a scalar to a translatable string.

View file

@ -27,6 +27,9 @@
#include "resources.hpp"
#include "units/map.hpp"
// This is an array so we can use sizeof()
static const char formulaKey[] = "formula";
void luaW_pushfaivariant(lua_State* L, variant val);
variant luaW_tofaivariant(lua_State* L, int i);
@ -233,7 +236,7 @@ int lua_formula_bridge::intf_compile_formula(lua_State* L)
luaL_typerror(L, 1, "string");
}
new(L) fwrapper(lua_tostring(L, 1));
lua_pushlightuserdata(L, formulaKey);
lua_pushlstring(L, formulaKey, sizeof(formulaKey));
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
return 1;
@ -277,7 +280,7 @@ static int impl_formula_tostring(lua_State* L)
std::string lua_formula_bridge::register_metatables(lua_State* L)
{
lua_pushlightuserdata(L, formulaKey);
lua_pushlstring(L, formulaKey, sizeof(formulaKey));
lua_createtable(L, 0, 4);
lua_pushcfunction(L, impl_formula_collect);
lua_setfield(L, -2, "__gc");

View file

@ -42,7 +42,6 @@
#include "config.hpp"
#include "log.hpp"
#include "scripting/lua_common.hpp"
#include "scripting/lua_types.hpp" // for getunitKey, dlgclbkKey, etc
#include "serialization/string_utils.hpp"
#include "tstring.hpp"

View file

@ -34,7 +34,6 @@
#include "scripting/lua_gui2.hpp"
#include "scripting/lua_map_location_ops.hpp"
#include "scripting/lua_rng.hpp"
#include "scripting/lua_types.hpp"
#include "scripting/push_check.hpp"
#include "version.hpp" // for do_version_check, etc

View file

@ -1,33 +0,0 @@
/*
Copyright (C) 2014 - 2016 by David White <dave@whitevine.net>
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 as published by
the Free Software Foundation; either version 2 of the License, 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.
*/
#include "lua_types.hpp"
/* Dummy pointer for getting unique keys for Lua's registry. */
static char const v_executeKey = 0;
static char const v_getunitKey = 0;
static char const v_unitvarKey = 0;
static char const v_ustatusKey = 0;
static char const v_uattacksKey = 0;
static char const v_uattackKey = 0;
static char const v_formulaKey = 0;
luatypekey const executeKey = static_cast<void *>(const_cast<char *>(&v_executeKey));
luatypekey const getunitKey = static_cast<void *>(const_cast<char *>(&v_getunitKey));
luatypekey const unitvarKey = static_cast<void *>(const_cast<char *>(&v_unitvarKey));
luatypekey const ustatusKey = static_cast<void *>(const_cast<char *>(&v_ustatusKey));
luatypekey const uattacksKey = static_cast<void *>(const_cast<char *>(&v_uattacksKey));
luatypekey const uattackKey = static_cast<void *>(const_cast<char *>(&v_uattackKey));
luatypekey const formulaKey = static_cast<void *>(const_cast<char *>(&v_formulaKey));

View file

@ -1,30 +0,0 @@
/*
Copyright (C) 2014 - 2016 by David White <dave@whitevine.net>
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 as published by
the Free Software Foundation; either version 2 of the License, 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 SCRIPTING_LUA_TYPES_HPP
#define SCRIPTING_LUA_TYPES_HPP
typedef void* luatypekey;
// i dont want to cast to void* each time ....
// a drawback is, that these are now normal static variables wich are initialised at initialisation time (so you shoudn't use these at/before initialisation time).
extern luatypekey const executeKey;
extern luatypekey const getunitKey;
extern luatypekey const unitvarKey;
extern luatypekey const ustatusKey;
extern luatypekey const uattacksKey;
extern luatypekey const uattackKey;
extern luatypekey const formulaKey;
#endif

View file

@ -13,7 +13,6 @@
*/
#include "lua_unit.hpp"
#include "lua_types.hpp"
#include "game_board.hpp"
#include "log.hpp"
@ -33,6 +32,11 @@ static lg::log_domain log_scripting_lua("scripting/lua");
#define LOG_LUA LOG_STREAM(info, log_scripting_lua)
#define ERR_LUA LOG_STREAM(err, log_scripting_lua)
// These are arrays so we can use sizeof
static const char getunitKey[] = "unit";
static const char ustatusKey[] = "unit status";
static const char unitvarKey[] = "unit variables";
lua_unit::~lua_unit()
{
}
@ -196,7 +200,7 @@ lua_unit* luaW_checkunit_ref(lua_State *L, int index)
void lua_unit::setmetatable(lua_State *L)
{
lua_pushlightuserdata(L, getunitKey);
lua_pushlstring(L, getunitKey, sizeof(getunitKey));
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
}
@ -328,7 +332,7 @@ static int impl_unit_get(lua_State *L)
lua_createtable(L, 1, 0);
lua_pushvalue(L, 1);
lua_rawseti(L, -2, 1);
lua_pushlightuserdata(L, ustatusKey);
lua_pushlstring(L, ustatusKey, sizeof(ustatusKey));
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
return 1;
@ -337,7 +341,7 @@ static int impl_unit_get(lua_State *L)
lua_createtable(L, 1, 0);
lua_pushvalue(L, 1);
lua_rawseti(L, -2, 1);
lua_pushlightuserdata(L, unitvarKey);
lua_pushlstring(L, unitvarKey, sizeof(unitvarKey));
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
return 1;
@ -531,7 +535,7 @@ namespace lua_units {
// Create the getunit metatable.
cmd_out << "Adding getunit metatable...\n";
lua_pushlightuserdata(L, getunitKey);
lua_pushlstring(L, getunitKey, sizeof(getunitKey));
lua_createtable(L, 0, 5);
lua_pushcfunction(L, impl_unit_collect);
lua_setfield(L, -2, "__gc");
@ -548,7 +552,7 @@ namespace lua_units {
// Create the unit status metatable.
cmd_out << "Adding unit status metatable...\n";
lua_pushlightuserdata(L, ustatusKey);
lua_pushlstring(L, ustatusKey, sizeof(ustatusKey));
lua_createtable(L, 0, 3);
lua_pushcfunction(L, impl_unit_status_get);
lua_setfield(L, -2, "__index");
@ -561,7 +565,7 @@ namespace lua_units {
// Create the unit variables metatable.
cmd_out << "Adding unit variables metatable...\n";
lua_pushlightuserdata(L, unitvarKey);
lua_pushlstring(L, unitvarKey, sizeof(unitvarKey));
lua_createtable(L, 0, 3);
lua_pushcfunction(L, impl_unit_variables_get);
lua_setfield(L, -2, "__index");

View file

@ -17,7 +17,6 @@
#include <cstddef>
#include <string>
#include "lua_types.hpp" // the luatype typedef
#include "scripting/lua_common.hpp"
#include "units/ptr.hpp"

View file

@ -13,7 +13,6 @@
*/
#include "lua_unit_attacks.hpp"
#include "lua_types.hpp"
#include "scripting/lua_common.hpp"
#include "scripting/lua_unit.hpp"
@ -22,13 +21,17 @@
#include "lua/lauxlib.h"
#include "lua/lua.h" // for lua_State, lua_settop, etc
// These are arrays so we can use sizeof
static const char uattacksKey[] = "unit attacks table";
static const char uattackKey[] = "unit attack";
void push_unit_attacks_table(lua_State* L, int idx)
{
lua_createtable(L, 1, 0);
lua_pushvalue(L, idx);
// hack: store the unit_type at -1 because we want positive indices to refer to the attacks.
lua_rawseti(L, -2, -1);
lua_pushlightuserdata(L, uattacksKey);
lua_pushlstring(L, uattacksKey, sizeof(uattacksKey));
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
}
@ -84,7 +87,7 @@ static int impl_unit_attacks_get(lua_State *L)
// stack { lua_unit }, id/index, lua_unit, table, attack id
lua_rawseti(L, -2, 2);
// stack { lua_unit }, id/index, lua_unit, table
lua_pushlightuserdata(L, uattackKey);
lua_pushlstring(L, uattackKey, sizeof(uattackKey));
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
return 1;
@ -210,7 +213,7 @@ namespace lua_units {
// Create the unit attacks metatable.
cmd_out << "Adding unit attacks metatable...\n";
lua_pushlightuserdata(L, uattacksKey);
lua_pushlstring(L, uattacksKey, sizeof(uattacksKey));
lua_createtable(L, 0, 3);
lua_pushcfunction(L, impl_unit_attacks_get);
lua_setfield(L, -2, "__index");
@ -221,7 +224,7 @@ namespace lua_units {
lua_rawset(L, LUA_REGISTRYINDEX);
// Create the unit attack metatable
lua_pushlightuserdata(L, uattackKey);
lua_pushlstring(L, uattackKey, sizeof(uattackKey));
lua_createtable(L, 0, 3);
lua_pushcfunction(L, impl_unit_attack_get);
lua_setfield(L, -2, "__index");

View file

@ -2380,11 +2380,11 @@ unit& unit::clone(bool is_temporary)
}
unit_movement_resetter::unit_movement_resetter(unit &u, bool operate) :
u_(u), moves_(u.movement_left(true))
unit_movement_resetter::unit_movement_resetter(const unit &u, bool operate) :
u_(const_cast<unit&>(u)), moves_(u.movement_left(true))
{
if (operate) {
u.set_movement(u.total_movement());
u_.set_movement(u_.total_movement());
}
}

View file

@ -548,7 +548,7 @@ private:
struct unit_movement_resetter
: private boost::noncopyable
{
unit_movement_resetter(unit& u, bool operate=true);
unit_movement_resetter(const unit& u, bool operate=true);
~unit_movement_resetter();
private: