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 };
28 ENABLE_ENUM_FLAGS(ParsedValueTextureFlags);
29
31 struct COGSCORE_DLL_API ParsedValue
32 {
33 ParsedValue() {};
34 ParsedValue(const ParsedValue & other);
35 ParsedValue(ParsedValue && other) noexcept;
36 ParsedValue & operator=(const ParsedValue & other);
37
39 std::string key;
40
42 std::string value;
43
45 ParsedDataType type = ParsedDataType::Unknown;
47 size_t valueHash = 0;
48
50 size_t dimension = static_cast<size_t>(-1);
51
52 public:
53 union
54 {
55 float floatValue;
56 glm::vec2 float2Value;
57 glm::vec3 float3Value;
58 glm::vec4 float4Value;
59 glm::mat4 float4x4Value;
60 int intValue;
61 glm::ivec2 int2Value;
62 glm::ivec3 int3Value;
63 glm::ivec4 int4Value;
64 uint32_t uintValue;
65 glm::uvec2 uint2Value;
66 glm::uvec3 uint3Value;
67 glm::uvec4 uint4Value;
68 double doubleValue;
69 bool boolValue;
70 struct {
71 ParsedValueTextureFlags flags;
72 } texture;
73 float _fill[16] = { 0.f };
74 };
75 std::vector<std::pair<size_t, std::string>> expressions;
76
77 std::vector<ParsedValue> values;
78
79 public:
80 bool asBool(bool& rv, bool complain = true) const;
81 bool asFloat(float& rv, bool complain = true) const;
82 bool asInt(int& rv, bool complain = true) const;
83 };
84
85 using TokenStream = std::vector<StringView>;
86
87 bool matchChars(const char c, const StringView & chars);
88
89 void COGSCORE_DLL_API split(const StringView & str, const StringView & splits, TokenStream & tokens);
90 void COGSCORE_DLL_API split(const StringView & str, const StringView & splits, const int grouper, TokenStream & tokens);
91 TokenStream COGSCORE_DLL_API split(const StringView & str, const StringView & splits);
92
93 TokenStream tokenize(const StringView & valueStr);
94 void tokenize(const StringView & valueStr, TokenStream & tokens);
95
96 void COGSCORE_DLL_API parseStringValue(const StringView & value, ParsedValue & v);
97
98 Cogs::TextureFormat COGSCORE_DLL_API parseTextureFormat(const StringView & str, Cogs::TextureFormat defaultFormat = TextureFormat::R8G8B8A8_UNORM_SRGB);
99
100 double parseDouble(const StringView & token, double defaultValue = 0);
101 float parseFloat(const StringView & token, float defaultValue = 0);
102 int32_t parseInt(const StringView & token, int32_t defaultValue = 0);
103 uint32_t parseUInt(const StringView & token, uint32_t defaultValue = 0);
104 bool parseBool(const StringView & token, bool defaultValue = false);
105
107 // Underscore postfix is to avoid name-resolution failure in a template.
108 bool parseBool_(bool& rv, const StringView& token);
109
111 // Underscore postfix is to avoid name-resolution failure in a template.
112 bool parseInt_(int32_t& rv, const StringView& token);
113
118 uint64_t COGSCORE_DLL_API parseISO8601(const StringView& iso8601);
119 uint64_t COGSCORE_DLL_API parseISO8601(const StringView& iso8601, int& tzOffset);
120
121 int COGSCORE_DLL_API parseEnum(const StringView & token, const Reflection::Type & type, int defaultValue);
122 int COGSCORE_DLL_API parseEnumFlags(const StringView & token, const Reflection::Type & type, int defaultValue);
123
124 template<typename T>
125 T parseEnum(const StringView & token, T defaultValue = T())
126 {
127 return static_cast<T>(parseEnum(token, Reflection::TypeDatabase::getType<T>(), static_cast<int>(defaultValue)));
128 }
129
130 template<typename T>
131 T parseEnumFlags(const StringView & token, T defaultValue = T())
132 {
133 return static_cast<T>(parseEnumFlags(token, Reflection::TypeDatabase::getType<T>(), static_cast<int>(defaultValue)));
134 }
135
136 void parseQueryString(std::vector<ParsedValue>& attributes, StringView query);
137
138 void parseValues(float * values, const TokenStream & tokens, size_t expected);
139 void parseValues(double * values, const TokenStream & tokens, size_t expected);
140 void parseValues(int * values, const TokenStream & tokens, size_t expected);
141 void parseValues(uint32_t * values, const TokenStream & tokens, size_t expected);
142 void parseValues(bool* values, const TokenStream & tokens, size_t expected);
143
144 void parseValues(std::vector<std::pair<size_t, std::string>>& expressions, float * values, const TokenStream & tokens, size_t expected);
145 void parseValues(std::vector<std::pair<size_t, std::string>>& expressions, double * values, const TokenStream & tokens, size_t expected);
146 void parseValues(std::vector<std::pair<size_t, std::string>>& expressions, int * values, const TokenStream & tokens, size_t expected);
147 void parseValues(std::vector<std::pair<size_t, std::string>>& expressions, uint32_t * values, const TokenStream & tokens, size_t expected);
148 void parseValues(std::vector<std::pair<size_t, std::string>>& expressions, bool* values, const TokenStream & tokens, size_t expected);
149}
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:538
uint64_t COGSCORE_DLL_API parseISO8601(const StringView &iso8601)
Parse ISO 8601 timestamps.
Definition: Parsing.cpp:575
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:559
Stores the parsed output of a key/value pair.
Definition: Parsing.h:32
std::string key
The input key.
Definition: Parsing.h:39
std::string value
The value is available for all non vector types.
Definition: Parsing.h:42