blob: 675b02a7e161df9044ab726ae5b50987fadc9087 [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
Adam Lesinski1ab598f2015-08-14 14:26:04 -070017#include "link/Linkers.h"
18
19#include <algorithm>
20#include <iterator>
21
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "android-base/logging.h"
23
24#include "ResourceTable.h"
25
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026namespace aapt {
27
28template <typename InputContainer, typename OutputIterator, typename Predicate>
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080029OutputIterator move_if(InputContainer& input_container, OutputIterator result, Predicate pred) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 const auto last = input_container.end();
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080031 auto new_end = std::find_if(input_container.begin(), input_container.end(), pred);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 if (new_end == last) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033 return result;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 *result = std::move(*new_end);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 auto first = new_end;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 ++first;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 for (; first != last; ++first) {
42 if (bool(pred(*first))) {
43 // We want to move this guy
44 *result = std::move(*first);
45 ++result;
46 } else {
47 // We want to keep this guy, but we will need to move it up the list to
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 // replace missing items.
49 *new_end = std::move(*first);
50 ++new_end;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 }
53
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 input_container.erase(new_end, last);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 return result;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056}
57
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080058bool PrivateAttributeMover::Consume(IAaptContext* context, ResourceTable* table) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 for (auto& package : table->packages) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 ResourceTableType* type = package->FindType(ResourceType::kAttr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 if (!type) {
62 continue;
63 }
64
Adam Lesinski71be7052017-12-12 16:48:07 -080065 if (type->visibility_level != Visibility::Level::kPublic) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 // No public attributes, so we can safely leave these private attributes
67 // where they are.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080068 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 }
70
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080071 std::vector<std::unique_ptr<ResourceEntry>> private_attr_entries;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080073 move_if(type->entries, std::back_inserter(private_attr_entries),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 [](const std::unique_ptr<ResourceEntry>& entry) -> bool {
Adam Lesinski71be7052017-12-12 16:48:07 -080075 return entry->visibility.level != Visibility::Level::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 });
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080077
78 if (private_attr_entries.empty()) {
79 // No private attributes.
80 continue;
81 }
82
83 ResourceTableType* priv_attr_type = package->FindOrCreateType(ResourceType::kAttrPrivate);
84 CHECK(priv_attr_type->entries.empty());
85 priv_attr_type->entries = std::move(private_attr_entries);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 }
87 return true;
88}
89
90} // namespace aapt