ObjectManagerIF improvements #73

Merged
gaisser merged 13 commits from KSat/fsfw:mueller_objectManagerIF into master 2020-06-08 12:08:26 +02:00
2 changed files with 68 additions and 62 deletions

View File

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

std::setfill is not reset.

std::setfill is not reset.
Review

should std::setw be reset as well? how to reset std::setfill?

should std::setw be reset as well? how to reset std::setfill?
Review

okay, i put std::setfill('') at the end

okay, i put std::setfill('') at the end
std::setw(8) << std::setfill('0')<< var << " failed to "
"initialize with code 0x" << result << std::dec <<
std::setfill(' ') << std::endl;
errorCount++;
}
}
if (error_count > 0) {
sif::error << "ObjectManager::ObjectManager: Counted " << error_count
if (errorCount > 0) {
sif::error << "ObjectManager::ObjectManager: Counted " << errorCount
<< " failed initializations." << std::endl;
}
//Init was successful. Now check successful interconnections.
error_count = 0;
for (std::map<object_id_t, SystemObjectIF*>::iterator it = this->objectList.begin(); it != objectList.end(); it++ ) {
return_value = it->second->checkObjectConnections();
if ( return_value != RETURN_OK ) {
sif::error << "Object " << std::hex << (int) it->first
<< " connection check failed with code 0x" << return_value
<< std::dec << std::endl;
error_count++;
errorCount = 0;
for (auto const& it : objectList) {
result = it.second->checkObjectConnections();
if ( result != RETURN_OK ) {
sif::error << "ObjectManager::ObjectManager: Object " << std::hex <<
(int) it.first << " connection check failed with code 0x"
<< result << std::dec << std::endl;
errorCount++;
}
}
if (error_count > 0) {
sif::error << "ObjectManager::ObjectManager: Counted " << error_count
if (errorCount > 0) {
sif::error << "ObjectManager::ObjectManager: Counted " << errorCount
<< " failed connection checks." << std::endl;
}
}
void ObjectManager::printList() {
std::map<object_id_t, SystemObjectIF*>::iterator it;
sif::debug << "ObjectManager: Object List contains:" << std::endl;
for (it = this->objectList.begin(); it != this->objectList.end(); it++) {
sif::debug << std::hex << it->first << " | " << it->second << std::endl;
for (auto const& it : objectList) {
sif::debug << std::hex << it.first << " | " << it.second << std::endl;
}
}

View File

@ -1,17 +1,10 @@
/**
* @file 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_
#ifndef FRAMEWORK_OBJECTMANAGER_OBJECTMANAGERIF_H_
#define FRAMEWORK_OBJECTMANAGER_OBJECTMANAGERIF_H_
#include <framework/objectmanager/frameworkObjects.h>
#include <config/objects/systemObjectList.h>
#include <framework/objectmanager/SystemObjectIF.h>
#include <framework/returnvalues/HasReturnvaluesIF.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
/**
* @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
* object, the call checks if the object implements the requested
* interface.
* \ingroup system_objects
* @author Bastian Baetz
* @ingroup system_objects
*/
class ObjectManagerIF : public HasReturnvaluesIF {
public:
static const uint8_t INTERFACE_ID = CLASS_ID::OBJECT_MANAGER_IF;
static const ReturnValue_t INSERTION_FAILED = MAKE_RETURN_CODE( 1 );
static const ReturnValue_t NOT_FOUND = MAKE_RETURN_CODE( 2 );
static constexpr uint8_t INTERFACE_ID = CLASS_ID::OBJECT_MANAGER_IF;
static constexpr ReturnValue_t INSERTION_FAILED = MAKE_RETURN_CODE( 1 );
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:
/**
* @brief This method is used to hide the template-based get call from
@ -78,15 +75,21 @@ public:
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.
*/
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_ */