GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
java_any.hpp
Go to the documentation of this file.
1 /**
2  * Copyright (c) 2009 Carnegie Mellon University.
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an "AS
13  * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14  * express or implied. See the License for the specific language
15  * governing permissions and limitations under the License.
16  *
17  * For more about this software visit:
18  *
19  * http://www.graphlab.ml.cmu.edu
20  *
21  */
22 
23 /**
24  * @file java_any.hpp
25  * @author Jiunn Haur Lim <[email protected]>
26  */
27 
28 #ifndef JAVA_ANY_HPP
29 #define JAVA_ANY_HPP
30 
31 #include <jni.h>
32 
33 namespace graphlab {
34 
35  /**
36  * Generic wrapper for Java objects (jobject). It creates a NewGlobalRef on
37  * the jobject in the default constructor, and deletes the GlobalRef in the
38  * destructor. An assignment operator is also provided to deal with creating
39  * and deleting edges. Subclasses should provide a copy constructor, and there
40  * are two scenarios: NewGlobalRef during copy, or object clone during copy.
41  */
42  class java_any {
43 
44  private:
45 
46  /** Java object */
47  jobject mobj;
48 
49  public:
50 
51  /**
52  * Constructor for java_any.
53  * Initializes this object with the associated Java object.
54  * @param[in] env JNI environment - used to create a new reference
55  * to obj
56  * @param[in] obj Java object. This constructor will create a new
57  * reference to the object to prevent garbage
58  * collection.
59  */
60  java_any(JNIEnv *env, jobject &obj);
61 
62  /** The default constructor does nothing. mobj is initialized to NULL. */
63  java_any();
64 
65  /**
66  * Copy constructor
67  * If `other` has a reference to a java object, increases the reference count.
68  * Child classes may wish to override this to implement clone behavior.
69  */
70  java_any(const java_any& other);
71 
72  /**
73  * Copy assignment operator for java_any.
74  * If \c other has a \c mobj, creates a new reference to it.
75  */
76  java_any &operator=(const java_any &other);
77 
78  /**
79  * Retrieves the associated Java object
80  */
81  jobject &obj();
82  const jobject &obj() const;
83 
84  /**
85  * Deletes the reference to the Java object so that it may be garbage
86  * collected.
87  */
88  ~java_any();
89 
90  protected:
91 
92  /**
93  * Deletes the current ref (if any) and creates a new ref if `obj` is not null.
94  * @param[in] obj replaces current object ref
95  */
96  void set_obj(jobject obj);
97 
98  /**
99  * Checks for and rethrows Java exceptions.
100  * @param[in] env JNI environment
101  * @return true if exception was found; false otherwise
102  */
103  bool handle_exception(JNIEnv *env) const;
104 
105  };
106 
107 };
108 
109 #endif