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
+ class opengeode_basic_api Database
42
+ {
43
+ OPENGEODE_DISABLE_COPY ( Database );
44
+ struct Storage ;
45
+
46
+ public:
47
+ using serializer_function = std::function< void ( PContext& ) >;
48
+
49
+ class opengeode_basic_api Data
50
+ {
51
+ public:
52
+ Data ( std::shared_ptr< Storage > storage );
53
+ ~Data ();
54
+ Data ( Data&& other );
55
+
56
+ template < typename DataType >
57
+ const DataType& get ()
58
+ {
59
+ const auto & typed_data =
60
+ dynamic_cast < const DataType& >( data () );
61
+ return typed_data;
62
+ }
63
+
64
+ private:
65
+ const Identifier& data () const ;
66
+
67
+ private:
68
+ IMPLEMENTATION_MEMBER ( impl_ );
69
+ };
70
+
71
+ public:
72
+ Database ( absl::string_view directory );
73
+ ~Database ();
74
+
75
+ index_t nb_data () const ;
76
+
77
+ template < typename DataType >
78
+ const uuid& register_data ( DataType&& data )
79
+ {
80
+ static_assert ( std::is_base_of< Identifier, DataType >::value,
81
+ " [Database::register_data] Data is not a subclass of "
82
+ " Identifier" );
83
+ return register_unique_data (
84
+ absl::make_unique< DataType >( std::move ( data ) ) );
85
+ }
86
+
87
+ template < typename DataType >
88
+ const uuid& register_data ( std::unique_ptr< DataType >&& data )
89
+ {
90
+ static_assert ( std::is_base_of< Identifier, DataType >::value,
91
+ " [Database::register_data] Data is not a subclass of "
92
+ " Identifier" );
93
+ return register_unique_data ( std::move ( data ) );
94
+ }
95
+
96
+ void delete_data ( const uuid& id );
97
+
98
+ Data get_data ( const uuid& id ) const ;
99
+
100
+ std::unique_ptr< Identifier > take_data ( const uuid& id );
101
+
102
+ void register_serializer_functions (
103
+ serializer_function serializer, serializer_function deserializer );
104
+
105
+ private:
106
+ const uuid& register_unique_data (
107
+ std::unique_ptr< Identifier >&& data );
108
+
109
+ private:
110
+ IMPLEMENTATION_MEMBER ( impl_ );
111
+ };
112
+ } // namespace geode
0 commit comments