• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_COMPONENT_STORAGE_DEFN_H
18 #define FRUIT_COMPONENT_STORAGE_DEFN_H
19 
20 #include <fruit/impl/component_storage/component_storage.h>
21 #include <fruit/impl/component_storage/component_storage_entry.h>
22 
23 namespace fruit {
24 namespace impl {
25 
ComponentStorage(FixedSizeVector<ComponentStorageEntry> && entries)26 inline ComponentStorage::ComponentStorage(FixedSizeVector<ComponentStorageEntry>&& entries) noexcept
27     : entries(std::move(entries)) {}
28 
ComponentStorage(const ComponentStorage & other)29 inline ComponentStorage::ComponentStorage(const ComponentStorage& other) {
30   *this = other;
31 }
32 
ComponentStorage(ComponentStorage && other)33 inline ComponentStorage::ComponentStorage(ComponentStorage&& other) noexcept {
34   *this = std::move(other);
35 }
36 
destroy()37 inline void ComponentStorage::destroy() {
38   for (ComponentStorageEntry& entry : entries) {
39     entry.destroy();
40   }
41   entries.clear();
42 }
43 
~ComponentStorage()44 inline ComponentStorage::~ComponentStorage() {
45   destroy();
46 }
47 
release()48 inline FixedSizeVector<ComponentStorageEntry> ComponentStorage::release() && {
49   return std::move(entries);
50 }
51 
numEntries()52 inline std::size_t ComponentStorage::numEntries() const {
53   return entries.size();
54 }
55 
56 inline ComponentStorage& ComponentStorage::operator=(const ComponentStorage& other) {
57   destroy();
58 
59   entries = FixedSizeVector<ComponentStorageEntry>(other.entries.size());
60   for (const ComponentStorageEntry& entry : other.entries) {
61     entries.push_back(entry.copy());
62   }
63 
64   return *this;
65 }
66 
67 inline ComponentStorage& ComponentStorage::operator=(ComponentStorage&& other) noexcept {
68   entries = std::move(other.entries);
69 
70   // We don't want other to have any entries after this operation because we might otherwise end up destroying those
71   // ComponentStorageEntry objects twice.
72   other.entries.clear();
73 
74   return *this;
75 }
76 
77 } // namespace impl
78 } // namespace fruit
79 
80 #endif // FRUIT_COMPONENT_STORAGE_DEFN_H
81