context in core

This commit is contained in:
AndreaRigoni
2026-03-21 16:30:56 +00:00
parent cd95f16221
commit 0bff36f8ba
6 changed files with 163 additions and 26 deletions

View File

@@ -10,6 +10,7 @@ set(HEADERS
Macros.h
Mpl.h
Object.h
ObjectsContext.h
Options.h
Serializable.h
Signal.h
@@ -26,6 +27,7 @@ set(SOURCES
Archives.cpp
Debug.cpp
Object.cpp
ObjectsContext.cpp
Options.cpp
Serializable.cpp
Signal.cpp

View File

@@ -0,0 +1,47 @@
#include "Core/ObjectsContext.h"
#include <algorithm>
namespace uLib {
ObjectsContext::ObjectsContext() : Object() {}
ObjectsContext::~ObjectsContext() {}
void ObjectsContext::AddObject(Object* obj) {
if (obj && std::find(m_objects.begin(), m_objects.end(), obj) == m_objects.end()) {
m_objects.push_back(obj);
this->Updated(); // Signal that the context has been updated
}
}
void ObjectsContext::RemoveObject(Object* obj) {
auto it = std::find(m_objects.begin(), m_objects.end(), obj);
if (it != m_objects.end()) {
m_objects.erase(it);
this->Updated(); // Signal that the context has been updated
}
}
void ObjectsContext::Clear() {
if (!m_objects.empty()) {
m_objects.clear();
this->Updated();
}
}
const std::vector<Object*>& ObjectsContext::GetObjects() const {
return m_objects;
}
size_t ObjectsContext::GetCount() const {
return m_objects.size();
}
Object* ObjectsContext::GetObject(size_t index) const {
if (index < m_objects.size()) {
return m_objects[index];
}
return nullptr;
}
} // namespace uLib

59
src/Core/ObjectsContext.h Normal file
View File

@@ -0,0 +1,59 @@
#ifndef U_CORE_OBJECTS_CONTEXT_H
#define U_CORE_OBJECTS_CONTEXT_H
#include "Core/Object.h"
#include <vector>
namespace uLib {
/**
* @brief ObjectsContext represents a collection of Object instances.
*/
class ObjectsContext : public Object {
public:
ObjectsContext();
virtual ~ObjectsContext();
/**
* @brief Adds an object to the context.
* @param obj Pointer to the object to add.
*/
void AddObject(Object* obj);
/**
* @brief Removes an object from the context.
* @param obj Pointer to the object to remove.
*/
void RemoveObject(Object* obj);
/**
* @brief Clears all objects from the context.
*/
void Clear();
/**
* @brief Returns the collection of objects.
* @return Const reference to the vector of object pointers.
*/
const std::vector<Object*>& GetObjects() const;
/**
* @brief Returns the number of objects in the context.
* @return Size of the collection.
*/
size_t GetCount() const;
/**
* @brief Returns an object by index.
* @param index The index of the object.
* @return Pointer to the object or nullptr if index is out of bounds.
*/
Object* GetObject(size_t index) const;
private:
std::vector<Object*> m_objects;
};
} // namespace uLib
#endif // U_CORE_OBJECTS_CONTEXT_H