Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 17 | #include "link/Linkers.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <iterator> |
| 21 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 22 | #include "android-base/logging.h" |
| 23 | |
| 24 | #include "ResourceTable.h" |
| 25 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | namespace aapt { |
| 27 | |
| 28 | template <typename InputContainer, typename OutputIterator, typename Predicate> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 29 | OutputIterator move_if(InputContainer& input_container, OutputIterator result, |
| 30 | Predicate pred) { |
| 31 | const auto last = input_container.end(); |
| 32 | auto new_end = |
| 33 | std::find_if(input_container.begin(), input_container.end(), pred); |
| 34 | if (new_end == last) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 | return result; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 36 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 38 | *result = std::move(*new_end); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 39 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 40 | auto first = new_end; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 41 | ++first; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | for (; first != last; ++first) { |
| 44 | if (bool(pred(*first))) { |
| 45 | // We want to move this guy |
| 46 | *result = std::move(*first); |
| 47 | ++result; |
| 48 | } else { |
| 49 | // We want to keep this guy, but we will need to move it up the list to |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 50 | // replace missing items. |
| 51 | *new_end = std::move(*first); |
| 52 | ++new_end; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 53 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 56 | input_container.erase(new_end, last); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 57 | return result; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 60 | bool PrivateAttributeMover::Consume(IAaptContext* context, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | ResourceTable* table) { |
| 62 | for (auto& package : table->packages) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 63 | ResourceTableType* type = package->FindType(ResourceType::kAttr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | if (!type) { |
| 65 | continue; |
| 66 | } |
| 67 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 68 | if (type->symbol_status.state != SymbolState::kPublic) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | // No public attributes, so we can safely leave these private attributes |
| 70 | // where they are. |
| 71 | return true; |
| 72 | } |
| 73 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 74 | ResourceTableType* priv_attr_type = |
| 75 | package->FindOrCreateType(ResourceType::kAttrPrivate); |
| 76 | CHECK(priv_attr_type->entries.empty()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 78 | move_if(type->entries, std::back_inserter(priv_attr_type->entries), |
| 79 | [](const std::unique_ptr<ResourceEntry>& entry) -> bool { |
| 80 | return entry->symbol_status.state != SymbolState::kPublic; |
| 81 | }); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | break; |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | } // namespace aapt |