1 /*
2  * Copyright 2014 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef FRUIT_NORMALIZED_COMPONENT_STORAGE_HOLDER_H
18 #define FRUIT_NORMALIZED_COMPONENT_STORAGE_HOLDER_H
19 
20 #include <fruit/fruit_forward_decls.h>
21 #include <fruit/impl/data_structures/arena_allocator.h>
22 #include <fruit/impl/data_structures/memory_pool.h>
23 #include <fruit/impl/fruit_internal_forward_decls.h>
24 #include <memory>
25 
26 namespace fruit {
27 namespace impl {
28 
29 /**
30  * A wrapper around NormalizedComponentStorage, holding the NormalizedComponentStorage
31  * through a unique_ptr so that we don't need to include NormalizedComponentStorage in
32  * fruit.h.
33  */
34 class NormalizedComponentStorageHolder {
35 private:
36   std::unique_ptr<NormalizedComponentStorage> storage;
37 
38   friend class InjectorStorage;
39 
40   template <typename... P>
41   friend class fruit::Injector;
42 
43 public:
44   // These are just used as tags to select the desired constructor.
45   struct WithUndoableCompression {};
46   struct WithPermanentCompression {};
47 
48   NormalizedComponentStorageHolder() noexcept = default;
49 
50 
51   /**
52    * The MemoryPool is only used during construction, the constructed object *can* outlive the memory pool.
53    */
54   NormalizedComponentStorageHolder(ComponentStorage&& component,
55                                    const std::vector<TypeId, ArenaAllocator<TypeId>>& exposed_types,
56                                    MemoryPool& memory_pool, WithUndoableCompression);
57 
58   NormalizedComponentStorageHolder(NormalizedComponentStorage&&) = delete;
59   NormalizedComponentStorageHolder(const NormalizedComponentStorage&) = delete;
60 
61   NormalizedComponentStorageHolder& operator=(NormalizedComponentStorageHolder&&) = delete;
62   NormalizedComponentStorageHolder& operator=(const NormalizedComponentStorageHolder&) = delete;
63 
64   // We don't use the default destructor because that would require the inclusion of
65   // normalized_component_storage.h. We define this in the cpp file instead.
66   ~NormalizedComponentStorageHolder() noexcept;
67 };
68 
69 } // namespace impl
70 } // namespace fruit
71 
72 #endif // FRUIT_NORMALIZED_COMPONENT_STORAGE_HOLDER_H
73