Page MenuHomePhorge

System.hpp
No OneTemporary

Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None

System.hpp

/*******************************************************************************
*
* File: System.hpp
*
* Contents: Declaration of System base-class.
*
* Copyright 2017
*
* Author: David Juhasz (david.juhasz@tuwien.ac.at)
*
******************************************************************************/
#ifndef ROSA_CORE_SYSTEM_HPP
#define ROSA_CORE_SYSTEM_HPP
#include "rosa/config/config.h"
#include "rosa/support/debug.hpp"
#include "rosa/support/log.h"
#include <atomic>
#include <memory>
#include <string>
namespace rosa {
// Forward declarations.
class Unit;
// Base-class for actual agent-systems, the class provides facitlities to keep
// track of Units of the system.
// NOTE: This class and any subclasses are supposed to provide thread-safe
// interfaces.
class System {
public:
// Signature of creator functions for Units.
template <typename T>
using UnitCreator = std::function<T *(const size_t, System &)noexcept>;
// Returns an object implementing the interface defined by the class.
static std::unique_ptr<System> createSystem(const std::string &Name) noexcept;
protected:
// Protected ctor, only subclasses can instantiate.
System(const std::string &Name) noexcept;
// No copy and move.
System(const System&) = delete;
System(System&&) = delete;
System &operator=(const System&) = delete;
System &operator=(System&&) = delete;
public:
// Dtor. Only a cleared System can be destroyed.
// PRE: SystemCleaned
virtual ~System(void);
protected:
// Sets SystemCleaned flag. Can be called only once and the System must not
// have any live Units.
// PRE: !SystemCleaned && empty()
// POST: SystemCleaned
void markCleaned(void) noexcept;
// Creates a Unit instance with the given UnitCreator, using the given Name
// if any, then registers the new Unit instance by calling the virtual
// member function registerUnit.
// NOTE: This function handles the member field CountUnits.
// STATIC PRE: std::is_base_of<Unit, T>::value
// PRE: !SystemCleaned
template <typename T>
T &createUnit(UnitCreator<T> C) noexcept;
// Registers the given Unit instance to the System.
// PRE: !isUnitRegistered(U)
// POST: isUnitRegistered(U)
virtual void registerUnit(Unit &U) noexcept = 0;
// Unregisters and destroys the given Unit.
// PRE: isUnitRegistered(U)
// POST: !isUnitRegistered(U) && 'U is destroyed'
virtual void destroyUnit(Unit &U) noexcept = 0;
// Returns if the given Unit is registered in the System.
virtual bool isUnitRegistered(const Unit &U) const noexcept = 0;
public:
// The textual name of the System.
const std::string Name;
private:
// Number of Units constructed in the system.
// NOTE: Should never be decremented!
std::atomic<size_t> CountUnits;
// Indicates that the System has been cleaned and is ready for destruction.
// The field is initialized as 'false' and can be set by the member function
// markCleaned.
// Subclasses must set the flag upon destroying the System instance, which
// indicates to the destructor of the base-class that all the managed
// resources has been properly released.
std::atomic<bool> SystemCleaned;
public:
// Returns the number of Units constructed in the System so far,
// including those being already destroyed.
size_t numberOfConstructedUnits(void) const noexcept;
// Returns the number of live Units, that have been constructed and not
// destroyed yet.
virtual size_t numberOfLiveUnits(void) const noexcept = 0;
// Returns if the System has no live Units.
virtual bool empty(void) const noexcept = 0;
};
template <typename T>
T &System::createUnit(UnitCreator<T> C) noexcept {
STATIC_ASSERT((std::is_base_of<Unit, T>::value), "not a Unit");
if (SystemCleaned) {
ROSA_CRITICAL("Trying to create a Unit in an already cleaned System (" +
Name + ")");
}
const uint64_t Id = ++CountUnits;
T *U = C(Id, *this);
registerUnit(*U);
LOG_TRACE("Unit created and registered (" + U->FullName + ")");
return *U;
}
} // End namespace rosa
#endif // ROSA_CORE_SYSTEM_HPP

File Metadata

Mime Type
text/x-c++
Expires
Fri, Nov 20, 10:34 PM (13 h, 6 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
416162
Default Alt Text
System.hpp (4 KB)

Event Timeline