2016-06-15 23:48:41 +02:00
|
|
|
#include <framework/objectmanager/ObjectManager.h>
|
|
|
|
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2020-06-04 21:11:49 +02:00
|
|
|
ObjectManager::ObjectManager( void (*setProducer)() ):
|
|
|
|
produceObjects(setProducer) {
|
2016-06-15 23:48:41 +02:00
|
|
|
//There's nothing special to do in the constructor.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ObjectManager::~ObjectManager() {
|
2020-06-04 21:24:44 +02:00
|
|
|
for (auto const& iter : objectList) {
|
|
|
|
delete iter.second;
|
|
|
|
}
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t ObjectManager::insert( object_id_t id, SystemObjectIF* object) {
|
2020-06-04 21:11:49 +02:00
|
|
|
auto returnPair = objectList.emplace(id, object);
|
|
|
|
if (returnPair.second) {
|
2020-04-23 19:13:18 +02:00
|
|
|
// sif::debug << "ObjectManager::insert: Object " << std::hex
|
|
|
|
// << (int)id << std::dec << " inserted." << std::endl;
|
2016-06-15 23:48:41 +02:00
|
|
|
return this->RETURN_OK;
|
|
|
|
} else {
|
2020-04-23 19:13:18 +02:00
|
|
|
sif::error << "ObjectManager::insert: Object id " << std::hex
|
|
|
|
<< (int)id << std::dec << " is already in use!" << std::endl;
|
2020-06-04 21:04:24 +02:00
|
|
|
//This is very severe and difficult to handle in other places.
|
|
|
|
std::exit(INSERTION_FAILED);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t ObjectManager::remove( object_id_t id ) {
|
|
|
|
if ( this->getSystemObject(id) != NULL ) {
|
|
|
|
this->objectList.erase( id );
|
2020-04-23 19:13:18 +02:00
|
|
|
sif::debug << "ObjectManager::removeObject: Object " << std::hex
|
|
|
|
<< (int)id << std::dec << " removed." << std::endl;
|
2016-06-15 23:48:41 +02:00
|
|
|
return RETURN_OK;
|
|
|
|
} else {
|
2020-04-23 19:13:18 +02:00
|
|
|
sif::error << "ObjectManager::removeObject: Requested object "
|
|
|
|
<< std::hex << (int)id << std::dec << " not found." << std::endl;
|
2016-06-15 23:48:41 +02:00
|
|
|
return NOT_FOUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SystemObjectIF* ObjectManager::getSystemObject( object_id_t id ) {
|
2020-06-04 21:11:49 +02:00
|
|
|
auto listIter = this->objectList.find( id );
|
|
|
|
if (listIter == this->objectList.end() ) {
|
|
|
|
return nullptr;
|
2016-06-15 23:48:41 +02:00
|
|
|
} else {
|
2020-06-04 21:11:49 +02:00
|
|
|
return listIter->second;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:11:49 +02:00
|
|
|
ObjectManager::ObjectManager() : produceObjects(nullptr) {
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectManager::initialize() {
|
2020-06-04 21:14:35 +02:00
|
|
|
if(produceObjects == nullptr) {
|
|
|
|
sif::error << "ObjectManager: Passed produceObjects functions is"
|
|
|
|
"nullptr!" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
2016-06-15 23:48:41 +02:00
|
|
|
this->produceObjects();
|
|
|
|
ReturnValue_t return_value = RETURN_FAILED;
|
|
|
|
uint32_t error_count = 0;
|
2020-06-04 21:27:37 +02:00
|
|
|
for (auto const& it : objectList) {
|
|
|
|
return_value = it.second->initialize();
|
2016-06-15 23:48:41 +02:00
|
|
|
if ( return_value != RETURN_OK ) {
|
2020-06-04 21:27:37 +02:00
|
|
|
object_id_t var = it.first;
|
2020-06-06 12:59:43 +02:00
|
|
|
sif::error << "ObjectManager::initialize: Object 0x" << std::hex <<
|
|
|
|
std::setw(8) << std::setfill('0')<< var << " failed to "
|
|
|
|
"initialize with code 0x" << return_value << std::dec <<
|
|
|
|
std::endl;
|
2016-06-15 23:48:41 +02:00
|
|
|
error_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error_count > 0) {
|
2020-04-23 19:13:18 +02:00
|
|
|
sif::error << "ObjectManager::ObjectManager: Counted " << error_count
|
|
|
|
<< " failed initializations." << std::endl;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
//Init was successful. Now check successful interconnections.
|
|
|
|
error_count = 0;
|
2020-06-04 21:27:37 +02:00
|
|
|
for (auto const& it : objectList) {
|
|
|
|
return_value = it.second->checkObjectConnections();
|
2016-06-15 23:48:41 +02:00
|
|
|
if ( return_value != RETURN_OK ) {
|
2020-06-04 21:27:37 +02:00
|
|
|
sif::error << "Object " << std::hex << (int) it.first
|
2020-04-23 19:13:18 +02:00
|
|
|
<< " connection check failed with code 0x" << return_value
|
|
|
|
<< std::dec << std::endl;
|
2016-06-15 23:48:41 +02:00
|
|
|
error_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error_count > 0) {
|
2020-04-23 19:13:18 +02:00
|
|
|
sif::error << "ObjectManager::ObjectManager: Counted " << error_count
|
|
|
|
<< " failed connection checks." << std::endl;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectManager::printList() {
|
|
|
|
std::map<object_id_t, SystemObjectIF*>::iterator it;
|
2020-04-23 19:13:18 +02:00
|
|
|
sif::debug << "ObjectManager: Object List contains:" << std::endl;
|
2020-06-04 21:24:44 +02:00
|
|
|
for (auto const& it : objectList) {
|
|
|
|
sif::debug << std::hex << it.first << " | " << it.second << std::endl;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|