1 // Copyright 2021 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 15 #pragma once 16 17 #include <cstring> 18 19 #include "pw_bytes/span.h" 20 #include "pw_log_rpc/log_filter.h" 21 #include "pw_result/result.h" 22 #include "pw_span/span.h" 23 24 namespace pw::log_rpc { 25 26 // Holds an inmutable Filter map, ordered by filter ID to facilitate set up. 27 class FilterMap { 28 public: FilterMap(span<Filter> filters)29 explicit constexpr FilterMap(span<Filter> filters) : filters_(filters) {} 30 31 // Not copyable nor movable. 32 FilterMap(FilterMap const&) = delete; 33 FilterMap& operator=(FilterMap const&) = delete; 34 FilterMap(FilterMap&&) = delete; 35 FilterMap& operator=(FilterMap&&) = delete; 36 GetFilterFromId(ConstByteSpan id)37 Result<Filter*> GetFilterFromId(ConstByteSpan id) const { 38 for (auto& filter : filters_) { 39 if (id.size() == filter.id().size() && 40 std::memcmp(id.data(), filter.id().data(), id.size()) == 0) { 41 return &filter; 42 } 43 } 44 return Status::NotFound(); 45 } 46 filters()47 const span<Filter>& filters() const { return filters_; } 48 49 protected: 50 const span<Filter> filters_; 51 }; 52 53 } // namespace pw::log_rpc 54