Skip to content

Commit

Permalink
Revert variant_callable handling to raw pointers temporarily
Browse files Browse the repository at this point in the history
Since there are some issues with deploying shared_ptr fully and it has not yet been done, there's the possibility
of pointers being freed twice. The raw pointer implementation results in memory leaks, but at least it should
suffice until full smart pointer support can be added.
  • Loading branch information
Vultraz committed Apr 2, 2017
1 parent 978c8d3 commit 9fe45ce
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/formula/function.cpp
Expand Up @@ -96,7 +96,7 @@ class dir_function : public function_expression {
private:
variant execute(const formula_callable& variables, formula_debugger *fdb) const {
variant var = args()[0]->evaluate(variables, fdb);
const formula_callable* callable = var.as_callable().get();
const formula_callable* callable = var.as_callable();
std::vector<formula_input> inputs = callable->inputs();
std::vector<variant> res;
for(size_t i=0; i<inputs.size(); ++i) {
Expand Down Expand Up @@ -1509,7 +1509,7 @@ variant formula_function_expression::execute(const formula_callable& variables,
variant var = args()[n]->evaluate(variables,fdb);
callable.add(arg_names_[n], var);
if(static_cast<int>(n) == star_arg_) {
callable.set_fallback(var.as_callable().get());
callable.set_fallback(var.as_callable());
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/formula/variant.hpp
Expand Up @@ -91,7 +91,7 @@ class variant

const std::string& as_string() const;

game_logic::const_formula_callable_ptr as_callable() const
const game_logic::formula_callable* as_callable() const
{
must_be(VARIANT_TYPE::TYPE_CALLABLE);
return value_cast<game_logic::variant_callable>()->get_callable();
Expand All @@ -104,13 +104,13 @@ class variant
return nullptr;
}

return dynamic_cast<T*>(const_cast<game_logic::formula_callable*>(as_callable().get()));
return dynamic_cast<T*>(const_cast<game_logic::formula_callable*>(as_callable()));
}

template<typename T>
T* convert_to() const
{
T* res = dynamic_cast<T*>(const_cast<game_logic::formula_callable*>(as_callable().get()));
T* res = dynamic_cast<T*>(const_cast<game_logic::formula_callable*>(as_callable()));
if(!res) {
throw type_error("could not convert type");
}
Expand Down
8 changes: 4 additions & 4 deletions src/formula/variant_value.cpp
Expand Up @@ -84,9 +84,9 @@ std::string variant_callable::get_debug_string(bool verbose) const
std::ostringstream ss;
ss << "{";

if(std::find(seen_stack.begin(), seen_stack.end(), callable_.get()) == seen_stack.end()) {
if(std::find(seen_stack.begin(), seen_stack.end(), callable_) == seen_stack.end()) {
if(!verbose) {
seen_stack.push_back(callable_.get());
seen_stack.push_back(callable_);
}

formula_input_vector v = callable_->inputs();
Expand Down Expand Up @@ -119,12 +119,12 @@ std::string variant_callable::get_debug_string(bool verbose) const

bool variant_callable::operator==(variant_value_base& other) const
{
return callable_->equals(value_ref_cast<variant_callable>(other).callable_.get());
return callable_->equals(value_ref_cast<variant_callable>(other).callable_);
}

bool variant_callable::operator<=(variant_value_base& other) const
{
return value_ref_cast<variant_callable>(other).callable_->less(callable_.get());
return value_ref_cast<variant_callable>(other).callable_->less(callable_);
}

std::string variant_string::get_serialized_string() const
Expand Down
4 changes: 2 additions & 2 deletions src/formula/variant_value.hpp
Expand Up @@ -275,7 +275,7 @@ class variant_callable : public virtual variant_value_base
return 1;
}

const_formula_callable_ptr get_callable() const
const formula_callable* get_callable() const
{
return callable_;
}
Expand Down Expand Up @@ -308,7 +308,7 @@ class variant_callable : public virtual variant_value_base
*/
static thread_local std::vector<const formula_callable*> seen_stack;

const_formula_callable_ptr callable_;
const formula_callable* callable_;
};


Expand Down
2 changes: 1 addition & 1 deletion src/scripting/lua_formula_bridge.cpp
Expand Up @@ -152,7 +152,7 @@ void luaW_pushfaivariant(lua_State* L, variant val) {
luaW_pushlocation(L, loc_ref->loc());
} else {
// If those fail, convert generically to a map
const formula_callable* obj = val.as_callable().get();
const formula_callable* obj = val.as_callable();
std::vector<formula_input> inputs;
obj->get_inputs(&inputs);
lua_newtable(L);
Expand Down

0 comments on commit 9fe45ce

Please sign in to comment.