From 848c72a517e273d8d445fb99417c7506c6968a3a Mon Sep 17 00:00:00 2001 From: Linas Vepstas Date: Thu, 3 Oct 2013 19:09:30 -0500 Subject: [PATCH] Convert more examples to new API. --- examples/modules/SingleAgentModule.cc | 22 +++++++++++----------- examples/modules/SingleAgentModule.h | 2 ++ opencog/server/CogServer.cc | 1 - 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/examples/modules/SingleAgentModule.cc b/examples/modules/SingleAgentModule.cc index 12bc3d61eaa..828ddc1b9bc 100644 --- a/examples/modules/SingleAgentModule.cc +++ b/examples/modules/SingleAgentModule.cc @@ -39,17 +39,15 @@ extern "C" const char* opencog_module_id() extern "C" Module* opencog_module_load(CogServer& cogserver) { cogserver.registerAgent(SingleAgentModule::info().id, &(SingleAgentModule::factory())); - SingleAgentModule* agent = - static_cast(cogserver.createAgent(SingleAgentModule::info().id, true)); + SingleAgentModulePtr agent = cogserver.createAgent(true); agent->name = "OriginalSingleAgentModule"; - return agent; + return agent.get(); } extern "C" void opencog_module_unload(Module* m) { SingleAgentModule* agent = static_cast(m); agent->stopAgent(); - delete m; } SingleAgentModule::SingleAgentModule(CogServer& cs) : Agent(cs, 100), Module(cs) @@ -65,7 +63,8 @@ SingleAgentModule::~SingleAgentModule() void SingleAgentModule::stopAgent() { - Module::_cogserver.stopAgent(this); + AgentPtr age = std::dynamic_pointer_cast(shared_from_this()); + Module::_cogserver.stopAgent(age); } void SingleAgentModule::run() @@ -77,13 +76,14 @@ void SingleAgentModule::init() { logger().info("[TestModule] init (%s)", name.c_str()); - SingleAgentModule* a = - static_cast(Module::_cogserver.createAgent(info().id, true)); + // Use static global vars -- the egent is destroyed when these go + // out of scope (during the C++ finalizer). + static SingleAgentModulePtr a = Module::_cogserver.createAgent(true); a->name = "SingleAgentModule1"; - a = static_cast(Module::_cogserver.createAgent(info().id, true)); - a->name = "SingleAgentModule2"; + static SingleAgentModulePtr b = Module::_cogserver.createAgent(true); + b->name = "SingleAgentModule2"; - a = static_cast(Module::_cogserver.createAgent(info().id, true)); - a->name = "SingleAgentModule3"; + static SingleAgentModulePtr c = Module::_cogserver.createAgent(true); + c->name = "SingleAgentModule3"; } diff --git a/examples/modules/SingleAgentModule.h b/examples/modules/SingleAgentModule.h index 6e734f66469..ad9e74061e7 100644 --- a/examples/modules/SingleAgentModule.h +++ b/examples/modules/SingleAgentModule.h @@ -62,6 +62,8 @@ class SingleAgentModule : public Agent, public Module }; // class +typedef std::shared_ptr SingleAgentModulePtr; + } // namespace opencog #endif // _OPENCOG_SINGLE_AGENT_MODULE_H diff --git a/opencog/server/CogServer.cc b/opencog/server/CogServer.cc index 2c1ba8d4760..28347468fad 100644 --- a/opencog/server/CogServer.cc +++ b/opencog/server/CogServer.cc @@ -363,7 +363,6 @@ void CogServer::destroyAgent(AgentPtr agent) { stopAgent(agent); logger().debug("[CogServer] deleting agent \"%s\"", agent->to_string().c_str()); - // delete agent; } void CogServer::destroyAllAgents(const std::string& id)