Add DISALLOW_COPY_AND_ASSIGN macro.
Make the DISALLOW_COPY_AND_ASSIGN macro delete the member functions rather
than just not defining them to improve analysis of the Scoped.. classes.
Add a few missing const qualifiers.
Change-Id: I48eee0c22908d45a05df6979aa61442d2eedcf36
diff --git a/JNIHelp.cpp b/JNIHelp.cpp
index 9de3dfc..49c4f94 100644
--- a/JNIHelp.cpp
+++ b/JNIHelp.cpp
@@ -59,12 +59,10 @@
}
private:
- C_JNIEnv* mEnv;
+ C_JNIEnv* const mEnv;
T mLocalRef;
- // Disallow copy and assignment.
- scoped_local_ref(const scoped_local_ref&);
- void operator=(const scoped_local_ref&);
+ DISALLOW_COPY_AND_ASSIGN(scoped_local_ref);
};
static jclass findClass(C_JNIEnv* env, const char* className) {
diff --git a/include/nativehelper/JNIHelp.h b/include/nativehelper/JNIHelp.h
index cfab8f7..bf01986 100644
--- a/include/nativehelper/JNIHelp.h
+++ b/include/nativehelper/JNIHelp.h
@@ -172,6 +172,20 @@
jniLogException(&env->functions, priority, tag, exception);
}
+#if !defined(DISALLOW_COPY_AND_ASSIGN)
+// DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. It goes in the private:
+// declarations in a class.
+#if __cplusplus >= 201103L
+#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
+ TypeName(const TypeName&) = delete; \
+ void operator=(const TypeName&) = delete
+#else
+#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
+ TypeName(const TypeName&); \
+ void operator=(const TypeName&)
+#endif // __has_feature(cxx_deleted_functions)
+#endif // !defined(DISALLOW_COPY_AND_ASSIGN)
+
#endif
/*
diff --git a/include/nativehelper/ScopedBytes.h b/include/nativehelper/ScopedBytes.h
index cb2614b..fec46e8 100644
--- a/include/nativehelper/ScopedBytes.h
+++ b/include/nativehelper/ScopedBytes.h
@@ -48,17 +48,15 @@
}
private:
- JNIEnv* mEnv;
- jobject mObject;
+ JNIEnv* const mEnv;
+ const jobject mObject;
jbyteArray mByteArray;
protected:
jbyte* mPtr;
private:
- // Disallow copy and assignment.
- ScopedBytes(const ScopedBytes&);
- void operator=(const ScopedBytes&);
+ DISALLOW_COPY_AND_ASSIGN(ScopedBytes);
};
class ScopedBytesRO : public ScopedBytes<true> {
diff --git a/include/nativehelper/ScopedFd.h b/include/nativehelper/ScopedFd.h
index 795f50c..adf67c0 100644
--- a/include/nativehelper/ScopedFd.h
+++ b/include/nativehelper/ScopedFd.h
@@ -52,9 +52,7 @@
private:
int fd;
- // Disallow copy and assignment.
- ScopedFd(const ScopedFd&);
- void operator=(const ScopedFd&);
+ DISALLOW_COPY_AND_ASSIGN(ScopedFd);
};
#endif // SCOPED_FD_H_included
diff --git a/include/nativehelper/ScopedLocalFrame.h b/include/nativehelper/ScopedLocalFrame.h
index 35b6ad8..b739e24 100644
--- a/include/nativehelper/ScopedLocalFrame.h
+++ b/include/nativehelper/ScopedLocalFrame.h
@@ -30,11 +30,9 @@
}
private:
- JNIEnv* mEnv;
+ JNIEnv* const mEnv;
- // Disallow copy and assignment.
- ScopedLocalFrame(const ScopedLocalFrame&);
- void operator=(const ScopedLocalFrame&);
+ DISALLOW_COPY_AND_ASSIGN(ScopedLocalFrame);
};
#endif // SCOPED_LOCAL_FRAME_H_included
diff --git a/include/nativehelper/ScopedLocalRef.h b/include/nativehelper/ScopedLocalRef.h
index 71d5776..6e30eb3 100644
--- a/include/nativehelper/ScopedLocalRef.h
+++ b/include/nativehelper/ScopedLocalRef.h
@@ -20,6 +20,7 @@
#include "jni.h"
#include <stddef.h>
+#include "JNIHelp.h" // for DISALLOW_COPY_AND_ASSIGN.
// A smart pointer that deletes a JNI local reference when it goes out of scope.
template<typename T>
@@ -52,12 +53,10 @@
}
private:
- JNIEnv* mEnv;
+ JNIEnv* const mEnv;
T mLocalRef;
- // Disallow copy and assignment.
- ScopedLocalRef(const ScopedLocalRef&);
- void operator=(const ScopedLocalRef&);
+ DISALLOW_COPY_AND_ASSIGN(ScopedLocalRef);
};
#endif // SCOPED_LOCAL_REF_H_included
diff --git a/include/nativehelper/ScopedPrimitiveArray.h b/include/nativehelper/ScopedPrimitiveArray.h
index d797b9d..b573bd6 100644
--- a/include/nativehelper/ScopedPrimitiveArray.h
+++ b/include/nativehelper/ScopedPrimitiveArray.h
@@ -50,11 +50,10 @@
const PRIMITIVE_TYPE& operator[](size_t n) const { return mRawArray[n]; } \
size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \
private: \
- JNIEnv* mEnv; \
+ JNIEnv* const mEnv; \
PRIMITIVE_TYPE ## Array mJavaArray; \
PRIMITIVE_TYPE* mRawArray; \
- Scoped ## NAME ## ArrayRO(const Scoped ## NAME ## ArrayRO&); \
- void operator=(const Scoped ## NAME ## ArrayRO&); \
+ DISALLOW_COPY_AND_ASSIGN(Scoped ## NAME ## ArrayRO); \
}
INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jboolean, Boolean);
@@ -101,11 +100,10 @@
PRIMITIVE_TYPE& operator[](size_t n) { return mRawArray[n]; } \
size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \
private: \
- JNIEnv* mEnv; \
+ JNIEnv* const mEnv; \
PRIMITIVE_TYPE ## Array mJavaArray; \
PRIMITIVE_TYPE* mRawArray; \
- Scoped ## NAME ## ArrayRW(const Scoped ## NAME ## ArrayRW&); \
- void operator=(const Scoped ## NAME ## ArrayRW&); \
+ DISALLOW_COPY_AND_ASSIGN(Scoped ## NAME ## ArrayRW); \
}
INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jboolean, Boolean);
diff --git a/include/nativehelper/ScopedStringChars.h b/include/nativehelper/ScopedStringChars.h
index cfbd247..688291d 100644
--- a/include/nativehelper/ScopedStringChars.h
+++ b/include/nativehelper/ScopedStringChars.h
@@ -61,14 +61,12 @@
}
private:
- JNIEnv* env_;
- jstring string_;
+ JNIEnv* const env_;
+ const jstring string_;
const jchar* chars_;
size_t size_;
- // Disallow copy and assignment.
- ScopedStringChars(const ScopedStringChars&);
- void operator=(const ScopedStringChars&);
+ DISALLOW_COPY_AND_ASSIGN(ScopedStringChars);
};
#endif // SCOPED_STRING_CHARS_H_included
diff --git a/include/nativehelper/ScopedUtfChars.h b/include/nativehelper/ScopedUtfChars.h
index 7761450..9cfa9a0 100644
--- a/include/nativehelper/ScopedUtfChars.h
+++ b/include/nativehelper/ScopedUtfChars.h
@@ -59,13 +59,11 @@
}
private:
- JNIEnv* env_;
- jstring string_;
+ JNIEnv* const env_;
+ const jstring string_;
const char* utf_chars_;
- // Disallow copy and assignment.
- ScopedUtfChars(const ScopedUtfChars&);
- void operator=(const ScopedUtfChars&);
+ DISALLOW_COPY_AND_ASSIGN(ScopedUtfChars);
};
#endif // SCOPED_UTF_CHARS_H_included
diff --git a/include/nativehelper/UniquePtr.h b/include/nativehelper/UniquePtr.h
index 50f75b2..7890d52 100644
--- a/include/nativehelper/UniquePtr.h
+++ b/include/nativehelper/UniquePtr.h
@@ -18,6 +18,7 @@
#define UNIQUE_PTR_H_included
#include <cstdlib> // For NULL.
+#include "JNIHelp.h" // For DISALLOW_COPY_AND_ASSIGN.
// This is a fake declaration of std::swap to avoid including <algorithm>
namespace std {
@@ -98,9 +99,7 @@
template <typename T2> bool operator==(const UniquePtr<T2>& p) const;
template <typename T2> bool operator!=(const UniquePtr<T2>& p) const;
- // Disallow copy and assignment.
- UniquePtr(const UniquePtr&);
- void operator=(const UniquePtr&);
+ DISALLOW_COPY_AND_ASSIGN(UniquePtr);
};
// Partial specialization for array types. Like std::unique_ptr, this removes
@@ -136,9 +135,7 @@
private:
T* mPtr;
- // Disallow copy and assignment.
- UniquePtr(const UniquePtr&);
- void operator=(const UniquePtr&);
+ DISALLOW_COPY_AND_ASSIGN(UniquePtr);
};
#if UNIQUE_PTR_TESTS