Porytiles
Loading...
Searching...
No Matches
define_statement.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <variant>
6
9
10namespace porytiles {
11
19using DefineValue = std::variant<std::int64_t, std::string, std::monostate>;
20
41 public:
47 DefineStatement(std::string name, std::int64_t value, SourcePosition position)
48 : name_{std::move(name)}, value_{value}, position_{position}
49 {
50 }
51
58 : name_{std::move(name)}, value_{std::move(value)}, position_{position}
59 {
60 }
61
67 : name_{std::move(name)}, value_{std::monostate{}}, position_{position}
68 {
69 }
70
74 [[nodiscard]] const std::string &name() const
75 {
76 return name_;
77 }
78
82 [[nodiscard]] const DefineValue &value() const
83 {
84 return value_;
85 }
86
90 [[nodiscard]] const SourcePosition &position() const
91 {
92 return position_;
93 }
94
98 [[nodiscard]] bool has_int_value() const
99 {
100 return std::holds_alternative<std::int64_t>(value_);
101 }
102
106 [[nodiscard]] bool has_string_value() const
107 {
108 return std::holds_alternative<std::string>(value_);
109 }
110
114 [[nodiscard]] bool is_flag() const
115 {
116 return std::holds_alternative<std::monostate>(value_);
117 }
118
123 [[nodiscard]] std::int64_t int_value() const
124 {
125 assert_or_panic(has_int_value(), "int_value() called on non-integer define");
126 return std::get<std::int64_t>(value_);
127 }
128
133 [[nodiscard]] const std::string &string_value() const
134 {
135 assert_or_panic(has_string_value(), "string_value() called on non-string define");
136 return std::get<std::string>(value_);
137 }
138
139 private:
140 std::string name_;
141 DefineValue value_;
142 SourcePosition position_;
143};
144
145} // namespace porytiles
Represents a parsed #define preprocessor statement.
const std::string & string_value() const
Returns the string value.
DefineStatement(std::string name, SourcePosition position)
Constructs a DefineStatement with no value (flag-like define).
bool is_flag() const
Checks if this define has no value (flag-like).
const std::string & name() const
Returns the macro name.
bool has_int_value() const
Checks if this define has an integer value.
bool has_string_value() const
Checks if this define has a string value.
const SourcePosition & position() const
Returns the source position of the #define.
DefineStatement(std::string name, std::int64_t value, SourcePosition position)
Constructs a DefineStatement with an integer value.
DefineStatement(std::string name, std::string value, SourcePosition position)
Constructs a DefineStatement with a string value.
std::int64_t int_value() const
Returns the integer value.
const DefineValue & value() const
Returns the value variant.
void assert_or_panic(bool condition, const StringViewSourceLoc &s)
Conditionally panics if the given condition is false.
Definition panic.cpp:53
std::variant< std::int64_t, std::string, std::monostate > DefineValue
Represents the value of a #define statement.
Represents a position within source content.