OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
UserFlags.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2//
3// Copyright 2026 Caleb Buahin
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
58
59#ifndef OPENSWMM_ENGINE_USER_FLAGS_HPP
60#define OPENSWMM_ENGINE_USER_FLAGS_HPP
61
62#include <string>
63#include <unordered_map>
64#include <vector>
65#include <variant>
66#include <optional>
67#include <stdexcept>
68
69#include "StringCase.hpp"
70
71namespace openswmm {
72
73// ============================================================================
74// Type tag
75// ============================================================================
76
81enum class UserFlagType : int {
82 BOOLEAN = 0,
83 INTEGER = 1,
84 REAL = 2,
85 STRING = 3
86};
87
88// ============================================================================
89// Schema definition
90// ============================================================================
91
102 std::string name;
104 std::string description;
105};
106
107// ============================================================================
108// Per-object value
109// ============================================================================
110
115using UserFlagValue = std::variant<bool, int, double, std::string>;
116
124 std::string object_type;
125 std::string object_name;
126 std::string flag_name;
128};
129
130// ============================================================================
131// Container
132// ============================================================================
133
147public:
148 // ------------------------------------------------------------------
149 // Schema (from [USER_FLAGS])
150 // ------------------------------------------------------------------
151
161 void define(UserFlagDef def) {
162 const auto it = def_index_.find(def.name);
163 if (it != def_index_.end()) {
164 defs_[it->second] = std::move(def);
165 } else {
166 def_index_[def.name] = defs_.size();
167 defs_.push_back(std::move(def));
168 }
169 }
170
181 bool undefine(const std::string& name) {
182 const auto it = def_index_.find(name);
183 if (it == def_index_.end())
184 return false;
185 const std::size_t idx = it->second;
186 defs_.erase(defs_.begin() + static_cast<std::ptrdiff_t>(idx));
187 def_index_.erase(it);
188 // Reindex definitions that followed the erased slot.
189 for (auto& kv : def_index_)
190 if (kv.second > idx)
191 --kv.second;
192 // Remove orphaned per-object values (key = "TYPE:NAME:FLAG";
193 // same left-to-right decomposition as InpWriter).
194 for (auto vit = values_.begin(); vit != values_.end();) {
195 const auto& key = vit->first;
196 const auto p1 = key.find(':');
197 const auto p2 = (p1 == std::string::npos) ? std::string::npos : key.find(':', p1 + 1);
198 if (p2 != std::string::npos && key.compare(p2 + 1, std::string::npos, name) == 0)
199 vit = values_.erase(vit);
200 else
201 ++vit;
202 }
203 return true;
204 }
205
212 bool is_defined(const std::string& name) const { return def_index_.count(name) != 0; }
213
222 const UserFlagDef& get_def(const std::string& name) const {
223 const auto it = def_index_.find(name);
224 if (it == def_index_.end())
225 throw std::out_of_range("UserFlags: no definition for flag '" + name + "'");
226 return defs_[it->second];
227 }
228
233 const std::vector<UserFlagDef>& all_defs() const noexcept { return defs_; }
234
239 std::size_t def_count() const noexcept { return defs_.size(); }
240
241 // ------------------------------------------------------------------
242 // Per-object values (from [USER_FLAG_VALUES])
243 // ------------------------------------------------------------------
244
259 void set(const std::string& object_type, const std::string& object_name,
260 const std::string& flag_name, UserFlagValue value) {
261 values_[make_key(object_type, object_name, flag_name)] = std::move(value);
262 }
263
273 bool unset(const std::string& object_type, const std::string& object_name,
274 const std::string& flag_name) {
275 return values_.erase(make_key(object_type, object_name, flag_name)) != 0;
276 }
277
287 bool has_value(const std::string& object_type, const std::string& object_name,
288 const std::string& flag_name) const {
289 return values_.count(make_key(object_type, object_name, flag_name)) != 0;
290 }
291
302 const UserFlagValue& get_value(const std::string& object_type, const std::string& object_name,
303 const std::string& flag_name) const {
304 const auto it = values_.find(make_key(object_type, object_name, flag_name));
305 if (it == values_.end())
306 throw std::out_of_range("UserFlags: no value for " + object_type + ":" + object_name +
307 ":" + flag_name);
308 return it->second;
309 }
310
321 std::optional<UserFlagValue> try_get_value(const std::string& object_type,
322 const std::string& object_name,
323 const std::string& flag_name) const {
324 const auto it = values_.find(make_key(object_type, object_name, flag_name));
325 if (it == values_.end())
326 return std::nullopt;
327 return it->second;
328 }
329
333 using ValueMap = std::unordered_map<std::string, UserFlagValue, CiHash, CiEqual>;
334
340 const ValueMap& all_values() const noexcept { return values_; }
341
346 std::size_t value_count() const noexcept { return values_.size(); }
347
348 // ------------------------------------------------------------------
349 // Reset
350 // ------------------------------------------------------------------
351
358 void clear() noexcept {
359 defs_.clear();
360 def_index_.clear();
361 values_.clear();
362 }
363
364 // ------------------------------------------------------------------
365 // Composite-key helper (public for tests)
366 // ------------------------------------------------------------------
367
380 static std::string make_key(const std::string& object_type, const std::string& object_name,
381 const std::string& flag_name) {
382 std::string key;
383 key.reserve(object_type.size() + 1 + object_name.size() + 1 + flag_name.size());
384 key += object_type;
385 key += ':';
386 key += object_name;
387 key += ':';
388 key += flag_name;
389 return key;
390 }
391
392private:
393 // Schema
394 std::vector<UserFlagDef> defs_;
395 std::unordered_map<std::string, std::size_t> def_index_;
396
397 // Per-object values
398 ValueMap values_;
399};
400
401} /* namespace openswmm */
402
403#endif /* OPENSWMM_ENGINE_USER_FLAGS_HPP */
Case-insensitive string helpers matching legacy SWMM name semantics.
Stores the full user-flags data: schema definitions + per-object values.
Definition UserFlags.hpp:146
std::optional< UserFlagValue > try_get_value(const std::string &object_type, const std::string &object_name, const std::string &flag_name) const
Copy a case-insensitive composite assignment when present.
Definition UserFlags.hpp:321
void define(UserFlagDef def)
Insert or replace an exact-case flag schema.
Definition UserFlags.hpp:161
const UserFlagValue & get_value(const std::string &object_type, const std::string &object_name, const std::string &flag_name) const
Borrow a case-insensitive composite assignment value.
Definition UserFlags.hpp:302
std::size_t def_count() const noexcept
Count schema definitions.
Definition UserFlags.hpp:239
static std::string make_key(const std::string &object_type, const std::string &object_name, const std::string &flag_name)
Join three unmodified components with colon delimiters.
Definition UserFlags.hpp:380
const ValueMap & all_values() const noexcept
Borrow the complete case-insensitive assignment map.
Definition UserFlags.hpp:340
void clear() noexcept
Erase all schemas and assignments.
Definition UserFlags.hpp:358
bool has_value(const std::string &object_type, const std::string &object_name, const std::string &flag_name) const
Test case-insensitive composite assignment membership.
Definition UserFlags.hpp:287
const UserFlagDef & get_def(const std::string &name) const
Borrow an exact-case schema definition.
Definition UserFlags.hpp:222
const std::vector< UserFlagDef > & all_defs() const noexcept
Borrow schema definitions in insertion order.
Definition UserFlags.hpp:233
bool undefine(const std::string &name)
Remove a schema and matching assignment suffixes.
Definition UserFlags.hpp:181
bool is_defined(const std::string &name) const
Test exact-case schema membership.
Definition UserFlags.hpp:212
std::unordered_map< std::string, UserFlagValue, CiHash, CiEqual > ValueMap
Definition UserFlags.hpp:333
void set(const std::string &object_type, const std::string &object_name, const std::string &flag_name, UserFlagValue value)
Assign an owned variant value using a case-insensitive composite key.
Definition UserFlags.hpp:259
std::size_t value_count() const noexcept
Count stored composite assignments.
Definition UserFlags.hpp:346
bool unset(const std::string &object_type, const std::string &object_name, const std::string &flag_name)
Remove a case-insensitive composite assignment.
Definition UserFlags.hpp:273
std::variant< bool, int, double, std::string > UserFlagValue
A concrete flag value assigned to a specific object.
Definition UserFlags.hpp:115
UserFlagType
Value type for a user flag.
Definition UserFlags.hpp:81
@ INTEGER
Signed integer.
Definition UserFlags.hpp:83
@ STRING
Arbitrary string.
Definition UserFlags.hpp:85
@ REAL
Double-precision floating-point.
Definition UserFlags.hpp:84
@ BOOLEAN
YES/NO/TRUE/FALSE/1/0.
Definition UserFlags.hpp:82
Definition ExchangeLaw.hpp:60
A (object_type, object_name, flag_name) → value assignment.
Definition UserFlags.hpp:123
std::string flag_name
Must match a name in UserFlags::defs_.
Definition UserFlags.hpp:126
std::string object_type
e.g. "NODE", "LINK", "SUBCATCHMENT"
Definition UserFlags.hpp:124
std::string object_name
Object identifier as it appears in the model.
Definition UserFlags.hpp:125
UserFlagValue value
Typed runtime value.
Definition UserFlags.hpp:127
Schema entry for a single user-defined flag.
Definition UserFlags.hpp:101
std::string description
Human-readable description (may be empty)
Definition UserFlags.hpp:104
UserFlagType type
Value type.
Definition UserFlags.hpp:103
std::string name
Flag identifier (uppercase)
Definition UserFlags.hpp:102