Home | History | Annotate | Download | only in ti-agent
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef ART_TEST_TI_AGENT_SCOPED_LOCAL_REF_H_
     18 #define ART_TEST_TI_AGENT_SCOPED_LOCAL_REF_H_
     19 
     20 #include "jni.h"
     21 
     22 #include <stddef.h>
     23 
     24 #include "android-base/macros.h"
     25 
     26 namespace art {
     27 
     28 template<typename T>
     29 class ScopedLocalRef {
     30  public:
     31   ScopedLocalRef(JNIEnv* env, T localRef) : mEnv(env), mLocalRef(localRef) {
     32   }
     33 
     34   ~ScopedLocalRef() {
     35     reset();
     36   }
     37 
     38   void reset(T ptr = nullptr) {
     39     if (ptr != mLocalRef) {
     40       if (mLocalRef != nullptr) {
     41         mEnv->DeleteLocalRef(mLocalRef);
     42       }
     43       mLocalRef = ptr;
     44     }
     45   }
     46 
     47   T release() WARN_UNUSED {
     48     T localRef = mLocalRef;
     49     mLocalRef = nullptr;
     50     return localRef;
     51   }
     52 
     53   T get() const {
     54     return mLocalRef;
     55   }
     56 
     57  private:
     58   JNIEnv* const mEnv;
     59   T mLocalRef;
     60 
     61   DISALLOW_COPY_AND_ASSIGN(ScopedLocalRef);
     62 };
     63 
     64 }  // namespace art
     65 
     66 #endif  // ART_TEST_TI_AGENT_SCOPED_LOCAL_REF_H_
     67