DBC Framework
DBC parsing and CAN signal decoding framework
Loading...
Searching...
No Matches
dbc_statement_handler.h
1// SPDX-License-Identifier: MIT
2#ifndef DBC_STATEMENT_HANDLER_H
3#define DBC_STATEMENT_HANDLER_H
4
5#include "dbc_database.h"
6#include "dbc_diagnostic.h"
7#include "typefactory.h"
8#include <stdexcept>
9#include <string>
10#include <unordered_map>
11
14 std::uint32_t canId;
16 std::string signalName;
17 ValueDescriptions descriptions;
18 std::size_t lineNumber;
19};
20
23 std::uint32_t canId;
25 bool isSignal;
26 std::string signalName;
27 std::string comment;
28 std::size_t lineNumber;
29};
30
32class DbcDiagnosticError : public std::runtime_error {
33public:
35 DbcDiagnosticError(DbcDiagnosticCode code, const std::string &filePath,
36 std::size_t lineNumber, const std::string &message)
37 : std::runtime_error(filePath + ":" + std::to_string(lineNumber) + ": " + message),
38 diagnostic{DbcDiagnosticSeverity::Error, code, filePath, lineNumber, message} {}
39
41};
42
45 std::string statement;
46 std::size_t lineNumber;
47};
48
53 bool enableJ1939 = false;
54 std::size_t lineNumber = 0U;
55 std::vector<PendingValueDescriptions> pendingValues = {};
57 std::vector<std::vector<std::size_t>> signalLines = {};
59 void ValidateMultiplexing(const std::string &filePath) const;
60
61 std::vector<PendingComment> pendingComments = {};
63 void ResolveComments(const std::string &filePath);
64
65 std::vector<PendingAttribute> pendingAttributes = {};
67 void ResolveAttributes(const std::string &filePath, std::vector<DbcDiagnostic> &diagnostics);
68
70 void ResolveValueDescriptions(const std::string &filePath);
71};
72
75public:
76 virtual ~DbcStatementHandler() = default;
78 virtual void Handle(const std::string &line, DbcParseContext &context) const = 0;
79};
80
83public:
84 void Handle(const std::string &line, DbcParseContext &context) const override;
85
86private:
87 static FrameInfo ParseFrameLine(const std::string &line);
88};
89
92public:
93 void Handle(const std::string &line, DbcParseContext &context) const override;
94
95private:
96 static SignalInfo ParseSignalLine(const std::string &line);
97};
98
101public:
102 void Handle(const std::string &line, DbcParseContext &context) const override;
103};
104
107public:
108 void Handle(const std::string &line, DbcParseContext &context) const override;
109};
110
113public:
114 void Handle(const std::string &line, DbcParseContext &context) const override;
115};
116
119public:
120 void Handle(const std::string &line, DbcParseContext &context) const override;
121};
122
125public:
126 void Handle(const std::string &line, DbcParseContext &context) const override;
127};
128
131public:
134
136 template<class Handler>
137 void RegisterType(const std::string &keyword) {
138 m_factory.registerType<Handler>(keyword);
139 m_handlers.insert_or_assign(keyword, m_factory.create(keyword));
140 }
141
143 bool Dispatch(const std::string &line, DbcParseContext &context) const;
144
145private:
146 using HandlerMap = std::unordered_map<std::string, std::shared_ptr<DbcStatementHandler>>;
148 HandlerMap m_handlers;
149};
150
151#endif /* DBC_STATEMENT_HANDLER_H */
Defer typed attribute statements for resolution.
void Handle(const std::string &line, DbcParseContext &context) const override
Process a trimmed statement using state belonging to this parse.
Defer frame and signal comments for target resolution.
void Handle(const std::string &line, DbcParseContext &context) const override
Process a trimmed statement using state belonging to this parse.
DbcDiagnosticError(DbcDiagnosticCode code, const std::string &filePath, std::size_t lineNumber, const std::string &message)
Create an exception with a structured source diagnostic.
Parse and append frame definitions.
void Handle(const std::string &line, DbcParseContext &context) const override
Process a trimmed statement using state belonging to this parse.
void Handle(const std::string &line, DbcParseContext &context) const override
Process a trimmed statement using state belonging to this parse.
Parse and append signals to the current frame.
void Handle(const std::string &line, DbcParseContext &context) const override
Process a trimmed statement using state belonging to this parse.
void RegisterType(const std::string &keyword)
Register or replace a handler and create its reusable instance.
DbcStatementDispatcher()
Register supported statement handlers through TypeFactory.
bool Dispatch(const std::string &line, DbcParseContext &context) const
Interface for processing a registered DBC statement.
virtual void Handle(const std::string &line, DbcParseContext &context) const =0
Process a trimmed statement using state belonging to this parse.
Reject unsupported extended multiplexing statements.
void Handle(const std::string &line, DbcParseContext &context) const override
Process a trimmed statement using state belonging to this parse.
Defer raw value descriptions for target resolution.
void Handle(const std::string &line, DbcParseContext &context) const override
Process a trimmed statement using state belonging to this parse.
Registry-based factory for creating objects by runtime identifier.
Definition typefactory.h:59
BasePtr create(const ClassId &id, Args... args) const
Creates an instance for the given id using provided arguments.
Definition typefactory.h:99
void registerType(const ClassId &id)
Registers a Derived type under the given id.
Definition typefactory.h:78
Parsed DBC database and attribute metadata.
Parsed DBC content stored in a simple internal form.
Diagnostic message and physical source location.
void ResolveAttributes(const std::string &filePath, std::vector< DbcDiagnostic > &diagnostics)
Resolve definitions, defaults, then assignments; append format warnings.
std::vector< PendingComment > pendingComments
void ValidateMultiplexing(const std::string &filePath) const
Validate selectors, branch ranges, and signal overlap.
void ResolveValueDescriptions(const std::string &filePath)
Resolve deferred raw descriptions against unique targets.
std::vector< PendingValueDescriptions > pendingValues
void ResolveComments(const std::string &filePath)
Resolve deferred comments after all definitions are known.
std::vector< std::vector< std::size_t > > signalLines
std::vector< PendingAttribute > pendingAttributes
Describes one CAN frame from a DBC file.
Definition frame_info.h:25
Attribute statement retained for ordered resolution.
Deferred frame or signal comment and its source location.
Deferred raw value descriptions and their target.
Describes one signal inside a DBC frame.
Definition signal_info.h:25