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