规范优化3

This commit is contained in:
sladro 2026-01-12 22:20:56 +08:00
parent 4d22858d0f
commit f5e5c2da1f
2 changed files with 18 additions and 17 deletions

View File

@ -80,11 +80,12 @@ public:
return static_cast<int>(AsNumber(static_cast<double>(def)));
}
const std::string& AsString(const std::string& def = EmptyString()) const {
// NOTE: Return by value to avoid lifetime issues when callers pass temporaries as defaults.
std::string AsString(std::string_view def = {}) const {
if (const auto* s = std::get_if<std::string>(&value_)) {
return *s;
}
return def;
return std::string(def);
}
const Array& AsArray() const {
@ -134,11 +135,6 @@ public:
private:
Variant value_;
static const std::string& EmptyString() {
static const std::string empty;
return empty;
}
};
inline void SkipWhitespace(std::string_view text, size_t& pos) {

View File

@ -1142,29 +1142,34 @@ bool GraphManager::BuildFromFile(const std::string& path, std::string& err) {
}
bool GraphManager::StartAll() {
std::lock_guard<std::mutex> lock(graphs_mu_);
std::scoped_lock lock(mu_, graphs_mu_);
if (running_) return true;
// Start all graphs; on failure, stop any already started graphs so we never leave a partially-running state.
std::vector<Graph*> started;
started.reserve(graphs_.size());
for (auto& g : graphs_) {
if (!g) continue;
if (!g->Start()) {
for (auto* sg : started) {
if (sg) sg->Stop();
}
return false;
}
started.push_back(g.get());
}
{
std::lock_guard<std::mutex> lock(mu_);
running_ = true;
}
running_ = true;
return true;
}
void GraphManager::StopAll() {
{
std::lock_guard<std::mutex> lock(mu_);
std::scoped_lock lock(mu_, graphs_mu_);
if (!running_) return;
running_ = false;
}
{
std::lock_guard<std::mutex> lock(graphs_mu_);
for (auto& g : graphs_) {
g->Stop();
if (g) g->Stop();
}
}
cv_.notify_all();