Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
SdlCommandParser.h
Go to the documentation of this file.
1#pragma once
2
3#include "SDL/sdl_fwd.h"
9#include "Utility/TSpan.h"
10
11#include <string>
12#include <string_view>
13#include <unordered_map>
14#include <memory>
15
16namespace ph
17{
18
20{
21 Unknown = 0,
23 Load,
24 Removal,
25 Update,
29};
30
32{
33public:
34 explicit SdlCommandParser(TSpanView<const SdlClass*> targetClasses);
35 SdlCommandParser(TSpanView<const SdlClass*> targetClasses, const Path& sceneWorkingDirectory);
37
38 // TODO: rename existing methods to enterAndWait() and flushAndWait() and add flush() and enter() once multithreading is added
39 // TODO: removeResource() callback for load error, etc.
40
53 void parse(std::string_view rawCommandSegment);
54
57 void flush();
58
59 void setPacketInterface(std::unique_ptr<SdlDataPacketInterface> interface);
60 void setSceneWorkingDirectory(const Path& directory);
62 const Path& getSceneWorkingDirectory() const;
64 std::size_t numParsedCommands() const;
65 std::size_t numParseErrors() const;
66
67protected:
71 virtual bool beginCommand(
72 ESdlCommandType commandType,
73 const SdlClass* targetClass,
74 SdlInputContext* out_ctx) = 0;
75
78 virtual void endCommand() = 0;
79
81 std::string_view resourceName,
82 const SdlInputContext& ctx,
83 ESdlCommandType commandType) = 0;
84
85 virtual void initResource(
86 std::string_view resourceName,
87 ISdlResource* resource,
88 const SdlInputContext& ctx,
89 SdlInputClauses& clauses,
90 ESdlCommandType commandType) = 0;
91
93 std::string_view resourceName,
94 const SdlInputContext& ctx) = 0;
95
110 virtual void runExecutor(
111 std::string_view executorName,
112 const SdlInputContext& ctx,
113 ISdlResource* targetResource,
114 SdlInputClauses& clauses,
115 ESdlCommandType commandType) = 0;
116
117 virtual void commandVersionSet(
118 const SemanticVersion& version,
119 const SdlInputContext& ctx) = 0;
120
122 std::string_view packetName,
123 const SdlInputClauses& packet,
124 const SdlInputContext& ctx) = 0;
125
126private:
127 // OPT: use view
128 struct CommandHeader final
129 {
131 std::string targetCategory;
132 std::string targetType;
133 std::string executorName;
134 std::string dataPacketName;
135 std::string reference;
136 std::string dataString;
137
138 bool isRecognized() const;
139 };
140
141 SemanticVersion m_commandVersion;
142
143 std::unordered_map<std::string, const SdlClass*> m_mangledNameToClass;
144 std::unique_ptr<SdlDataPacketInterface> m_packetInterface;
145
146 Path m_sceneWorkingDirectory;
147 bool m_isInSingleLineComment;
148 std::string m_processedCommandCache;
149 std::size_t m_generatedNameCounter;
150 std::size_t m_numParsedCommands;
151 std::size_t m_numParseErrors;
152
153private:
158 void enterProcessed(std::string_view processedCommandSegment);
159
160 void parseCommand(const std::string& command);
161 void parseSingleCommand(const CommandHeader& command);
162
163 void parseLoadCommand(const CommandHeader& command);
164 void parseExecutionCommand(const CommandHeader& command);
165 void parseDirectiveCommand(const CommandHeader& command);
166 void parseNamedDataPacketCommand(const CommandHeader& command);
167
170 void getClauses(
171 std::string_view packetCommand,
172 const SdlInputContext& ctx,
173 std::string_view targetName,
174 ISdlResource* targetInstance,
175 SdlInputClauses* out_clauses);
176
177 const SdlClass* getSdlClass(const std::string& mangledClassName) const;
178 const SdlClass* getSdlClass(std::string_view categoryName, std::string_view typeName) const;
179
180private:
181 static std::string getMangledName(std::string_view categoryName, std::string_view typeName);
182 static void getMangledName(std::string_view categoryName, std::string_view typeName, std::string* out_mangledName);
183 static CommandHeader parseCommandHeader(std::string_view command);
184};
185
186// In-header Implementations:
187
189{
190 return m_sceneWorkingDirectory;
191}
192
194{
195 return m_commandVersion;
196}
197
198inline std::size_t SdlCommandParser::numParsedCommands() const
199{
200 return m_numParsedCommands;
201}
202
203inline std::size_t SdlCommandParser::numParseErrors() const
204{
205 return m_numParseErrors;
206}
207
208inline bool SdlCommandParser::CommandHeader::isRecognized() const
209{
210 return commandType != ESdlCommandType::Unknown;
211}
212
213}// end namespace ph
Interface for all SDL resource.
Definition ISdlResource.h:22
General path representation. Does not check whether the target actually exists (e....
Definition Path.h:21
Definition SdlClass.h:25
Definition SdlCommandParser.h:32
SdlCommandParser(TSpanView< const SdlClass * > targetClasses)
Definition SdlCommandParser.cpp:33
virtual ISdlResource * createResource(std::string_view resourceName, const SdlInputContext &ctx, ESdlCommandType commandType)=0
virtual ~SdlCommandParser()
virtual bool beginCommand(ESdlCommandType commandType, const SdlClass *targetClass, SdlInputContext *out_ctx)=0
Called when the parser starts processing a command.
virtual void storeNamedDataPacket(std::string_view packetName, const SdlInputClauses &packet, const SdlInputContext &ctx)=0
virtual ISdlResource * getResource(std::string_view resourceName, const SdlInputContext &ctx)=0
virtual void runExecutor(std::string_view executorName, const SdlInputContext &ctx, ISdlResource *targetResource, SdlInputClauses &clauses, ESdlCommandType commandType)=0
Called when an executor command has been processed and is ready to run.
virtual void initResource(std::string_view resourceName, ISdlResource *resource, const SdlInputContext &ctx, SdlInputClauses &clauses, ESdlCommandType commandType)=0
SdlDataPacketInterface & getPacketInterface()
Definition SdlCommandParser.cpp:78
void setSceneWorkingDirectory(const Path &directory)
Definition SdlCommandParser.cpp:541
virtual void commandVersionSet(const SemanticVersion &version, const SdlInputContext &ctx)=0
void flush()
Force the parse of commands that were cached in the parser.
Definition SdlCommandParser.cpp:203
std::size_t numParsedCommands() const
Definition SdlCommandParser.h:198
std::size_t numParseErrors() const
Definition SdlCommandParser.h:203
virtual void endCommand()=0
Called when the parser finishes processing a command.
void setPacketInterface(std::unique_ptr< SdlDataPacketInterface > interface)
Definition SdlCommandParser.cpp:536
const Path & getSceneWorkingDirectory() const
Definition SdlCommandParser.h:188
const SemanticVersion & getCommandVersion() const
Definition SdlCommandParser.h:193
void parse(std::string_view rawCommandSegment)
Enters a string and parse it as one or more commands.
Definition SdlCommandParser.cpp:84
Parsing & generating a group of SDL clauses.
Definition SdlDataPacketInterface.h:14
Container for input clauses.
Definition SdlInputClauses.h:18
Data that SDL input process can rely on.
Definition SdlInputContext.h:19
Convenient software version handling routines. See https://semver.org/ for a detailed explaination of...
Definition SemanticVersion.h:16
The root for all renderer implementations.
Definition EEngineProject.h:6
std::span< const T, EXTENT > TSpanView
Same as TSpan, except that the objects are const-qualified. Note that for pointer types,...
Definition TSpan.h:19
ESdlCommandType
Definition SdlCommandParser.h:20