buffet: Change shared ownership of types/values to exclusive
In order to support array types in Buffet's type system, PropValue
must maintain a strong reference to the underlying PropType since
the array elements will have their own type reference and the current
implementation of using raw pointer for this isn't going to work.
In order to resolve this I had to make a lot of changes to the object
ownership inside Buffet's type system. I made it possible for both
PropType and PropValue to make a deep copy of itself using their
Clone() methods. Because of this now it is possible to have exclusive
ownership of objects and I got rid of most of shared pointer usage in
ObjectSchema, PropType and PropValue.
BUG=brillo:107
TEST=`FEATURES=test emerge-link buffet`
Change-Id: I02de455dfd40d4833041b63cbb80bcb00293b5a9
Reviewed-on: https://chromium-review.googlesource.com/261336
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Trybot-Ready: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/commands/object_schema.cc b/buffet/commands/object_schema.cc
index b89de98..5b565ed 100644
--- a/buffet/commands/object_schema.cc
+++ b/buffet/commands/object_schema.cc
@@ -221,9 +221,22 @@
} // anonymous namespace
+ObjectSchema::ObjectSchema() {}
+ObjectSchema::~ObjectSchema() {}
+
+std::unique_ptr<ObjectSchema> ObjectSchema::Clone() const {
+ std::unique_ptr<ObjectSchema> cloned{new ObjectSchema};
+ for (const auto& pair : properties_) {
+ cloned->properties_.emplace(pair.first, pair.second->Clone());
+ }
+ cloned->extra_properties_allowed_ = extra_properties_allowed_;
+ return cloned;
+}
+
void ObjectSchema::AddProp(const std::string& name,
- std::shared_ptr<PropType> prop) {
- properties_[name] = prop;
+ std::unique_ptr<PropType> prop) {
+ // Not using emplace() here to make sure we override existing properties.
+ properties_[name] = std::move(prop);
}
const PropType* ObjectSchema::GetProp(const std::string& name) const {
@@ -254,7 +267,7 @@
object_schema ? object_schema->GetProp(iter.key()) : nullptr;
auto prop_type = PropFromJson(iter.value(), base_schema, error);
if (prop_type) {
- properties.insert(std::make_pair(iter.key(), std::move(prop_type)));
+ properties.emplace(iter.key(), std::move(prop_type));
} else {
chromeos::Error::AddToPrintf(error, FROM_HERE, errors::commands::kDomain,
errors::commands::kInvalidPropDef,
@@ -301,4 +314,8 @@
return {};
}
+std::unique_ptr<ObjectSchema> ObjectSchema::Create() {
+ return std::unique_ptr<ObjectSchema>{new ObjectSchema};
+}
+
} // namespace buffet