1 /*
2  * Copyright (c) 2024 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 #include "io/proxy_directory.h"
17 
18 #include <cstdint>
19 #include <unordered_set>
20 #include <utility>
21 
22 #include <base/containers/iterator.h>
23 #include <base/containers/string.h>
24 #include <base/containers/type_traits.h>
25 #include <base/containers/unique_ptr.h>
26 #include <base/containers/vector.h>
27 #include <base/namespace.h>
28 #include <base/util/compile_time_hashes.h>
29 #include <core/io/intf_directory.h>
30 #include <core/namespace.h>
31 
BASE_BEGIN_NAMESPACE()32 BASE_BEGIN_NAMESPACE()
33 template<>
34 uint64_t hash(const CORE_NS::IDirectory::Entry::Type& val)
35 {
36     return static_cast<uint64_t>(val);
37 }
38 BASE_END_NAMESPACE()
39 
40 template<>
41 struct std::hash<CORE_NS::IDirectory::Entry> {
42     using argument_type = CORE_NS::IDirectory::Entry;
43     using result_type = uint64_t;
operator ()std::hash44     result_type operator()(argument_type const& s) const noexcept
45     {
46         return BASE_NS::Hash(s.type, s.name);
47     }
48 };
49 
50 CORE_BEGIN_NAMESPACE()
51 using BASE_NS::move;
52 using BASE_NS::vector;
53 
operator ==(const IDirectory::Entry & lhs,const IDirectory::Entry & rhs)54 bool operator==(const IDirectory::Entry& lhs, const IDirectory::Entry& rhs)
55 {
56     return lhs.type == rhs.type && lhs.name == rhs.name;
57 }
58 
ProxyDirectory(vector<IDirectory::Ptr> && directories)59 ProxyDirectory::ProxyDirectory(vector<IDirectory::Ptr>&& directories) : directories_(move(directories)) {}
60 
Close()61 void ProxyDirectory::Close() {}
62 
GetEntries() const63 vector<IDirectory::Entry> ProxyDirectory::GetEntries() const
64 {
65     vector<IDirectory::Entry> result;
66     std::unordered_set<IDirectory::Entry> entries;
67     for (auto const& dir : directories_) {
68         for (auto&& entry : dir->GetEntries()) {
69             entries.insert(entry);
70         }
71     }
72     result.reserve(entries.size());
73     // std::unordered_set cannot really move unless the hash type and value type are the same. Should playaround with
74     // BASE_NS::unordered_map to see if it would suite better.
75     std::move(entries.begin(), entries.end(), std::back_inserter(result));
76     return result;
77 }
78 CORE_END_NAMESPACE()
79