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
51 std::uint32_t canId;
52 bool isExtended;
53 std::string signalName;
54 std::string selectorName;
55 std::vector<MultiplexRange> ranges;
56 std::size_t lineNumber;
57};
58
63 bool enableJ1939 = false;
64 std::size_t lineNumber = 0U;
65 std::vector<PendingValueDescriptions> pendingValues = {};
67 std::vector<PendingMultiplexing> pendingMultiplexing = {};
68 void ResolveMultiplexing(const std::string &filePath);
69
70 std::vector<std::vector<std::size_t>> signalLines = {};
72 void ValidateMultiplexing(const std::string &filePath) const;
73
74 std::vector<PendingComment> pendingComments = {};
76 void ResolveComments(const std::string &filePath);
77
78 std::vector<PendingAttribute> pendingAttributes = {};
80 void ResolveAttributes(const std::string &filePath, std::vector<DbcDiagnostic> &diagnostics);
81
83 void ResolveValueDescriptions(const std::string &filePath);
84};
85
88public:
89 virtual ~DbcStatementHandler() = default;
91 virtual void Handle(const std::string &line, DbcParseContext &context) const = 0;
92};
93
96public:
97 void Handle(const std::string &line, DbcParseContext &context) const override;
98
99private:
100 static FrameInfo ParseFrameLine(const std::string &line);
101};
102
105public:
106 void Handle(const std::string &line, DbcParseContext &context) const override;
107
108private:
109 static SignalInfo ParseSignalLine(const std::string &line);
110};
111
114public:
115 void Handle(const std::string &line, DbcParseContext &context) const override;
116};
117
120public:
121 void Handle(const std::string &line, DbcParseContext &context) const override;
122};
123
126public:
127 void Handle(const std::string &line, DbcParseContext &context) const override;
128};
129
132public:
133 void Handle(const std::string &line, DbcParseContext &context) const override;
134};
135
138public:
139 void Handle(const std::string &line, DbcParseContext &context) const override;
140};
141
144public:
147
149 template<class Handler>
150 void RegisterType(const std::string &keyword) {
151 m_factory.registerType<Handler>(keyword);
152 m_handlers.insert_or_assign(keyword, m_factory.create(keyword));
153 }
154
156 bool Dispatch(const std::string &line, DbcParseContext &context) const;
157
158private:
159 using HandlerMap = std::unordered_map<std::string, std::shared_ptr<DbcStatementHandler>>;
161 HandlerMap m_handlers;
162};
163
164#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 extended multiplexing conditions for deferred resolution.
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.
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 extended multiplexing condition and source location.
Deferred raw value descriptions and their target.
Describes one signal inside a DBC frame.
Definition signal_info.h:25