Ethan Nicholas | 58014fa | 2021-09-29 17:18:18 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020 Google LLC |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "include/private/SkSLProgramElement.h" |
| 9 | #include "include/private/SkSLString.h" |
| 10 | #include "src/sksl/SkSLIntrinsicMap.h" |
| 11 | |
| 12 | namespace SkSL { |
| 13 | |
| 14 | void IntrinsicMap::insertOrDie(String key, std::unique_ptr<ProgramElement> element) { |
| 15 | SkASSERT(fIntrinsics.find(key) == fIntrinsics.end()); |
| 16 | fIntrinsics[key] = Intrinsic{std::move(element), false}; |
| 17 | } |
| 18 | |
| 19 | const ProgramElement* IntrinsicMap::find(const String& key) { |
| 20 | auto iter = fIntrinsics.find(key); |
| 21 | if (iter == fIntrinsics.end()) { |
| 22 | return fParent ? fParent->find(key) : nullptr; |
| 23 | } |
| 24 | return iter->second.fIntrinsic.get(); |
| 25 | } |
| 26 | |
| 27 | // Only returns an intrinsic that isn't already marked as included, and then marks it. |
| 28 | const ProgramElement* IntrinsicMap::findAndInclude(const String& key) { |
| 29 | auto iter = fIntrinsics.find(key); |
| 30 | if (iter == fIntrinsics.end()) { |
| 31 | return fParent ? fParent->findAndInclude(key) : nullptr; |
| 32 | } |
| 33 | if (iter->second.fAlreadyIncluded) { |
| 34 | return nullptr; |
| 35 | } |
| 36 | iter->second.fAlreadyIncluded = true; |
| 37 | return iter->second.fIntrinsic.get(); |
| 38 | } |
| 39 | |
| 40 | void IntrinsicMap::resetAlreadyIncluded() { |
| 41 | for (auto& pair : fIntrinsics) { |
| 42 | pair.second.fAlreadyIncluded = false; |
| 43 | } |
| 44 | if (fParent) { |
| 45 | fParent->resetAlreadyIncluded(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | } // namespace SkSL |