eliminate using namespace

This commit is contained in:
Martin Michelsen
2026-05-25 16:38:31 -07:00
parent 4503d09c77
commit e9c2ac34a3
98 changed files with 6022 additions and 6531 deletions
+57 -63
View File
@@ -19,22 +19,17 @@
#include "SaveFileFormats.hh"
#include "Text.hh"
using namespace std;
IntegralExpression::IntegralExpression(const string& text)
: root(this->parse_expr(text)) {}
IntegralExpression::IntegralExpression(const std::string& text) : root(this->parse_expr(text)) {}
IntegralExpression::BinaryOperatorNode::BinaryOperatorNode(
Type type, unique_ptr<const Node>&& left, unique_ptr<const Node>&& right)
: type(type),
left(std::move(left)),
right(std::move(right)) {}
Type type, std::unique_ptr<const Node>&& left, std::unique_ptr<const Node>&& right)
: type(type), left(std::move(left)), right(std::move(right)) {}
bool IntegralExpression::BinaryOperatorNode::operator==(const Node& other) const {
try {
const BinaryOperatorNode& other_bin = dynamic_cast<const BinaryOperatorNode&>(other);
return other_bin.type == this->type && *other_bin.left == *this->left && *other_bin.right == *this->right;
} catch (const bad_cast&) {
} catch (const std::bad_cast&) {
return false;
}
}
@@ -78,11 +73,11 @@ int64_t IntegralExpression::BinaryOperatorNode::evaluate(const Env& env) const {
case Type::MODULUS:
return this->left->evaluate(env) % this->right->evaluate(env);
default:
throw logic_error("invalid binary operator type");
throw std::logic_error("invalid binary operator type");
}
}
string IntegralExpression::BinaryOperatorNode::str() const {
std::string IntegralExpression::BinaryOperatorNode::str() const {
switch (this->type) {
case Type::LOGICAL_OR:
return "(" + this->left->str() + ") || (" + this->right->str() + ")";
@@ -121,19 +116,18 @@ string IntegralExpression::BinaryOperatorNode::str() const {
case Type::MODULUS:
return "(" + this->left->str() + ") % (" + this->right->str() + ")";
default:
throw logic_error("invalid binary operator type");
throw std::logic_error("invalid binary operator type");
}
}
IntegralExpression::UnaryOperatorNode::UnaryOperatorNode(Type type, unique_ptr<const Node>&& sub)
: type(type),
sub(std::move(sub)) {}
IntegralExpression::UnaryOperatorNode::UnaryOperatorNode(Type type, std::unique_ptr<const Node>&& sub)
: type(type), sub(std::move(sub)) {}
bool IntegralExpression::UnaryOperatorNode::operator==(const Node& other) const {
try {
const UnaryOperatorNode& other_un = dynamic_cast<const UnaryOperatorNode&>(other);
return other_un.type == this->type && *other_un.sub == *this->sub;
} catch (const bad_cast&) {
} catch (const std::bad_cast&) {
return false;
}
}
@@ -147,11 +141,11 @@ int64_t IntegralExpression::UnaryOperatorNode::evaluate(const Env& env) const {
case Type::NEGATIVE:
return -this->sub->evaluate(env);
default:
throw logic_error("invalid unary operator type");
throw std::logic_error("invalid unary operator type");
}
}
string IntegralExpression::UnaryOperatorNode::str() const {
std::string IntegralExpression::UnaryOperatorNode::str() const {
switch (this->type) {
case Type::LOGICAL_NOT:
return "!(" + this->sub->str() + ")";
@@ -160,7 +154,7 @@ string IntegralExpression::UnaryOperatorNode::str() const {
case Type::NEGATIVE:
return "-(" + this->sub->str() + ")";
default:
throw logic_error("invalid unary operator type");
throw std::logic_error("invalid unary operator type");
}
}
@@ -170,19 +164,19 @@ bool IntegralExpression::FlagLookupNode::operator==(const Node& other) const {
try {
const FlagLookupNode& other_flag = dynamic_cast<const FlagLookupNode&>(other);
return other_flag.flag_index == this->flag_index;
} catch (const bad_cast&) {
} catch (const std::bad_cast&) {
return false;
}
}
int64_t IntegralExpression::FlagLookupNode::evaluate(const Env& env) const {
if (!env.flags) {
throw runtime_error("quest flags not available");
throw std::runtime_error("quest flags not available");
}
return env.flags->get(this->flag_index) ? 1 : 0;
}
string IntegralExpression::FlagLookupNode::str() const {
std::string IntegralExpression::FlagLookupNode::str() const {
return std::format("F_{:04X}", this->flag_index);
}
@@ -193,14 +187,14 @@ bool IntegralExpression::ChallengeCompletionLookupNode::operator==(const Node& o
try {
const ChallengeCompletionLookupNode& other_cc = dynamic_cast<const ChallengeCompletionLookupNode&>(other);
return other_cc.episode == this->episode && other_cc.stage_index == this->stage_index;
} catch (const bad_cast&) {
} catch (const std::bad_cast&) {
return false;
}
}
int64_t IntegralExpression::ChallengeCompletionLookupNode::evaluate(const Env& env) const {
if (!env.challenge_records) {
throw runtime_error("challenge records not available");
throw std::runtime_error("challenge records not available");
}
if (this->episode == Episode::EP1) {
return env.challenge_records->times_ep1_online.at(this->stage_index).has_value();
@@ -210,17 +204,17 @@ int64_t IntegralExpression::ChallengeCompletionLookupNode::evaluate(const Env& e
return false;
}
string IntegralExpression::ChallengeCompletionLookupNode::str() const {
std::string IntegralExpression::ChallengeCompletionLookupNode::str() const {
return std::format("CC_{}_{}", abbreviation_for_episode(this->episode), static_cast<uint8_t>(this->stage_index + 1));
}
IntegralExpression::TeamRewardLookupNode::TeamRewardLookupNode(const string& reward_name) : reward_name(reward_name) {}
IntegralExpression::TeamRewardLookupNode::TeamRewardLookupNode(const std::string& reward_name) : reward_name(reward_name) {}
bool IntegralExpression::TeamRewardLookupNode::operator==(const Node& other) const {
try {
const TeamRewardLookupNode& other_team_reward = dynamic_cast<const TeamRewardLookupNode&>(other);
return other_team_reward.reward_name == this->reward_name;
} catch (const bad_cast&) {
} catch (const std::bad_cast&) {
return false;
}
}
@@ -229,7 +223,7 @@ int64_t IntegralExpression::TeamRewardLookupNode::evaluate(const Env& env) const
return (env.team && env.team->has_reward(this->reward_name)) ? 1 : 0;
}
string IntegralExpression::TeamRewardLookupNode::str() const {
std::string IntegralExpression::TeamRewardLookupNode::str() const {
return "T_" + this->reward_name;
}
@@ -243,7 +237,7 @@ int64_t IntegralExpression::NumPlayersLookupNode::evaluate(const Env& env) const
return env.num_players;
}
string IntegralExpression::NumPlayersLookupNode::str() const {
std::string IntegralExpression::NumPlayersLookupNode::str() const {
return "V_NumPlayers";
}
@@ -257,7 +251,7 @@ int64_t IntegralExpression::EventLookupNode::evaluate(const Env& env) const {
return env.event;
}
string IntegralExpression::EventLookupNode::str() const {
std::string IntegralExpression::EventLookupNode::str() const {
return "V_Event";
}
@@ -271,7 +265,7 @@ int64_t IntegralExpression::V1PresenceLookupNode::evaluate(const Env& env) const
return env.v1_present ? 1 : 0;
}
string IntegralExpression::V1PresenceLookupNode::str() const {
std::string IntegralExpression::V1PresenceLookupNode::str() const {
return "V_V1Present";
}
@@ -282,7 +276,7 @@ bool IntegralExpression::ConstantNode::operator==(const Node& other) const {
try {
const ConstantNode& other_const = dynamic_cast<const ConstantNode&>(other);
return other_const.value == this->value;
} catch (const bad_cast&) {
} catch (const std::bad_cast&) {
return false;
}
}
@@ -291,11 +285,11 @@ int64_t IntegralExpression::ConstantNode::evaluate(const Env&) const {
return this->value;
}
string IntegralExpression::ConstantNode::str() const {
std::string IntegralExpression::ConstantNode::str() const {
return std::format("{}", this->value);
}
unique_ptr<const IntegralExpression::Node> IntegralExpression::parse_expr(string_view text) {
std::unique_ptr<const IntegralExpression::Node> IntegralExpression::parse_expr(std::string_view text) {
// Strip off spaces and fully-enclosing parentheses
for (;;) {
size_t starting_size = text.size();
@@ -329,22 +323,22 @@ unique_ptr<const IntegralExpression::Node> IntegralExpression::parse_expr(string
}
}
if (text.empty()) {
throw runtime_error("invalid expression");
throw std::runtime_error("invalid expression");
}
// Check for binary operators at the root level
using BinType = BinaryOperatorNode::Type;
static const vector<vector<pair<std::string, BinaryOperatorNode::Type>>> binary_operator_levels = {
{{make_pair("||", BinType::LOGICAL_OR)}},
{{make_pair("&&", BinType::LOGICAL_AND)}},
{{make_pair("|", BinType::BITWISE_OR)}},
{{make_pair("^", BinType::BITWISE_XOR)}},
{{make_pair("&", BinType::BITWISE_AND)}},
{{make_pair("==", BinType::EQUAL)}, {make_pair("!=", BinType::NOT_EQUAL)}},
{{make_pair("<=", BinType::LESS_OR_EQUAL)}, {make_pair(">=", BinType::GREATER_OR_EQUAL)}, {make_pair("<", BinType::LESS_THAN)}, {make_pair(">", BinType::GREATER_THAN)}},
{{make_pair("<<", BinType::LEFT_SHIFT)}, {make_pair(">>", BinType::RIGHT_SHIFT)}},
{{make_pair("+", BinType::ADD)}, {make_pair("-", BinType::SUBTRACT)}},
{{make_pair("*", BinType::MULTIPLY)}, {make_pair("/", BinType::DIVIDE)}, {make_pair("%", BinType::MODULUS)}},
static const std::vector<std::vector<std::pair<std::string, BinaryOperatorNode::Type>>> binary_operator_levels = {
{{std::make_pair("||", BinType::LOGICAL_OR)}},
{{std::make_pair("&&", BinType::LOGICAL_AND)}},
{{std::make_pair("|", BinType::BITWISE_OR)}},
{{std::make_pair("^", BinType::BITWISE_XOR)}},
{{std::make_pair("&", BinType::BITWISE_AND)}},
{{std::make_pair("==", BinType::EQUAL)}, {std::make_pair("!=", BinType::NOT_EQUAL)}},
{{std::make_pair("<=", BinType::LESS_OR_EQUAL)}, {std::make_pair(">=", BinType::GREATER_OR_EQUAL)}, {std::make_pair("<", BinType::LESS_THAN)}, {std::make_pair(">", BinType::GREATER_THAN)}},
{{std::make_pair("<<", BinType::LEFT_SHIFT)}, {std::make_pair(">>", BinType::RIGHT_SHIFT)}},
{{std::make_pair("+", BinType::ADD)}, {std::make_pair("-", BinType::SUBTRACT)}},
{{std::make_pair("*", BinType::MULTIPLY)}, {std::make_pair("/", BinType::DIVIDE)}, {std::make_pair("%", BinType::MODULUS)}},
};
for (const auto& operators : binary_operator_levels) {
size_t paren_level = 0;
@@ -390,12 +384,12 @@ unique_ptr<const IntegralExpression::Node> IntegralExpression::parse_expr(string
char* endptr = nullptr;
uint64_t flag = strtoul(text.data() + 2, &endptr, 16);
if (endptr != text.data() + text.size()) {
throw runtime_error("invalid flag lookup token");
throw std::runtime_error("invalid flag lookup token");
}
if (flag >= 0x400) {
throw runtime_error("invalid flag index");
throw std::runtime_error("invalid flag index");
}
return make_unique<FlagLookupNode>(flag);
return std::make_unique<FlagLookupNode>(flag);
}
if (text.starts_with("CC_")) {
Episode episode;
@@ -404,45 +398,45 @@ unique_ptr<const IntegralExpression::Node> IntegralExpression::parse_expr(string
} else if (text.starts_with("CC_Ep2_")) {
episode = Episode::EP2;
} else {
throw runtime_error("invalid challenge episode");
throw std::runtime_error("invalid challenge episode");
}
char* endptr = nullptr;
uint64_t stage_index = strtoul(text.data() + 7, &endptr, 0) - 1;
if (endptr != text.data() + text.size()) {
throw runtime_error("invalid challenge completion lookup token");
throw std::runtime_error("invalid challenge completion lookup token");
}
if ((episode == Episode::EP1 && stage_index > 8) || (episode == Episode::EP2 && stage_index > 4)) {
throw runtime_error("invalid challenge stage index");
throw std::runtime_error("invalid challenge stage index");
}
return make_unique<ChallengeCompletionLookupNode>(episode, stage_index);
return std::make_unique<ChallengeCompletionLookupNode>(episode, stage_index);
}
if (text.starts_with("T_")) {
return make_unique<TeamRewardLookupNode>(string(text.substr(2)));
return std::make_unique<TeamRewardLookupNode>(std::string(text.substr(2)));
}
if (text == "V_NumPlayers") {
return make_unique<NumPlayersLookupNode>();
return std::make_unique<NumPlayersLookupNode>();
}
if (text == "V_Event") {
return make_unique<EventLookupNode>();
return std::make_unique<EventLookupNode>();
}
if (text == "V_V1Present") {
return make_unique<V1PresenceLookupNode>();
return std::make_unique<V1PresenceLookupNode>();
}
// Check for constants
if (text == "true") {
return make_unique<ConstantNode>(1);
return std::make_unique<ConstantNode>(1);
}
if (text == "false") {
return make_unique<ConstantNode>(0);
return std::make_unique<ConstantNode>(0);
}
try {
size_t endpos;
int64_t v = stoll(string(text), &endpos, 0);
int64_t v = std::stoll(std::string(text), &endpos, 0);
if (endpos == text.size()) {
return make_unique<ConstantNode>(v);
return std::make_unique<ConstantNode>(v);
}
} catch (const exception&) {
} catch (const std::exception&) {
}
throw runtime_error("unparseable expression");
throw std::runtime_error("unparseable expression");
}