C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
local_ref.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include "../compat.hpp"
26#include "common_types.hpp"
27
28#include <concepts>
29#include <type_traits>
30#include <utility>
31
32#include <jni.h>
33
34namespace essence::jni {
35 class local_ref {
36 public:
37 ES_API(JNISUPPORT) local_ref() noexcept;
38 ES_API(JNISUPPORT) explicit local_ref(jobject obj);
39 ES_API(JNISUPPORT) local_ref(jobject obj, bool take_over_only);
40 ES_API(JNISUPPORT) local_ref(const local_ref& other);
41 ES_API(JNISUPPORT) local_ref(local_ref&& other) noexcept;
42 ES_API(JNISUPPORT) ~local_ref();
43 ES_API(JNISUPPORT) local_ref& operator=(const local_ref& right);
44 ES_API(JNISUPPORT) local_ref& operator=(local_ref&& right) noexcept;
45 ES_API(JNISUPPORT) explicit operator bool() const noexcept;
46 [[nodiscard]] ES_API(JNISUPPORT) jobject get() const noexcept;
47 ES_API(JNISUPPORT) jobject detach() noexcept;
48
49 private:
50 jobject ref_;
51 };
52
53 template <typename T>
54 concept local_ref_like = std::convertible_to<std::decay_t<T>, local_ref>;
55
56 template <derived_from_jobject JObject>
58 public:
59 local_ref_ex() noexcept = default;
60
61 explicit local_ref_ex(JObject obj) : ref_{obj} {}
62
63 local_ref_ex(JObject obj, bool take_over_only) : ref_{obj, take_over_only} {}
64
65 local_ref_ex(const local_ref_ex& other) = default;
66
67 local_ref_ex(local_ref_ex&& other) noexcept : ref_{std::move(other.ref_)} {}
68
69 local_ref_ex& operator=(const local_ref_ex& right) = default;
70
71 local_ref_ex& operator=(local_ref_ex&& right) noexcept {
72 ref_ = std::move(right.ref_);
73
74 return *this;
75 }
76
77 explicit operator bool() const noexcept {
78 return ref_.operator bool();
79 }
80
81 operator const local_ref&() const noexcept { // NOLINT(*-explicit-constructor)
82 return ref_;
83 }
84
85 operator local_ref&() noexcept { // NOLINT(*-explicit-constructor)
86 return ref_;
87 }
88
89 JObject get() const noexcept {
90 return static_cast<JObject>(ref_.get());
91 }
92
93 JObject detach() noexcept {
94 return static_cast<JObject>(ref_.detach());
95 }
96
97 private:
98 local_ref ref_;
99 };
100} // namespace essence::jni
Definition local_ref.hpp:57
Definition local_ref.hpp:35
Definition local_ref.hpp:54