Chromium Code Reviews| Index: base/android/auto_jobject.h |
| diff --git a/base/android/auto_jobject.h b/base/android/auto_jobject.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a4f3c38adaed70cae8d28b5bd38420b8dab7eee4 |
| --- /dev/null |
| +++ b/base/android/auto_jobject.h |
| @@ -0,0 +1,43 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef BASE_ANDROID_AUTO_JOBJECT_H_ |
| +#define BASE_ANDROID_AUTO_JOBJECT_H_ |
| + |
| +#include <jni.h> |
| + |
| +namespace base { |
| +namespace android { |
| + |
| +// Holds a local reference to a Java object. The global reference is scoped |
| +// to the lifetime of this object. |
| +class AutoJObject { |
| + public: |
| + // Assumes that obj is a local reference to a Java object and takes ownership |
| + // of this local reference. The local /reference is deleted when this object |
| + // goes out of scope. |
| + AutoJObject(JNIEnv* env, jobject obj); |
| + // Takes a new local reference to the Java object held by other. The local |
| + // reference is deleted when this object goes out of scope. |
| + AutoJObject(const AutoJObject& other); |
| + ~AutoJObject(); |
| + |
| + inline JNIEnv* env() const { return env_; } |
|
brettw
2011/08/02 15:55:39
We wouldn't normally write "inline" here. C++ says
|
| + inline jobject obj() const { return obj_; } |
| + // Releases the local reference to the caller. The caller *must* delete the |
| + // local reference when it is done with it. |
| + jobject Release(); |
| + |
| + private: |
| + AutoJObject(); // Not permitted. |
|
brettw
2011/08/02 15:55:39
If you're returning by value, not permitting the d
|
| + void operator=(const AutoJObject&); // DISALLOW_ASSIGN |
| + |
| + JNIEnv* env_; |
| + jobject obj_; |
| +}; |
| + |
| +} // namespace android |
| +} // namespace base |
| + |
| +#endif // BASE_ANDROID_AUTO_JOBJECT_H_ |