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#include <glm/glm.hpp>
19
21{
24
27};
28
46
52enum { INDEX_NONE = -1 };
53
55namespace EWorldType
56{
83}
84
88template <typename KeyType, typename ValueType>
90{
91public:
92 typedef std::map<KeyType, ValueType> AMap;
93 typedef typename AMap::iterator AnIterator;
94
101 {
102 }
103
109 void Add(const KeyType& Key, const ValueType& Value)
110 {
111 m_KeyValuePair.insert(typename AMap::value_type(Key, Value));
112 }
113
122 bool Contains(const KeyType& Key) const
123 {
124 return m_KeyValuePair.find(Key) != m_KeyValuePair.end();
125 }
126
138 ValueType& FindOrAdd(const KeyType& Key)
139 {
140 ValueType aValue;
141 std::pair<AnIterator, bool> const& result = m_KeyValuePair.insert(typename AMap::value_type(Key, aValue));
142
143 // a simple return.first->second could work. doing this for better understandibility
144 if(result.second)
145 {
146 // Value was inserted, meaning wasn't there earlier
147 return result.first->second;
148 }
149 else
150 {
151 // Nothing inserted, meaning value with key already exists
152 return result.first->second;
153 }
154 }
155
165
166 ValueType* Find(const KeyType& Key)
167 {
168 AnIterator result = m_KeyValuePair.find(Key);
169
170 if(result != m_KeyValuePair.end())
171 {
172 return &result->second;
173 }
174
175 return nullptr;
176 }
177
188 ValueType& operator[](const KeyType& Key)
189 {
190 return m_KeyValuePair[Key];
191 }
192
202 const ValueType& operator[](const KeyType& Key) const
203 {
204 return m_KeyValuePair.at(Key);
205 }
206
207 uint32_t Num() const
208 {
209 return (uint32_t) m_KeyValuePair.size();
210 }
211
212 auto begin()
213 {
214 return m_KeyValuePair.begin();
215 }
216
217 auto end()
218 {
219 return m_KeyValuePair.end();
220 }
221
222 auto begin() const
223 {
224 return m_KeyValuePair.cbegin();
225 }
226
227 auto end() const
228 {
229 return m_KeyValuePair.cend();
230 }
231
232protected:
233 AMap m_KeyValuePair;
234};
235
241template<typename BuildingBlock>
243{
244public:
251 {
252 }
253
260 {
261 }
262
273 uint32_t Remove(BuildingBlock aBlock)
274 {
275 uint32_t occurences = 0;
276 typename std::vector<BuildingBlock>::iterator iterator = m_Elements.begin();
277
278 while (iterator != m_Elements.end())
279 {
280 if (*iterator == aBlock)
281 {
282 iterator = m_Elements.erase(iterator);
283 occurences++;
284 }
285 else
286 {
287 ++iterator;
288 }
289 }
290
291 return occurences;
292 }
293
300 void Add(BuildingBlock aBlock)
301 {
302 m_Elements.push_back(aBlock);
303 }
304
310 void RangeCheck(uint32_t Index) const
311 {
312 KR_CORE_ASSERT((Index >= 0 && Index < Num()), "KarmaVector: Range check failed");
313 }
314
326 FORCEINLINE int32_t AddUnique(const BuildingBlock& Item)
327 {
328 int32_t index = Find(Item);
329
330 if(index == -1)
331 {
332 Add(Item);
333 return Num() - 1;
334 }
335 else
336 {
337 return index;
338 }
339 }
340
352 int32_t Find(const BuildingBlock& Item) const
353 {
354 int32_t returnIndex = 0;
355
356 typename std::vector<BuildingBlock>::const_iterator iter = m_Elements.begin();
357
358 for (; iter != m_Elements.end(); iter++)
359 {
360 if (m_Elements[returnIndex] == Item)
361 {
362 break;
363 }
364 returnIndex++;
365 }
366
367 if (iter != m_Elements.end())
368 {
369 return returnIndex;
370 }
371
372 return INDEX_NONE;
373 }
374
383 bool Contains(BuildingBlock aBlock) const
384 {
385 typename std::vector<BuildingBlock>::const_iterator iterator = m_Elements.begin();
386
387 while (iterator != m_Elements.end())
388 {
389 if (*iterator == aBlock)
390 {
391 return true;
392 }
393 else
394 {
395 ++iterator;
396 }
397 }
398
399 return false;
400 }
401
413 void RemoveAtSwap(int32_t Index/*, EAllowShrinking AllowShrinking = EAllowShrinking::Yes*/)
414 {
415 RangeCheck(Index);
416 RemoveAtSwapImpl(Index);
417
418 //if (AllowShrinking == EAllowShrinking::Yes)
419 //{
420 // not functional yet
421 //}
422 }
423
435 uint32_t RemoveSingleSwap(const BuildingBlock& Item/*, EAllowShrinking AllowShrinking = EAllowShrinking::Yes*/)
436 {
437 int32_t Index = Find(Item);
438
439 if (Index == INDEX_NONE)
440 {
441 return 0;
442 }
443
444 RemoveAtSwap(Index/*, AllowShrinking*/);
445
446 return 1;
447 }
448
454 uint32_t Num() const
455 {
456 return (uint32_t) m_Elements.size();
457 }
458
470 void Reset()
471 {
472 // Maybe make smartpointer instead of manually deleting
473 typename std::vector<BuildingBlock>::iterator iter = m_Elements.begin();
474
475 // 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
476 if constexpr(std::is_pointer_v<BuildingBlock>)
477 {
478 for (iter = m_Elements.begin(); iter != m_Elements.end(); iter++)
479 {
480 if (*iter != nullptr)
481 {
482 KR_CORE_INFO("Deleting UObjects");
483 delete *iter;
484 *iter = nullptr;
485 }
486 }
487 }
488
489 m_Elements.clear();
490 }
491
492 void Resize(uint32_t NewSize)
493 {
494 m_Elements.resize(NewSize);
495 }
496
506 {
507 m_Elements.clear();
508 }
509
518 void SetVectorElementByIndex(int32_t Index, BuildingBlock Value)
519 {
520 m_Elements[Index] = Value;
521 }
522
529 inline void Clear() { m_Elements.clear(); }
530
536 inline const std::vector<BuildingBlock>& GetElements() const { return m_Elements; }
537
544 inline std::vector<BuildingBlock>& ModifyElements() { return m_Elements; }
545
551 typename std::vector<BuildingBlock>::iterator begin()
552 {
553 return m_Elements.begin();
554 }
555
561 typename std::vector<BuildingBlock>::iterator end()
562 {
563 return m_Elements.end();
564 }
565
566 typename std::vector<BuildingBlock>::const_iterator begin() const
567 {
568 return m_Elements.cbegin();
569 }
570
571 typename std::vector<BuildingBlock>::const_iterator end() const
572 {
573 return m_Elements.cend();
574 }
575
584 FORCEINLINE BuildingBlock& IndexToObject(int32_t Index)
585 {
586 KR_CORE_ASSERT(Index >= 0, "");
587
588 if(Index < Num())
589 {
590 return m_Elements.at(Index);
591 }
592
593 KR_CORE_ASSERT(false, "Shouldn't happen");
594
595 static BuildingBlock aBlock;
596 return aBlock;
597 }
598
607 FORCEINLINE bool IsValidIndex(int32_t Index) const
608 {
609 return Index >= 0 && Index < Num();
610 }
611
617 FORCEINLINE BuildingBlock const* GetData() const { return m_Elements.data(); }
618
619 BuildingBlock& operator[](int32_t Index)
620 {
621 RangeCheck(Index);
622 return m_Elements[Index];
623 }
624
625 const BuildingBlock& operator[](int32_t Index) const
626 {
627 RangeCheck(Index);
628 return m_Elements[Index];
629 }
630
631private:
638 void RemoveAtSwapImpl(int32_t Index)
639 {
640 const BuildingBlock* Data = GetData();
641 const BuildingBlock* Dest = Data + Index;
642
643 // Number of elements (after the generated vacancy) to move
644 const int32_t NumElementsAfterHole = Num() - Index - 1;
645 const int32_t NumElementsToMoveIntoHole = glm::min(1, NumElementsAfterHole);
646
647 if (NumElementsToMoveIntoHole)
648 {
649 //std::memmove((void *) Dest, (const void*)(Data + Num() - NumElementsToMoveIntoHole),
650 // sizeof(BuildingBlock) * NumElementsToMoveIntoHole);
651 const int32_t LastElementIndex = Num() - 1;
652
653 m_Elements[Index] = m_Elements[LastElementIndex];
654 }
655 m_Elements.pop_back();
656 }
657
658protected:
659 std::vector<BuildingBlock> m_Elements;
660};
661
679
681enum class ETeleportType : uint8_t
682{
685
688
691};
692
697{
698 std::string m_DefaultProtocol;
699 std::string m_DefaultName;
700 std::string m_DefaultHost;
701 std::string m_DefaultPortal;
702 std::string m_DefaultSaveExt;
703 int32_t m_DefaultPort;
704
708 void Init();
709
713 void Reset();
714};
715
720{
722 std::string m_Protocol;
723
725 std::string m_Host;
726
728 int32_t m_Port;
729
730 int32_t m_Valid;
731
733 std::string m_Map;
734
736 std::string m_RedirectURL;
737
738 // Options.
739 //TArray<FString> Op;
740
742 std::string m_Portal;
743
746 static bool m_bDefaultsInitialized;
747
751 //explicit FURL(ENoInit) { }
752
756 FURL(const std::string& Filename = "");
757
761 //FURL(FURL* Base, const TCHAR* TextURL, ETravelType Type);
762
763 //static void StaticInit();
764 //static void StaticExit();
765
771 //static void FilterURLString(const std::string& Str);
772
779 //bool IsInternal() const;
780
785 //bool IsLocalInternal() const;
786
790 //bool HasOption(const TCHAR* Test) const;
791
800 //const TCHAR* GetOption(const TCHAR* Match, const TCHAR* Default) const;
801
805 //void LoadURLConfig(const TCHAR* Section, const std::string& Filename = "GGameIni");
806
810 //void SaveURLConfig(const TCHAR* Section, const TCHAR* Item, const std::string& Filename = "GGameIni") const;
811
815 //void AddOption(const TCHAR* Str);
816
820 //void RemoveOption(const TCHAR* Key, const TCHAR* Section = nullptr, const std::string& Filename = "GGameIni");
821
825 //std::string ToString(bool FullyQualified = 0) const;
826
830 //std::string GetHostPortString() const;
831
835 //ENGINE_API friend FArchive& operator<<(FArchive& Ar, FURL& U);
836
840 //bool operator==(const FURL& Other) const;
841};
#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:170
ETeleportType
Definition KarmaTypes.h:682
@ TeleportPhysics
Definition KarmaTypes.h:687
@ None
Definition KarmaTypes.h:684
@ ResetPhysics
Definition KarmaTypes.h:690
EAllowShrinking
Definition KarmaTypes.h:21
@ Yes
Definition KarmaTypes.h:23
@ No
Definition KarmaTypes.h:26
ETravelType
Traveling from server to server.
Definition KarmaTypes.h:666
@ TRAVEL_Partial
Definition KarmaTypes.h:671
@ TRAVEL_Absolute
Definition KarmaTypes.h:668
@ TRAVEL_Relative
Definition KarmaTypes.h:674
@ TRAVEL_MAX
Definition KarmaTypes.h:677
#define KR_CORE_INFO(...)
A macro for logging information in the Core part.
Definition Log.h:85
bool Contains(const KeyType &Key) const
Checks if the map contains a specified key.
Definition KarmaTypes.h:122
ValueType * Find(const KeyType &Key)
Find the value associated with a specified key.
Definition KarmaTypes.h:166
const ValueType & operator[](const KeyType &Key) const
Overloaded subscript operator for accessing values by key (const version).
Definition KarmaTypes.h:202
ValueType & operator[](const KeyType &Key)
Overloaded subscript operator for accessing values by key.
Definition KarmaTypes.h:188
void Add(const KeyType &Key, const ValueType &Value)
Addes a key-value pair to the map.
Definition KarmaTypes.h:109
KarmaMap()
Constructor.
Definition KarmaTypes.h:100
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:138
std::vector< BuildingBlock >::iterator end()
Getter for the last vector element.
Definition KarmaTypes.h:561
~KarmaVector()
Destructor.
Definition KarmaTypes.h:259
KarmaVector()
Constructor.
Definition KarmaTypes.h:250
uint32_t Num() const
Returns the total number of elements in a vector.
Definition KarmaTypes.h:454
FORCEINLINE int32_t AddUnique(const BuildingBlock &Item)
Definition KarmaTypes.h:326
std::vector< BuildingBlock >::iterator begin()
Getter for first vector element.
Definition KarmaTypes.h:551
std::vector< BuildingBlock > & ModifyElements()
Getter for elements of vector for modification in appropriate way.
Definition KarmaTypes.h:544
FORCEINLINE bool IsValidIndex(int32_t Index) const
Definition KarmaTypes.h:607
void RemoveAtSwap(int32_t Index)
Removes an element at given location, swapping the last element into its place, and shrinking the arr...
Definition KarmaTypes.h:413
void Add(BuildingBlock aBlock)
Add an element to the vector.
Definition KarmaTypes.h:300
int32_t Find(const BuildingBlock &Item) const
Definition KarmaTypes.h:352
bool Contains(BuildingBlock aBlock) const
Sees if the vector contains the specified element.
Definition KarmaTypes.h:383
void SmartReset()
Just clear the elements.
Definition KarmaTypes.h:505
FORCEINLINE BuildingBlock & IndexToObject(int32_t Index)
Definition KarmaTypes.h:584
void RangeCheck(uint32_t Index) const
Sanity check for the index.
Definition KarmaTypes.h:310
uint32_t Remove(BuildingBlock aBlock)
Removes an element from the vector.
Definition KarmaTypes.h:273
void SetVectorElementByIndex(int32_t Index, BuildingBlock Value)
Set the element of a vector using the vector index.
Definition KarmaTypes.h:518
FORCEINLINE BuildingBlock const * GetData() const
Helper function to return typed pointer to the first entry of array.
Definition KarmaTypes.h:617
void Clear()
Clears the vector from all the elements.
Definition KarmaTypes.h:529
const std::vector< BuildingBlock > & GetElements() const
Getter for the elements of vector.
Definition KarmaTypes.h:536
uint32_t RemoveSingleSwap(const BuildingBlock &Item)
Removes a first appearence of single element from the array, swapping the last element into its place...
Definition KarmaTypes.h:435
void Reset()
We just reset the vector. UE has the following implementation.
Definition KarmaTypes.h:470
Specifies why an actor is being deleted/removed from a level.
Definition KarmaTypes.h:31
Type
Definition KarmaTypes.h:33
@ EndPlayInEditor
Definition KarmaTypes.h:39
@ Destroyed
Definition KarmaTypes.h:35
@ RemovedFromWorld
Definition KarmaTypes.h:41
@ Quit
Definition KarmaTypes.h:43
@ LevelTransition
Definition KarmaTypes.h:37
Specifies the goal/source of a UWorld object.
Definition KarmaTypes.h:56
Type
Definition KarmaTypes.h:58
@ GamePreview
Definition KarmaTypes.h:75
@ None
Definition KarmaTypes.h:60
@ Editor
Definition KarmaTypes.h:66
@ Game
Definition KarmaTypes.h:63
@ Inactive
Definition KarmaTypes.h:81
@ PIE
Definition KarmaTypes.h:69
@ EditorPreview
Definition KarmaTypes.h:72
@ GameRPC
Definition KarmaTypes.h:78
int32_t m_Port
Definition KarmaTypes.h:728
std::string m_Host
Definition KarmaTypes.h:725
static FUrlConfig UrlConfig
Definition KarmaTypes.h:745
std::string m_Portal
Definition KarmaTypes.h:742
FURL(const std::string &Filename="")
Definition KarmaTypes.cpp:29
std::string m_Map
Definition KarmaTypes.h:733
std::string m_Protocol
Definition KarmaTypes.h:722
std::string m_RedirectURL
Definition KarmaTypes.h:736
Helper for obtaining the default Url configuration.
Definition KarmaTypes.h:697
void Reset()
Definition KarmaTypes.cpp:18
void Init()
Definition KarmaTypes.cpp:7