source: rrlib_jni/tJNIWrappable.cpp @ 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.5 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/tJNIWrappable.cpp
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2013-05-07
27 *
28 */
29//----------------------------------------------------------------------
30#include "rrlib/jni/tJNIWrappable.h"
31
32//----------------------------------------------------------------------
33// External includes (system with <>, local with "")
34//----------------------------------------------------------------------
35#include "rrlib/thread/tLock.h"
36
37//----------------------------------------------------------------------
38// Internal includes with ""
39//----------------------------------------------------------------------
40
41//----------------------------------------------------------------------
42// Debugging
43//----------------------------------------------------------------------
44#include <cassert>
45
46//----------------------------------------------------------------------
47// Namespace usage
48//----------------------------------------------------------------------
49
50//----------------------------------------------------------------------
51// Namespace declaration
52//----------------------------------------------------------------------
53namespace rrlib
54{
55namespace jni
56{
57
58//----------------------------------------------------------------------
59// Forward declarations / typedefs / enums
60//----------------------------------------------------------------------
61
62//----------------------------------------------------------------------
63// Const values
64//----------------------------------------------------------------------
65
66//----------------------------------------------------------------------
67// Implementation
68//----------------------------------------------------------------------
69
70/*! Mutex for creating Java objects - non-static would be faster at creation, but would consume more memory */
71rrlib::thread::tMutex tJNIWrappable::create_mutex;
72
73tJNIWrappable::tJNIWrappable() :
74  java_wrapper_object()
75{}
76
77tJNIWrappable::~tJNIWrappable()
78{}
79
80jobject tJNIWrappable::CreateJavaWrapper()
81{
82  rrlib::thread::tLock l(create_mutex); // avoid that two threads create wrapper object at the same time
83  jobject obj = java_wrapper_object.Get();
84  if (obj != NULL)   // "double-checked locking" - should be safe though
85  {
86    return obj;
87  }
88  JNIEnv* env = GetEnv();
89  __attribute__((unused)) // prevents warning in release mode
90  const char* class_name = GetJavaClassName();
91  assert(class_name != NULL && "Cannot create java class without class name - your class needs to override getJavaClassName()");
92  jclass clazz = env->FindClass(GetJavaClassName());
93  assert(clazz != NULL && "Class not found");
94
95  /* Get the method ID for the clazz(long) constructor */
96  jmethodID cid = env->GetMethodID(clazz, "<init>", "(L)V");
97  assert(cid != NULL && "No Constructor taking long argument (pointer) found for Java class");
98
99  obj = env->NewObject(clazz, cid, (jlong)this);
100  assert(obj != NULL && "Java object creation failed for whatever reason");
101  java_wrapper_object.Set(obj, true);
102  env->DeleteLocalRef(clazz);
103  return obj;
104}
105
106void tJNIWrappable::SetJavaWrapper(jobject obj, bool cpp_responsible_)
107{
108  rrlib::thread::tLock l(create_mutex); // avoid that two threads create wrapper object at the same time
109  if (java_wrapper_object.Get() == obj)
110  {
111    return;
112  }
113  assert((java_wrapper_object.Get() == NULL || obj == NULL) && "Java Object already set to a different instance");
114  java_wrapper_object.Set(obj, cpp_responsible_);
115}
116
117const char* tJNIWrappable::GetJavaClassName() const
118{
119  return NULL;
120}
121
122
123//----------------------------------------------------------------------
124// End of namespace declaration
125//----------------------------------------------------------------------
126}
127}
Note: See TracBrowser for help on using the repository browser.