Files
qt5-legatus-legacy/Lts/Viewer/SituationMap/Map/object_container.h
T
2026-08-13 18:33:10 +02:00

205 lines
4.8 KiB
C++
Executable File

/**
* @RDE_HEADER@
* Interface for Storing of MapObjects
* $Id$
* $HeadURL$
* $Author$
*/
#pragma once
// Own
#include "abstract_map_object.h"
#include <Common/situation_map_logging.h>
// Qt
#include <QMap>
#include <QSharedPointer>
#include <typeindex>
//----------------------------------------------------------------------------
namespace SituationMap
{
//forward
template<class> class RefPointer;
/**
* @brief The IdGenerator class generates a new UniqueMapId in a very simple manner.
*/
class SITUATIONMAP_EXPORT IdGenerator // final
{
public:
static const UniqueMapId INVALID_ID = SituationMap::INVALID_MAP_ID;
IdGenerator();
virtual ~IdGenerator(); // leave virtual to let UnitTestInternal succeed
IdGenerator(const IdGenerator &other) = delete;
IdGenerator &operator=(IdGenerator &other) = delete;
/**
* @brief Get next UniqueMapId
* @return Next UniqueMapId
*/
UniqueMapId getNextId()
{
DEBUG_LOG(m_logger) << LOGFUNCTION << " nextId=" << m_counter;
return m_counter++;
}
/**
* @brief Check validity
* @param id to be checked
* @return True if id is not equal INVALID_ID
* @note Prefer SituationMap::isUniqueMapIdValid() due to runtime aspects.
*/
static bool idIsValid(const UniqueMapId &id)
{
return isUniqueMapIdValid(id); // same as ( id != INVALID_ID );
}
UniqueMapId nextFreeId() const { return m_counter; }
protected:
LOGGER_T m_logger; ///< Interface of the Logging-Facility
UniqueMapId m_counter = 0;
};
//----------------------------------------------------------------------------
/**
* @brief The ObjectContainer class maps UniqueMapId to MapObjects.
* It uses an external IdGenerator to generate a next UniqueMapId returned
* while inserting.
*/
class SITUATIONMAP_EXPORT ObjectContainer
{
public:
using UniqueObjectId = UniqueMapId;
using Object = AbstractMapObject;
/*! \name Construction, Destruction */
//@{
/**
* @brief Constructor of ObjectContainer.
* @param idGenerator to be used
* (can be shared among different ObjectContainer instances).
*/
explicit ObjectContainer(const QSharedPointer<IdGenerator> &idGenerator);
/**
* @brief Destructor. Will delete the IdGenerator when it goes
* out of scope, if no other QSharedPointer objects are referencing it.
*/
virtual ~ObjectContainer();
ObjectContainer(const ObjectContainer &other) = delete;
ObjectContainer &operator=(ObjectContainer &other) = delete;
/**
* @brief Get current IdGenerator.
* @return Current IdGenerator.
*/
virtual QSharedPointer<IdGenerator> idGenerator() const;
//@}
/*! \name Insert, Remove, Clear */
//@{
/**
* @brief insertObject.
* @param object to be inserted.
* @return UniqueId of inserted object.
*/
virtual UniqueObjectId insertObject(Object *object) = 0;
virtual UniqueObjectId insertObject(ObjectContainer::Object *object, const ObjectContainer::UniqueObjectId &uid) = 0;
/**
* @brief Remove object from the container.
* @param id to be removed.
* @return True on success, otherwise false.
*/
virtual bool removeObject(const UniqueObjectId &id) = 0;
/**
* @brief Clear the container (and remove objects from the view)
*/
virtual void clear() = 0;
//@}
/*! \name Access */
//@{
/**
* @brief getObject.
* @param id to access.
* @return Object if found, otherwise null.
*/
virtual Object *getObject(const UniqueObjectId &id) const = 0;
/**
* @brief Retrieves all UniqueIds.
* @return Get all UniqueIds.
*/
virtual QList<UniqueObjectId> getAllKeys() const = 0;
/**
* @brief Get all objects of any type.
* @return All objects.
*/
virtual QList<Object *> getAllObjects() const = 0;
/**
* @brief Get all objects of a certain type.
* @param type requested type.
* @return All objects fo requested type
*/
virtual QList<Object *> getAllObjects(const std::type_index &type) const = 0;
/**
* @brief Get mutable Iterator
* @return mutable Iterator
*/
virtual QMutableMapIterator<UniqueObjectId, RefPointer<Object>> mutableIterator() = 0;
//@}
/*! \name Check */
//@{
/**
* @brief Check existence.
* @param id to be checked.
* @return True if exists, otherwise false.
*/
virtual bool containsObject(const UniqueObjectId &id) const = 0;
//@}
/*! \name Count */
//@{
virtual qint64 countObjects() const = 0;
//@}
protected:
QSharedPointer<IdGenerator> m_idGenerator;
LOGGER_T m_logger; ///< Interface of the Logging-Facility
};
} // namespace SituationMap