Change the ownership of AidlDefinedType objects
Previously, AidlDefinedType objects were directly owned by the
AidlTypenames object, although an AidlDefinedType is conceptually a part
of the AidlDocument object wheere the AidlDefinedType is found. At the
time, AidlDocument merely have references to AidlDefinedTypes that it
(conceptually) owns.
This change fixes the wrong ownership relationship. Now, the ownership
hierarchy is: AIdlTypenames -> AidlDocument -> AidlDefinedType. The
ownership is enforced by keeping AidlDocument and AidlDefinedType using
std::unique_ptr, and making them non-copyable and non-movable.
This change also alters the way AidlDefinedType objects are returned
from the load_and_validate_aidl function. Previously, we simply
returned the references (pointers) that were stored in the AidlDocument
via an out param. Now, the client of the function can get the objects
via typenames.MainDocument().DefinedTypes().
Bug: 160367901
Test: aidl_unittests
Change-Id: If3fcd2acaccce3a484e6329f8fa68bb959b743be
diff --git a/aidl_language.h b/aidl_language.h
index 41c915a..86bd928 100644
--- a/aidl_language.h
+++ b/aidl_language.h
@@ -739,6 +739,8 @@
const std::vector<std::string> split_package_;
DISALLOW_COPY_AND_ASSIGN(AidlDefinedType);
+ AidlDefinedType(AidlDefinedType&&) = delete;
+ AidlDefinedType& operator=(AidlDefinedType&&) = delete;
};
class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
@@ -899,12 +901,21 @@
class AidlDocument : public AidlNode {
public:
AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
- std::vector<AidlDefinedType*>& defined_types)
- : AidlNode(location), imports_(std::move(imports)), defined_types_(defined_types) {}
+ std::vector<std::unique_ptr<AidlDefinedType>>&& defined_types)
+ : AidlNode(location),
+ imports_(std::move(imports)),
+ defined_types_(std::move(defined_types)) {}
const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
- const std::vector<AidlDefinedType*>& DefinedTypes() const { return defined_types_; }
+ const std::vector<std::unique_ptr<AidlDefinedType>>& DefinedTypes() const {
+ return defined_types_;
+ }
+ AidlDocument(const AidlDocument&) = delete;
+ AidlDocument(AidlDocument&&) = delete;
+ AidlDocument& operator=(const AidlDocument&) = delete;
+ AidlDocument& operator=(AidlDocument&&) = delete;
+ ~AidlDocument() = default;
private:
const std::vector<std::unique_ptr<AidlImport>> imports_;
- const std::vector<AidlDefinedType*> defined_types_;
+ const std::vector<std::unique_ptr<AidlDefinedType>> defined_types_;
};