1 /* 2 * Copyright (c) 2022-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://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, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_PUBLISHER_ITEM_H 17 #define OHOS_PUBLISHER_ITEM_H 18 #include <mutex> 19 #include <string> 20 #include <set> 21 22 #include "refbase.h" 23 24 #include "ipublisher_listener.h" 25 #include "single_instance.h" 26 27 namespace OHOS { 28 namespace DistributedHardware { 29 struct ListenerCompare { 30 public: operatorListenerCompare31 bool operator () (const sptr<IPublisherListener> originalListener, const sptr<IPublisherListener> currentListener) 32 { 33 return originalListener->AsObject().GetRefPtr() < currentListener->AsObject().GetRefPtr(); 34 } 35 }; 36 class PublisherItem { 37 REMOVE_NO_USE_CONSTRUCTOR(PublisherItem); 38 public: 39 PublisherItem(); 40 explicit PublisherItem(DHTopic topic); 41 virtual ~PublisherItem(); 42 void AddListener(const sptr<IPublisherListener> listener); 43 void RemoveListener(const sptr<IPublisherListener> listener); 44 void PublishMessage(const std::string &message); 45 private: 46 DHTopic topic_; 47 std::mutex mutex_; 48 std::set<sptr<IPublisherListener>, ListenerCompare> listeners_; 49 }; 50 } // namespace DistributedHardware 51 } // namespace OHOS 52 #endif // OHOS_PUBLISHER_ITEM_H