Use dynamic_cast instead of reinterpret_cast in Target.hpp and ParseComponents.hpp, and extend the ParseComponent class virtually in the ParseTree and NamedIdentifier classes

Signed-off-by: Username404 <w.iron.zombie@gmail.com>
This commit is contained in:
Username404 2022-02-08 18:18:34 +01:00
parent 68dc7ea1a1
commit de2d936e19
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
3 changed files with 4 additions and 8 deletions

View File

@ -65,10 +65,6 @@ namespace Yerbacon {
};
}
#include <memory>
template<typename T, typename U>
constexpr T& pointerAs(const std::unique_ptr<U>& ptr) { return reinterpret_cast<T&>(*ptr); }
#undef YBCON_VERSION
#undef YBCON_FLAGS
#undef YBCON_COMPILER

View File

@ -20,7 +20,7 @@ struct ParseComponent {
virtual ~ParseComponent() = default;
};
struct NamedIdentifier: ParseComponent {
struct NamedIdentifier: virtual ParseComponent {
const string name;
explicit NamedIdentifier(string_view nameText): name(nameText) {}
};
@ -28,7 +28,7 @@ struct NamedIdentifier: ParseComponent {
typedef unique_ptr<ParseComponent> component_ptr;
#define IS_PARSECOMPONENT IS(ParseComponent)
class ParseTree: public ParseComponent {
class ParseTree: public virtual ParseComponent {
mutable vector<component_ptr> subComponents;
using array_type = decltype(subComponents);
using iterator = array_type::iterator;
@ -61,7 +61,7 @@ public:
vector<T*> filteredComponents;
for_each(cbegin(), cend(), [&filteredComponents](const component_ptr& it) {
if (it->getId() == typeid(T)) {
filteredComponents.push_back(reinterpret_cast<T*>(it.get()));
filteredComponents.push_back(dynamic_cast<T*>(it.get()));
}
});
return filteredComponents;

View File

@ -66,7 +66,7 @@ protected:
}
typedef function<void (const ParseTree& parsedTree, unsigned int& index)> task;
#define make_task_base(start, type, captures, function_body) make_pair(type_index(typeid(type)), [captures](const ParseTree& parsedTree, unsigned int& index) { start; function_body })
#define make_task_base_R(T, C, F) make_task_base(const T& parseComponent = pointerAs<T>(parsedTree[index]), T, C, F)
#define make_task_base_R(T, C, F) make_task_base(const T& parseComponent = dynamic_cast<T&>(*parsedTree[index]), T, C, F)
#define make_task(T, F) make_task_base_R(T, this, F)
#define make_task_noR(T, F) make_task_base(,T, this, F)
#define make_nonlocal_task(T, F) make_task_base_R(T, , F)