1
+ /*
2
+ * Copyright (c) 2019 - 2022 Geode-solutions
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 THE
20
+ * SOFTWARE.
21
+ *
22
+ */
23
+
24
+ #pragma once
25
+
26
+ #include < geode/basic/common.h>
27
+ #include < geode/basic/logger.h>
28
+ #include < geode/basic/pimpl.h>
29
+
30
+ #include < geode/basic/identifier.h>
31
+ #include < geode/basic/uuid.h>
32
+
33
+ namespace geode
34
+ {
35
+ class Identifier ;
36
+ struct uuid ;
37
+ } // namespace geode
38
+
39
+ namespace geode
40
+ {
41
+ /* !
42
+ * Stores any classes inherited from Identifier. It owns every data
43
+ * registered. Data is also stored on disk and offload from the memory after
44
+ * some unused time to save memory and performances.
45
+ */
46
+ class opengeode_basic_api Database
47
+ {
48
+ OPENGEODE_DISABLE_COPY ( Database );
49
+ struct Storage ;
50
+
51
+ public:
52
+ using serializer_function = std::function< void ( PContext& ) >;
53
+
54
+ /* !
55
+ * Classe holding a const reference of a data.
56
+ * @warning Do not destroy this Data class before the const reference
57
+ * obtained using its get() method is no longer in used
58
+ */
59
+ class opengeode_basic_api Data
60
+ {
61
+ public:
62
+ Data ( std::shared_ptr< Storage > storage );
63
+ ~Data ();
64
+ Data ( Data&& other );
65
+
66
+ template < typename DataType >
67
+ const DataType& get ()
68
+ {
69
+ const auto * typed_data =
70
+ dynamic_cast < const DataType* >( &data () );
71
+ OPENGEODE_EXCEPTION (
72
+ typed_data, " [Data::get] Cannot cast data into DataType" );
73
+ return *typed_data;
74
+ }
75
+
76
+ private:
77
+ const Identifier& data () const ;
78
+
79
+ private:
80
+ IMPLEMENTATION_MEMBER ( impl_ );
81
+ };
82
+
83
+ public:
84
+ Database ( absl::string_view directory );
85
+ ~Database ();
86
+
87
+ index_t nb_data () const ;
88
+
89
+ template < typename DataType >
90
+ const uuid& register_data ( DataType&& data )
91
+ {
92
+ static_assert ( std::is_base_of< Identifier, DataType >::value,
93
+ " [Database::register_data] Data is not a subclass of "
94
+ " Identifier" );
95
+ return register_unique_data (
96
+ absl::make_unique< DataType >( std::move ( data ) ) );
97
+ }
98
+
99
+ template < typename DataType >
100
+ const uuid& register_data ( std::unique_ptr< DataType >&& data )
101
+ {
102
+ static_assert ( std::is_base_of< Identifier, DataType >::value,
103
+ " [Database::register_data] Data is not a subclass of "
104
+ " Identifier" );
105
+ return register_unique_data ( std::move ( data ) );
106
+ }
107
+
108
+ void delete_data ( const uuid& id );
109
+
110
+ /* !
111
+ * Retrieve a read only reference to the data corresponding to the given
112
+ * uuid.
113
+ */
114
+ Data get_data ( const uuid& id ) const ;
115
+
116
+ template < typename DataType >
117
+ std::unique_ptr< DataType > take_data ( const uuid& id )
118
+ {
119
+ get_data ( id ).get < DataType >();
120
+ auto * data =
121
+ dynamic_cast < DataType* >( steal_data ( id ).release () );
122
+ return std::unique_ptr< DataType >{ data };
123
+ }
124
+
125
+ /* !
126
+ * Use this method to register custom serializer functions to allow
127
+ * saving any custom Object on disk
128
+ */
129
+ void register_serializer_functions (
130
+ serializer_function serializer, serializer_function deserializer );
131
+
132
+ private:
133
+ const uuid& register_unique_data (
134
+ std::unique_ptr< Identifier >&& data );
135
+
136
+ std::unique_ptr< Identifier > steal_data ( const uuid& id );
137
+
138
+ private:
139
+ IMPLEMENTATION_MEMBER ( impl_ );
140
+ };
141
+ } // namespace geode
0 commit comments