Cogs.Core
Parsing.h
1#pragma once
2
3#include "Base.h"
4#include "Flags.h"
5#include "Utilities/Strings.h"
6#include "ValueVariant.h"
7
8#include "Rendering/DataFormat.h"
9
10#include "Foundation/StringView.h"
11#include "Foundation/Reflection/TypeDatabase.h"
12
13#include <glm/glm.hpp>
14
15#include <vector>
16
17
18namespace Cogs::Core
19{
20 extern const size_t ParsedDataTypeSizes[];
21 extern const char * ParsedDataTypeNames[];
22
23 enum struct ParsedValueTextureFlags
24 {
25 None = 0,
26 LinearSampler = 1<<0, // Use linear sampler instead of point sampling.
27 DepthTexture = 1<<1,
28 };
29 ENABLE_ENUM_FLAGS(ParsedValueTextureFlags);
30
31 enum struct ParsedValueBufferFlags
32 {
33 None = 0,
34 Structured = 1 << 0,
35 };
36 ENABLE_ENUM_FLAGS(ParsedValueBufferFlags);
37
39 struct COGSCORE_DLL_API ParsedValue
40 {
41 ParsedValue() {};
42 ParsedValue(const ParsedValue & other);
43 ParsedValue(ParsedValue && other) noexcept;
44 ParsedValue & operator=(const ParsedValue & other);
45
47 std::string key;
48
50 std::string value;
51
53 ParsedDataType type = ParsedDataType::Unknown;
55 size_t valueHash = 0;
56
58 size_t dimension = static_cast<size_t>(-1);
59
60 public:
61 union
62 {
63 float floatValue;
64 glm::vec2 float2Value;
65 glm::vec3 float3Value;
66 glm::vec4 float4Value;
67 glm::mat4 float4x4Value;
68 int intValue;
69 glm::ivec2 int2Value;
70 glm::ivec3 int3Value;
71 glm::ivec4 int4Value;
72 uint32_t uintValue;
73 glm::uvec2 uint2Value;
74 glm::uvec3 uint3Value;
75 glm::uvec4 uint4Value;
76 double doubleValue;
77 bool boolValue;
78 struct {
79 ParsedValueTextureFlags flags;
80 ParsedDataType dataType;
81 int samples;
82 } texture;
83 struct {
84 ParsedValueBufferFlags flags;
85 ParsedDataType dataType;
86 } buffer;
87
88 float _fill[16] = { 0.f };
89 };
90 std::vector<std::pair<size_t, std::string>> expressions;
91
92 std::vector<ParsedValue> values;
93
94 public:
95 bool asBool(bool& rv, bool complain = true) const;
96 bool asFloat(float& rv, bool complain = true) const;
97 bool asInt(int& rv, bool complain = true) const;
98 };
99
100 using TokenStream = std::vector<StringView>;
101
102 bool matchChars(const char c, const StringView & chars);
103
104 void COGSCORE_DLL_API split(const StringView & str, const StringView & splits, TokenStream & tokens);
105 void COGSCORE_DLL_API split(const StringView & str, const StringView & splits, const int grouper, TokenStream & tokens);
106 TokenStream COGSCORE_DLL_API split(const StringView & str, const StringView & splits);
107
108 TokenStream tokenize(const StringView & valueStr);
109 void tokenize(const StringView & valueStr, TokenStream & tokens);
110
111 void COGSCORE_DLL_API parseStringValue(const StringView & value, ParsedValue & v);
112
113 Cogs::TextureFormat COGSCORE_DLL_API parseTextureFormat(const StringView & str, Cogs::TextureFormat defaultFormat = TextureFormat::R8G8B8A8_UNORM_SRGB);
114
115 double parseDouble(const StringView & token, double defaultValue = 0);
116 float parseFloat(const StringView & token, float defaultValue = 0);
117 int32_t parseInt(const StringView & token, int32_t defaultValue = 0);
118 uint32_t parseUInt(const StringView & token, uint32_t defaultValue = 0);
119 bool parseBool(const StringView & token, bool defaultValue = false);
120
122 // Underscore postfix is to avoid name-resolution failure in a template.
123 bool parseBool_(bool& rv, const StringView& token);
124
126 // Underscore postfix is to avoid name-resolution failure in a template.
127 bool parseInt_(int32_t& rv, const StringView& token);
128
133 uint64_t COGSCORE_DLL_API parseISO8601(const StringView& iso8601);
134 uint64_t COGSCORE_DLL_API parseISO8601(const StringView& iso8601, int& tzOffset);
135
136 int COGSCORE_DLL_API parseEnum(const StringView & token, const Reflection::Type & type, int defaultValue);
137 int COGSCORE_DLL_API parseEnumFlags(const StringView & token, const Reflection::Type & type, int defaultValue);
138
139 template<typename T>
140 T parseEnum(const StringView & token, T defaultValue = T())
141 {
142 return static_cast<T>(parseEnum(token, Reflection::TypeDatabase::getType<T>(), static_cast<int>(defaultValue)));
143 }
144
145 template<typename T>
146 T parseEnumFlags(const StringView & token, T defaultValue = T())
147 {
148 return static_cast<T>(parseEnumFlags(token, Reflection::TypeDatabase::getType<T>(), static_cast<int>(defaultValue)));
149 }
150
151 void parseQueryString(std::vector<ParsedValue>& attributes, StringView query);
152
153 void parseValues(float * values, const TokenStream & tokens, size_t expected);
154 void parseValues(double * values, const TokenStream & tokens, size_t expected);
155 void parseValues(int * values, const TokenStream & tokens, size_t expected);
156 void parseValues(uint32_t * values, const TokenStream & tokens, size_t expected);
157 void parseValues(bool* values, const TokenStream & tokens, size_t expected);
158
159 void parseValues(std::vector<std::pair<size_t, std::string>>& expressions, float * values, const TokenStream & tokens, size_t expected);
160 void parseValues(std::vector<std::pair<size_t, std::string>>& expressions, double * values, const TokenStream & tokens, size_t expected);
161 void parseValues(std::vector<std::pair<size_t, std::string>>& expressions, int * values, const TokenStream & tokens, size_t expected);
162 void parseValues(std::vector<std::pair<size_t, std::string>>& expressions, uint32_t * values, const TokenStream & tokens, size_t expected);
163 void parseValues(std::vector<std::pair<size_t, std::string>>& expressions, bool* values, const TokenStream & tokens, size_t expected);
164}
Represents a discrete type definition, describing a native type class.
Definition: Type.h:89
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....
bool parseBool_(bool &rv, const StringView &token)
Parse a bool putting return value in rv and returning whether or not parsing was successful.
Definition: Parsing.cpp:638
uint64_t COGSCORE_DLL_API parseISO8601(const StringView &iso8601)
Parse ISO 8601 timestamps.
Definition: Parsing.cpp:675
bool parseInt_(int32_t &rv, const StringView &token)
Parse an int putting return value in rv and returning whether or not parsing was successful.
Definition: Parsing.cpp:659
Stores the parsed output of a key/value pair.
Definition: Parsing.h:40
std::string key
The input key.
Definition: Parsing.h:47
std::string value
The value is available for all non vector types.
Definition: Parsing.h:50