From 900875bd910f1e301d9727e2d87642c304aa873d Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Thu, 17 Apr 2025 14:22:21 -0400 Subject: [PATCH] MT#62181 AmArg: take ownership of ValueStruct Change from pointer to directly owned/contained object. Update const usages. Add non-const asStruct() accessor. Change-Id: Idd87c153fa09d1c36faa7ec3317ed872a43dd3c3 --- apps/dsm/DSMStateEngine.cpp | 4 ++-- apps/sbc/arg_conversion.cpp | 4 ++-- core/AmArg.cpp | 45 ++++++++++++++++--------------------- core/AmArg.h | 5 +++-- core/jsonArg.cpp | 4 ++-- 5 files changed, 28 insertions(+), 34 deletions(-) diff --git a/apps/dsm/DSMStateEngine.cpp b/apps/dsm/DSMStateEngine.cpp index dc2fd261..e9d255a8 100644 --- a/apps/dsm/DSMStateEngine.cpp +++ b/apps/dsm/DSMStateEngine.cpp @@ -831,8 +831,8 @@ void varPrintArg(const AmArg& a, map& dst, const string& name) { varPrintArg(a.get(i), dst, name+"["+int2str((unsigned int)i)+"]"); return; case AmArg::Struct: - for (AmArg::ValueStruct::const_iterator it = a.asStruct()->begin(); - it != a.asStruct()->end(); it ++) { + for (AmArg::ValueStruct::const_iterator it = a.asStruct().begin(); + it != a.asStruct().end(); it ++) { varPrintArg(it->second, dst, name+"."+it->first); } return; diff --git a/apps/sbc/arg_conversion.cpp b/apps/sbc/arg_conversion.cpp index cdc9949d..b05468a0 100644 --- a/apps/sbc/arg_conversion.cpp +++ b/apps/sbc/arg_conversion.cpp @@ -31,8 +31,8 @@ static string arg2string(const AmArg &a) case AmArg::Struct: sprintf(tmp, "%c%zd/", STRUCT_LABEL, a.size()); s = tmp; - for (AmArg::ValueStruct::const_iterator it = a.asStruct()->begin(); - it != a.asStruct()->end(); ++it) { + for (AmArg::ValueStruct::const_iterator it = a.asStruct().begin(); + it != a.asStruct().end(); ++it) { sprintf(tmp, "%zd/", it->first.size()); s += tmp; s += it->first; diff --git a/core/AmArg.cpp b/core/AmArg.cpp index 8c773618..e9db4a6b 100644 --- a/core/AmArg.cpp +++ b/core/AmArg.cpp @@ -49,9 +49,6 @@ const char* AmArg::t2str(int type) { AmArg::AmArg(const AmArg& v) : type(v.type) { switch (type) { - case Struct: - value = new ValueStruct(*std::get(v.value)); - break; default: value = v.value; break; @@ -63,7 +60,7 @@ AmArg::AmArg(std::map& v) assertStruct(); for (std::map::iterator it= v.begin();it!= v.end();it++) - (*std::get(value))[it->first] = AmArg(it->second.c_str()); + std::get(value)[it->first] = AmArg(it->second.c_str()); } AmArg::AmArg(std::map& v) @@ -71,7 +68,7 @@ AmArg::AmArg(std::map& v) assertStruct(); for (std::map::iterator it= v.begin();it!= v.end();it++) - (*std::get(value))[it->first] = it->second; + std::get(value)[it->first] = it->second; } AmArg::AmArg(vector& v) @@ -134,7 +131,7 @@ void AmArg::assertStruct() { return; if (Undef == type) { type = Struct; - value = new ValueStruct(); + value = ValueStruct(); return; } throw TypeMismatchException(); @@ -146,7 +143,6 @@ void AmArg::assertStruct() const { } void AmArg::invalidate() { - if(type == Struct) { delete std::get(value); } type = Undef; value = std::monostate(); } @@ -158,7 +154,7 @@ void AmArg::push(const AmArg& a) { void AmArg::push(const string &key, const AmArg &val) { assertStruct(); - (*std::get(value))[key] = val; + std::get(value)[key] = val; } void AmArg::pop(AmArg &a) { @@ -207,7 +203,7 @@ size_t AmArg::size() const { return std::get(value).size(); if (Struct == type) - return std::get(value)->size(); + return std::get(value).size(); throw TypeMismatchException(); } @@ -248,9 +244,6 @@ AmArg& AmArg::operator=(const AmArg& v) { invalidate(); type = v.type; switch (type) { - case Struct: - value = new ValueStruct(*std::get(v.value)); - break; default: value = v.value; break; @@ -292,22 +285,22 @@ const AmArg& AmArg::operator[](int idx) const { AmArg& AmArg::operator[](std::string key) { assertStruct(); - return (*std::get(value))[key]; + return std::get(value)[key]; } const AmArg& AmArg::operator[](std::string key) const { assertStruct(); - return (*std::get(value))[key]; + return std::get(value).at(key); } AmArg& AmArg::operator[](const char* key) { assertStruct(); - return (*std::get(value))[key]; + return std::get(value)[key]; } const AmArg& AmArg::operator[](const char* key) const { assertStruct(); - return (*std::get(value))[key]; + return std::get(value).at(key); } bool AmArg::operator==(const char *val) const { @@ -315,40 +308,40 @@ bool AmArg::operator==(const char *val) const { } bool AmArg::hasMember(const char* name) const { - return type == Struct && std::get(value)->find(name) != std::get(value)->end(); + return type == Struct && std::get(value).find(name) != std::get(value).end(); } bool AmArg::hasMember(const string& name) const { - return type == Struct && std::get(value)->find(name) != std::get(value)->end(); + return type == Struct && std::get(value).find(name) != std::get(value).end(); } std::vector AmArg::enumerateKeys() const { assertStruct(); std::vector res; - for (ValueStruct::iterator it = - std::get(value)->begin(); it != std::get(value)->end(); it++) + for (ValueStruct::const_iterator it = + std::get(value).begin(); it != std::get(value).end(); it++) res.push_back(it->first); return res; } AmArg::ValueStruct::const_iterator AmArg::begin() const { assertStruct(); - return std::get(value)->begin(); + return std::get(value).begin(); } AmArg::ValueStruct::const_iterator AmArg::end() const { assertStruct(); - return std::get(value)->end(); + return std::get(value).end(); } void AmArg::erase(const char* name) { assertStruct(); - std::get(value)->erase(name); + std::get(value).erase(name); } void AmArg::erase(const std::string& name) { assertStruct(); - std::get(value)->erase(name); + std::get(value).erase(name); } void AmArg::assertArrayFmt(const char* format) const { @@ -435,8 +428,8 @@ string AmArg::print(const AmArg &a) { return s; case Struct: s = "{"; - for (AmArg::ValueStruct::const_iterator it = a.asStruct()->begin(); - it != a.asStruct()->end(); it ++) { + for (AmArg::ValueStruct::const_iterator it = a.asStruct().begin(); + it != a.asStruct().end(); it ++) { s += "'"+it->first + "': "; s += print(it->second); s += ", "; diff --git a/core/AmArg.h b/core/AmArg.h index 80bec021..8a38386f 100644 --- a/core/AmArg.h +++ b/core/AmArg.h @@ -118,7 +118,7 @@ class AmArg // value std::variant value; + AmDynInvoke*, ArgBlob, ValueArray, ValueStruct> value; void invalidate(); @@ -263,7 +263,8 @@ class AmArg AmObject* asObject() const { return std::get(value); } AmDynInvoke* asDynInv() const { return std::get(value); } const ArgBlob& asBlob() const { return std::get(value); } - ValueStruct* asStruct() const { return std::get(value); } + const ValueStruct& asStruct() const { return std::get(value); } + ValueStruct& asStruct() { return std::get(value); } vector asStringVector() const; vector asIntVector() const; diff --git a/core/jsonArg.cpp b/core/jsonArg.cpp index 88ce7346..80446b09 100644 --- a/core/jsonArg.cpp +++ b/core/jsonArg.cpp @@ -138,8 +138,8 @@ string arg2json(const AmArg &a) { case AmArg::Struct: s = "{"; - for (AmArg::ValueStruct::const_iterator it = a.asStruct()->begin(); - it != a.asStruct()->end(); it ++) { + for (AmArg::ValueStruct::const_iterator it = a.asStruct().begin(); + it != a.asStruct().end(); it ++) { s += '"'+it->first + "\": "; s += arg2json(it->second); s += ", ";