Engine getting complicated - need some JSON for troubleshooting.

This commit is contained in:
jmsgrogan 2022-10-21 08:28:32 +01:00
parent 350c20efa6
commit 30e30c8a7b
3 changed files with 70 additions and 9 deletions

View file

@ -25,6 +25,11 @@ public:
return mChildren.size();
}
virtual std::string getIdentifier() const
{
return {};
}
TemplateNode* getChild(std::size_t index) const
{
return mChildren[index].get();
@ -41,13 +46,23 @@ public:
}
template<typename T>
T* getFirstChildShallow() const
T* getFirstChildShallow(const std::string& identifier = {}) const
{
for (const auto& child : mChildren)
{
if (auto ret = dynamic_cast<T*>(child.get()))
{
return ret;
if (!identifier.empty())
{
if (child->getIdentifier() == identifier)
{
return ret;
}
}
else
{
return ret;
}
}
}
return nullptr;
@ -58,12 +73,17 @@ public:
mExtensionParent = parent;
}
virtual std::string render()
virtual std::string render(TemplateNode* parentContext = nullptr)
{
if (!parentContext && mExtensionParent)
{
parentContext = mExtensionParent;
}
std::string content;
for (size_t idx = 0; idx < mChildren.size(); idx++)
{
content += mChildren[idx]->render();
content += mChildren[idx]->render(parentContext);
}
return content;
}
@ -80,10 +100,9 @@ class TemplateExtends : public TemplateNode
{
public:
TemplateExtends(TemplateNode* parent, const std::string& path)
: TemplateNode(parent),
mPath(path)
: TemplateNode(parent)
{
mPath = StringUtils::stripQuotes(path);
};
std::string getRawContent() const override
@ -122,6 +141,28 @@ public:
return content;
}
std::string renderAsParent(TemplateNode* base)
{
return {};
}
std::string render(TemplateNode* parentContext) override
{
std::string content;
if (parentContext)
{
if (auto parent_node = parentContext->getFirstChildShallow<TemplateBlock>(getIdentifier()))
{
content = dynamic_cast<TemplateBlock*>(parent_node)->renderAsParent(this);
}
}
else
{
}
return content;
}
private:
std::string mName;
std::vector<std::string> mBody;
@ -165,12 +206,12 @@ public:
return !mContent.empty();
}
std::string render() override
std::string render(TemplateNode* parentContext) override
{
std::string content;
for (const auto& line : mContent)
{
content += line;
content += line + '\n';
}
return content;
}

View file

@ -121,3 +121,22 @@ std::string StringUtils::ToPaddedString(unsigned numBytes, unsigned entry)
sstr << std::setfill('0') << std::setw(numBytes) << entry;
return sstr.str();
}
std::string StringUtils::stripQuotes(const std::string& input)
{
if (input.size() < 3)
{
return input;
}
std::size_t start_index = 0;
std::size_t end_index = input.size()-1;
if (input[start_index] == '"')
{
start_index = 1;
}
if (input[end_index] == '"')
{
end_index = end_index - 1;
}
return input.substr(start_index, end_index - start_index + 1);
}

View file

@ -23,4 +23,5 @@ public:
static std::string ToPaddedString(unsigned numBytes, unsigned entry);
static std::vector<std::string> split(const std::string& input);
static std::string strip(const std::string& input);
static std::string stripQuotes(const std::string& input);
};