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
21using DefineValue = std::variant<std::int64_t, std::string, std::monostate>;
22
44 public:
52 DefineStatement(std::string name, std::int64_t value, SourcePosition position)
53 : name_{std::move(name)}, value_{value}, position_{position}
54 {
55 }
56
65 : name_{std::move(name)}, value_{std::move(value)}, position_{position}
66 {
67 }
68
76 : name_{std::move(name)}, value_{std::monostate{}}, position_{position}
77 {
78 }
79
85 [[nodiscard]] const std::string &name() const
86 {
87 return name_;
88 }
89
95 [[nodiscard]] const DefineValue &value() const
96 {
97 return value_;
98 }
99
105 [[nodiscard]] const SourcePosition &position() const
106 {
107 return position_;
108 }
109
115 [[nodiscard]] bool has_int_value() const
116 {
117 return std::holds_alternative<std::int64_t>(value_);
118 }
119
125 [[nodiscard]] bool has_string_value() const
126 {
127 return std::holds_alternative<std::string>(value_);
128 }
129
135 [[nodiscard]] bool is_flag() const
136 {
137 return std::holds_alternative<std::monostate>(value_);
138 }
139
146 [[nodiscard]] std::int64_t int_value() const
147 {
148 assert_or_panic(has_int_value(), "int_value() called on non-integer define");
149 return std::get<std::int64_t>(value_);
150 }
151
158 [[nodiscard]] const std::string &string_value() const
159 {
160 assert_or_panic(has_string_value(), "string_value() called on non-string define");
161 return std::get<std::string>(value_);
162 }
163
164 private:
165 std::string name_;
166 DefineValue value_;
167 SourcePosition position_;
168};
169
170} // 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.