95 lines
2.8 KiB
C++

// =====================================================================================================================
// fennec, a free and open source game engine
// Copyright © 2025 Medusa Slockbower
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================
///
/// \file dynarray.h
/// \brief fennec::array definition & implementation
///
///
/// \details
/// \author Medusa Slockbower
///
/// \copyright Copyright © 2025 Medusa Slockbower ([GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html))
///
///
#ifndef FENNEC_CONTAINERS_DYNARRAY_H
#define FENNEC_CONTAINERS_DYNARRAY_H
#include <fennec/lang/utility.h>
#include <fennec/memory/allocator.h>
#include <fennec/memory/new.h>
namespace fennec
{
template<class T, class Alloc>
class dynarray
{
public:
using element_t = T;
using alloc_t = Alloc;
///
/// \brief Default Constructor, initializes an empty allocation.
dynarray() : _alloc(), _size(0) {}
///
/// \breif Alloc Constructor, initalize empty allocation with allocator instance.
/// \param alloc An allocator object to copy, for instances where the allocator needs to be initialized with some data.
dynarray(const alloc_t& alloc) : _alloc(alloc), _size(0) {}
///
/// \brief Create an allocation with a size of `n` elements. All elements are initialized with the default constructor.
dynarray(size_t n) : _alloc(n), _size(n)
{
element_t* addr = _alloc.data();
for(; n > 0; --n, ++addr) { fennec::construct(addr); }
}
///
/// \brief Create an allocation of size `n`, with each element constructed using the copy constructor
/// \brief n the number of elements
dynarray(size_t n, const element_t& val)
{
element_t* addr = _alloc.data();
for(; n > 0; --n, ++addr) { fennec::construct(addr, val); }
}
template<typename...ArgsT>
dynarray(size_t n, ArgsT...args)
{
element_t* addr = _alloc.data();
for(; n > 0; --n, ++addr) { fennec::construct(addr, args...); }
}
~dynarray()
{
element_t* addr = _alloc.data();
for(; n > 0; --n, ++addr) { fennec::destruct(addr); }
}
private:
allocation<element_t, alloc_t> _alloc;
size_t _size;
};
}
#endif // FENNEC_CONTAINERS_DYNARRAY_H