Skip to content

Commit

Permalink
Avoid member function call with this == null
Browse files Browse the repository at this point in the history
This member function of oracle_t was just a constant function.  It
didn't need to be a member function, so I made it a free function.

In the tests it was being called on null oracle_t* values.
  • Loading branch information
jbytheway committed Jan 25, 2020
1 parent dbfa7f1 commit a1ee2fe
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/behavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void behavior::load_behavior( const JsonObject &jo, const std::string &src )

node_t::node_t()
{
predicate = &oracle_t::return_running;
predicate = &return_running;
}

void node_t::load( const JsonObject &jo, const std::string & )
Expand Down
7 changes: 3 additions & 4 deletions src/behavior_oracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
#include "optional.h"
#include "ret_val.h"

using namespace behavior;
namespace behavior
{

status_t oracle_t::return_running() const
status_t return_running( const oracle_t * )
{
return running;
}
Expand Down Expand Up @@ -129,8 +130,6 @@ make_function( status_t ( character_oracle_t::* fun )() const )
return static_cast<status_t ( oracle_t::* )() const>( fun );
}

namespace behavior
{
std::unordered_map<std::string, std::function<status_t( const oracle_t * )>> predicate_map = {{
{ "npc_needs_warmth_badly", make_function( &character_oracle_t::needs_warmth_badly ) },
{ "npc_needs_water_badly", make_function( &character_oracle_t::needs_water_badly ) },
Expand Down
4 changes: 2 additions & 2 deletions src/behavior_oracle.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ enum status_t : char;
*/
class oracle_t
{
public:
status_t return_running() const;
};

status_t return_running( const oracle_t * );

class character_oracle_t : public oracle_t
{
public:
Expand Down

0 comments on commit a1ee2fe

Please sign in to comment.