blob: 3c8af4f81ffe63c3d3a713f6d3af789d273d416e [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) {
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
56bool 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
Adam Lesinski9e10ac72015-10-16 14:37:48 -070063 if (type->symbolStatus.state != SymbolState::kPublic) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064 // 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 {
Adam Lesinski9e10ac72015-10-16 14:37:48 -070073 return entry->symbolStatus.state != SymbolState::kPublic;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074 });
75 break;
76 }
77 return true;
78}
79
80} // namespace aapt