OpenBarnyard
 
Loading...
Searching...
No Matches
STL.h
Go to the documentation of this file.
1#pragma once
2#include "Toshi/Memory/TMemory.h"
3
4#include <memory>
5#include <vector>
6#include <unordered_map>
7#include <map>
8
10
11namespace STL
12{
13template <class Type>
15{
16public:
17 using value_type = Type;
18
19 using pointer = Type*;
20 using const_pointer = const Type*;
21
22 using void_pointer = void*;
23 using const_void_pointer = const void*;
24
25 using size_type = size_t;
26 using difference_type = std::ptrdiff_t;
27
28 TAllocator() = default;
29 ~TAllocator() = default;
30
31 template <class U>
32 TAllocator( const TAllocator<U>& other ) {}
33
34 inline pointer allocate( size_type count )
35 {
36 return static_cast<pointer>( TMalloc( sizeof( value_type ) * count ) );
37 }
38
40 {
41 return allocate( count );
42 }
43
44 inline void deallocate( pointer ptr, size_type count )
45 {
46 TFree( ptr );
47 }
48
49 inline size_type max_size() const
50 {
51 return std::numeric_limits<size_type>::max();
52 }
53
54 template <class U, class... Args>
55 inline void construct( U* ptr, Args&&... args )
56 {
57 new ( ptr ) U( std::forward<Args>( args )... );
58 }
59
60 template <class U>
61 inline void destroy( U* ptr )
62 {
63 ptr->~U();
64 }
65};
66
67template <class Type>
69{
70public:
71 inline void operator()( Type* ptr ) { delete ptr; }
72};
73
74template <typename T>
75using Ref = std::shared_ptr<T>;
76
77template <typename T>
78using Scope = std::unique_ptr<T, MemoryDeleter<T>>;
79
80template <typename T>
81using Vector = std::vector<T, Toshi::STL::TAllocator<T>>;
82
83template <typename Key, typename T>
84using Map = std::map<Key, T, std::less<Key>, Toshi::STL::TAllocator<std::pair<const Key, T>>>;
85
86template <typename Key, typename T>
87using UnorderedMap = std::unordered_map<Key, T, std::hash<Key>, std::equal_to<Key>, Toshi::STL::TAllocator<std::pair<const Key, T>>>;
88
89template <typename T, typename... Args>
90constexpr Ref<T> CreateRef( Args&&... args )
91{
92 return std::allocate_shared<T>( Toshi::STL::TAllocator<T>(), std::forward<Args>( args )... );
93}
94
95template <typename T, typename... Args>
96constexpr Scope<T> CreateScope( Args&&... args )
97{
98 return Scope<T>( new T( std::forward<Args>( args )... ) );
99}
100
101template <typename T, typename Result, typename... Args>
102constexpr Scope<Result> CreateScope( Args&&... args )
103{
104 return Scope<Result>( new T( std::forward<Args>( args )... ) );
105}
106} // namespace STL
107
void * TMalloc(TSIZE a_uiSize, Toshi::TMemory::MemBlock *a_pMemBlock, const TCHAR *a_szFileName, TINT a_iLineNum)
Allocates memory from a specific memory block.
Definition TMemory.cpp:973
void TFree(void *a_pMem)
Frees previously allocated memory.
Definition TMemory.cpp:1054
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
Definition STL.h:12
std::unordered_map< Key, T, std::hash< Key >, std::equal_to< Key >, Toshi::STL::TAllocator< std::pair< const Key, T > > > UnorderedMap
Definition STL.h:87
constexpr Scope< T > CreateScope(Args &&... args)
Definition STL.h:96
std::map< Key, T, std::less< Key >, Toshi::STL::TAllocator< std::pair< const Key, T > > > Map
Definition STL.h:84
std::shared_ptr< T > Ref
Definition STL.h:75
constexpr Ref< T > CreateRef(Args &&... args)
Definition STL.h:90
std::vector< T, Toshi::STL::TAllocator< T > > Vector
Definition STL.h:81
std::unique_ptr< T, MemoryDeleter< T > > Scope
Definition STL.h:78
TAllocator()=default
const void * const_void_pointer
Definition STL.h:23
Type value_type
Definition STL.h:17
size_t size_type
Definition STL.h:25
pointer allocate(size_type count)
Definition STL.h:34
~TAllocator()=default
void construct(U *ptr, Args &&... args)
Definition STL.h:55
Type * pointer
Definition STL.h:19
void * void_pointer
Definition STL.h:22
void deallocate(pointer ptr, size_type count)
Definition STL.h:44
void destroy(U *ptr)
Definition STL.h:61
std::ptrdiff_t difference_type
Definition STL.h:26
size_type max_size() const
Definition STL.h:49
pointer allocate(size_type count, const_void_pointer hint)
Definition STL.h:39
TAllocator(const TAllocator< U > &other)
Definition STL.h:32
const Type * const_pointer
Definition STL.h:20
void operator()(Type *ptr)
Definition STL.h:71