1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SCROLLABLE_SCROLLABLE_ITEM_POOL_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SCROLLABLE_SCROLLABLE_ITEM_POOL_H
18 
19 #include <functional>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "base/memory/referenced.h"
24 
25 namespace OHOS::Ace::NG {
26 class ScrollableItem;
27 class Pattern;
28 using PatternCreator = std::function<RefPtr<Pattern>(void)>;
29 class ScrollableItemPool {
30 public:
31     static ScrollableItemPool& GetInstance();
32     RefPtr<ScrollableItem> Allocate(const std::string& tag, int32_t nodeId, const PatternCreator& patternCreator);
33 
34     void Deallocate(ScrollableItem* obj);
35 
36 private:
ScrollableItemPool(size_t size)37     explicit ScrollableItemPool(size_t size) : size_(size) {}
38     std::unordered_map<std::string, std::vector<ScrollableItem*>> pool_;
39     size_t size_;
40 };
41 
42 template<class T>
43 struct ObjectPoolDeleter {
44     ScrollableItemPool* allocator = nullptr;
45 
operatorObjectPoolDeleter46     void operator()(T* obj) const
47     {
48         if (allocator) {
49             allocator->Deallocate(obj);
50         }
51     }
52 };
53 } // namespace OHOS::Ace::NG
54 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SCROLLABLE_SCROLLABLE_ITEM_POOL_H
55