| 1 | /* |
| 2 | * LegacyClonk |
| 3 | * |
| 4 | * Copyright (c) RedWolf Design |
| 5 | * Copyright (c) 2017-2019, 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 | // Stellt eine einfache dynamische Integer-Liste bereit. |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include "C4Value.h" |
| 22 | #include "C4ValueStandardRefCountedContainer.h" |
| 23 | |
| 24 | #include <span> |
| 25 | #include <vector> |
| 26 | |
| 27 | class C4ValueList |
| 28 | { |
| 29 | public: |
| 30 | enum { MaxSize = 1000000, }; // ye shalt not create arrays larger than that! |
| 31 | |
| 32 | C4ValueList() = default; |
| 33 | C4ValueList(std::int32_t size); |
| 34 | C4ValueList(const C4ValueList &other); |
| 35 | |
| 36 | template<typename T> |
| 37 | C4ValueList(const std::span<T> data) |
| 38 | { |
| 39 | values.reserve(n: data.size()); |
| 40 | |
| 41 | for (const auto value : data) |
| 42 | { |
| 43 | values.emplace_back(value); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | C4ValueList &operator=(const C4ValueList &ValueList2); |
| 48 | |
| 49 | protected: |
| 50 | std::vector<C4Value> values; |
| 51 | |
| 52 | public: |
| 53 | std::int32_t GetSize() const { return static_cast<std::int32_t>(values.size()); } |
| 54 | |
| 55 | const C4Value &GetItem(const std::int32_t index) const { return Inside(ival: index, lbound: 0, rbound: GetSize() - 1) ? values[index] : C4VNull; } |
| 56 | C4Value &GetItem(std::int32_t index); |
| 57 | |
| 58 | C4Value operator[](const std::int32_t index) const { return GetItem(index); } |
| 59 | C4Value &operator[](const std::int32_t index) { return GetItem(index); } |
| 60 | |
| 61 | void Reset(); |
| 62 | void SetSize(std::int32_t size); // (enlarge only!) |
| 63 | |
| 64 | void DenumeratePointers(); |
| 65 | |
| 66 | // comparison |
| 67 | bool operator==(const C4ValueList &other) const = default; |
| 68 | |
| 69 | // Compilation |
| 70 | void CompileFunc(class StdCompiler *pComp); |
| 71 | |
| 72 | private: |
| 73 | friend class C4SortObject; |
| 74 | }; |
| 75 | |
| 76 | // value list with reference count, used for arrays |
| 77 | class C4ValueArray : public C4ValueList, public C4ValueStandardRefCountedContainer<C4ValueArray> |
| 78 | { |
| 79 | public: |
| 80 | C4ValueArray(); |
| 81 | C4ValueArray(std::int32_t size); |
| 82 | |
| 83 | template<typename T> |
| 84 | C4ValueArray(const std::span<T> values) : C4ValueList{values} {} |
| 85 | |
| 86 | ~C4ValueArray(); |
| 87 | |
| 88 | // Change length, return self or new copy if necessary |
| 89 | C4ValueArray *SetLength(std::int32_t size); |
| 90 | virtual bool hasIndex(const C4Value &index) const override; |
| 91 | virtual C4Value &operator[](const C4Value &index) override; |
| 92 | using C4ValueList::operator[]; |
| 93 | |
| 94 | virtual void CompileFunc(StdCompiler *pComp) override |
| 95 | { |
| 96 | C4ValueList::CompileFunc(pComp); |
| 97 | } |
| 98 | |
| 99 | virtual void DenumeratePointers() override |
| 100 | { |
| 101 | C4ValueList::DenumeratePointers(); |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | // Only for IncRef/AddElementRef |
| 106 | friend C4ValueStandardRefCountedContainer<C4ValueArray>; |
| 107 | C4ValueArray(const C4ValueArray &Array2); |
| 108 | }; |
| 109 | |