ASCII art diagram converter: Difference between revisions

C++ - added description, and error case
(Added C++ implementation)
(C++ - added description, and error case)
Line 1,014:
</pre>
=={{header|C++}}==
Uses C++20
 
The ASCII diagram is parsed at compile time into a data structure that is used at runtime to encode and decode the fields.
 
<lang cpp>#include <array>
#include <bitset>
Line 1,196 ⟶ 1,200:
ARCOUNT 0110100110100100
</pre>
 
An invalid table will cause a compilation error - here the ANCOUNT field is missing.
<lang cpp> static constexpr char missingFieldArt[] = R"(
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR| Opcode |AA|TC|RD|RA| Z | RCODE |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| QDCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| NSCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ARCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+)";
</lang>
 
GCC 11 gives this error
 
{{out}}
<pre>
../ascii-art-diagram-converter.cpp: In instantiation of 'void Decode(auto:12) [with const char* T = (& missingFieldArt); auto:12 = std::array<unsigned char, 12>]':
../ascii-art-diagram-converter.cpp:181:36: required from here
../ascii-art-diagram-converter.cpp:98:40: error: static assertion failed: Invalid ASCII talble
98 | static_assert(parsedDiagram.second > 0, "Invalid ASCII talble");
| ~~~~~~~~~~~~~~~~~~~~~^~~
../ascii-art-diagram-converter.cpp:98:40: note: '(((int)parsedDiagram.std::pair<std::array<FieldDetails, 13>, int>::second) > 0)' evaluates to false
</pre>
 
=={{header|D}}==
This solution generates anonymous struct code at compile-time, that can be mixed-in inside a struct or class.
125

edits