83 lines
2.4 KiB
C++
83 lines
2.4 KiB
C++
// SPDX-License-Identifier: MIT
|
|
#ifndef DBC_STATEMENT_HANDLER_H
|
|
#define DBC_STATEMENT_HANDLER_H
|
|
|
|
#include "dbc_database.h"
|
|
#include "typefactory.h"
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
struct PendingValueDescriptions {
|
|
std::uint32_t canId;
|
|
bool isExtended;
|
|
std::string signalName;
|
|
ValueDescriptions descriptions;
|
|
std::size_t lineNumber;
|
|
};
|
|
|
|
/** Mutable state belonging to one file parse. Handlers do not retain it. */
|
|
struct DbcParseContext {
|
|
DbcDatabase &database;
|
|
FrameInfo *currentFrame = nullptr;
|
|
std::size_t lineNumber = 0U;
|
|
std::vector<PendingValueDescriptions> pendingValues = {};
|
|
|
|
void ResolveValueDescriptions(const std::string &filePath);
|
|
};
|
|
|
|
class DbcStatementHandler {
|
|
public:
|
|
virtual ~DbcStatementHandler() = default;
|
|
virtual void Handle(const std::string &line, DbcParseContext &context) const = 0;
|
|
};
|
|
|
|
class DbcFrameHandler : public DbcStatementHandler {
|
|
public:
|
|
void Handle(const std::string &line, DbcParseContext &context) const override;
|
|
|
|
private:
|
|
static FrameInfo ParseFrameLine(const std::string &line);
|
|
};
|
|
|
|
class DbcSignalHandler : public DbcStatementHandler {
|
|
public:
|
|
void Handle(const std::string &line, DbcParseContext &context) const override;
|
|
|
|
private:
|
|
static SignalInfo ParseSignalLine(const std::string &line);
|
|
};
|
|
|
|
class DbcCommentHandler : public DbcStatementHandler {
|
|
public:
|
|
void Handle(const std::string &line, DbcParseContext &context) const override;
|
|
|
|
private:
|
|
static void ParseCommentLine(const std::string &line, DbcDatabase &database);
|
|
};
|
|
|
|
class DbcValueDescriptionHandler : public DbcStatementHandler {
|
|
public:
|
|
void Handle(const std::string &line, DbcParseContext &context) const override;
|
|
};
|
|
|
|
/** Exact-key dispatch to handlers created once per registration via TypeFactory. */
|
|
class DbcStatementDispatcher {
|
|
public:
|
|
DbcStatementDispatcher();
|
|
|
|
template<class Handler>
|
|
void RegisterType(const std::string &keyword) {
|
|
m_factory.registerType<Handler>(keyword);
|
|
m_handlers.insert_or_assign(keyword, m_factory.create(keyword));
|
|
}
|
|
|
|
/** Returns false for an unregistered keyword; input must be trimmed. */
|
|
bool Dispatch(const std::string &line, DbcParseContext &context) const;
|
|
|
|
private:
|
|
TypeFactory<std::string, DbcStatementHandler> m_factory;
|
|
std::unordered_map<std::string, std::shared_ptr<DbcStatementHandler>> m_handlers;
|
|
};
|
|
|
|
#endif /* DBC_STATEMENT_HANDLER_H */
|