Observable.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <folly/experimental/observer/Observer.h>
  18. namespace folly {
  19. namespace observer {
  20. namespace detail {
  21. template <typename Observable, typename Traits>
  22. class ObserverCreatorContext;
  23. }
  24. template <typename Observable>
  25. struct ObservableTraits {
  26. using element_type =
  27. typename std::remove_reference<Observable>::type::element_type;
  28. static std::shared_ptr<const element_type> get(Observable& observable) {
  29. return observable.get();
  30. }
  31. template <typename F>
  32. static void subscribe(Observable& observable, F&& callback) {
  33. observable.subscribe(std::forward<F>(callback));
  34. }
  35. static void unsubscribe(Observable& observable) {
  36. observable.unsubscribe();
  37. }
  38. };
  39. template <typename Observable, typename Traits = ObservableTraits<Observable>>
  40. class ObserverCreator {
  41. public:
  42. using T = typename Traits::element_type;
  43. template <typename... Args>
  44. explicit ObserverCreator(Args&&... args);
  45. Observer<T> getObserver() &&;
  46. private:
  47. using Context = detail::ObserverCreatorContext<Observable, Traits>;
  48. std::shared_ptr<Context> context_;
  49. };
  50. } // namespace observer
  51. } // namespace folly
  52. #include <folly/experimental/observer/Observable-inl.h>