MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
polyglot_collectable.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024, 2025, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU General Public License, version 2.0, for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#ifndef MYSQLSHDK_SCRIPTING_POLYGLOT_NATIVE_WRAPPERS_POLYGLOT_COLLECTABLE_H_
27#define MYSQLSHDK_SCRIPTING_POLYGLOT_NATIVE_WRAPPERS_POLYGLOT_COLLECTABLE_H_
28
29#include <functional>
30#include <memory>
31#include <mutex>
32#include <unordered_set>
33#include <utility>
34#include <vector>
35
36namespace shcore {
37namespace polyglot {
38
39enum class Collectable_type {
40 OBJECT,
42 METHOD,
43 ARRAY,
44 MAP,
46};
47
48class Polyglot_language;
49class Collectable_registry;
50
51/**
52 * Base collectable interface to be able to determine the type of a collectable
53 * object without with no need to cast it.
54 */
56 public:
58 std::weak_ptr<Polyglot_language> language);
59 virtual ~ICollectable() = default;
60 Collectable_type type() const { return m_type; }
61 std::shared_ptr<Polyglot_language> language() const;
63
64 private:
66 std::weak_ptr<Polyglot_language> m_language;
68};
69
70/**
71 * Represents a data object to be associated to a Polyglot wrapper for C++
72 * elements. It contains the necessary to:
73 *
74 * - Identify the target object type
75 * - Identify the target object itself
76 * - Context information to perform Polyglot related operations
77 */
78template <typename T, Collectable_type t>
79class Collectable : public ICollectable {
80 public:
81 Collectable(const std::shared_ptr<T> &d,
82 std::weak_ptr<Polyglot_language> language)
83 : ICollectable(t, std::move(language)), m_data(d) {}
84
85 Collectable(const Collectable &) = delete;
87
88 Collectable &operator=(const Collectable &) = delete;
90
91 ~Collectable() override = default;
92
93 const std::shared_ptr<T> &data() const { return m_data; }
94
95 private:
96 std::shared_ptr<T> m_data;
97};
98
99/**
100 * When a Polyglot wrapper for a C++ object is created, a collectable instance
101 * is created to be passed to the Polyglot object (Java). The memory associated
102 * to the collectable must be released when it is no longer needed in the Java
103 * side.
104 *
105 * Each collectable will be registered here and marked to be deleted under the
106 * following scenarios:
107 *
108 * - When the Java Garbage Collector marks the object as a phantom reference.
109 * - When the Java context is being finalized
110 *
111 * The memory is released after calling clean() method.
112 */
114 public:
116
119
122
124
125 void add(ICollectable *target);
126 void remove(ICollectable *target);
127
128 /**
129 * Deletes all collectables which were marked as to be deleted.
130 */
131 void clean();
132
133 private:
134 void clean_unsafe();
135
136 std::mutex m_cleanup_mutex;
137 std::unordered_set<ICollectable *> m_live_collectables;
138 std::vector<ICollectable *> m_phantom_collectables;
139};
140
141} // namespace polyglot
142} // namespace shcore
143
144#endif // MYSQLSHDK_SCRIPTING_POLYGLOT_NATIVE_WRAPPERS_POLYGLOT_COLLECTABLE_H_
When a Polyglot wrapper for a C++ object is created, a collectable instance is created to be passed t...
Definition: polyglot_collectable.h:113
void clean()
Deletes all collectables which were marked as to be deleted.
Definition: polyglot_collectable.cc:83
void remove(ICollectable *target)
Definition: polyglot_collectable.cc:73
Collectable_registry(Collectable_registry &&)=delete
std::vector< ICollectable * > m_phantom_collectables
Definition: polyglot_collectable.h:138
Collectable_registry & operator=(const Collectable_registry &)=delete
~Collectable_registry()
Definition: polyglot_collectable.cc:60
Collectable_registry & operator=(Collectable_registry &&)=delete
void add(ICollectable *target)
Definition: polyglot_collectable.cc:68
std::unordered_set< ICollectable * > m_live_collectables
Definition: polyglot_collectable.h:137
Collectable_registry(const Collectable_registry &)=delete
std::mutex m_cleanup_mutex
Definition: polyglot_collectable.h:136
void clean_unsafe()
Definition: polyglot_collectable.cc:88
Represents a data object to be associated to a Polyglot wrapper for C++ elements.
Definition: polyglot_collectable.h:79
Collectable(Collectable &&)=delete
Collectable & operator=(Collectable &&)=delete
Collectable(const std::shared_ptr< T > &d, std::weak_ptr< Polyglot_language > language)
Definition: polyglot_collectable.h:81
const std::shared_ptr< T > & data() const
Definition: polyglot_collectable.h:93
std::shared_ptr< T > m_data
Definition: polyglot_collectable.h:96
~Collectable() override=default
Collectable(const Collectable &)=delete
Collectable & operator=(const Collectable &)=delete
Base collectable interface to be able to determine the type of a collectable object without with no n...
Definition: polyglot_collectable.h:55
virtual ~ICollectable()=default
std::weak_ptr< Polyglot_language > m_language
Definition: polyglot_collectable.h:66
std::shared_ptr< Polyglot_language > language() const
Definition: polyglot_collectable.cc:54
Collectable_registry * registry() const
Definition: polyglot_collectable.cc:58
Collectable_type m_type
Definition: polyglot_collectable.h:65
Collectable_type type() const
Definition: polyglot_collectable.h:60
ICollectable(Collectable_type type, std::weak_ptr< Polyglot_language > language)
Definition: polyglot_collectable.cc:42
Collectable_registry * m_registry
Definition: polyglot_collectable.h:67
Collectable_type
Definition: polyglot_collectable.h:39
Definition: file_system_exceptions.h:34
Definition: gcs_xcom_synode.h:64