Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:51:49

0001 #ifndef BASE_OBJECT_REGISTRY_H
0002 #define BASE_OBJECT_REGISTRY_H
0003 
0004 /**
0005  * @file BaseObjectRegistry.h
0006  * @author Bryan BERTHOU (SPhN / CEA Saclay)
0007  * @date June 25, 2015
0008  * @version 1.0
0009  */
0010 
0011 #include <stddef.h>
0012 #include <SFML/System/Mutex.hpp>
0013 #include <map>
0014 #include <string>
0015 
0016 #include "BaseObject.h"
0017 #include "beans/List.h"
0018 
0019 namespace PARTONS {
0020 
0021 /** @class BaseObjectRegistry
0022  *
0023  * @brief The Registry is the analog of a phonebook, which lists all available objects (modules or services most of the time) identified by a unique integer identifier or by a unique string (class name) for translation. And only one species of each.
0024  *
0025  * From the point of view of software engineering, the registry corresponds to the singleton design pattern which ensures that it is unique.
0026  * When a new module or services are created, the first thing to do is to call this unique instance, and to register the new module or services with a name provided by the developer of the class.
0027  * In turn the Registry gives a unique identifier encoded in a integer variable for performance purposes.
0028  * Registry stores pointers to all objects in a generic way, i.e. whatever their nature are: pointers to GPDModule, to RunningAlphaStrongModule, to AutomationService, etc.
0029  * This is achieved by requiring all objects to derive from a single parent class named BaseObject.
0030  */
0031 class BaseObjectRegistry {
0032 public:
0033     /**
0034      * Static function to be able to retrieve a unique instance pointer of this class anywhere in the code.
0035      *
0036      * @return a unique instance of this class
0037      */
0038     static BaseObjectRegistry* getInstance();
0039 
0040     /**
0041      * Default destructor
0042      */
0043     virtual ~BaseObjectRegistry();
0044 
0045     /**
0046      * Store a unique instance of a module identified by a unique string character key.
0047      * @param pBaseObject: an instance of the module built by its default constructor.
0048      * @return A unique identifier by class
0049      */
0050     unsigned int registerBaseObject(BaseObject * pBaseObject);
0051 
0052     /**
0053      * Some objects like modules depend on each other.
0054      * But it is impossible to guarantee the order in which they are added to the registry will be made upon the resolution of statics variables (mechanism of self-registration)
0055      * So we need to perform this task just before the use of PARTONS software to resolve dependencies between all kind of registered objects.
0056      */
0057     void resolveBaseObjectDependencies();
0058 
0059     /**
0060      * Try to find stored object identified by its unique class identifier.
0061      *
0062      * @param classId
0063      * @return reference to object in memory or throw an exception
0064      */
0065     BaseObject* get(unsigned int classId) const;
0066 
0067     /**
0068      * Try to find stored object identified by its class name.
0069      *
0070      * @param className
0071      * @return reference to object in memory or throw an exception
0072      */
0073     BaseObject* get(const std::string &className) const;
0074 
0075     /**
0076      * Print information about current state of the registry (size of the registry, which objects are stored, ...).
0077      *
0078      * @return string
0079      */
0080     virtual std::string toString() const;
0081 
0082     /**
0083      * Return the size of the registry.
0084      * How many objects are stored.
0085      *
0086      * @return Registry size.
0087      */
0088     size_t size() const;
0089 
0090     unsigned int getObjectClassIdByClassName(
0091             const std::string &className);
0092 
0093     /**
0094      * Get names of modules stored.
0095      * @return List of modules names.
0096      */
0097     List<std::string> getBaseObjectClassNames() const;
0098 
0099 private:
0100     /**
0101      * Private pointer of this class for a unique instance.
0102      */
0103     static BaseObjectRegistry* m_pInstance;
0104 
0105     /**
0106      * Private default constructor for a unique instance.
0107      */
0108     BaseObjectRegistry();
0109 
0110     /**
0111      * Because of PARTONS is a program using threads we must ensure that only one object manipulates the registry at the same time to avoid memory access violation.
0112      * It's performed by a mechanism called mutex or semaphore.
0113      */
0114     mutable sf::Mutex m_mutex;
0115 
0116     std::map<unsigned int, BaseObject*> m_baseObjectList; ///< List of registered objects identified by their unique integer identifier.
0117     std::map<std::string, BaseObject*> m_translateList; ///< List of registered objects identified by their class name.
0118     std::map<std::string, unsigned int> m_classIdByClassName; ///< Translation map between class id and class name.
0119 
0120     static unsigned int m_uniqueClassIdCounter; ///< Increment unique class identifier.
0121 
0122     /**
0123      * For performance purposes, it's faster to compare two integers than two strings when we search for an object in the registry.
0124      * This method return a unique integer identifier when a new class object is added to the registry.
0125      * It's just an integer incremented one by one.
0126      *
0127      * @return Unique identifier by class.
0128      */
0129     unsigned int getUniqueClassId();
0130 
0131     /**
0132      * Try to find an object into the registry by its class name.
0133      *
0134      * @param className
0135      * @return Pointer to object if class name found into registry, NULL else.
0136      */
0137     BaseObject* isAvailable(const std::string &className) const;
0138 
0139     /**
0140      * Try to find an object into the registry by its class identifier.
0141      *
0142      * @param classId
0143      * @return Pointer to object if class name found into registry, NULL else.
0144      */
0145     BaseObject* isAvailable(const unsigned int classId) const;
0146 
0147     /**
0148      * Return last referenced object by the iterator on m_translateList.
0149      *
0150      * @return Reference to the object in memory.
0151      */
0152     BaseObject* getLastAvailableObjectIdentifiedByClassName() const;
0153 
0154     /**
0155      * Return last referenced object by the iterator on m_baseObjectList.
0156      *
0157      * @return Reference to the object in memory.
0158      */
0159     BaseObject* getLastAvailableObjectIdentifiedByClassId() const;
0160 
0161     // Stop the compiler generating methods of copy the object
0162     BaseObjectRegistry(BaseObjectRegistry const& other); // Not Implemented
0163     BaseObjectRegistry& operator=(BaseObjectRegistry const& other); // Not Implemented
0164 };
0165 
0166 } /* namespace PARTONS */
0167 
0168 #endif /* BASE_OBJECT_REGISTRY_H */