closes #4 Refactor DBC parsing into TypeFactory statement handlers
This commit is contained in:
1 parent
3bd9339baf
commit
88b5883dda
9 files changed
+511
-349
No files matched your search
@@ -151,8 +151,11 @@ Files:
|
||||
|
||||
- `dbc_parser.h`
|
||||
- `dbc_parser.cpp`
|
||||
- `dbc_statement_handler.h`
|
||||
- `dbc_statement_handler.cpp`
|
||||
- `typefactory.h`
|
||||
|
||||
These parse the DBC file itself.
|
||||
The parser reads files and reports errors; registered statement handlers parse DBC constructs.
|
||||
|
||||
### Demo
|
||||
|
||||
@@ -767,20 +770,47 @@ That is why `DecodeDatabase` is a separate layer.
|
||||
|
||||
---
|
||||
|
||||
## Why No Abstract Factory Is Used
|
||||
## Statement Handler Factory
|
||||
|
||||
At the current stage, abstract factory is intentionally avoided.
|
||||
`DbcParser` owns file reading, trimming, line numbers, and error reporting.
|
||||
Each `ParseFile()` call creates a fresh `DbcParseContext` and a
|
||||
`DbcStatementDispatcher`. The context contains the parsed database and current
|
||||
frame; handlers receive it by reference and retain no parsing state.
|
||||
|
||||
The current design is already clean:
|
||||
The dispatcher uses `TypeFactory<std::string, DbcStatementHandler>` to create
|
||||
registered handlers once per parsing session, then reuses them for all matching
|
||||
lines:
|
||||
|
||||
```text
|
||||
parser → parsed database → decode database
|
||||
↘ tree builder → UI tree
|
||||
- `BO_` → `DbcFrameHandler`
|
||||
- `SG_` → `DbcSignalHandler`
|
||||
- `CM_` → `DbcCommentHandler`
|
||||
|
||||
Grammar implementations live in `dbc_statement_handler.cpp`; their interface,
|
||||
context, and dispatcher live in `dbc_statement_handler.h`. `typefactory.h`
|
||||
provides the generic registry and object creation.
|
||||
|
||||
To add a construct, implement a default-constructible handler deriving from
|
||||
`DbcStatementHandler` with:
|
||||
|
||||
```cpp
|
||||
void Handle(const std::string &line, DbcParseContext &context) const override;
|
||||
```
|
||||
|
||||
Introducing factory layers now would increase complexity without solving an immediate problem.
|
||||
Register it in the dispatcher constructor:
|
||||
|
||||
If later the project requires multiple output representations or multiple build strategies, that can be added then.
|
||||
```cpp
|
||||
RegisterType<DbcValueHandler>("VAL_"); // after implementing DbcValueHandler
|
||||
```
|
||||
|
||||
Registration creates one handler; replacing a registration replaces that instance.
|
||||
Register during initialization before dispatching. A line is routed using its
|
||||
full first whitespace-delimited keyword, so `BO_TX_BU_` cannot match `BO_`.
|
||||
Unregistered keywords remain ignored. Existing `SG_` and `CM_` syntax policies
|
||||
are preserved, including requiring a literal space after those keywords.
|
||||
|
||||
This factory handles statements inside DBC files. A separate format-level
|
||||
factory can be introduced when another file format is implemented. The decoder
|
||||
and database builders remain independent of statement dispatch.
|
||||
|
||||
---
|
||||
|
||||
|
||||
Reference in new issue
Block a user