43#ifndef OPENSWMM_ENGINE_USER_FLAGS_HPP
44#define OPENSWMM_ENGINE_USER_FLAGS_HPP
47#include <unordered_map>
139 const auto it = def_index_.find(def.
name);
140 if (it != def_index_.end()) {
141 defs_[it->second] = std::move(def);
143 def_index_[def.
name] = defs_.size();
144 defs_.push_back(std::move(def));
152 return def_index_.count(name) != 0;
160 const auto it = def_index_.find(name);
161 if (it == def_index_.end())
162 throw std::out_of_range(
"UserFlags: no definition for flag '" + name +
"'");
163 return defs_[it->second];
169 const std::vector<UserFlagDef>&
all_defs() const noexcept {
return defs_; }
174 std::size_t
def_count() const noexcept {
return defs_.size(); }
185 void set(
const std::string& object_type,
186 const std::string& object_name,
187 const std::string& flag_name,
189 values_[
make_key(object_type, object_name, flag_name)] = std::move(value);
196 const std::string& object_name,
197 const std::string& flag_name)
const {
198 return values_.count(
make_key(object_type, object_name, flag_name)) != 0;
206 const std::string& object_name,
207 const std::string& flag_name)
const {
208 const auto it = values_.find(
make_key(object_type, object_name, flag_name));
209 if (it == values_.end())
210 throw std::out_of_range(
211 "UserFlags: no value for " + object_type +
":" +
212 object_name +
":" + flag_name);
220 const std::string& object_name,
221 const std::string& flag_name)
const {
222 const auto it = values_.find(
make_key(object_type, object_name, flag_name));
223 if (it == values_.end())
return std::nullopt;
230 const std::unordered_map<std::string, UserFlagValue>&
all_values() const noexcept {
237 std::size_t
value_count() const noexcept {
return values_.size(); }
262 static std::string
make_key(
const std::string& object_type,
263 const std::string& object_name,
264 const std::string& flag_name) {
266 key.reserve(object_type.size() + 1 + object_name.size() + 1 + flag_name.size());
277 std::vector<UserFlagDef> defs_;
278 std::unordered_map<std::string, std::size_t> def_index_;
281 std::unordered_map<std::string, UserFlagValue> values_;
Stores the full user-flags data: schema definitions + per-object values.
Definition UserFlags.hpp:127
std::optional< UserFlagValue > try_get_value(const std::string &object_type, const std::string &object_name, const std::string &flag_name) const
Retrieve the value, or nullopt if not assigned.
Definition UserFlags.hpp:219
void define(UserFlagDef def)
Register a flag definition.
Definition UserFlags.hpp:138
const UserFlagValue & get_value(const std::string &object_type, const std::string &object_name, const std::string &flag_name) const
Retrieve the value for (object_type, object_name, flag_name).
Definition UserFlags.hpp:205
std::size_t def_count() const noexcept
Number of defined flag schemas.
Definition UserFlags.hpp:174
static std::string make_key(const std::string &object_type, const std::string &object_name, const std::string &flag_name)
Build the composite lookup key "OBJECTTYPE:OBJECTNAME:FLAGNAME".
Definition UserFlags.hpp:262
void clear() noexcept
Clear both schema definitions and per-object value assignments.
Definition UserFlags.hpp:246
bool has_value(const std::string &object_type, const std::string &object_name, const std::string &flag_name) const
Check whether a value has been assigned for a given triple.
Definition UserFlags.hpp:195
const UserFlagDef & get_def(const std::string &name) const
Retrieve a flag definition by name.
Definition UserFlags.hpp:159
const std::unordered_map< std::string, UserFlagValue > & all_values() const noexcept
All per-object value assignments, keyed by composite string.
Definition UserFlags.hpp:230
const std::vector< UserFlagDef > & all_defs() const noexcept
All flag definitions, in insertion order.
Definition UserFlags.hpp:169
bool is_defined(const std::string &name) const
Check whether a flag name has been defined (schema).
Definition UserFlags.hpp:151
void set(const std::string &object_type, const std::string &object_name, const std::string &flag_name, UserFlagValue value)
Assign a value to (object_type, object_name, flag_name).
Definition UserFlags.hpp:185
std::size_t value_count() const noexcept
Number of per-object value assignments.
Definition UserFlags.hpp:237
std::variant< bool, int, double, std::string > UserFlagValue
A concrete flag value assigned to a specific object.
Definition UserFlags.hpp:97
UserFlagType
Value type for a user flag.
Definition UserFlags.hpp:63
@ STRING
Arbitrary string.
@ REAL
Double-precision floating-point.
@ BOOLEAN
YES/NO/TRUE/FALSE/1/0.
Definition Controls.cpp:24
A (object_type, object_name, flag_name) → value assignment.
Definition UserFlags.hpp:105
std::string flag_name
Must match a name in UserFlags::defs_.
Definition UserFlags.hpp:108
std::string object_type
e.g. "NODE", "LINK", "SUBCATCHMENT"
Definition UserFlags.hpp:106
std::string object_name
Object identifier as it appears in the model.
Definition UserFlags.hpp:107
UserFlagValue value
Typed runtime value.
Definition UserFlags.hpp:109
Schema entry for a single user-defined flag.
Definition UserFlags.hpp:83
std::string description
Human-readable description (may be empty)
Definition UserFlags.hpp:86
UserFlagType type
Value type.
Definition UserFlags.hpp:85
std::string name
Flag identifier (uppercase)
Definition UserFlags.hpp:84