Cogs.Core
PropertiesManager.h
1#pragma once
2
3#include "Utilities/Strings.h"
4
5#include "Foundation/Memory/MemoryBuffer.h"
6
7#include <glm/vec3.hpp>
8
9#include <span>
10#include <vector>
11
12namespace Cogs::Core
13{
14 //TODO: Review functionality common with variable system.
15 //TODO: Review usage of type enums, consider merging.
16 enum struct PropertyType
17 {
18 Unknown = 0,
19 FirstInlineType,
20 // Types with inline storage in PropertyInfo struct.
21 Bool = FirstInlineType,
22 Integer,
23 Int2,
24 UnsignedInteger,
25 UInt2,
26 Float,
27 Float2,
28 Double,
29 StringRef,
30 // ----
31 LastInlineType,
32 // Types with allocated storage from data array.
33 String,
34 DoubleArray,
35 FloatArray,
36 IntArray,
37 UIntArray,
38 // ----
39 LastType
40 };
41
43 {
44 StringRef key;
45 uint32_t index;
46 PropertyType type = PropertyType::Unknown;
47 union
48 {
49 bool boolValue;
50 float floatValue;
51 float float2Value[2];
52 double doubleValue;
53 int32_t intValue;
54 int32_t int2Value[2];
55 uint32_t uintValue;
56 uint32_t uint2Value[2];
57 StringRef stringRefValue;
58 struct
59 {
60 // Offset in bytes from start of storage.
61 uint32_t offset;
62 // Size in bytes, including null terminator for strings.
63 uint32_t size;
64 } data;
65 };
66 };
67
74 struct COGSCORE_DLL_API PropertyStore
75 {
77 static constexpr uint32_t NoProperty = static_cast<uint32_t>(-1);
78
79 PropertyStore(MemBlockType memType = MemBlockType::PropertyStore);
81 PropertyStore(PropertyStore&&) noexcept;
82 PropertyStore & operator=(const PropertyStore &);
83 PropertyStore& operator=(PropertyStore&&) noexcept;
84
85 bool getProperty(StringView key, bool defaultValue);
86 float getProperty(StringView key, float defaultValue);
87 int getProperty(StringView key, int defaultValue);
88 uint32_t getProperty(StringView key, uint32_t defaultValue);
89 StringView getProperty(StringView key, const char * defaultValue) { return getProperty(key, StringView(defaultValue)); }
90 StringView getProperty(StringView key, StringView defaultValue);
91 std::span<const float> getProperty(StringView key, std::span<const float> defaultValue);
92 glm::vec3 getProperty(StringView key, glm::vec3 defaultValue);
93
94 uint32_t addProperty(StringView key, bool value);
95 uint32_t addProperty(StringView key, int32_t value);
96 uint32_t addProperty(StringView key, uint32_t value);
97 uint32_t addProperty(StringView key, float value);
98 uint32_t addProperty(StringView key, double value);
99 uint32_t addProperty(StringView key, const char * value) { return addProperty(key, StringView(value)); }
100 uint32_t addProperty(StringView key, StringRef value);
101 uint32_t addProperty(StringView key, StringView value);
102 uint32_t addProperty(StringView key, std::span<const float> values);
103 uint32_t addProperty(StringView key, std::span<const double> values);
104 uint32_t addProperty(StringView key, std::span<const int32_t> values);
105 uint32_t addProperty(StringView key, std::span<const uint32_t> values);
106 uint32_t addProperty(StringView key, const glm::vec3& values);
107 uint32_t addProperty(const PropertyStore& other, uint32_t otherIndex);
108
109 PropertyInfo & getProperty(StringView key);
110 PropertyInfo & getProperty(StringRef key);
111 PropertyInfo & getPropertyByIndex(uint32_t index);
112 const PropertyInfo & getPropertyByIndex(uint32_t index) const;
113
114 uint32_t findProperty(StringView key) const;
115 uint32_t findProperty(uint32_t first, uint32_t count, StringRef key) const;
116 bool hasProperty(StringView key) const { return findProperty(key) != NoProperty; }
117
118 uint32_t size() const { return static_cast<uint32_t>(headers.size()); }
119
120 StringView getKey(const PropertyInfo & p) const;
121 StringView getString(const PropertyInfo & p) const;
122 StringView getString(uint32_t index) const;
123
124 std::span<float> getFloatArray(uint32_t index);
125 std::span<const float> getFloatArray(const PropertyInfo& p) const;
126 std::span<const float> getFloatArray(uint32_t index) const;
127
128 std::span<const double> getDoubleArray(const PropertyInfo& p) const;
129 std::span<const double> getDoubleArray(uint32_t index) const;
130
131 std::span<const int32_t> getIntArray(const PropertyInfo& p) const;
132 std::span<const int32_t> getIntArray(uint32_t index) const;
133
134 std::span<const uint32_t> getUIntArray(const PropertyInfo& p) const;
135 std::span<const uint32_t> getUIntArray(uint32_t index) const;
136
137 std::span<const PropertyInfo> getHeaders() const { return std::span<const PropertyInfo>(headers.begin(), headers.end()); }
138 const uint8_t * getData() const { return static_cast<const uint8_t*>(data.data()); }
139
140 void copyProperties(const PropertyStore & other, uint32_t offset, uint32_t count);
141
142 size_t currentByteSize() const { return headers.byteSize() + dataSize; }
143 size_t allocatedByteSize() const { return headers.byteSize() + data.size(); }
144
145 private:
146 PropertyInfo & addProperty(StringView key, PropertyType type);
147 void addPropertyData(PropertyInfo & p, const void * data, size_t size);
148 uint8_t* getPropertyData(const PropertyInfo& p);
149 const uint8_t* getPropertyData(const PropertyInfo& p) const;
150
152 {
153 void * data;
154 uint32_t offset;
155 };
156
157 PropertyAlloc allocate(size_t propertySize);
158
159 //TODO: Use indirection capable of both owning and non-owning storage of properties.
160 // Evaluate usage of a single buffer (owned/non-owned) per store.
163 size_t dataSize = 0;
164 };
165
166 struct COGSCORE_DLL_API PropertyRange
167 {
168 uint32_t getPropertyIndex(StringRef key) const;
169 float getProperty(StringRef key, float defaultValue) const;
170 int getProperty(StringRef key, int defaultValue) const;
171 StringView getProperty(StringRef key, StringView defaultValue) const;
172 std::span<const float> getProperty(StringRef key, std::span<const float> defaultValue) const;
173 std::span<const uint32_t> getProperty(StringRef key, std::span<const uint32_t> defaultValue) const;
174
175 const PropertyStore * store = nullptr;
176 uint32_t firstProperty = 0;
177 uint32_t numProperties = 0;
178 };
179}
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....