Karma Engine
Loading...
Searching...
No Matches
KarmaTypes.h
1#pragma once
2
3#include <vector>
4#include <cstdint>
5#include <map>
6#include <utility>
7#include <algorithm>
8
26
28namespace EWorldType
29{
30 enum Type
31 {
34
37
40
43
46
49
52
53 /* An editor world that was loaded but not currently being edited in the level editor */
54 Inactive
55 };
56}
57
61template <typename KeyType, typename ValueType>
63{
64public:
65 typedef std::map<KeyType, ValueType> AMap;
66 typedef typename AMap::iterator AnIterator;
67
68 KarmaMap()
69 {
70
71 }
72
82 ValueType& FindOrAdd(const KeyType& Key)
83 {
84 ValueType aValue;
85 std::pair<AnIterator, bool> const& result = m_KeyValuePair.insert(typename AMap::value_type(Key, aValue));
86
87 // a simple return.first->second could work. doing this for better understandibility
88 if(result.second)
89 {
90 // Value was inserted, meaning wasn't there earlier
91 return result.first->second;
92 }
93 else
94 {
95 // Nothing inserted, meaning value with key already exists
96 return result.first->second;
97 }
98 }
99
108 ValueType* Find(const KeyType& Key)
109 {
110 AnIterator result = m_KeyValuePair.find(Key);
111
112 if(result != m_KeyValuePair.end())
113 {
114 return &result->second;
115 }
116
117 return nullptr;
118 }
119protected:
120 AMap m_KeyValuePair;
121};
122
126template<typename BuildingBlock>
128{
129public:
131 {
132 }
133
135 {
136 }
137
138 uint32_t Remove(BuildingBlock aBlock)
139 {
140 uint32_t occurences = 0;
141 typename std::vector<BuildingBlock>::iterator iterator = m_Elements.begin();
142
143 while (iterator != m_Elements.end())
144 {
145 if (*iterator == aBlock)
146 {
147 iterator = m_Elements.erase(iterator);
148 occurences++;
149 }
150 else
151 {
152 ++iterator;
153 }
154 }
155
156 return occurences;
157 }
158
159 void Add(BuildingBlock aBlock)
160 {
161 m_Elements.push_back(aBlock);
162 }
163
174 FORCEINLINE int32_t AddUnique(const BuildingBlock& Item)
175 {
176 int32_t index = Find(Item);
177
178 if(index == -1)
179 {
180 Add(Item);
181 return Num() - 1;
182 }
183 else
184 {
185 return index;
186 }
187 }
188
198 int32_t Find(const BuildingBlock& Item) const
199 {
200 int32_t returnIndex = 0;
201
202 typename std::vector<BuildingBlock>::const_iterator iter = m_Elements.begin();
203
204 for (; iter != m_Elements.end(); iter++)
205 {
206 if (m_Elements[returnIndex] == Item)
207 {
208 break;
209 }
210 returnIndex++;
211 }
212
213 if (iter != m_Elements.end())
214 {
215 return returnIndex;
216 }
217
218 // Make an enum with name INDEX_NONE and value -1
219 return -1;
220 }
221
222 bool Contains(BuildingBlock aBlock) const
223 {
224 typename std::vector<BuildingBlock>::const_iterator iterator = m_Elements.begin();
225
226 while (iterator != m_Elements.end())
227 {
228 if (*iterator == aBlock)
229 {
230 return true;
231 }
232 else
233 {
234 ++iterator;
235 }
236 }
237
238 return false;
239 }
240
241 uint32_t Num() const
242 {
243 return (uint32_t) m_Elements.size();
244 }
245
254 void Reset()
255 {
256 // Maybe make smartpointer instead of manually deleting
257 typename std::vector<BuildingBlock>::iterator iter = m_Elements.begin();
258
259 // solution got from https://www.reddit.com/r/cpp_questions/comments/panivh/constexpr_if_statement_to_check_if_template/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
260 if constexpr(std::is_pointer_v<BuildingBlock>)
261 {
262 for (iter = m_Elements.begin(); iter != m_Elements.end(); iter++)
263 {
264 if (*iter != nullptr)
265 {
266 KR_CORE_INFO("Deleting UObjects");
267 delete *iter;
268 *iter = nullptr;
269 }
270 }
271 }
272
273 m_Elements.clear();
274 }
275
280 {
281 m_Elements.clear();
282 }
283
284 void SetVectorElementByIndex(int32_t Index, BuildingBlock Value)
285 {
286 m_Elements[Index] = Value;
287 }
288
289 inline const std::vector<BuildingBlock>& GetElements() const { return m_Elements; }
290 inline std::vector<BuildingBlock>& ModifyElements() { return m_Elements; }
291
292 typename std::vector<BuildingBlock>::iterator begin()
293 {
294 return m_Elements.begin();
295 }
296
297 typename std::vector<BuildingBlock>::iterator end()
298 {
299 return m_Elements.end();
300 }
301
308 FORCEINLINE BuildingBlock& IndexToObject(int32_t Index)
309 {
310 KR_CORE_ASSERT(Index >= 0, "");
311 if(Index < m_Elements.size())
312 {
313 return m_Elements.at(Index);
314 }
315
316 KR_CORE_ASSERT(false, "Shouldn't happen");
317
318 static BuildingBlock aBlock;
319 return aBlock;
320 }
321
328 FORCEINLINE bool IsValidIndex(int32_t Index) const
329 {
330 return Index >= 0 && Index < m_Elements.size();
331 }
332
333
334protected:
335 std::vector<BuildingBlock> m_Elements;
336};
337
338// Traveling from server to server.
339enum ETravelType
340{
342 TRAVEL_Absolute,
343
345 TRAVEL_Partial,
346
348 TRAVEL_Relative,
349
350 TRAVEL_MAX,
351};
352
356struct KARMA_API FUrlConfig
357{
358 std::string m_DefaultProtocol;
359 std::string m_DefaultName;
360 std::string m_DefaultHost;
361 std::string m_DefaultPortal;
362 std::string m_DefaultSaveExt;
363 int32_t m_DefaultPort;
364
368 void Init();
369
373 void Reset();
374};
375
376//URL structure.
377struct KARMA_API FURL
378{
379 // Protocol, i.e. "karma" or "http".
380 std::string m_Protocol;
381
382 // Optional hostname, i.e. "204.157.115.40" or "unreal.epicgames.com", blank if local.
383 std::string m_Host;
384
385 // Optional host port.
386 int32_t m_Port;
387
388 int32_t m_Valid;
389
390 // Map name, i.e. "SkyCity", default is "Entry".
391 std::string m_Map;
392
393 // Optional place to download Map if client does not possess it
394 std::string m_RedirectURL;
395
396 // Options.
397 //TArray<FString> Op;
398
399 // Portal to enter through, default is "".
400 std::string m_Portal;
401
402 // Statics.
403 static FUrlConfig UrlConfig;
404 static bool m_bDefaultsInitialized;
405
409 //explicit FURL(ENoInit) { }
410
414 FURL(const std::string& Filename = "");
415
419 //FURL(FURL* Base, const TCHAR* TextURL, ETravelType Type);
420
421 //static void StaticInit();
422 //static void StaticExit();
423
429 //static void FilterURLString(const std::string& Str);
430
437 //bool IsInternal() const;
438
443 //bool IsLocalInternal() const;
444
448 //bool HasOption(const TCHAR* Test) const;
449
458 //const TCHAR* GetOption(const TCHAR* Match, const TCHAR* Default) const;
459
463 //void LoadURLConfig(const TCHAR* Section, const std::string& Filename = "GGameIni");
464
468 //void SaveURLConfig(const TCHAR* Section, const TCHAR* Item, const std::string& Filename = "GGameIni") const;
469
473 //void AddOption(const TCHAR* Str);
474
478 //void RemoveOption(const TCHAR* Key, const TCHAR* Section = nullptr, const std::string& Filename = "GGameIni");
479
483 //std::string ToString(bool FullyQualified = 0) const;
484
488 //std::string GetHostPortString() const;
489
493 //ENGINE_API friend FArchive& operator<<(FArchive& Ar, FURL& U);
494
498 //bool operator==(const FURL& Other) const;
499};
Karma's std::map wrapper.
Definition KarmaTypes.h:63
ValueType * Find(const KeyType &Key)
Definition KarmaTypes.h:108
ValueType & FindOrAdd(const KeyType &Key)
Definition KarmaTypes.h:82
Karma's std::vector wrapper.
Definition KarmaTypes.h:128
FORCEINLINE int32_t AddUnique(const BuildingBlock &Item)
Definition KarmaTypes.h:174
FORCEINLINE bool IsValidIndex(int32_t Index) const
Definition KarmaTypes.h:328
int32_t Find(const BuildingBlock &Item) const
Definition KarmaTypes.h:198
void SmartReset()
Definition KarmaTypes.h:279
FORCEINLINE BuildingBlock & IndexToObject(int32_t Index)
Definition KarmaTypes.h:308
void Reset()
Definition KarmaTypes.h:254
Definition KarmaTypes.h:11
Type
Definition KarmaTypes.h:13
@ EndPlayInEditor
Definition KarmaTypes.h:19
@ Destroyed
Definition KarmaTypes.h:15
@ RemovedFromWorld
Definition KarmaTypes.h:21
@ Quit
Definition KarmaTypes.h:23
@ LevelTransition
Definition KarmaTypes.h:17
Definition KarmaTypes.h:29
Type
Definition KarmaTypes.h:31
@ GamePreview
Definition KarmaTypes.h:48
@ None
Definition KarmaTypes.h:33
@ Editor
Definition KarmaTypes.h:39
@ Game
Definition KarmaTypes.h:36
@ PIE
Definition KarmaTypes.h:42
@ EditorPreview
Definition KarmaTypes.h:45
@ GameRPC
Definition KarmaTypes.h:51
Definition KarmaTypes.h:378
Definition KarmaTypes.h:357