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/command_dictionary.cc b/buffet/commands/command_dictionary.cc
index 14bead8..68e963c 100644
--- a/buffet/commands/command_dictionary.cc
+++ b/buffet/commands/command_dictionary.cc
@@ -26,7 +26,7 @@
const std::string& category,
const CommandDictionary* base_commands,
chromeos::ErrorPtr* error) {
- std::map<std::string, std::shared_ptr<const CommandDefinition>> new_defs;
+ CommandMap new_defs;
// |json| contains a list of nested objects with the following structure:
// {"<pkg_name>": {"<cmd_name>": {"parameters": {object_schema}}, ...}, ...}
@@ -72,8 +72,8 @@
if (base_commands) {
auto cmd = base_commands->FindCommand(full_command_name);
if (cmd) {
- base_parameters_def = cmd->GetParameters().get();
- base_results_def = cmd->GetResults().get();
+ base_parameters_def = cmd->GetParameters();
+ base_results_def = cmd->GetResults();
}
// If the base command dictionary was provided but the command was not
@@ -111,10 +111,11 @@
if (!results_schema)
return false;
- auto command_def = std::make_shared<CommandDefinition>(category,
- parameters_schema,
- results_schema);
- new_defs.insert(std::make_pair(full_command_name, command_def));
+ std::unique_ptr<CommandDefinition> command_def{
+ new CommandDefinition{category, std::move(parameters_schema),
+ std::move(results_schema)}
+ };
+ new_defs.emplace(full_command_name, std::move(command_def));
command_iter.Advance();
}
@@ -140,17 +141,18 @@
definitions_.erase(name);
// Insert new definitions into the global map.
- definitions_.insert(new_defs.begin(), new_defs.end());
+ for (auto& pair : new_defs)
+ definitions_.emplace(pair.first, std::move(pair.second));
return true;
}
-std::shared_ptr<ObjectSchema> CommandDictionary::BuildObjectSchema(
+std::unique_ptr<ObjectSchema> CommandDictionary::BuildObjectSchema(
const base::DictionaryValue* command_def_json,
const char* property_name,
const ObjectSchema* base_def,
const std::string& command_name,
chromeos::ErrorPtr* error) {
- auto object_schema = std::make_shared<ObjectSchema>();
+ auto object_schema = ObjectSchema::Create();
const base::DictionaryValue* schema_def = nullptr;
if (!command_def_json->GetDictionaryWithoutPathExpansion(property_name,
@@ -204,11 +206,10 @@
return dict;
}
-std::shared_ptr<const CommandDefinition> CommandDictionary::FindCommand(
+const CommandDefinition* CommandDictionary::FindCommand(
const std::string& command_name) const {
auto pair = definitions_.find(command_name);
- return (pair != definitions_.end()) ? pair->second :
- std::shared_ptr<const CommandDefinition>();
+ return (pair != definitions_.end()) ? pair->second.get() : nullptr;
}
void CommandDictionary::Clear() {