| // Copyright 2015 The Chromium OS Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef THERMALD_KEY_VALUE_PUBLISHER_H_ |
| #define THERMALD_KEY_VALUE_PUBLISHER_H_ |
| |
| #include <memory> |
| #include <string> |
| |
| #include "base/callback.h" |
| #include "base/callback_list.h" |
| #include "base/macros.h" |
| |
| namespace thermald { |
| |
| // The KeyValuePublisher provides an interface to set a value identified by a |
| // a key. Subscribers are notified when a value is set. |
| class KeyValuePublisher { |
| public: |
| typedef base::Callback<void(const std::string &, int)> |
| KeyValuePublisherCallback; |
| #if BASE_VER < 860220 |
| typedef base::CallbackList<void(const std::string &, int)>::Subscription |
| Subscription; |
| #endif |
| |
| KeyValuePublisher() {} |
| |
| void Publish(const std::string &key, int value) { |
| subscribers_.Notify(key, value); |
| } |
| |
| // Subscribe to be notified when a value is set. |
| // |
| // Returns a subscription object. The caller must keep a reference to this |
| // object for the time the subscription should be active. The subscription |
| // is canceled when the object is deleted. |
| #if BASE_VER < 860220 |
| std::unique_ptr<Subscription> Subscribe(const KeyValuePublisherCallback &cb) { |
| #else |
| base::CallbackListSubscription Subscribe( |
| const KeyValuePublisherCallback &cb) { |
| #endif |
| return subscribers_.Add(cb); |
| } |
| |
| private: |
| base::CallbackList<void(const std::string &, int)> subscribers_; |
| |
| DISALLOW_COPY_AND_ASSIGN(KeyValuePublisher); |
| }; |
| |
| } // namespace thermald |
| |
| #endif // THERMALD_KEY_VALUE_PUBLISHER_H_ |