Use std::variant and automatic allocation for file stack node data

This commit is contained in:
Rangi42
2024-02-28 13:49:12 -05:00
committed by Sylvie
parent e1ac51d7da
commit 0cc49782ab
4 changed files with 54 additions and 38 deletions

View File

@@ -7,6 +7,7 @@
#include <stdint.h>
#include <stdio.h>
#include <string>
#include <variant>
#include <vector>
#include "helpers.hpp"
@@ -35,10 +36,18 @@ struct FileStackNode {
uint32_t lineNo;
enum FileStackNodeType type;
union {
std::string *name; // NODE_FILE, NODE_MACRO
std::vector<uint32_t> *iters; // NODE_REPT
};
std::variant<
std::monostate, // Default constructed; `.type` and `.data` must be set manually
std::vector<uint32_t>, // NODE_REPT
std::string // NODE_FILE, NODE_MACRO
> data;
// REPT iteration counts since last named node, in reverse depth order
std::vector<uint32_t> &iters();
std::vector<uint32_t> const &iters() const;
// File name for files, file::macro name for macros
std::string &name();
std::string const &name() const;
};
// Helper macro for printing verbose-mode messages