Merge pull request 'ObjectManagerIF improvements' (#73) from KSat/fsfw:mueller_objectManagerIF into master

* Removed include does make a include of the mission object list necessary in the user code. But this is the clean way to include mission code.
* Personally, I'm not a fan of "auto" but I can see, that this is useful here.
This commit is contained in:
Steffen Gaisser 2020-06-08 12:08:26 +02:00
commit 29b051f288
2 changed files with 68 additions and 62 deletions

View File

@ -2,37 +2,38 @@
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <cstdlib> #include <cstdlib>
ObjectManager::ObjectManager( void (*setProducer)() ) : produceObjects(setProducer) { ObjectManager::ObjectManager( void (*setProducer)() ):
produceObjects(setProducer) {
//There's nothing special to do in the constructor. //There's nothing special to do in the constructor.
} }
ObjectManager::~ObjectManager() { ObjectManager::~ObjectManager() {
std::map<object_id_t, SystemObjectIF*>::iterator it; for (auto const& iter : objectList) {
for (it = this->objectList.begin(); it != this->objectList.end(); it++) { delete iter.second;
delete it->second;
} }
} }
ReturnValue_t ObjectManager::insert( object_id_t id, SystemObjectIF* object) { ReturnValue_t ObjectManager::insert( object_id_t id, SystemObjectIF* object) {
bool insert_return = this->objectList.insert( std::pair< object_id_t, SystemObjectIF* >( id, object ) ).second; auto returnPair = objectList.emplace(id, object);
if (insert_return == true) { if (returnPair.second) {
// sif::debug << "ObjectManager::insert: Object " << std::hex // sif::debug << "ObjectManager::insert: Object " << std::hex
// << (int)id << std::dec << " inserted." << std::endl; // << (int)id << std::dec << " inserted." << std::endl;
return this->RETURN_OK; return this->RETURN_OK;
} else { } else {
sif::error << "ObjectManager::insert: Object id " << std::hex sif::error << "ObjectManager::insert: Object id " << std::hex
<< (int)id << std::dec << " is already in use!" << std::endl; << (int)id << std::dec << " is already in use!" << std::endl;
exit(0); //This is very severe and difficult to handle in other places. sif::error << "Terminating program." << std::endl;
return this->INSERTION_FAILED; //This is very severe and difficult to handle in other places.
std::exit(INSERTION_FAILED);
} }
} }
ReturnValue_t ObjectManager::remove( object_id_t id ) { ReturnValue_t ObjectManager::remove( object_id_t id ) {
if ( this->getSystemObject(id) != NULL ) { if ( this->getSystemObject(id) != NULL ) {
this->objectList.erase( id ); this->objectList.erase( id );
sif::debug << "ObjectManager::removeObject: Object " << std::hex //sif::debug << "ObjectManager::removeObject: Object " << std::hex
<< (int)id << std::dec << " removed." << std::endl; // << (int)id << std::dec << " removed." << std::endl;
return RETURN_OK; return RETURN_OK;
} else { } else {
sif::error << "ObjectManager::removeObject: Requested object " sif::error << "ObjectManager::removeObject: Requested object "
@ -44,61 +45,63 @@ ReturnValue_t ObjectManager::remove( object_id_t id ) {
SystemObjectIF* ObjectManager::getSystemObject( object_id_t id ) { SystemObjectIF* ObjectManager::getSystemObject( object_id_t id ) {
std::map<object_id_t, SystemObjectIF*>::iterator it = this->objectList.find( id ); auto listIter = this->objectList.find( id );
if (it == this->objectList.end() ) { if (listIter == this->objectList.end() ) {
//Changed for testing different method. return nullptr;
// SystemObjectIF* object = this->produceObjects( id );
// return object;
return NULL;
} else { } else {
return it->second; return listIter->second;
} }
} }
ObjectManager::ObjectManager( ) : produceObjects(NULL) { ObjectManager::ObjectManager() : produceObjects(nullptr) {
} }
void ObjectManager::initialize() { void ObjectManager::initialize() {
if(produceObjects == nullptr) {
sif::error << "ObjectManager::initialize: Passed produceObjects "
"functions is nullptr!" << std::endl;
return;
}
this->produceObjects(); this->produceObjects();
ReturnValue_t return_value = RETURN_FAILED; ReturnValue_t result = RETURN_FAILED;
uint32_t error_count = 0; uint32_t errorCount = 0;
for (std::map<object_id_t, SystemObjectIF*>::iterator it = this->objectList.begin(); it != objectList.end(); it++ ) { for (auto const& it : objectList) {
return_value = it->second->initialize(); result = it.second->initialize();
if ( return_value != RETURN_OK ) { if ( result != RETURN_OK ) {
object_id_t var = it->first; object_id_t var = it.first;
sif::error << "Object " << std::hex << (int) var sif::error << "ObjectManager::initialize: Object 0x" << std::hex <<
<< " failed to initialize with code 0x" << return_value std::setw(8) << std::setfill('0')<< var << " failed to "
<< std::dec << std::endl; "initialize with code 0x" << result << std::dec <<
error_count++; std::setfill(' ') << std::endl;
errorCount++;
} }
} }
if (error_count > 0) { if (errorCount > 0) {
sif::error << "ObjectManager::ObjectManager: Counted " << error_count sif::error << "ObjectManager::ObjectManager: Counted " << errorCount
<< " failed initializations." << std::endl; << " failed initializations." << std::endl;
} }
//Init was successful. Now check successful interconnections. //Init was successful. Now check successful interconnections.
error_count = 0; errorCount = 0;
for (std::map<object_id_t, SystemObjectIF*>::iterator it = this->objectList.begin(); it != objectList.end(); it++ ) { for (auto const& it : objectList) {
return_value = it->second->checkObjectConnections(); result = it.second->checkObjectConnections();
if ( return_value != RETURN_OK ) { if ( result != RETURN_OK ) {
sif::error << "Object " << std::hex << (int) it->first sif::error << "ObjectManager::ObjectManager: Object " << std::hex <<
<< " connection check failed with code 0x" << return_value (int) it.first << " connection check failed with code 0x"
<< std::dec << std::endl; << result << std::dec << std::endl;
error_count++; errorCount++;
} }
} }
if (error_count > 0) { if (errorCount > 0) {
sif::error << "ObjectManager::ObjectManager: Counted " << error_count sif::error << "ObjectManager::ObjectManager: Counted " << errorCount
<< " failed connection checks." << std::endl; << " failed connection checks." << std::endl;
} }
} }
void ObjectManager::printList() { void ObjectManager::printList() {
std::map<object_id_t, SystemObjectIF*>::iterator it; std::map<object_id_t, SystemObjectIF*>::iterator it;
sif::debug << "ObjectManager: Object List contains:" << std::endl; sif::debug << "ObjectManager: Object List contains:" << std::endl;
for (it = this->objectList.begin(); it != this->objectList.end(); it++) { for (auto const& it : objectList) {
sif::debug << std::hex << it->first << " | " << it->second << std::endl; sif::debug << std::hex << it.first << " | " << it.second << std::endl;
} }
} }

View File

@ -1,17 +1,10 @@
/** #ifndef FRAMEWORK_OBJECTMANAGER_OBJECTMANAGERIF_H_
* @file ObjectManagerIF.h #define FRAMEWORK_OBJECTMANAGER_OBJECTMANAGERIF_H_
* @brief This file contains the implementation of the ObjectManagerIF interface
* @date 19.09.2012
* @author Bastian Baetz
*/
#ifndef OBJECTMANAGERIF_H_
#define OBJECTMANAGERIF_H_
#include <framework/objectmanager/frameworkObjects.h> #include <framework/objectmanager/frameworkObjects.h>
#include <config/objects/systemObjectList.h>
#include <framework/objectmanager/SystemObjectIF.h> #include <framework/objectmanager/SystemObjectIF.h>
#include <framework/returnvalues/HasReturnvaluesIF.h> #include <framework/returnvalues/HasReturnvaluesIF.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
/** /**
* @brief This class provides an interface to the global object manager. * @brief This class provides an interface to the global object manager.
@ -20,13 +13,17 @@
* inserted, removed and retrieved from the list. On getting the * inserted, removed and retrieved from the list. On getting the
* object, the call checks if the object implements the requested * object, the call checks if the object implements the requested
* interface. * interface.
* \ingroup system_objects * @author Bastian Baetz
* @ingroup system_objects
*/ */
class ObjectManagerIF : public HasReturnvaluesIF { class ObjectManagerIF : public HasReturnvaluesIF {
public: public:
static const uint8_t INTERFACE_ID = CLASS_ID::OBJECT_MANAGER_IF; static constexpr uint8_t INTERFACE_ID = CLASS_ID::OBJECT_MANAGER_IF;
static const ReturnValue_t INSERTION_FAILED = MAKE_RETURN_CODE( 1 ); static constexpr ReturnValue_t INSERTION_FAILED = MAKE_RETURN_CODE( 1 );
static const ReturnValue_t NOT_FOUND = MAKE_RETURN_CODE( 2 ); static constexpr ReturnValue_t NOT_FOUND = MAKE_RETURN_CODE( 2 );
static constexpr ReturnValue_t CHILD_INIT_FAILED = MAKE_RETURN_CODE( 3 );
static constexpr ReturnValue_t INTERNAL_ERR_REPORTER_UNINIT = MAKE_RETURN_CODE( 4 );
protected: protected:
/** /**
* @brief This method is used to hide the template-based get call from * @brief This method is used to hide the template-based get call from
@ -78,15 +75,21 @@ public:
virtual void printList() = 0; virtual void printList() = 0;
}; };
template <typename T>
T* ObjectManagerIF::get( object_id_t id ) {
SystemObjectIF* temp = this->getSystemObject(id);
return dynamic_cast<T*>(temp);
}
/** /**
* @brief This is the forward declaration of the global objectManager instance. * @brief This is the forward declaration of the global objectManager instance.
*/ */
extern ObjectManagerIF *objectManager; extern ObjectManagerIF *objectManager;
/*Documentation can be found in the class method declaration above.*/
template <typename T>
T* ObjectManagerIF::get( object_id_t id ) {
if(objectManager == nullptr) {
sif::error << "ObjectManagerIF: Global object manager has not "
"been initialized yet!" << std::endl;
}
SystemObjectIF* temp = this->getSystemObject(id);
return dynamic_cast<T*>(temp);
}
#endif /* OBJECTMANAGERIF_H_ */ #endif /* OBJECTMANAGERIF_H_ */