diff --git a/gutil/collections.h b/gutil/collections.h index cceff703..66479f4f 100644 --- a/gutil/collections.h +++ b/gutil/collections.h @@ -54,10 +54,24 @@ absl::StatusOr FindOrStatus(const M &m, } } -// Returns a non-null pointer of the value associated with a given key +// Returns a const, non-null pointer of the value associated with a given key // if it exists, or a status failure if it does not. template absl::StatusOr FindPtrOrStatus( + const M &m, const KeyType &k) { + auto it = m.find(k); + if (it != m.end()) return &it->second; + if constexpr (std::is_same_v) { + return absl::NotFoundError(absl::StrCat("Key not found: '", k, "'")); + } else { + return absl::NotFoundError("Key not found"); + } +} + +// Returns a mutable, non-null pointer of the value associated with a given key +// if it exists, or a status failure if it does not. +template +absl::StatusOr FindMutablePtrOrStatus( M &m, const KeyType &k) { auto it = m.find(k); if (it != m.end()) return &it->second; diff --git a/sai_p4/instantiations/google/tests/p4_constraints_integration_test.cc b/sai_p4/instantiations/google/tests/p4_constraints_integration_test.cc index 433a60e1..3894b116 100644 --- a/sai_p4/instantiations/google/tests/p4_constraints_integration_test.cc +++ b/sai_p4/instantiations/google/tests/p4_constraints_integration_test.cc @@ -1,3 +1,19 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + #include "gmock/gmock.h" #include "gtest/gtest.h" #include "gutil/proto.h"