Skip to content

Commit

Permalink
Reduce use of const_cast
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Aug 12, 2016
1 parent 4bb5d2c commit de72bec
Show file tree
Hide file tree
Showing 25 changed files with 79 additions and 152 deletions.
6 changes: 0 additions & 6 deletions projectfiles/Xcode/Wesnoth.xcodeproj/project.pbxproj
Expand Up @@ -279,7 +279,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 */; };
Expand Down Expand Up @@ -1260,7 +1259,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 */; };
Expand Down Expand Up @@ -4144,8 +4142,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 */,
Expand Down Expand Up @@ -4953,7 +4949,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 */,
Expand Down Expand Up @@ -5612,7 +5607,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 */,
Expand Down
3 changes: 1 addition & 2 deletions src/ai/contexts.cpp
Expand Up @@ -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) {
Expand Down
25 changes: 13 additions & 12 deletions src/ai/lua/core.cpp
Expand Up @@ -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 {

Expand All @@ -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);
}
Expand All @@ -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_);

Expand All @@ -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_);

Expand All @@ -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_);

Expand All @@ -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_);

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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_);
Expand All @@ -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]
Expand All @@ -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_);
Expand Down
1 change: 0 additions & 1 deletion src/ai/lua/lua_object.cpp
Expand Up @@ -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"

Expand Down
12 changes: 8 additions & 4 deletions src/config.cpp
Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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);
}
}
}

Expand Down
18 changes: 5 additions & 13 deletions src/game_initialization/mp_options.cpp
Expand Up @@ -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;
}
}

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/map/label.cpp
Expand Up @@ -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()) {
Expand Down
9 changes: 4 additions & 5 deletions src/map/label.hpp
Expand Up @@ -41,7 +41,9 @@ class map_labels
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,
Expand Down Expand Up @@ -81,10 +83,7 @@ class map_labels
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.

Expand Down
6 changes: 3 additions & 3 deletions src/pathfind/pathfind.cpp
Expand Up @@ -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)
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/play_controller.cpp
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/play_controller.hpp
Expand Up @@ -198,7 +198,7 @@ class play_controller : public controller_base, public events::observer, 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_; }

Expand Down
1 change: 0 additions & 1 deletion src/scripting/application_lua_kernel.cpp
Expand Up @@ -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"

Expand Down
5 changes: 2 additions & 3 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down
23 changes: 11 additions & 12 deletions src/scripting/lua_common.cpp
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -535,13 +536,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);
Expand Down Expand Up @@ -912,7 +911,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);
Expand All @@ -923,7 +922,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);
Expand Down

0 comments on commit de72bec

Please sign in to comment.