| 1 | /***************************************************************************** |
| 2 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | you may not use this file except in compliance with the License. |
| 4 | You may obtain a copy of the License at |
| 5 | |
| 6 | http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | |
| 8 | Unless required by applicable law or agreed to in writing, software |
| 9 | distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | See the License for the specific language governing permissions and |
| 12 | limitations under the License. |
| 13 | |
| 14 | See NOTICE file for details. |
| 15 | *****************************************************************************/ |
| 16 | #ifndef _JPVALUE_H_ |
| 17 | #define _JPVALUE_H_ |
| 18 | |
| 19 | #include "jp_class.h" |
| 20 | #include "jp_javaframe.h" |
| 21 | |
| 22 | /** Lightweight representative of a jvalue with its corresponding class. |
| 23 | * This should not have any memory management. The user of the class |
| 24 | * must take a global reference if needed. |
| 25 | */ |
| 26 | class JPValue |
| 27 | { |
| 28 | public: |
| 29 | |
| 30 | JPValue() |
| 31 | { |
| 32 | m_Value.l = nullptr; |
| 33 | } |
| 34 | |
| 35 | JPValue(JPClass* clazz, const jvalue& value) |
| 36 | : m_Class(clazz), m_Value(value) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | JPValue(JPClass* clazz, jobject value) |
| 41 | : m_Class(clazz) |
| 42 | { |
| 43 | m_Value.l = value; |
| 44 | } |
| 45 | |
| 46 | ~JPValue() = default; |
| 47 | |
| 48 | JPClass* getClass() const |
| 49 | { |
| 50 | return m_Class; |
| 51 | } |
| 52 | |
| 53 | jvalue& getValue() |
| 54 | { |
| 55 | return m_Value; |
| 56 | } |
| 57 | |
| 58 | const jvalue& getValue() const |
| 59 | { |
| 60 | return m_Value; |
| 61 | } |
| 62 | |
| 63 | jobject getJavaObject() const; |
| 64 | |
| 65 | // Cast operators to jvalue. |
| 66 | // TODO: these could be explicit too, right? |
| 67 | operator jvalue&() |
| 68 | { |
| 69 | return m_Value; |
| 70 | } |
| 71 | |
| 72 | operator const jvalue&() const |
| 73 | { |
| 74 | return m_Value; |
| 75 | } |
| 76 | |
| 77 | // TODO: never used. |
| 78 | JPValue& global(JPJavaFrame& frame) |
| 79 | { |
| 80 | m_Value.l = frame.NewGlobalRef(m_Value.l); |
| 81 | return *this; |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | JPClass* m_Class{}; |
| 86 | jvalue m_Value{}; |
| 87 | } ; |
| 88 | |
| 89 | #endif // _JPVALUE_H_ |