Skip to content

Commit

Permalink
Safeguard against accidentally registering an AI component twice
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel authored and mattsc committed Mar 22, 2016
1 parent e97026b commit d00cc80
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/ai/composite/aspect.cpp
Expand Up @@ -168,4 +168,14 @@ std::string lua_aspect_visitor::quote_string(const std::string& s)
}
}

// This is defined in the source file so that it can easily access the logger
bool aspect_factory::is_duplicate(const std::string& name)
{
if (get_list().find(name) != get_list().end()) {
ERR_AI_ASPECT << "Error: Attempt to double-register aspect " << name << std::endl;
return true;
}
return false;
}

} //end of namespace ai
4 changes: 4 additions & 0 deletions src/ai/composite/aspect.hpp
Expand Up @@ -461,6 +461,7 @@ class lua_aspect : public typesafe_aspect<T>


class aspect_factory{
bool is_duplicate(const std::string &name);
public:
typedef boost::shared_ptr< aspect_factory > factory_ptr;
typedef std::map<std::string, factory_ptr> factory_map;
Expand All @@ -478,6 +479,9 @@ class aspect_factory{

aspect_factory( const std::string &name )
{
if (is_duplicate(name)) {
return;
}
factory_ptr ptr_to_this(this);
get_list().insert(make_pair(name,ptr_to_this));
}
Expand Down
10 changes: 10 additions & 0 deletions src/ai/composite/engine.cpp
Expand Up @@ -147,6 +147,16 @@ readonly_context& engine::get_readonly_context()
{
return ai_;
}

// This is defined in the source file so that it can easily access the logger
bool engine_factory::is_duplicate(const std::string& name)
{
if (get_list().find(name) != get_list().end()) {
ERR_AI_ENGINE << "Error: Attempt to double-register engine " << name << std::endl;
return true;
}
return false;
}


} //end of namespace ai
4 changes: 4 additions & 0 deletions src/ai/composite/engine.hpp
Expand Up @@ -116,6 +116,7 @@ class engine : public component {
class engine_factory;

class engine_factory{
bool is_duplicate(const std::string &name);
public:
typedef boost::shared_ptr< engine_factory > factory_ptr;
typedef std::map<std::string, factory_ptr> factory_map;
Expand All @@ -134,6 +135,9 @@ class engine_factory{

engine_factory( const std::string &name )
{
if (is_duplicate(name)) {
return;
}
factory_ptr ptr_to_this(this);
get_list().insert(make_pair(name,ptr_to_this));
}
Expand Down
10 changes: 10 additions & 0 deletions src/ai/composite/goal.cpp
Expand Up @@ -366,5 +366,15 @@ void lua_goal::add_targets(std::back_insert_iterator< std::vector< target > > ta
}


// This is defined in the source file so that it can easily access the logger
bool goal_factory::is_duplicate(const std::string& name)
{
if (get_list().find(name) != get_list().end()) {
ERR_AI_GOAL << "Error: Attempt to double-register goal " << name << std::endl;
return true;
}
return false;
}


} //end of namespace ai
4 changes: 4 additions & 0 deletions src/ai/composite/goal.hpp
Expand Up @@ -179,6 +179,7 @@ class lua_goal : public goal {


class goal_factory{
bool is_duplicate(const std::string &name);
public:
typedef boost::shared_ptr< goal_factory > factory_ptr;
typedef std::map<std::string, factory_ptr> factory_map;
Expand All @@ -196,6 +197,9 @@ class goal_factory{

goal_factory( const std::string &name )
{
if (is_duplicate(name)) {
return;
}
factory_ptr ptr_to_this(this);
get_list().insert(make_pair(name,ptr_to_this));
}
Expand Down
10 changes: 10 additions & 0 deletions src/ai/composite/rca.cpp
Expand Up @@ -108,6 +108,16 @@ bool candidate_action::to_be_removed()
return to_be_removed_;
}

// This is defined in the source file so that it can easily access the logger
bool candidate_action_factory::is_duplicate(const std::string& name)
{
if (get_list().find(name) != get_list().end()) {
ERR_AI_STAGE_RCA << "Error: Attempt to double-register candidate action " << name << std::endl;
return true;
}
return false;
}

//============================================================================

} // of namespace ai
Expand Down
4 changes: 4 additions & 0 deletions src/ai/composite/rca.hpp
Expand Up @@ -150,6 +150,7 @@ typedef boost::shared_ptr<candidate_action> candidate_action_ptr;
class candidate_action_factory;

class candidate_action_factory{
bool is_duplicate(const std::string &name);
public:
typedef boost::shared_ptr< candidate_action_factory > factory_ptr;
typedef std::map<std::string, factory_ptr> factory_map;
Expand All @@ -167,6 +168,9 @@ class candidate_action_factory{

candidate_action_factory( const std::string &name )
{
if (is_duplicate(name)) {
return;
}
factory_ptr ptr_to_this(this);
get_list().insert(make_pair(name,ptr_to_this));
}
Expand Down
10 changes: 10 additions & 0 deletions src/ai/composite/stage.cpp
Expand Up @@ -110,5 +110,15 @@ bool idle_stage::do_play_stage(){
}


// This is defined in the source file so that it can easily access the logger
bool stage_factory::is_duplicate(const std::string& name)
{
if (get_list().find(name) != get_list().end()) {
ERR_AI_STAGE << "Error: Attempt to double-register stage " << name << std::endl;
return true;
}
return false;
}


} //end of namespace ai
4 changes: 4 additions & 0 deletions src/ai/composite/stage.hpp
Expand Up @@ -99,6 +99,7 @@ class idle_stage : public stage {


class stage_factory{
bool is_duplicate(const std::string &name);
public:
typedef boost::shared_ptr< stage_factory > factory_ptr;
typedef std::map<std::string, factory_ptr> factory_map;
Expand All @@ -116,6 +117,9 @@ class stage_factory{

stage_factory( const std::string &name )
{
if (is_duplicate(name)) {
return;
}
factory_ptr ptr_to_this(this);
get_list().insert(make_pair(name,ptr_to_this));
}
Expand Down
13 changes: 13 additions & 0 deletions src/ai/interface.cpp
Expand Up @@ -18,9 +18,12 @@
*/

#include "interface.hpp"
#include "log.hpp"

namespace ai {

static lg::log_domain log_ai("ai/general");

// =======================================================================
//
// =======================================================================
Expand All @@ -29,4 +32,14 @@ std::string interface::describe_self() const
return "? [ai]";
}

// This is defined in the source file so that it can easily access the logger
bool ai_factory::is_duplicate(const std::string& name)
{
if (get_list().find(name) != get_list().end()) {
LOG_STREAM(err, log_ai) << "Error: Attempt to double-register AI " << name << std::endl;
return true;
}
return false;
}

} //end of namespace ai
4 changes: 4 additions & 0 deletions src/ai/interface.hpp
Expand Up @@ -81,6 +81,7 @@ class interface : savegame::savegame_config {
class ai_factory;

class ai_factory{
bool is_duplicate(const std::string &name);
public:
typedef boost::shared_ptr< ai_factory > factory_ptr;
typedef std::map<std::string, factory_ptr> factory_map;
Expand All @@ -98,6 +99,9 @@ class ai_factory{

ai_factory( const std::string &name )
{
if (is_duplicate(name)) {
return;
}
factory_ptr ptr_to_this(this);
get_list().insert(make_pair(name,ptr_to_this));
}
Expand Down

0 comments on commit d00cc80

Please sign in to comment.