conditional.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "base/optional.h"
  9. #include "base/variant.h"
  10. #include <rpl/map.h>
  11. #include <rpl/producer.h>
  12. namespace rpl {
  13. template <
  14. typename Value,
  15. typename Error,
  16. typename GeneratorTest,
  17. typename GeneratorA,
  18. typename GeneratorB>
  19. inline auto conditional(
  20. rpl::producer<bool, Error, GeneratorTest> &&test,
  21. rpl::producer<Value, Error, GeneratorA> &&a,
  22. rpl::producer<Value, Error, GeneratorB> &&b) {
  23. return rpl::combine(
  24. std::move(test),
  25. std::move(a),
  26. std::move(b)
  27. ) | rpl::map([](bool test, Value &&a, Value &&b) {
  28. return test ? std::move(a) : std::move(b);
  29. });
  30. //struct conditional_state {
  31. // std::optional<Value> a;
  32. // std::optional<Value> b;
  33. // char state = -1;
  34. // int working = 3;
  35. //};
  36. //return rpl::make_producer<Value, Error>([
  37. // test = std::move(test),
  38. // a = std::move(a),
  39. // b = std::move(b)
  40. //](const auto &consumer) mutable {
  41. // auto result = lifetime();
  42. // const auto state = result.make_state<conditional_state>();
  43. // result.add(std::move(test).start())
  44. // return result;
  45. //});
  46. }
  47. } // namespace rpl