Today's the day. Renamed platform to framework.
This commit is contained in:
106
objectmanager/ObjectManager.cpp
Normal file
106
objectmanager/ObjectManager.cpp
Normal file
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* ObjectManager.cpp
|
||||
*
|
||||
* Created on: Sep 18, 2012
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#include <framework/objectmanager/ObjectManager.h>
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
#include <cstdlib>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
// debug << "ObjectManager::insert: Object " << std::hex << (int)id << std::dec << " inserted." << std::endl;
|
||||
return this->RETURN_OK;
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t ObjectManager::remove( object_id_t id ) {
|
||||
std::map<object_id_t, SystemObjectIF*>::iterator it = this->objectList.find( id );
|
||||
if ( this->getSystemObject(id) != NULL ) {
|
||||
this->objectList.erase( id );
|
||||
debug << "ObjectManager::removeObject: Object " << std::hex << (int)id << std::dec << " removed." << std::endl;
|
||||
return RETURN_OK;
|
||||
} else {
|
||||
error << "ObjectManager::removeObject: Requested object "<< std::hex << (int)id << std::dec << " not found." << std::endl;
|
||||
return NOT_FOUND;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
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;
|
||||
} else {
|
||||
return it->second;
|
||||
}
|
||||
}
|
||||
|
||||
ObjectManager::ObjectManager( ) : produceObjects(NULL) {
|
||||
|
||||
}
|
||||
|
||||
void ObjectManager::initialize() {
|
||||
//TODO: Include check if already initialized?
|
||||
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;
|
||||
error << "Object " << std::hex << (int) var << " failed to initialize with code 0x" << return_value << std::dec << std::endl;
|
||||
error_count++;
|
||||
}
|
||||
}
|
||||
if (error_count > 0) {
|
||||
error << "ObjectManager::ObjectManager: Counted " << error_count << " 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 ) {
|
||||
error << "Object " << std::hex << (int) it->first << " connection check failed with code 0x" << return_value << std::dec << std::endl;
|
||||
error_count++;
|
||||
}
|
||||
}
|
||||
if (error_count > 0) {
|
||||
error << "ObjectManager::ObjectManager: Counted " << error_count << " failed connection checks." << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ObjectManager::printList() {
|
||||
std::map<object_id_t, SystemObjectIF*>::iterator it;
|
||||
debug << "ObjectManager: Object List contains:" << std::endl;
|
||||
for (it = this->objectList.begin(); it != this->objectList.end(); it++) {
|
||||
debug << std::hex << it->first << " | " << it->second << std::endl;
|
||||
}
|
||||
}
|
67
objectmanager/ObjectManager.h
Normal file
67
objectmanager/ObjectManager.h
Normal file
@ -0,0 +1,67 @@
|
||||
/**
|
||||
* @file ObjectManager.h
|
||||
* @brief This file contains the implementation of the ObjectManager class
|
||||
* @date 18.09.2012
|
||||
* @author Bastian Baetz
|
||||
*/
|
||||
|
||||
#ifndef OBJECTMANAGER_H_
|
||||
#define OBJECTMANAGER_H_
|
||||
|
||||
#include <framework/objectmanager/ObjectManagerIF.h>
|
||||
#include <framework/objectmanager/SystemObjectIF.h>
|
||||
#include <map>
|
||||
|
||||
/**
|
||||
* @brief This class implements a global object manager.
|
||||
* @details This manager handles a list of available objects with system-wide
|
||||
* relevance, such as device handlers, and TM/TC services. Objects can
|
||||
* be inserted, removed and retrieved from the list. In addition, the
|
||||
* class holds a so-called factory, that creates and inserts new
|
||||
* objects if they are not already in the list. This feature automates
|
||||
* most of the system initialization.
|
||||
* As the system is static after initialization, no new objects are
|
||||
* created or inserted into the list after startup.
|
||||
* \ingroup system_objects
|
||||
*/
|
||||
class ObjectManager : public ObjectManagerIF {
|
||||
private:
|
||||
//comparison?
|
||||
/**
|
||||
* \brief This is the map of all initialized objects in the manager.
|
||||
* \details Objects in the List must inherit the SystemObjectIF.
|
||||
*/
|
||||
std::map<object_id_t, SystemObjectIF*> objectList;
|
||||
protected:
|
||||
SystemObjectIF* getSystemObject( object_id_t id );
|
||||
/**
|
||||
* @brief This attribute is initialized with the factory function
|
||||
* that creates new objects.
|
||||
* @details The function is called if an object was requested with
|
||||
* getSystemObject, but not found in objectList.
|
||||
* @param The id of the object to be created.
|
||||
* @return Returns a pointer to the newly created object or NULL.
|
||||
*/
|
||||
void (*produceObjects)();
|
||||
public:
|
||||
/**
|
||||
* @brief Apart from setting the producer function, nothing special
|
||||
* happens in the constructor.
|
||||
* @param setProducer A pointer to a factory function.
|
||||
*/
|
||||
ObjectManager( void (*produce)() );
|
||||
ObjectManager();
|
||||
/**
|
||||
* @brief In the class's destructor, all objects in the list are deleted.
|
||||
*/
|
||||
//TODO: Check, if deleting works!
|
||||
virtual ~ObjectManager( void );
|
||||
ReturnValue_t insert( object_id_t id, SystemObjectIF* object );
|
||||
ReturnValue_t remove( object_id_t id );
|
||||
void initialize();
|
||||
void printList();
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif /* OBJECTMANAGER_H_ */
|
91
objectmanager/ObjectManagerIF.h
Normal file
91
objectmanager/ObjectManagerIF.h
Normal file
@ -0,0 +1,91 @@
|
||||
/**
|
||||
* @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_
|
||||
|
||||
#include <config/objects/systemObjectList.h>
|
||||
#include <framework/objectmanager/SystemObjectIF.h>
|
||||
#include <framework/returnvalues/HasReturnvaluesIF.h>
|
||||
|
||||
/**
|
||||
* @brief This class provides an interface to the global object manager.
|
||||
* @details This manager handles a list of available objects with system-wide
|
||||
* relevance, such as device handlers, and TM/TC services. They can be
|
||||
* inserted, removed and retrieved from the list. On getting the
|
||||
* object, the call checks if the object implements the requested
|
||||
* interface.
|
||||
* \ingroup system_objects
|
||||
*/
|
||||
class ObjectManagerIF : public HasReturnvaluesIF {
|
||||
public:
|
||||
static const uint8_t INTERFACE_ID = OBJECT_MANAGER_IF;
|
||||
static const ReturnValue_t INSERTION_FAILED = MAKE_RETURN_CODE( 1 );
|
||||
static const ReturnValue_t NOT_FOUND = MAKE_RETURN_CODE( 2 );
|
||||
protected:
|
||||
/**
|
||||
* @brief This method is used to hide the template-based get call from
|
||||
* a specific implementation.
|
||||
* @details So, an implementation only has to implement this call.
|
||||
* @param id The object id of the requested object.
|
||||
* @return The method returns a pointer to an object implementing (at
|
||||
* least) the SystemObjectIF, or NULL.
|
||||
*/
|
||||
virtual SystemObjectIF* getSystemObject( object_id_t id ) = 0;
|
||||
public:
|
||||
/**
|
||||
* @brief This is the empty virtual destructor as requested by C++ interfaces.
|
||||
*/
|
||||
virtual ~ObjectManagerIF( void ) {};
|
||||
/**
|
||||
* @brief With this call, new objects are inserted to the list.
|
||||
* @details The implementation shall return an error code in case the
|
||||
* object can't be added (e.g. the id is already in use).
|
||||
* @param id The new id to be added to the list.
|
||||
* @param object A pointer to the object to be added.
|
||||
* @return @li INSERTION_FAILED in case the object could not be inserted.
|
||||
* @li RETURN_OK in case the object was successfully inserted
|
||||
*/
|
||||
virtual ReturnValue_t insert( object_id_t id, SystemObjectIF* object ) = 0;
|
||||
/**
|
||||
* @brief With the get call, interfaces of an object can be retrieved in
|
||||
* a type-safe manner.
|
||||
* @details With the template-based call, the object list is searched with the
|
||||
* getSystemObject method and afterwards it is checked, if the object
|
||||
* implements the requested interface (with a dynamic_cast).
|
||||
* @param id The object id of the requested object.
|
||||
* @return The method returns a pointer to an object implementing the
|
||||
* requested interface, or NULL.
|
||||
*/
|
||||
template <typename T> T* get( object_id_t id );
|
||||
/**
|
||||
* @brief With this call, an object is removed from the list.
|
||||
* @param id The object id of the object to be removed.
|
||||
* @return \li NOT_FOUND in case the object was not found
|
||||
* \li RETURN_OK in case the object was successfully removed
|
||||
*/
|
||||
virtual ReturnValue_t remove( object_id_t id ) = 0;
|
||||
virtual void initialize() = 0;
|
||||
/**
|
||||
* @brief This is a debug function, that prints the current content of the
|
||||
* object list.
|
||||
*/
|
||||
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;
|
||||
|
||||
#endif /* OBJECTMANAGERIF_H_ */
|
44
objectmanager/SystemObject.cpp
Normal file
44
objectmanager/SystemObject.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* SystemObject.cpp
|
||||
*
|
||||
* Created on: 07.11.2012
|
||||
* Author: mohr
|
||||
*/
|
||||
|
||||
#include <framework/events/EventManagerIF.h>
|
||||
#include <framework/objectmanager/ObjectManager.h>
|
||||
#include <framework/objectmanager/SystemObject.h>
|
||||
|
||||
SystemObject::SystemObject(object_id_t setObjectId, bool doRegister) :
|
||||
objectId(setObjectId), registered(doRegister) {
|
||||
if (registered) {
|
||||
objectManager->insert(objectId, this);
|
||||
}
|
||||
}
|
||||
|
||||
SystemObject::~SystemObject() {
|
||||
if (registered) {
|
||||
objectManager->remove(objectId);
|
||||
}
|
||||
}
|
||||
|
||||
const object_id_t SystemObject::getObjectId() const {
|
||||
return objectId;
|
||||
}
|
||||
|
||||
void SystemObject::triggerEvent(Event event, uint32_t parameter1,
|
||||
uint32_t parameter2) {
|
||||
EventManagerIF::triggerEvent(objectId, event, parameter1, parameter2);
|
||||
}
|
||||
|
||||
ReturnValue_t SystemObject::initialize() {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t SystemObject::checkObjectConnections() {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
void SystemObject::forwardEvent(Event event, uint32_t parameter1, uint32_t parameter2) const {
|
||||
EventManagerIF::triggerEvent(objectId, event, parameter1, parameter2);
|
||||
}
|
61
objectmanager/SystemObject.h
Normal file
61
objectmanager/SystemObject.h
Normal file
@ -0,0 +1,61 @@
|
||||
/**
|
||||
* @file SystemObject.h
|
||||
* @brief This file contains the definition of the SystemObject class.
|
||||
* @date 07.11.2012
|
||||
* @author Ulrich Mohr
|
||||
*/
|
||||
|
||||
#ifndef SYSTEMOBJECT_H_
|
||||
#define SYSTEMOBJECT_H_
|
||||
|
||||
#include <framework/events/Event.h>
|
||||
#include <framework/events/EventReportingProxyIF.h>
|
||||
#include <framework/ipc/MessageQueue.h>
|
||||
#include <framework/objectmanager/SystemObjectIF.h>
|
||||
|
||||
/**
|
||||
* @brief This class automates insertion into the ObjectManager and
|
||||
* management of the object id.
|
||||
* @details This class is more a base class, which shall be inherited by any
|
||||
* class that is announced to ObjectManager. It automatically includes
|
||||
* itself (and therefore the inheriting class) in the object manager's
|
||||
* list.
|
||||
* \ingroup system_objects
|
||||
*/
|
||||
class SystemObject: public SystemObjectIF {
|
||||
private:
|
||||
/**
|
||||
* @brief This is the id the class instant got assigned.
|
||||
*/
|
||||
const object_id_t objectId;
|
||||
const bool registered;
|
||||
public:
|
||||
|
||||
/**
|
||||
* Helper function to send Event Messages to the Event Manager
|
||||
* @param event
|
||||
* @param parameter1
|
||||
* @param parameter2
|
||||
*/
|
||||
virtual void triggerEvent(Event event, uint32_t parameter1 = 0, uint32_t parameter2 = 0);
|
||||
|
||||
/**
|
||||
* @brief The class's constructor.
|
||||
* @details In the constructor, the object id is set and the class is
|
||||
* inserted in the object manager.
|
||||
* @param setObjectId The id the object shall have.
|
||||
* @param doRegister Determines if the object is registered in the global object manager.
|
||||
*/
|
||||
SystemObject(object_id_t setObjectId, bool doRegister = true);
|
||||
/**
|
||||
* @brief On destruction, the object removes itself from the list.
|
||||
*/
|
||||
virtual ~SystemObject();
|
||||
const object_id_t getObjectId() const;
|
||||
virtual ReturnValue_t initialize();
|
||||
virtual ReturnValue_t checkObjectConnections();
|
||||
|
||||
virtual void forwardEvent(Event event, uint32_t parameter1 = 0, uint32_t parameter2 = 0) const;
|
||||
};
|
||||
|
||||
#endif /* SYSTEMOBJECT_H_ */
|
64
objectmanager/SystemObjectIF.h
Normal file
64
objectmanager/SystemObjectIF.h
Normal file
@ -0,0 +1,64 @@
|
||||
/**
|
||||
* @file SystemObjectIF.h
|
||||
* @brief This file contains the definition of the SystemObjectIF interface.
|
||||
* @date 18.09.2012
|
||||
* @author Bastian Baetz
|
||||
*/
|
||||
|
||||
#ifndef SYSTEMOBJECTIF_H_
|
||||
#define SYSTEMOBJECTIF_H_
|
||||
|
||||
#include <framework/events/EventReportingProxyIF.h>
|
||||
#include <framework/returnvalues/HasReturnvaluesIF.h>
|
||||
#include <stdint.h>
|
||||
/**
|
||||
* \defgroup system_objects Software System Object Management
|
||||
* The classes to create System Objects and classes to manage these are contained in this group.
|
||||
* System Objects are software elements that can be controlled externally. They all have a unique
|
||||
* object identifier.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This is the typedef for object identifiers.
|
||||
* \ingroup system_objects
|
||||
*/
|
||||
typedef uint32_t object_id_t;
|
||||
|
||||
/**
|
||||
* This interface allows a class to be included in the object manager
|
||||
* list.
|
||||
* It does not provide any method definitions, still it is required to
|
||||
* perform a type check with dynamic_cast.
|
||||
* \ingroup system_objects
|
||||
*/
|
||||
class SystemObjectIF : public EventReportingProxyIF {
|
||||
public:
|
||||
/**
|
||||
* This is a simple getter to return the object identifier.
|
||||
* @return Returns the object id of this object.
|
||||
*/
|
||||
virtual const object_id_t getObjectId() const = 0;
|
||||
/**
|
||||
* The empty virtual destructor as required for C++ interfaces.
|
||||
*/
|
||||
virtual ~SystemObjectIF() {
|
||||
}
|
||||
/**
|
||||
* Initializes all inter-object dependencies.
|
||||
* This is necessary to avoid circular dependencies of not-fully
|
||||
* initialized objects on start up.
|
||||
* @return - \c RETURN_OK in case the initialization was successful
|
||||
* - \c RETURN_FAILED otherwise
|
||||
*/
|
||||
virtual ReturnValue_t initialize() = 0;
|
||||
/**
|
||||
* Checks, if all object-object interconnections are satisfying for operation.
|
||||
* Some objects need certain other objects (or a certain number), to be registered as children.
|
||||
* These checks can be done in this method.
|
||||
* @return - \c RETURN_OK in case the check was successful
|
||||
* - \c any other code otherwise
|
||||
*/
|
||||
virtual ReturnValue_t checkObjectConnections() = 0;
|
||||
};
|
||||
|
||||
#endif /* SYSTEMOBJECTIF_H_ */
|
Reference in New Issue
Block a user