// Copyright (c) 2012-2020 Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Core/Delegate.h"
#include "Engine/Core/Collections/Array.h"
#include "Engine/Platform/CriticalSection.h"
class StreamableResource;
///
/// Container for collection of Streamable Resources.
///
class StreamableResourcesCollection
{
private:
CriticalSection _locker;
Array _resources;
public:
///
/// Initializes a new instance of the class.
///
StreamableResourcesCollection()
: _resources(4096)
{
}
public:
///
/// Event called on new resource added to the collection.
///
Delegate Added;
///
/// Event called on new resource added from the collection.
///
Delegate Removed;
public:
///
/// Gets the amount of registered resources.
///
/// The resources count.
int32 ResourcesCount() const
{
_locker.Lock();
const int32 result = _resources.Count();
_locker.Unlock();
return result;
}
///
/// Gets the resource at the given index.
///
/// The index.
/// The resource at the given index.
StreamableResource* operator[](int32 index) const
{
_locker.Lock();
StreamableResource* result = _resources.At(index);
_locker.Unlock();
return result;
}
public:
///
/// Adds the resource to the collection.
///
/// The resource to add.
void Add(StreamableResource* resource)
{
ASSERT(resource);
_locker.Lock();
ASSERT(_resources.Contains(resource) == false);
_resources.Add(resource);
_locker.Unlock();
Added(resource);
}
///
/// Removes resource from the collection.
///
/// The resource to remove.
void Remove(StreamableResource* resource)
{
ASSERT(resource);
_locker.Lock();
ASSERT(_resources.Contains(resource) == true);
_resources.Remove(resource);
_locker.Unlock();
Removed(resource);
}
};