diff options
author | George Burgess IV <gbiv@google.com> | 2019-03-20 16:30:46 -0700 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2019-03-20 16:30:46 -0700 |
commit | d5a3f9fe4383070c592c47a41ef2a08fae5cc0c3 (patch) | |
tree | a0175955e8aea03a65fe9e1156554a98314b332b | |
parent | 79a4ad97c0a6b1ace3e86d265fcabd6759b54291 (diff) | |
parent | 7ec3a5e8f33e2625299c6d6fa87ee7fcf3d52d9a (diff) | |
download | aidl-d5a3f9fe4383070c592c47a41ef2a08fae5cc0c3.tar.gz |
Make TypeNamespace::Add take a unique_ptr am: 2c6d06f733 am: d77c584f3d
am: 7ec3a5e8f3
Change-Id: I13213ae0645d516ac7641b138795c272f9791054
-rw-r--r-- | type_cpp.cpp | 113 | ||||
-rw-r--r-- | type_cpp.h | 6 | ||||
-rw-r--r-- | type_java.cpp | 118 | ||||
-rw-r--r-- | type_namespace.h | 13 |
4 files changed, 116 insertions, 134 deletions
diff --git a/type_cpp.cpp b/type_cpp.cpp index e6756502..58cc119a 100644 --- a/type_cpp.cpp +++ b/type_cpp.cpp @@ -18,6 +18,7 @@ #include <algorithm> #include <iostream> +#include <memory> #include <vector> #include <android-base/stringprintf.h> @@ -364,32 +365,20 @@ Type::Type(int kind, bool Type::CanWriteToParcel() const { return true; } void TypeNamespace::Init() { - Add(new ByteType()); - Add(new PrimitiveType( - "int", - "cstdint", "int32_t", "readInt32", "writeInt32", - "readInt32Vector", "writeInt32Vector")); - Add(new PrimitiveType( - "long", - "cstdint", "int64_t", "readInt64", "writeInt64", - "readInt64Vector", "writeInt64Vector")); - Add(new PrimitiveType( - "float", - kNoHeader, "float", "readFloat", "writeFloat", - "readFloatVector", "writeFloatVector")); - Add(new PrimitiveType( - "double", - kNoHeader, "double", "readDouble", "writeDouble", - "readDoubleVector", "writeDoubleVector")); - Add(new PrimitiveType( - "boolean", - kNoHeader, "bool", "readBool", "writeBool", - "readBoolVector", "writeBoolVector")); + Add(std::make_unique<ByteType>()); + Add(std::make_unique<PrimitiveType>("int", "cstdint", "int32_t", "readInt32", "writeInt32", + "readInt32Vector", "writeInt32Vector")); + Add(std::make_unique<PrimitiveType>("long", "cstdint", "int64_t", "readInt64", "writeInt64", + "readInt64Vector", "writeInt64Vector")); + Add(std::make_unique<PrimitiveType>("float", kNoHeader, "float", "readFloat", "writeFloat", + "readFloatVector", "writeFloatVector")); + Add(std::make_unique<PrimitiveType>("double", kNoHeader, "double", "readDouble", "writeDouble", + "readDoubleVector", "writeDoubleVector")); + Add(std::make_unique<PrimitiveType>("boolean", kNoHeader, "bool", "readBool", "writeBool", + "readBoolVector", "writeBoolVector")); // C++11 defines the char16_t type as a built in for Unicode characters. - Add(new PrimitiveType( - "char", - kNoHeader, "char16_t", "readChar", "writeChar", - "readCharVector", "writeCharVector")); + Add(std::make_unique<PrimitiveType>("char", kNoHeader, "char16_t", "readChar", "writeChar", + "readCharVector", "writeCharVector")); Type* string_array_type = new CppArrayType( ValidatableType::KIND_BUILT_IN, "java.lang", "String", @@ -402,11 +391,11 @@ void TypeNamespace::Init() { {"memory", "utils/String16.h"}, "::std::unique_ptr<::android::String16>", "readString16", "writeString16"); - string_type_ = new Type(ValidatableType::KIND_BUILT_IN, "java.lang", "String", - {"utils/String16.h"}, "::android::String16", - "readString16", "writeString16", - string_array_type, nullable_string_type); - Add(string_type_); + AddAndSetMember(&string_type_, + std::make_unique<Type>(ValidatableType::KIND_BUILT_IN, "java.lang", "String", + std::vector<std::string>{"utils/String16.h"}, + "::android::String16", "readString16", "writeString16", + string_array_type, nullable_string_type)); using ::android::aidl::kAidlReservedTypePackage; using ::android::aidl::kUtf8InCppStringClass; @@ -419,33 +408,31 @@ void TypeNamespace::Init() { "string", "::std::string", "::std::unique_ptr<::std::string>", "readUtf8VectorFromUtf16Vector", "writeUtf8VectorAsUtf16Vector", false); - Type* nullable_cpp_utf8_string_type = new Type( - ValidatableType::KIND_BUILT_IN, - kAidlReservedTypePackage, kUtf8InCppStringClass, - {"string", "memory"}, "::std::unique_ptr<::std::string>", - "readUtf8FromUtf16", "writeUtf8AsUtf16"); - Add(new Type( - ValidatableType::KIND_BUILT_IN, - kAidlReservedTypePackage, kUtf8InCppStringClass, - {"string"}, "::std::string", "readUtf8FromUtf16", "writeUtf8AsUtf16", - cpp_utf8_string_array, nullable_cpp_utf8_string_type)); + Type* nullable_cpp_utf8_string_type = + new Type(ValidatableType::KIND_BUILT_IN, kAidlReservedTypePackage, kUtf8InCppStringClass, + std::vector<std::string>{"string", "memory"}, "::std::unique_ptr<::std::string>", + "readUtf8FromUtf16", "writeUtf8AsUtf16"); + Add(std::make_unique<Type>(ValidatableType::KIND_BUILT_IN, kAidlReservedTypePackage, + kUtf8InCppStringClass, std::vector<std::string>{"string"}, + "::std::string", "readUtf8FromUtf16", "writeUtf8AsUtf16", + cpp_utf8_string_array, nullable_cpp_utf8_string_type)); Type* nullable_ibinder = new Type( ValidatableType::KIND_BUILT_IN, "android.os", "IBinder", {"binder/IBinder.h"}, "::android::sp<::android::IBinder>", "readNullableStrongBinder", "writeStrongBinder"); - ibinder_type_ = new Type( - ValidatableType::KIND_BUILT_IN, "android.os", "IBinder", - {"binder/IBinder.h"}, "::android::sp<::android::IBinder>", - "readStrongBinder", "writeStrongBinder", - kNoArrayType, nullable_ibinder); - Add(ibinder_type_); - Add(new MapType()); + AddAndSetMember(&ibinder_type_, + std::make_unique<Type>(ValidatableType::KIND_BUILT_IN, "android.os", "IBinder", + std::vector<std::string>{"binder/IBinder.h"}, + "::android::sp<::android::IBinder>", "readStrongBinder", + "writeStrongBinder", kNoArrayType, nullable_ibinder)); - Add(new BinderListType()); - Add(new StringListType()); - Add(new Utf8InCppStringListType()); + Add(std::make_unique<MapType>()); + + Add(std::make_unique<BinderListType>()); + Add(std::make_unique<StringListType>()); + Add(std::make_unique<Utf8InCppStringListType>()); Type* fd_vector_type = new CppArrayType( ValidatableType::KIND_BUILT_IN, kNoPackage, "FileDescriptor", @@ -454,11 +441,10 @@ void TypeNamespace::Init() { "readUniqueFileDescriptorVector", "writeUniqueFileDescriptorVector", false); - Add(new Type( - ValidatableType::KIND_BUILT_IN, kNoPackage, "FileDescriptor", - {"android-base/unique_fd.h"}, "::android::base::unique_fd", - "readUniqueFileDescriptor", "writeUniqueFileDescriptor", - fd_vector_type)); + Add(std::make_unique<Type>(ValidatableType::KIND_BUILT_IN, kNoPackage, "FileDescriptor", + std::vector<std::string>{"android-base/unique_fd.h"}, + "::android::base::unique_fd", "readUniqueFileDescriptor", + "writeUniqueFileDescriptor", fd_vector_type)); Type* pfd_vector_type = new CppArrayType(ValidatableType::KIND_BUILT_IN, "android.os", "ParcelFileDescriptor", @@ -468,16 +454,17 @@ void TypeNamespace::Init() { Type* nullable_pfd_type = new Type(ValidatableType::KIND_BUILT_IN, "android.os", "ParcelFileDescriptor", - {"memory", "binder/ParcelFileDescriptor.h"}, + std::vector<std::string>{"memory", "binder/ParcelFileDescriptor.h"}, "::std::unique_ptr<::android::os::ParcelFileDescriptor>", "readParcelable", "writeNullableParcelable"); - Add(new Type(ValidatableType::KIND_BUILT_IN, "android.os", "ParcelFileDescriptor", - {"binder/ParcelFileDescriptor.h"}, "::android::os::ParcelFileDescriptor", - "readParcelable", "writeParcelable", pfd_vector_type, nullable_pfd_type)); + Add(std::make_unique<Type>(ValidatableType::KIND_BUILT_IN, "android.os", "ParcelFileDescriptor", + std::vector<std::string>{"binder/ParcelFileDescriptor.h"}, + "::android::os::ParcelFileDescriptor", "readParcelable", + "writeParcelable", pfd_vector_type, nullable_pfd_type)); - void_type_ = new class VoidType(); - Add(void_type_); + // Qualify VoidType so we don't get collisions with the VoidType method + AddAndSetMember(&void_type_, std::make_unique<class VoidType>()); } bool TypeNamespace::AddParcelableType(const AidlParcelable& p, const std::string& filename) { @@ -488,12 +475,12 @@ bool TypeNamespace::AddParcelableType(const AidlParcelable& p, const std::string return false; } - Add(new ParcelableType(p, cpp_header, filename)); + Add(std::make_unique<ParcelableType>(p, cpp_header, filename)); return true; } bool TypeNamespace::AddBinderType(const AidlInterface& b, const std::string& filename) { - Add(new BinderType(b, filename)); + Add(std::make_unique<BinderType>(b, filename)); return true; } @@ -106,9 +106,9 @@ class TypeNamespace : public ::android::aidl::LanguageTypeNamespace<Type> { const Type* IBinderType() const { return ibinder_type_; } private: - Type* void_type_ = nullptr; - Type* string_type_ = nullptr; - Type* ibinder_type_ = nullptr; + const Type* void_type_ = nullptr; + const Type* string_type_ = nullptr; + const Type* ibinder_type_ = nullptr; DISALLOW_COPY_AND_ASSIGN(TypeNamespace); }; // class TypeNamespace diff --git a/type_java.cpp b/type_java.cpp index 27fe9d40..1298eac6 100644 --- a/type_java.cpp +++ b/type_java.cpp @@ -17,6 +17,7 @@ #include "type_java.h" #include <sys/types.h> +#include <memory> #include <android-base/strings.h> @@ -245,80 +246,69 @@ ClassLoaderType::ClassLoaderType(const JavaTypeNamespace* types) // ================================================================ void JavaTypeNamespace::Init() { - Add(new BasicType(this, "void", "XXX", "XXX", "XXX", "XXX", "XXX")); + Add(std::make_unique<BasicType>(this, "void", "XXX", "XXX", "XXX", "XXX", "XXX")); - m_bool_type = new BooleanType(this); - Add(m_bool_type); + AddAndSetMember(&m_bool_type, std::make_unique<BooleanType>(this)); - Add(new BasicType(this, "byte", "writeByte", "readByte", "writeByteArray", "createByteArray", - "readByteArray")); + Add(std::make_unique<BasicType>(this, "byte", "writeByte", "readByte", "writeByteArray", + "createByteArray", "readByteArray")); - Add(new CharType(this)); + Add(std::make_unique<CharType>(this)); - m_int_type = new BasicType(this, "int", "writeInt", "readInt", "writeIntArray", "createIntArray", - "readIntArray"); - Add(m_int_type); + AddAndSetMember(&m_int_type, + std::make_unique<BasicType>(this, "int", "writeInt", "readInt", "writeIntArray", + "createIntArray", "readIntArray")); - Add(new BasicType(this, "long", "writeLong", "readLong", "writeLongArray", "createLongArray", - "readLongArray")); + Add(std::make_unique<BasicType>(this, "long", "writeLong", "readLong", "writeLongArray", + "createLongArray", "readLongArray")); - Add(new BasicType(this, "float", "writeFloat", "readFloat", "writeFloatArray", "createFloatArray", - "readFloatArray")); + Add(std::make_unique<BasicType>(this, "float", "writeFloat", "readFloat", "writeFloatArray", + "createFloatArray", "readFloatArray")); - Add(new BasicType(this, "double", "writeDouble", "readDouble", "writeDoubleArray", - "createDoubleArray", "readDoubleArray")); + Add(std::make_unique<BasicType>(this, "double", "writeDouble", "readDouble", "writeDoubleArray", + "createDoubleArray", "readDoubleArray")); - m_string_type = new class StringType(this, "java.lang", "String"); - Add(m_string_type); - Add(new class StringType(this, ::android::aidl::kAidlReservedTypePackage, - ::android::aidl::kUtf8InCppStringClass)); + AddAndSetMember(&m_string_type, std::make_unique<class StringType>(this, "java.lang", "String")); + Add(std::make_unique<class StringType>(this, ::android::aidl::kAidlReservedTypePackage, + ::android::aidl::kUtf8InCppStringClass)); - Add(new Type(this, "java.lang", "Object", ValidatableType::KIND_BUILT_IN, false)); + Add(std::make_unique<Type>(this, "java.lang", "Object", ValidatableType::KIND_BUILT_IN, false)); - Add(new FileDescriptorType(this)); + Add(std::make_unique<FileDescriptorType>(this)); - Add(new ParcelFileDescriptorType(this)); + Add(std::make_unique<ParcelFileDescriptorType>(this)); - Add(new CharSequenceType(this)); + Add(std::make_unique<CharSequenceType>(this)); - Add(new MapType(this)); + Add(std::make_unique<MapType>(this)); - Add(new ListType(this)); + Add(std::make_unique<ListType>(this)); - m_text_utils_type = - new Type(this, "android.text", "TextUtils", ValidatableType::KIND_BUILT_IN, false); - Add(m_text_utils_type); + AddAndSetMember(&m_text_utils_type, + std::make_unique<Type>(this, "android.text", "TextUtils", + ValidatableType::KIND_BUILT_IN, false)); - m_remote_exception_type = new class RemoteExceptionType(this); - Add(m_remote_exception_type); + AddAndSetMember(&m_remote_exception_type, std::make_unique<class RemoteExceptionType>(this)); - m_runtime_exception_type = new class RuntimeExceptionType(this); - Add(m_runtime_exception_type); + AddAndSetMember(&m_runtime_exception_type, std::make_unique<class RuntimeExceptionType>(this)); - m_ibinder_type = new class IBinderType(this); - Add(m_ibinder_type); + AddAndSetMember(&m_ibinder_type, std::make_unique<class IBinderType>(this)); - m_iinterface_type = new class IInterfaceType(this); - Add(m_iinterface_type); + AddAndSetMember(&m_iinterface_type, std::make_unique<class IInterfaceType>(this)); - m_binder_native_type = new class BinderType(this); - Add(m_binder_native_type); + AddAndSetMember(&m_binder_native_type, std::make_unique<BinderType>(this)); - m_binder_proxy_type = new class BinderProxyType(this); - Add(m_binder_proxy_type); + AddAndSetMember(&m_binder_proxy_type, std::make_unique<class BinderProxyType>(this)); - m_parcel_type = new class ParcelType(this); - Add(m_parcel_type); + AddAndSetMember(&m_parcel_type, std::make_unique<class ParcelType>(this)); - m_parcelable_interface_type = new class ParcelableInterfaceType(this); - Add(m_parcelable_interface_type); + AddAndSetMember(&m_parcelable_interface_type, + std::make_unique<class ParcelableInterfaceType>(this)); - m_context_type = - new class Type(this, "android.content", "Context", ValidatableType::KIND_BUILT_IN, false); - Add(m_context_type); + AddAndSetMember(&m_context_type, std::make_unique<Type>(this, "android.content", "Context", + ValidatableType::KIND_BUILT_IN, false)); - m_classloader_type = new class ClassLoaderType(this); - Add(m_classloader_type); + AddAndSetMember(&m_classloader_type, std::make_unique<class ClassLoaderType>(this)); NULL_VALUE = new LiteralExpression("null"); THIS_VALUE = new LiteralExpression("this"); @@ -329,27 +319,27 @@ void JavaTypeNamespace::Init() { bool JavaTypeNamespace::AddParcelableType(const AidlParcelable& p, const std::string& filename) { - Type* type = new UserDataType(this, p.GetPackage(), p.GetName(), false, true, filename); - return Add(type); + return Add( + std::make_unique<UserDataType>(this, p.GetPackage(), p.GetName(), false, true, filename)); } bool JavaTypeNamespace::AddBinderType(const AidlInterface& b, const std::string& filename) { // for interfaces, add the stub, proxy, and interface types. - Type* stub = new Type(this, b.GetPackage(), b.GetName() + ".Stub", - ValidatableType::KIND_GENERATED, false, filename); - Type* proxy = new Type(this, b.GetPackage(), b.GetName() + ".Stub.Proxy", - ValidatableType::KIND_GENERATED, false, filename); - Type* defaultImpl = new Type(this, b.GetPackage(), b.GetName() + ".Default", - ValidatableType::KIND_GENERATED, false, filename); - Type* type = new InterfaceType(this, b.GetPackage(), b.GetName(), false, filename, -1, stub, - proxy, defaultImpl); + auto stub = std::make_unique<Type>(this, b.GetPackage(), b.GetName() + ".Stub", + ValidatableType::KIND_GENERATED, false, filename); + auto proxy = std::make_unique<Type>(this, b.GetPackage(), b.GetName() + ".Stub.Proxy", + ValidatableType::KIND_GENERATED, false, filename); + auto defaultImpl = std::make_unique<Type>(this, b.GetPackage(), b.GetName() + ".Default", + ValidatableType::KIND_GENERATED, false, filename); + auto type = std::make_unique<InterfaceType>(this, b.GetPackage(), b.GetName(), false, filename, + -1, stub.get(), proxy.get(), defaultImpl.get()); bool success = true; - success &= Add(type); - success &= Add(stub); - success &= Add(proxy); - success &= Add(defaultImpl); + success &= Add(std::move(type)); + success &= Add(std::move(stub)); + success &= Add(std::move(proxy)); + success &= Add(std::move(defaultImpl)); return success; } @@ -358,7 +348,7 @@ bool JavaTypeNamespace::AddListType(const std::string& contained_type_name) { if (!contained_type) { return false; } - Add(new GenericListType(this, contained_type)); + Add(std::make_unique<GenericListType>(this, contained_type)); return true; } diff --git a/type_namespace.h b/type_namespace.h index 947d0a14..a7952fbc 100644 --- a/type_namespace.h +++ b/type_namespace.h @@ -163,7 +163,12 @@ class LanguageTypeNamespace : public TypeNamespace { const std::string& value_type_name) = 0; protected: - bool Add(const T* type); + bool Add(std::unique_ptr<const T> type); + void AddAndSetMember(const T** member, std::unique_ptr<const T> type) { + const T* ptr_value = type.get(); + CHECK(Add(std::move(type))); + *member = ptr_value; + } private: // Returns true iff the name can be canonicalized to a container type. @@ -182,11 +187,11 @@ class LanguageTypeNamespace : public TypeNamespace { DISALLOW_COPY_AND_ASSIGN(LanguageTypeNamespace); }; // class LanguageTypeNamespace -template<typename T> -bool LanguageTypeNamespace<T>::Add(const T* type) { +template <typename T> +bool LanguageTypeNamespace<T>::Add(std::unique_ptr<const T> type) { const T* existing = FindTypeByCanonicalName(type->CanonicalName()); if (!existing) { - types_.emplace_back(type); + types_.push_back(std::move(type)); return true; } |