|
| 1 | +#ifndef __UN_CORE_H__ |
| 2 | +#define __UN_CORE_H__ |
| 3 | + |
| 4 | +#include <vector> |
| 5 | +#include <string> |
| 6 | + |
| 7 | +template<typename T> |
| 8 | +struct TArray |
| 9 | +{ |
| 10 | +private: |
| 11 | + std::vector<T> items; |
| 12 | + |
| 13 | +public: |
| 14 | + FORCEINLINE T* GetData() |
| 15 | + { |
| 16 | + return items.size() > 0 ? &items[0] : NULL; |
| 17 | + } |
| 18 | + FORCEINLINE const T* GetData() const |
| 19 | + { |
| 20 | + return items.size() > 0 ? &items[0] : NULL; |
| 21 | + } |
| 22 | + FORCEINLINE T& operator[](int index) |
| 23 | + { |
| 24 | + return items[index]; |
| 25 | + } |
| 26 | + FORCEINLINE const T& operator[](int index) const |
| 27 | + { |
| 28 | + return items[index]; |
| 29 | + } |
| 30 | + FORCEINLINE int Num() const |
| 31 | + { |
| 32 | + return (int)items.size(); |
| 33 | + } |
| 34 | + inline void Empty(int Count = 0) |
| 35 | + { |
| 36 | + items.resize(0); |
| 37 | + if (Count) |
| 38 | + { |
| 39 | + items.reserve(Count); |
| 40 | + } |
| 41 | + } |
| 42 | + FORCEINLINE void Reset(int Count = 0) |
| 43 | + { |
| 44 | + Empty(Count); |
| 45 | + } |
| 46 | + FORCEINLINE int Add(const T& item) |
| 47 | + { |
| 48 | + size_t OldCount = items.size(); |
| 49 | + items.push_back(item); |
| 50 | + return (int)OldCount; |
| 51 | + } |
| 52 | + inline int AddZeroed(int Count = 1) |
| 53 | + { |
| 54 | + size_t OldCount = items.size(); |
| 55 | + items.resize(OldCount + Count); |
| 56 | + return (int)OldCount; |
| 57 | + } |
| 58 | + FORCEINLINE int AddUninitialized(int Count = 1) |
| 59 | + { |
| 60 | + return AddZeroed(Count); |
| 61 | + } |
| 62 | + FORCEINLINE void ResizeTo(int count) |
| 63 | + { |
| 64 | + items.resize(count); |
| 65 | + } |
| 66 | + void RemoveAt(int index, int count = 1) |
| 67 | + { |
| 68 | + items.erase(items.begin() + index, items.begin() + index + count); |
| 69 | + } |
| 70 | + FORCEINLINE void RemoveAtSwap(int index, int count = 1) |
| 71 | + { |
| 72 | + RemoveAt(index, count); |
| 73 | + } |
| 74 | + int FindItem(const T& item, int startIndex = 0) const |
| 75 | + { |
| 76 | + for (int i = startIndex; i < items.size(); i++) |
| 77 | + { |
| 78 | + if (items[i] == item) |
| 79 | + { |
| 80 | + return i; |
| 81 | + } |
| 82 | + } |
| 83 | + return -1; |
| 84 | + } |
| 85 | + FORCEINLINE void Sort(int (*cmpFunc)(const T*, const T*)) |
| 86 | + { |
| 87 | + if (items.size() > 0) |
| 88 | + { |
| 89 | + qsort(&items[0], items.size(), sizeof(T), (int (*)(const void*, const void*)) cmpFunc); |
| 90 | + } |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +template<typename T> |
| 95 | +inline void* operator new(size_t size, TArray<T> &Array) |
| 96 | +{ |
| 97 | + assert(size == sizeof(T)); // allocating wrong object? can't disallow allocating of "int" inside "TArray<FString>" at compile time ... |
| 98 | + int index = Array.AddUninitialized(1); |
| 99 | + return Array.GetData() + index; |
| 100 | +} |
| 101 | + |
| 102 | +// Just shut up compiler |
| 103 | +template<typename T> |
| 104 | +inline void operator delete(void* ptr, TArray<T> &Array) |
| 105 | +{ |
| 106 | + assert(0); |
| 107 | +} |
| 108 | + |
| 109 | +struct FString |
| 110 | +{ |
| 111 | +private: |
| 112 | + std::string str; |
| 113 | + |
| 114 | +public: |
| 115 | + FString() |
| 116 | + {} |
| 117 | + FString(const char* src) |
| 118 | + : str(src) |
| 119 | + {} |
| 120 | + FString& operator=(const char* src) |
| 121 | + { |
| 122 | + str = src; |
| 123 | + return *this; |
| 124 | + } |
| 125 | + FString& operator+=(const char* text) |
| 126 | + { |
| 127 | + str += text; |
| 128 | + return *this; |
| 129 | + } |
| 130 | + FORCEINLINE FString& operator+=(const FString& Str) |
| 131 | + { |
| 132 | + str += *Str; |
| 133 | + return *this; |
| 134 | + } |
| 135 | + FORCEINLINE FString& AppendChar(char ch) |
| 136 | + { |
| 137 | + str += ch; |
| 138 | + return *this; |
| 139 | + } |
| 140 | + FORCEINLINE const char* operator*() const |
| 141 | + { |
| 142 | + return str.c_str(); |
| 143 | + } |
| 144 | + FORCEINLINE bool IsEmpty() const |
| 145 | + { |
| 146 | + return str.length() == 0; |
| 147 | + } |
| 148 | + FORCEINLINE char& operator[](int index) |
| 149 | + { |
| 150 | + return str[index]; |
| 151 | + } |
| 152 | + FORCEINLINE const char& operator[](int index) const |
| 153 | + { |
| 154 | + return str[index]; |
| 155 | + } |
| 156 | + FORCEINLINE int Len() const |
| 157 | + { |
| 158 | + return (int)str.length(); |
| 159 | + } |
| 160 | +}; |
| 161 | + |
| 162 | +// No TStaticArray FStaticString |
| 163 | + |
| 164 | +template<typename T, int N> |
| 165 | +struct TStaticArray : public TArray<T> |
| 166 | +{ |
| 167 | +}; |
| 168 | + |
| 169 | +template<int N> |
| 170 | +struct FStaticString : public FString |
| 171 | +{ |
| 172 | + FORCEINLINE FStaticString& operator=(const char* src) |
| 173 | + { |
| 174 | + return (FStaticString&) FString::operator=(src); |
| 175 | + } |
| 176 | + FORCEINLINE FStaticString& operator=(const FString& src) |
| 177 | + { |
| 178 | + return (FStaticString&) FString::operator=(src); |
| 179 | + } |
| 180 | +}; |
| 181 | + |
| 182 | +#endif // __UN_CORE_H__ |
0 commit comments