KarmaEngine
Game Engine for practical learning and research purposes
Loading...
Searching...
No Matches
KarmaTypes.h
Go to the documentation of this file.
1
10
11#pragma once
12
13#include <vector>
14#include <cstdint>
15#include <map>
16#include <utility>
17#include <algorithm>
18
36
41enum { INDEX_NONE = -1 };
42
44namespace EWorldType
45{
72}
73
77template <typename KeyType, typename ValueType>
79{
80public:
81 typedef std::map<KeyType, ValueType> AMap;
82 typedef typename AMap::iterator AnIterator;
83
90 {
91 }
92
104 ValueType& FindOrAdd(const KeyType& Key)
105 {
106 ValueType aValue;
107 std::pair<AnIterator, bool> const& result = m_KeyValuePair.insert(typename AMap::value_type(Key, aValue));
108
109 // a simple return.first->second could work. doing this for better understandibility
110 if(result.second)
111 {
112 // Value was inserted, meaning wasn't there earlier
113 return result.first->second;
114 }
115 else
116 {
117 // Nothing inserted, meaning value with key already exists
118 return result.first->second;
119 }
120 }
121
131
132 ValueType* Find(const KeyType& Key)
133 {
134 AnIterator result = m_KeyValuePair.find(Key);
135
136 if(result != m_KeyValuePair.end())
137 {
138 return &result->second;
139 }
140
141 return nullptr;
142 }
143protected:
144 AMap m_KeyValuePair;
145};
146
150template<typename BuildingBlock>
152{
153public:
160 {
161 }
162
169 {
170 }
171
178 uint32_t Remove(BuildingBlock aBlock)
179 {
180 uint32_t occurences = 0;
181 typename std::vector<BuildingBlock>::iterator iterator = m_Elements.begin();
182
183 while (iterator != m_Elements.end())
184 {
185 if (*iterator == aBlock)
186 {
187 iterator = m_Elements.erase(iterator);
188 occurences++;
189 }
190 else
191 {
192 ++iterator;
193 }
194 }
195
196 return occurences;
197 }
198
205 void Add(BuildingBlock aBlock)
206 {
207 m_Elements.push_back(aBlock);
208 }
209
221 FORCEINLINE int32_t AddUnique(const BuildingBlock& Item)
222 {
223 int32_t index = Find(Item);
224
225 if(index == -1)
226 {
227 Add(Item);
228 return Num() - 1;
229 }
230 else
231 {
232 return index;
233 }
234 }
235
247 int32_t Find(const BuildingBlock& Item) const
248 {
249 int32_t returnIndex = 0;
250
251 typename std::vector<BuildingBlock>::const_iterator iter = m_Elements.begin();
252
253 for (; iter != m_Elements.end(); iter++)
254 {
255 if (m_Elements[returnIndex] == Item)
256 {
257 break;
258 }
259 returnIndex++;
260 }
261
262 if (iter != m_Elements.end())
263 {
264 return returnIndex;
265 }
266
267 return INDEX_NONE;
268 }
269
278 bool Contains(BuildingBlock aBlock) const
279 {
280 typename std::vector<BuildingBlock>::const_iterator iterator = m_Elements.begin();
281
282 while (iterator != m_Elements.end())
283 {
284 if (*iterator == aBlock)
285 {
286 return true;
287 }
288 else
289 {
290 ++iterator;
291 }
292 }
293
294 return false;
295 }
296
302 uint32_t Num() const
303 {
304 return (uint32_t) m_Elements.size();
305 }
306
318 void Reset()
319 {
320 // Maybe make smartpointer instead of manually deleting
321 typename std::vector<BuildingBlock>::iterator iter = m_Elements.begin();
322
323 // 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
324 if constexpr(std::is_pointer_v<BuildingBlock>)
325 {
326 for (iter = m_Elements.begin(); iter != m_Elements.end(); iter++)
327 {
328 if (*iter != nullptr)
329 {
330 KR_CORE_INFO("Deleting UObjects");
331 delete *iter;
332 *iter = nullptr;
333 }
334 }
335 }
336
337 m_Elements.clear();
338 }
339
349 {
350 m_Elements.clear();
351 }
352
361 void SetVectorElementByIndex(int32_t Index, BuildingBlock Value)
362 {
363 m_Elements[Index] = Value;
364 }
365
371 inline const std::vector<BuildingBlock>& GetElements() const { return m_Elements; }
372
379 inline std::vector<BuildingBlock>& ModifyElements() { return m_Elements; }
380
386 typename std::vector<BuildingBlock>::iterator begin()
387 {
388 return m_Elements.begin();
389 }
390
396 typename std::vector<BuildingBlock>::iterator end()
397 {
398 return m_Elements.end();
399 }
400
409 FORCEINLINE BuildingBlock& IndexToObject(int32_t Index)
410 {
411 KR_CORE_ASSERT(Index >= 0, "");
412
413 if(Index < m_Elements.size())
414 {
415 return m_Elements.at(Index);
416 }
417
418 KR_CORE_ASSERT(false, "Shouldn't happen");
419
420 static BuildingBlock aBlock;
421 return aBlock;
422 }
423
432 FORCEINLINE bool IsValidIndex(int32_t Index) const
433 {
434 return Index >= 0 && Index < m_Elements.size();
435 }
436
437
438protected:
439 std::vector<BuildingBlock> m_Elements;
440};
441
459
464{
465 std::string m_DefaultProtocol;
466 std::string m_DefaultName;
467 std::string m_DefaultHost;
468 std::string m_DefaultPortal;
469 std::string m_DefaultSaveExt;
470 int32_t m_DefaultPort;
471
475 void Init();
476
480 void Reset();
481};
482
487{
489 std::string m_Protocol;
490
492 std::string m_Host;
493
495 int32_t m_Port;
496
497 int32_t m_Valid;
498
500 std::string m_Map;
501
503 std::string m_RedirectURL;
504
505 // Options.
506 //TArray<FString> Op;
507
509 std::string m_Portal;
510
513 static bool m_bDefaultsInitialized;
514
518 //explicit FURL(ENoInit) { }
519
523 FURL(const std::string& Filename = "");
524
528 //FURL(FURL* Base, const TCHAR* TextURL, ETravelType Type);
529
530 //static void StaticInit();
531 //static void StaticExit();
532
538 //static void FilterURLString(const std::string& Str);
539
546 //bool IsInternal() const;
547
552 //bool IsLocalInternal() const;
553
557 //bool HasOption(const TCHAR* Test) const;
558
567 //const TCHAR* GetOption(const TCHAR* Match, const TCHAR* Default) const;
568
572 //void LoadURLConfig(const TCHAR* Section, const std::string& Filename = "GGameIni");
573
577 //void SaveURLConfig(const TCHAR* Section, const TCHAR* Item, const std::string& Filename = "GGameIni") const;
578
582 //void AddOption(const TCHAR* Str);
583
587 //void RemoveOption(const TCHAR* Key, const TCHAR* Section = nullptr, const std::string& Filename = "GGameIni");
588
592 //std::string ToString(bool FullyQualified = 0) const;
593
597 //std::string GetHostPortString() const;
598
602 //ENGINE_API friend FArchive& operator<<(FArchive& Ar, FURL& U);
603
607 //bool operator==(const FURL& Other) const;
608};
#define KARMA_API
Defining Karma's API macro for storage class information.
Definition Core.h:41
#define FORCEINLINE
Typical inlining macro for clarity.
Definition Core.h:157
ETravelType
Traveling from server to server.
Definition KarmaTypes.h:446
@ TRAVEL_Partial
Definition KarmaTypes.h:451
@ TRAVEL_Absolute
Definition KarmaTypes.h:448
@ TRAVEL_Relative
Definition KarmaTypes.h:454
@ TRAVEL_MAX
Definition KarmaTypes.h:457
#define KR_CORE_INFO(...)
A macro for logging information in the Core part.
Definition Log.h:85
ValueType * Find(const KeyType &Key)
Find the value associated with a specified key.
Definition KarmaTypes.h:132
KarmaMap()
Constructor.
Definition KarmaTypes.h:89
ValueType & FindOrAdd(const KeyType &Key)
Find the value associated with a specified key, or if none exists, adds a value using the default con...
Definition KarmaTypes.h:104
std::vector< BuildingBlock >::iterator end()
Getter for the last vector element.
Definition KarmaTypes.h:396
~KarmaVector()
Destructor.
Definition KarmaTypes.h:168
KarmaVector()
Constructor.
Definition KarmaTypes.h:159
uint32_t Num() const
Returns the total number of elements in a vector.
Definition KarmaTypes.h:302
FORCEINLINE int32_t AddUnique(const BuildingBlock &Item)
Definition KarmaTypes.h:221
std::vector< BuildingBlock >::iterator begin()
Getter for first vector element.
Definition KarmaTypes.h:386
std::vector< BuildingBlock > & ModifyElements()
Getter for elements of vector for modification in appropriate way.
Definition KarmaTypes.h:379
FORCEINLINE bool IsValidIndex(int32_t Index) const
Definition KarmaTypes.h:432
void Add(BuildingBlock aBlock)
Add an element to the vector.
Definition KarmaTypes.h:205
int32_t Find(const BuildingBlock &Item) const
Definition KarmaTypes.h:247
bool Contains(BuildingBlock aBlock) const
Sees if the vector contains the specified element.
Definition KarmaTypes.h:278
void SmartReset()
Just clear the elements.
Definition KarmaTypes.h:348
FORCEINLINE BuildingBlock & IndexToObject(int32_t Index)
Definition KarmaTypes.h:409
uint32_t Remove(BuildingBlock aBlock)
Removes an element from the vector.
Definition KarmaTypes.h:178
void SetVectorElementByIndex(int32_t Index, BuildingBlock Value)
Set the element of a vector using the vector index.
Definition KarmaTypes.h:361
const std::vector< BuildingBlock > & GetElements() const
Getter for the elements of vector.
Definition KarmaTypes.h:371
void Reset()
We just reset the vector. UE has the following implementation.
Definition KarmaTypes.h:318
Specifies why an actor is being deleted/removed from a level.
Definition KarmaTypes.h:21
Type
Definition KarmaTypes.h:23
@ EndPlayInEditor
Definition KarmaTypes.h:29
@ Destroyed
Definition KarmaTypes.h:25
@ RemovedFromWorld
Definition KarmaTypes.h:31
@ Quit
Definition KarmaTypes.h:33
@ LevelTransition
Definition KarmaTypes.h:27
Specifies the goal/source of a UWorld object.
Definition KarmaTypes.h:45
Type
Definition KarmaTypes.h:47
@ GamePreview
Definition KarmaTypes.h:64
@ None
Definition KarmaTypes.h:49
@ Editor
Definition KarmaTypes.h:55
@ Game
Definition KarmaTypes.h:52
@ Inactive
Definition KarmaTypes.h:70
@ PIE
Definition KarmaTypes.h:58
@ EditorPreview
Definition KarmaTypes.h:61
@ GameRPC
Definition KarmaTypes.h:67
int32_t m_Port
Definition KarmaTypes.h:495
std::string m_Host
Definition KarmaTypes.h:492
static FUrlConfig UrlConfig
Definition KarmaTypes.h:512
std::string m_Portal
Definition KarmaTypes.h:509
FURL(const std::string &Filename="")
Definition KarmaTypes.cpp:29
std::string m_Map
Definition KarmaTypes.h:500
std::string m_Protocol
Definition KarmaTypes.h:489
std::string m_RedirectURL
Definition KarmaTypes.h:503
Helper for obtaining the default Url configuration.
Definition KarmaTypes.h:464
void Reset()
Definition KarmaTypes.cpp:18
void Init()
Definition KarmaTypes.cpp:7