source: rrlib_jni/tJavaObjectReference.h @ 24:d13dfecf897b

Last change on this file since 24:d13dfecf897b was 20:c4f1bedb7be9, checked in by Tobias Föhst <foehst@…>, 7 years ago

Added and updated license information

File size: 4.6 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/jni/tJavaObjectReference.h
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2013-05-07
27 *
28 * \brief   Contains tJavaObjectReference
29 *
30 * \b tJavaObjectReference
31 *
32 * Maintains a global reference to the specified java object.
33 * It releases reference upon destruction or reassignment.
34 *
35 */
36//----------------------------------------------------------------------
37#ifndef __rrlib__jni__tJavaObjectReference_h__
38#define __rrlib__jni__tJavaObjectReference_h__
39
40//----------------------------------------------------------------------
41// External includes (system with <>, local with "")
42//----------------------------------------------------------------------
43
44//----------------------------------------------------------------------
45// Internal includes with ""
46//----------------------------------------------------------------------
47#include "rrlib/jni/jvm.h"
48
49//----------------------------------------------------------------------
50// Namespace declaration
51//----------------------------------------------------------------------
52namespace rrlib
53{
54namespace jni
55{
56
57//----------------------------------------------------------------------
58// Forward declarations / typedefs / enums
59//----------------------------------------------------------------------
60
61//----------------------------------------------------------------------
62// Class declaration
63//----------------------------------------------------------------------
64//! Java object reference
65/*!
66 * Maintains a global reference to the specified java object (in a JVM).
67 * It releases reference upon destruction or reassignment.
68 */
69class tJavaObjectReference
70{
71
72//----------------------------------------------------------------------
73// Public methods and typedefs
74//----------------------------------------------------------------------
75public:
76
77  tJavaObjectReference() :
78    java_object(NULL),
79    cpp_responsible(false)
80  {}
81
82  /*! \param java_object_ Java object to hold initially */
83  tJavaObjectReference(jobject java_object_, bool cpp_responsible_) :
84    java_object(NULL),
85    cpp_responsible(false)
86  {
87    Set(java_object_, cpp_responsible_);
88  }
89
90  ~tJavaObjectReference()
91  {
92    Reset();
93  }
94
95  /*!
96   * Hold global reference to specified java object. Releases global reference to any old object.
97   * AND local reference to specified object
98   *
99   * \param java_object_ Java object to hold reference to
100   */
101  void Set(jobject java_object_, bool cpp_responsible_)
102  {
103    Reset();
104    cpp_responsible = cpp_responsible_;
105    if (cpp_responsible_)
106    {
107      java_object = GetEnv()->NewGlobalRef(java_object_);
108    }
109    else
110    {
111      java_object = GetEnv()->NewWeakGlobalRef(java_object_);
112    }
113    GetEnv()->DeleteLocalRef(java_object_);
114  }
115
116  /*!
117   * Get current java object reference
118   */
119  jobject Get()
120  {
121    return java_object;
122  }
123
124//----------------------------------------------------------------------
125// Private fields and methods
126//----------------------------------------------------------------------
127private:
128
129  /*! Java Object currently "locked" */
130  jobject java_object;
131
132  /*! Is C++ responsible for cleaning up this object and "holding" Java object? */
133  bool cpp_responsible;
134
135
136  tJavaObjectReference(const tJavaObjectReference&) = delete;
137  tJavaObjectReference& operator=(const tJavaObjectReference&) = delete;
138
139  /*!  Releases global reference to any old object. */
140  void Reset()
141  {
142    if (java_object != NULL)
143    {
144      if (cpp_responsible)
145      {
146        GetEnv()->DeleteGlobalRef(java_object);
147      }
148      else
149      {
150        GetEnv()->DeleteWeakGlobalRef(java_object);
151      }
152      java_object = NULL;
153    }
154  }
155};
156
157//----------------------------------------------------------------------
158// End of namespace declaration
159//----------------------------------------------------------------------
160}
161}
162
163
164#endif
Note: See TracBrowser for help on using the repository browser.