BehaviorContainer.cpp
Go to the documentation of this file.
00001 /*
00002  * BehaviorContainer.cpp
00003  *
00004  *  Created on: Nov 3, 2011
00005  *      Author: mriedel
00006  */
00007 
00008 #include <tk_behavior/BehaviorContainer.hpp>
00009 
00010 #include <boost/foreach.hpp>
00011 
00012 namespace TELEKYB_NAMESPACE {
00013 
00014 // Static Initialization
00015 //pluginlib::ClassLoader<Behavior> BehaviorContainer::behaviorLoader(
00016 //              "tk_behavior", std::string( TELEKYB_NAMESPACE_STRING ) + "::Behavior");
00017 std::set<Behavior*> BehaviorContainer::globalBehaviorInstances;
00018 
00019 
00020 BehaviorContainer::BehaviorContainer()
00021         : behaviorLoader("tk_behavior", std::string( TELEKYB_NAMESPACE_STRING ) + "::Behavior")
00022 {
00023 
00024 }
00025 
00026 BehaviorContainer::~BehaviorContainer()
00027 {
00028         // delete all Behaviors that are still loaded
00029         BOOST_FOREACH(Behavior *b, behaviorInstances) {
00030                 b->destroy();
00031                 delete b;
00032         }
00033 }
00034 
00035 Behavior* BehaviorContainer::loadBehavior(const std::string& behaviorClassName)
00036 {
00037         Behavior *b = NULL;
00038         try {
00039                 b = behaviorLoader.createClassInstance(behaviorClassName);
00040 
00041                 // inform behavior about its name.
00042                 //b->setName(behaviorClassName);
00043                 // initialize
00044                 b->initialize();
00045 
00046                 // add local
00047                 behaviorInstances.insert(b);
00048                 // add global
00049                 globalBehaviorInstances.insert(b);
00050 
00051                 // success
00052                 //ROS_INFO_STREAM("Successfully loaded Behavior: " << behaviorClassName << ", Instance: " << (long)b);
00053         } catch (pluginlib::PluginlibException &e) {
00054                 ROS_ERROR_STREAM("Behavior Plugin " << behaviorClassName << " failed to load. Message: " << e.what());
00055         }
00056 
00057         return b;
00058 }
00059 
00060 bool BehaviorContainer::unloadBehavior(Behavior* b)
00061 {
00062         if(behaviorInstances.erase(b)) {
00063                 // erase from global
00064                 globalBehaviorInstances.erase(b);
00065                 // Inform behavior
00066                 b->destroy();
00067                 // delete
00068                 delete b;
00069                 ROS_INFO_STREAM("Successfully unloaded Behavior with ID: " << (long)b);
00070                 return true;
00071         } else {
00072                 // was not element of BehaviorContainer
00073                 ROS_ERROR_STREAM("Could not delete Behavior with ID: " << (long)b << ". Did not exist!");
00074                 return false;
00075         }
00076 }
00077 
00078 void BehaviorContainer::getAvailableBehaviors(std::vector<std::string>& behaviorClassNames)
00079 {
00080         behaviorClassNames = behaviorLoader.getDeclaredClasses();
00081 }
00082 
00083 bool BehaviorContainer::behaviorInstanceExists(Behavior* instance)
00084 {
00085         return globalBehaviorInstances.count(instance) != 0;
00086 }
00087 
00088 
00089 
00090 
00091 
00092 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines


tk_behavior
Author(s): Dr. Antonio Franchi and Martin Riedel
autogenerated on Mon Nov 11 2013 11:13:36