Skip to content

Commit bd3ac66

Browse files
-replace getInterface with dynamic_cast based as method
1 parent 4e80f8d commit bd3ac66

20 files changed

+62
-181
lines changed

src/attribute_node.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "errors.h"
33
#include "streamreader.h"
44
#include <gsl/assert>
5+
#include <gsl/narrow>
56

67
namespace htcpp {
78

@@ -23,7 +24,8 @@ std::string_view AttributeNode::value() const
2324

2425
void AttributeNode::load(StreamReader& stream)
2526
{
26-
Expects(stream.read(name_.size()) == name_);
27+
Expects(stream.read(gsl::narrow_cast<int>(std::ssize(name_))) == name_);
28+
2729
stream.skipWhitespace();
2830
if (stream.peek() != "=")
2931
throw TemplateError{stream.position(), "Attribute '" + name_ + "'s value is missing"};

src/attribute_node.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#pragma once
22
#include "iattribute.h"
33
#include "idocumentnode.h"
4+
#include <string>
5+
#include <string_view>
46

57
namespace htcpp {
68

79
class StreamReader;
810

911
class AttributeNode : public IDocumentNode,
1012
public IAttribute {
11-
DOCUMENT_NODE_INTERFACE_ACCESS(IAttribute)
12-
1313
public:
1414
explicit AttributeNode(std::string name, StreamReader& stream);
1515
std::string_view name() const override;

src/codenode.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "codenode.h"
2-
#include "streamreader.h"
32
#include "errors.h"
3+
#include "streamreader.h"
44
#include "utils.h"
5+
#include <gsl/assert>
56
#include <utility>
67

78
namespace htcpp{

src/codenode.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#pragma once
2-
#include "document_node_interface_access.h"
32
#include "idocumentnode.h"
43
#include "idocumentnoderenderer.h"
54
#include "nodeextension.h"
@@ -27,7 +26,6 @@ class CodeNode {
2726

2827
class ExpressionNode : public IDocumentNode,
2928
public IDocumentNodeRenderer {
30-
DOCUMENT_NODE_INTERFACE_ACCESS(IDocumentNodeRenderer)
3129
public:
3230
explicit ExpressionNode(StreamReader& stream);
3331
std::string renderingCode() const override;
@@ -38,8 +36,6 @@ class ExpressionNode : public IDocumentNode,
3836

3937
class StatementNode : public IDocumentNode,
4038
public IDocumentNodeRenderer {
41-
DOCUMENT_NODE_INTERFACE_ACCESS(IDocumentNodeRenderer)
42-
4339
public:
4440
explicit StatementNode(StreamReader& stream);
4541
std::string renderingCode() const override;
@@ -50,7 +46,6 @@ class StatementNode : public IDocumentNode,
5046

5147
class GlobalStatementNode : public IDocumentNode,
5248
public IDocumentNodeRenderer {
53-
DOCUMENT_NODE_INTERFACE_ACCESS(IDocumentNodeRenderer)
5449
public:
5550
explicit GlobalStatementNode(StreamReader& stream);
5651
std::string renderingCode() const override;

src/control_flow_statement_node.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#pragma once
2-
#include "document_node_interface_access.h"
32
#include "idocumentnode.h"
43
#include "idocumentnoderenderer.h"
54
#include "nodeextension.h"
@@ -15,8 +14,6 @@ enum class ControlFlowStatementNodeType {
1514

1615
class ControlFlowStatementNode : public IDocumentNode,
1716
public IDocumentNodeRenderer {
18-
DOCUMENT_NODE_INTERFACE_ACCESS(IDocumentNodeRenderer)
19-
2017
public:
2118
ControlFlowStatementNode(ControlFlowStatementNodeType, NodeExtension nodeExtension);
2219
std::string renderingCode() const override;

src/document_node_interface_access.h

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/iconvertible_to_procedure.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#pragma once
22
#include <sfun/interface.h>
3+
#include <optional>
34
#include <string_view>
45

5-
namespace htcpp{
6+
namespace htcpp {
67

78
class IConvertibleToProcedure : private sfun::interface<IConvertibleToProcedure> {
89
public:
9-
virtual std::string_view procedureName() const = 0;
10+
virtual std::optional<std::string_view> procedureName() const = 0;
1011
};
1112

12-
}
13+
} //namespace htcpp

src/idocumentnode.h

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
#pragma once
2-
#include "document_node_interface_access.h"
32
#include <sfun/interface.h>
43
#include <sfun/optional_ref.h>
5-
#include <gsl/assert>
6-
#include <string>
7-
#include <typeinfo>
8-
#include <memory>
9-
#include <vector>
104

115
namespace htcpp {
126
class INodeCollection;
@@ -22,39 +16,24 @@ class IDocumentNode : private sfun::interface<IDocumentNode>
2216
{
2317
public:
2418
template<typename TInterface>
25-
auto getInterface() const
19+
sfun::optional_ref<const TInterface> as() const
2620
{
27-
return (this->*InterfaceGetterMapping<TInterface>::getterPtr())();
21+
auto obj = dynamic_cast<const TInterface*>(this);
22+
return obj ? sfun::optional_ref<const TInterface>{*obj} : sfun::optional_ref<const TInterface>{};
2823
}
2924

3025
template<typename TInterface>
31-
auto getInterface()
26+
sfun::optional_ref<TInterface> as()
3227
{
33-
return (this->*InterfaceGetterMapping<TInterface>::getterPtr())();
28+
auto obj = dynamic_cast<TInterface*>(this);
29+
return obj ? sfun::optional_ref<TInterface>{*obj} : sfun::optional_ref<TInterface>{};
3430
}
3531

3632
template<typename T>
37-
bool is()
33+
bool is() const
3834
{
39-
return typeid(*this) == typeid(T);
35+
return as<T>().has_value();
4036
}
41-
42-
private:
43-
DOCUMENT_NODE_ADD_INTERFACE_GETTER(INodeCollection);
44-
DOCUMENT_NODE_ADD_INTERFACE_GETTER(IDocumentNodeRenderer);
45-
DOCUMENT_NODE_ADD_INTERFACE_GETTER(IRenderedAsStringPart);
46-
DOCUMENT_NODE_ADD_INTERFACE_GETTER(IConvertibleToProcedure);
47-
DOCUMENT_NODE_ADD_INTERFACE_GETTER(IAttribute);
48-
49-
template<typename TInterface>
50-
friend struct InterfaceGetterMapping;
5137
};
5238

53-
DOCUMENT_NODE_REGISTER_INTERFACE(INodeCollection);
54-
DOCUMENT_NODE_REGISTER_INTERFACE(IDocumentNodeRenderer);
55-
DOCUMENT_NODE_REGISTER_INTERFACE(IRenderedAsStringPart);
56-
DOCUMENT_NODE_REGISTER_INTERFACE(IConvertibleToProcedure);
57-
DOCUMENT_NODE_REGISTER_INTERFACE(IAttribute);
58-
59-
6039
} // namespace htcpp

src/node_utils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ std::vector<std::unique_ptr<IDocumentNode>> flattenNodes(std::vector<std::unique
88
{
99
auto flattenNodes = std::vector<std::unique_ptr<IDocumentNode>>{};
1010
for (auto& node : nodes) {
11-
if (const auto nodeCollection = node->template getInterface<INodeCollection>())
11+
if (const auto nodeCollection = node->template as<INodeCollection>())
1212
std::ranges::move(nodeCollection->flatten(), std::back_inserter(flattenNodes));
1313
else
1414
flattenNodes.emplace_back(std::move(node));
@@ -20,9 +20,9 @@ std::vector<std::unique_ptr<IDocumentNode>> optimizeNodes(std::vector<std::uniqu
2020
{
2121
auto processedNodes = std::vector<std::unique_ptr<IDocumentNode>>{};
2222
for (auto& node : nodes) {
23-
if (auto text = node->getInterface<IRenderedAsStringPart>()) {
24-
if (!processedNodes.empty() && processedNodes.back()->getInterface<IRenderedAsStringPart>()) {
25-
auto prevText = processedNodes.back()->getInterface<IRenderedAsStringPart>()->content();
23+
if (auto text = node->as<IRenderedAsStringPart>()) {
24+
if (!processedNodes.empty() && processedNodes.back()->as<IRenderedAsStringPart>()) {
25+
auto prevText = processedNodes.back()->as<IRenderedAsStringPart>()->content();
2626
processedNodes.back() = std::make_unique<TextNode>(prevText + text->content());
2727
}
2828
else

src/procedurenode.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#include "procedurenode.h"
22
#include "errors.h"
33
#include "idocumentnoderenderer.h"
4+
#include "node_utils.h"
45
#include "nodereader.h"
56
#include "streamreader.h"
67
#include "utils.h"
7-
#include "node_utils.h"
8+
#include <gsl/assert>
89

910
namespace htcpp{
1011

@@ -58,7 +59,7 @@ std::string ProcedureNode::renderingCode() const
5859
{
5960
auto result = std::string{};
6061
for (auto& node : contentNodes_)
61-
result += node->getInterface<IDocumentNodeRenderer>()->renderingCode();
62+
result += node->as<IDocumentNodeRenderer>()->renderingCode();
6263
return result;
6364
}
6465

0 commit comments

Comments
 (0)