blob: 174b41f20d5e9e7dbdf810c9db0a109a0bbb7934 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018#include "link/Linkers.h"
19
20#include <algorithm>
21#include <iterator>
22
23namespace aapt {
24
25template <typename InputContainer, typename OutputIterator, typename Predicate>
26OutputIterator moveIf(InputContainer& inputContainer, OutputIterator result,
27 Predicate pred) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028 const auto last = inputContainer.end();
29 auto newEnd =
30 std::find_if(inputContainer.begin(), inputContainer.end(), pred);
31 if (newEnd == last) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032 return result;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 *result = std::move(*newEnd);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 auto first = newEnd;
38 ++first;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 for (; first != last; ++first) {
41 if (bool(pred(*first))) {
42 // We want to move this guy
43 *result = std::move(*first);
44 ++result;
45 } else {
46 // We want to keep this guy, but we will need to move it up the list to
47 // replace
48 // missing items.
49 *newEnd = std::move(*first);
50 ++newEnd;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 }
53
54 inputContainer.erase(newEnd, last);
55 return result;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056}
57
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058bool PrivateAttributeMover::consume(IAaptContext* context,
59 ResourceTable* table) {
60 for (auto& package : table->packages) {
61 ResourceTableType* type = package->findType(ResourceType::kAttr);
62 if (!type) {
63 continue;
64 }
65
66 if (type->symbolStatus.state != SymbolState::kPublic) {
67 // No public attributes, so we can safely leave these private attributes
68 // where they are.
69 return true;
70 }
71
72 ResourceTableType* privAttrType =
73 package->findOrCreateType(ResourceType::kAttrPrivate);
74 assert(privAttrType->entries.empty());
75
76 moveIf(type->entries, std::back_inserter(privAttrType->entries),
77 [](const std::unique_ptr<ResourceEntry>& entry) -> bool {
78 return entry->symbolStatus.state != SymbolState::kPublic;
79 });
80 break;
81 }
82 return true;
83}
84
85} // namespace aapt