1 // Copyright 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "osp/impl/receiver_list.h" 6 7 #include <algorithm> 8 9 namespace openscreen { 10 namespace osp { 11 12 ReceiverList::ReceiverList() = default; 13 ReceiverList::~ReceiverList() = default; 14 OnReceiverAdded(const ServiceInfo & info)15void ReceiverList::OnReceiverAdded(const ServiceInfo& info) { 16 receivers_.emplace_back(info); 17 } 18 OnReceiverChanged(const ServiceInfo & info)19Error ReceiverList::OnReceiverChanged(const ServiceInfo& info) { 20 auto existing_info = std::find_if(receivers_.begin(), receivers_.end(), 21 [&info](const ServiceInfo& x) { 22 return x.service_id == info.service_id; 23 }); 24 if (existing_info == receivers_.end()) 25 return Error::Code::kItemNotFound; 26 27 *existing_info = info; 28 return Error::None(); 29 } 30 OnReceiverRemoved(const ServiceInfo & info)31Error ReceiverList::OnReceiverRemoved(const ServiceInfo& info) { 32 const auto it = std::remove(receivers_.begin(), receivers_.end(), info); 33 if (it == receivers_.end()) 34 return Error::Code::kItemNotFound; 35 36 receivers_.erase(it, receivers_.end()); 37 return Error::None(); 38 } 39 OnAllReceiversRemoved()40Error ReceiverList::OnAllReceiversRemoved() { 41 const auto empty = receivers_.empty(); 42 receivers_.clear(); 43 return empty ? Error::Code::kItemNotFound : Error::None(); 44 } 45 46 } // namespace osp 47 } // namespace openscreen 48