| 1 | /* |
| 2 | * LegacyClonk |
| 3 | * |
| 4 | * Copyright (c) RedWolf Design |
| 5 | * Copyright (c) 2017-2022, The LegacyClonk Team and contributors |
| 6 | * |
| 7 | * Distributed under the terms of the ISC license; see accompanying file |
| 8 | * "COPYING" for details. |
| 9 | * |
| 10 | * "Clonk" is a registered trademark of Matthes Bender, used with permission. |
| 11 | * See accompanying file "TRADEMARK" for details. |
| 12 | * |
| 13 | * To redistribute this file separately, substitute the full license texts |
| 14 | * for the above references. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <C4Value.h> |
| 20 | |
| 21 | // implements a list of C4Values associated with a name list. |
| 22 | // the list is split in the two components (data/names) to make it possible |
| 23 | // to have multiple data lists using a single name list |
| 24 | |
| 25 | class C4ValueMapNames; |
| 26 | |
| 27 | // the data list |
| 28 | class C4ValueMapData |
| 29 | { |
| 30 | friend class C4ValueMapNames; |
| 31 | |
| 32 | public: |
| 33 | // construction/destruction |
| 34 | C4ValueMapData(); |
| 35 | C4ValueMapData(const C4ValueMapData &) = delete; |
| 36 | virtual ~C4ValueMapData(); |
| 37 | C4ValueMapData &operator=(const C4ValueMapData &DataToCopy); |
| 38 | |
| 39 | bool operator==(const C4ValueMapData &Data) const; |
| 40 | |
| 41 | // data array |
| 42 | C4Value *pData; |
| 43 | |
| 44 | // pointer on name list |
| 45 | C4ValueMapNames *pNames; |
| 46 | |
| 47 | // using temporary name list? |
| 48 | // (delete when changing list) |
| 49 | bool bTempNameList; |
| 50 | |
| 51 | // returns the item specified or 0 if it doesn't exist |
| 52 | C4Value *GetItem(const char *strName); |
| 53 | C4Value *GetItem(int32_t iNr); |
| 54 | |
| 55 | // sets the name list |
| 56 | void SetNameList(C4ValueMapNames *pnNames); |
| 57 | |
| 58 | // creates a new (empty) temporary name list for this data class |
| 59 | C4ValueMapNames *CreateTempNameList(); |
| 60 | |
| 61 | void Reset(); // resets content & unreg name list |
| 62 | |
| 63 | int32_t GetAnzItems(); |
| 64 | |
| 65 | C4Value &operator[](int32_t iNr) { return *GetItem(iNr); } |
| 66 | C4Value &operator[](const char *strName) { return *GetItem(strName); } |
| 67 | |
| 68 | void DenumeratePointers(); |
| 69 | |
| 70 | void CompileFunc(StdCompiler *pComp); |
| 71 | |
| 72 | private: |
| 73 | // a list linking all data lists using the same name list together. |
| 74 | C4ValueMapData *pNext; |
| 75 | |
| 76 | void Register(C4ValueMapNames *pnNames); |
| 77 | void UnRegister(); |
| 78 | |
| 79 | // called by names list to tell us that the name list |
| 80 | // was changed and from SetNameList (the data list has to be rordered...) |
| 81 | void OnNameListChanged(const char *const *pOldNames, int32_t iOldSize); |
| 82 | |
| 83 | // (Re)Allocs data list |
| 84 | // old data will be deleted! |
| 85 | // (size taken from pNames->iSize) |
| 86 | void ReAllocList(); |
| 87 | }; |
| 88 | |
| 89 | // the names list |
| 90 | class C4ValueMapNames |
| 91 | { |
| 92 | friend class C4ValueMapData; |
| 93 | |
| 94 | public: |
| 95 | // construction/destruction |
| 96 | C4ValueMapNames(); |
| 97 | C4ValueMapNames(C4ValueMapNames &) = delete; |
| 98 | C4ValueMapNames &operator=(C4ValueMapNames &NamesToCopy); |
| 99 | virtual ~C4ValueMapNames(); |
| 100 | |
| 101 | // name array |
| 102 | char **pNames; |
| 103 | |
| 104 | // extra data array |
| 105 | int32_t *; |
| 106 | |
| 107 | // item count |
| 108 | int32_t iSize; |
| 109 | |
| 110 | // set name array |
| 111 | void SetNameArray(const char *const *pnNames, int32_t nSize); |
| 112 | |
| 113 | // add a name; return index of added element (or index of current if it's already in the list) |
| 114 | int32_t AddName(const char *pnName, int32_t = 0); |
| 115 | |
| 116 | // returns the nr of the given name |
| 117 | // (= nr of value in "child" data lists) |
| 118 | // returns -1 if no item with given name exists |
| 119 | int32_t GetItemNr(const char *strName); |
| 120 | |
| 121 | void Reset(); |
| 122 | |
| 123 | private: |
| 124 | // points to first data list using this name list |
| 125 | C4ValueMapData *pFirst; |
| 126 | |
| 127 | void Register(C4ValueMapData *pData); |
| 128 | void UnRegister(C4ValueMapData *pData); |
| 129 | |
| 130 | // changes the name list |
| 131 | void ChangeNameList(const char *const *pnNames, int32_t *, int32_t nSize); |
| 132 | }; |
| 133 | |