Add Builder for VintfObject
Clean up the dependencies of VintfObject and use
Builder pattern.
Test: builds
Test: libvintf_test
Test: vintf_object_test
Bug: 118634720
Change-Id: Ia785e150e65ecb403e1b1973f3ac51588244bfbf
diff --git a/VintfObject.cpp b/VintfObject.cpp
index 6a5685a..64f4a61 100644
--- a/VintfObject.cpp
+++ b/VintfObject.cpp
@@ -82,20 +82,11 @@
return propertyFetcher;
}
-VintfObject::VintfObject(std::unique_ptr<FileSystem>&& fileSystem,
- std::unique_ptr<ObjectFactory<RuntimeInfo>>&& runtimeInfoFactory,
- std::unique_ptr<PropertyFetcher>&& propertyFetcher)
- : mFileSystem(fileSystem ? std::move(fileSystem) : createDefaultFileSystem()),
- mRuntimeInfoFactory(runtimeInfoFactory ? std::move(runtimeInfoFactory)
- : std::make_unique<ObjectFactory<RuntimeInfo>>()),
- mPropertyFetcher(propertyFetcher ? std::move(propertyFetcher)
- : createDefaultPropertyFetcher()) {}
-
details::LockedSharedPtr<VintfObject> VintfObject::sInstance{};
std::shared_ptr<VintfObject> VintfObject::GetInstance() {
std::unique_lock<std::mutex> lock(sInstance.mutex);
if (sInstance.object == nullptr) {
- sInstance.object = std::make_shared<VintfObject>();
+ sInstance.object = std::shared_ptr<VintfObject>(VintfObject::Builder().build().release());
}
return sInstance.object;
}
@@ -743,5 +734,33 @@
return mRuntimeInfoFactory;
}
+// make_unique does not work because VintfObject constructor is private.
+VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
+
+VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
+ mObject->mFileSystem = std::move(e);
+ return *this;
+}
+
+VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
+ std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
+ mObject->mRuntimeInfoFactory = std::move(e);
+ return *this;
+}
+
+VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
+ std::unique_ptr<PropertyFetcher>&& e) {
+ mObject->mPropertyFetcher = std::move(e);
+ return *this;
+}
+
+std::unique_ptr<VintfObject> VintfObject::Builder::build() {
+ if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
+ if (!mObject->mRuntimeInfoFactory)
+ mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
+ if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
+ return std::move(mObject);
+}
+
} // namespace vintf
} // namespace android