Skip to content

Commit

Permalink
vconfig optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
gfgtdf committed Aug 27, 2017
1 parent c6e5c57 commit dac7829
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/ai/lua/lua_object.cpp
Expand Up @@ -47,8 +47,7 @@ namespace ai {
}
lua_getfield(L, n, "own");
if(lua_istable(L, -1)) {
config cfg;
vconfig vcfg(cfg, true);
vconfig vcfg(config(), true);
if(luaW_tovconfig(L, -1, vcfg)) {
att->filter_own_.reset(new unit_filter(vcfg, resources::filter_con));
}
Expand All @@ -59,8 +58,7 @@ namespace ai {
}
lua_getfield(L, n, "enemy");
if(lua_istable(L, -1)) {
config cfg;
vconfig vcfg(cfg, true);
vconfig vcfg(config(), true);
if(luaW_tovconfig(L, -1, vcfg)) {
att->filter_enemy_.reset(new unit_filter(vcfg, resources::filter_con));
}
Expand Down
2 changes: 1 addition & 1 deletion src/scripting/lua_common.cpp
Expand Up @@ -799,7 +799,7 @@ bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg)
config cfg;
bool ok = luaW_toconfig(L, index, cfg);
if (!ok) return false;
vcfg = vconfig(cfg, true);
vcfg = vconfig(std::move(cfg));
break;
}
case LUA_TUSERDATA:
Expand Down
5 changes: 3 additions & 2 deletions src/variable.cpp
Expand Up @@ -124,16 +124,17 @@ vconfig vconfig::unconstructed_vconfig()
* It is perfectly safe to call this for a vconfig that already manages its memory.
* This does not work on a null() vconfig.
*/
void vconfig::make_safe() const
const vconfig& vconfig::make_safe() const
{
// Nothing to do if we already manage our own memory.
if ( memory_managed() )
return;
return *this;

// Make a copy of our config.
cache_.reset(new config(*cfg_));
// Use our copy instead of the original.
cfg_ = cache_.get();
return *this;
}

config vconfig::get_parsed_config() const
Expand Down
3 changes: 2 additions & 1 deletion src/variable.hpp
Expand Up @@ -60,6 +60,7 @@ class vconfig
/// Equivalent to vconfig(cfg, false).
/// Do not use if the vconfig will persist after @a cfg is destroyed!
explicit vconfig(const config &cfg) : cache_(), cfg_(&cfg) {}
explicit vconfig(config &&cfg) : cache_(new config(std::move(cfg))), cfg_(cache_.get()) { }
vconfig(const config &cfg, bool manage_memory);
~vconfig();

Expand All @@ -70,7 +71,7 @@ class vconfig
explicit operator bool() const { return !null(); }

bool null() const { assert(cfg_); return cfg_ == &default_empty_config; }
void make_safe() const; //!< instruct the vconfig to make a private copy of its underlying data.
const vconfig& make_safe() const; //!< instruct the vconfig to make a private copy of its underlying data.
const config& get_config() const { return *cfg_; }
config get_parsed_config() const;

Expand Down

0 comments on commit dac7829

Please sign in to comment.