1 /*
2 * Copyright (C) 2015 The Android Open Source Project
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 #include "ResourceTable.h"
18 #include "link/Linkers.h"
19
20 #include <algorithm>
21 #include <iterator>
22
23 namespace aapt {
24
25 template <typename InputContainer, typename OutputIterator, typename Predicate>
moveIf(InputContainer & inputContainer,OutputIterator result,Predicate pred)26 OutputIterator moveIf(InputContainer& inputContainer, OutputIterator result,
27 Predicate pred) {
28 const auto last = inputContainer.end();
29 auto newEnd = std::find_if(inputContainer.begin(), inputContainer.end(), pred);
30 if (newEnd == last) {
31 return result;
32 }
33
34 *result = std::move(*newEnd);
35
36 auto first = newEnd;
37 ++first;
38
39 for (; first != last; ++first) {
40 if (bool(pred(*first))) {
41 // We want to move this guy
42 *result = std::move(*first);
43 ++result;
44 } else {
45 // We want to keep this guy, but we will need to move it up the list to replace
46 // missing items.
47 *newEnd = std::move(*first);
48 ++newEnd;
49 }
50 }
51
52 inputContainer.erase(newEnd, last);
53 return result;
54 }
55
consume(IAaptContext * context,ResourceTable * table)56 bool PrivateAttributeMover::consume(IAaptContext* context, ResourceTable* table) {
57 for (auto& package : table->packages) {
58 ResourceTableType* type = package->findType(ResourceType::kAttr);
59 if (!type) {
60 continue;
61 }
62
63 if (type->symbolStatus.state != SymbolState::kPublic) {
64 // No public attributes, so we can safely leave these private attributes where they are.
65 return true;
66 }
67
68 ResourceTableType* privAttrType = package->findOrCreateType(ResourceType::kAttrPrivate);
69 assert(privAttrType->entries.empty());
70
71 moveIf(type->entries, std::back_inserter(privAttrType->entries),
72 [](const std::unique_ptr<ResourceEntry>& entry) -> bool {
73 return entry->symbolStatus.state != SymbolState::kPublic;
74 });
75 break;
76 }
77 return true;
78 }
79
80 } // namespace aapt
81