MPD  0.20.6
Ref.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010-2011 Max Kellermann <max.kellermann@gmail.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20  * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27  * OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef JAVA_REF_HXX
31 #define JAVA_REF_HXX
32 
33 #include "Global.hxx"
34 
35 #include <jni.h>
36 
37 #include <assert.h>
38 
39 namespace Java {
43  template<typename T>
44  class LocalRef {
45  JNIEnv *const env;
46  const T value;
47 
48  public:
52  LocalRef(JNIEnv *_env, T _value):env(_env), value(_value) {
53  assert(env != nullptr);
54  assert(value != nullptr);
55  }
56 
58  env->DeleteLocalRef(value);
59  }
60 
61  LocalRef(const LocalRef &other) = delete;
62  LocalRef &operator=(const LocalRef &other) = delete;
63 
64  T Get() const {
65  return value;
66  }
67 
68  operator T() const {
69  return value;
70  }
71  };
72 
76  template<typename T>
77  class GlobalRef {
78  T value;
79 
80  public:
85  GlobalRef() = default;
86 
87  GlobalRef(JNIEnv *env, T _value):value(_value) {
88  assert(env != nullptr);
89  assert(value != nullptr);
90 
91  value = (T)env->NewGlobalRef(value);
92  }
93 
95  GetEnv()->DeleteGlobalRef(value);
96  }
97 
98  GlobalRef(const GlobalRef &other) = delete;
99  GlobalRef &operator=(const GlobalRef &other) = delete;
100 
105  void Set(JNIEnv *env, T _value) {
106  assert(_value != nullptr);
107 
108  value = (T)env->NewGlobalRef(_value);
109  }
110 
111  T Get() const {
112  return value;
113  }
114 
115  operator T() const {
116  return value;
117  }
118  };
119 
127  template<typename T>
128  class TrivialRef {
129  T value;
130 
131  public:
132  constexpr TrivialRef() {};
133 
134  TrivialRef(const TrivialRef &other) = delete;
135  TrivialRef &operator=(const TrivialRef &other) = delete;
136 
137  bool IsDefined() const {
138  return value != nullptr;
139  }
140 
145  void Set(JNIEnv *env, T _value) {
146  assert(value == nullptr);
147  assert(_value != nullptr);
148 
149  value = (T)env->NewGlobalRef(_value);
150  }
151 
155  void Clear(JNIEnv *env) {
156  assert(value != nullptr);
157 
158  env->DeleteGlobalRef(value);
159  value = nullptr;
160  }
161 
166  void ClearOptional(JNIEnv *env) {
167  if (value != nullptr)
168  Clear(env);
169  }
170 
171  T Get() const {
172  return value;
173  }
174 
175  operator T() const {
176  return value;
177  }
178  };
179 }
180 
181 #endif
void Set(JNIEnv *env, T _value)
Obtain a global reference on the specified object and store it.
Definition: Ref.hxx:145
void ClearOptional(JNIEnv *env)
Release the global reference and clear this object.
Definition: Ref.hxx:166
Hold a global reference on a JNI object.
Definition: Ref.hxx:77
TrivialRef & operator=(const TrivialRef &other)=delete
void Set(JNIEnv *env, T _value)
Sets the object, ignoring the previous value.
Definition: Ref.hxx:105
Container for a global reference to a JNI object that gets initialised and deinitialised explicitly...
Definition: Ref.hxx:128
GlobalRef()=default
Constructs an uninitialized object.
T Get() const
Definition: Ref.hxx:64
bool IsDefined() const
Definition: Ref.hxx:137
T Get() const
Definition: Ref.hxx:111
T Get() const
Definition: Ref.hxx:171
static gcc_pure JNIEnv * GetEnv()
Definition: Global.hxx:50
LocalRef & operator=(const LocalRef &other)=delete
GlobalRef(JNIEnv *env, T _value)
Definition: Ref.hxx:87
void Clear(JNIEnv *env)
Release the global reference and clear this object.
Definition: Ref.hxx:155
Definition: Class.hxx:37
LocalRef(JNIEnv *_env, T _value)
The local reference is obtained by the caller.
Definition: Ref.hxx:52
constexpr TrivialRef()
Definition: Ref.hxx:132
Hold a local reference on a JNI object.
Definition: Ref.hxx:44
GlobalRef & operator=(const GlobalRef &other)=delete