2 * Copyright 2020 Google LLC
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/sksl/SkSLBuiltinMap.h"
10 #include "include/core/SkTypes.h"
11 #include "include/private/SkSLProgramElement.h"
17 void BuiltinMap::insertOrDie(std::string key, std::unique_ptr<ProgramElement> element) {
18 SkASSERT(!fElements.find(key));
19 fElements.set(std::move(key), BuiltinElement{std::move(element), /*fAlreadyIncluded=*/false});
22 const ProgramElement* BuiltinMap::find(const std::string& key) {
23 BuiltinElement* elem = fElements.find(key);
25 return fParent ? fParent->find(key) : nullptr;
27 return elem->fElement.get();
30 // Only returns a builtin element that isn't already marked as included, and then marks it.
31 const ProgramElement* BuiltinMap::findAndInclude(const std::string& key) {
32 BuiltinElement* elem = fElements.find(key);
34 return fParent ? fParent->findAndInclude(key) : nullptr;
36 if (elem->fAlreadyIncluded) {
39 elem->fAlreadyIncluded = true;
40 return elem->fElement.get();
43 void BuiltinMap::resetAlreadyIncluded() {
44 fElements.foreach([](const std::string&, BuiltinElement* elem) {
45 elem->fAlreadyIncluded = false;
48 fParent->resetAlreadyIncluded();
52 void BuiltinMap::foreach(
53 const std::function<void(const std::string&, const ProgramElement&)>& fn) const {
54 fElements.foreach([&](const std::string& name, const BuiltinElement& elem) {
55 fn(name, *elem.fElement);