// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. #pragma once #if !defined(RXCPP_RX_GROUPED_OBSERVABLE_HPP) #define RXCPP_RX_GROUPED_OBSERVABLE_HPP #include "rx-includes.hpp" namespace rxcpp { namespace detail { template struct has_on_get_key_for { struct not_void {}; template static auto check(int) -> decltype((*(CS*)nullptr).on_get_key()); template static not_void check(...); typedef decltype(check(0)) detail_result; static const bool value = std::is_same>::value; }; } template class dynamic_grouped_observable : public dynamic_observable { public: typedef rxu::decay_t key_type; typedef tag_dynamic_grouped_observable dynamic_observable_tag; private: struct state_type : public std::enable_shared_from_this { typedef std::function ongetkey_type; ongetkey_type on_get_key; }; std::shared_ptr state; template friend bool operator==(const dynamic_grouped_observable&, const dynamic_grouped_observable&); template void construct(const dynamic_grouped_observable& o, const tag_dynamic_grouped_observable&) { state = o.state; } template void construct(dynamic_grouped_observable&& o, const tag_dynamic_grouped_observable&) { state = std::move(o.state); } template void construct(SO&& source, const rxs::tag_source&) { auto so = std::make_shared>(std::forward(source)); state->on_get_key = [so]() mutable { return so->on_get_key(); }; } public: dynamic_grouped_observable() { } template explicit dynamic_grouped_observable(SOF sof) : dynamic_observable(sof) , state(std::make_shared()) { construct(std::move(sof), typename std::conditional::value, tag_dynamic_grouped_observable, rxs::tag_source>::type()); } template dynamic_grouped_observable(SF&& sf, CF&& cf) : dynamic_observable(std::forward(sf)) , state(std::make_shared()) { state->on_connect = std::forward(cf); } using dynamic_observable::on_subscribe; key_type on_get_key() const { return state->on_get_key(); } }; template inline bool operator==(const dynamic_grouped_observable& lhs, const dynamic_grouped_observable& rhs) { return lhs.state == rhs.state; } template inline bool operator!=(const dynamic_grouped_observable& lhs, const dynamic_grouped_observable& rhs) { return !(lhs == rhs); } template grouped_observable make_dynamic_grouped_observable(Source&& s) { return grouped_observable(dynamic_grouped_observable(std::forward(s))); } /*! \brief a source of observables which each emit values from one category specified by the key selector. \ingroup group-observable */ template class grouped_observable : public observable { typedef grouped_observable this_type; typedef observable base_type; typedef rxu::decay_t source_operator_type; static_assert(detail::has_on_get_key_for::value, "inner must have on_get_key method key_type()"); public: typedef rxu::decay_t key_type; typedef tag_grouped_observable observable_tag; grouped_observable() { } explicit grouped_observable(const SourceOperator& o) : base_type(o) { } explicit grouped_observable(SourceOperator&& o) : base_type(std::move(o)) { } // implicit conversion between observables of the same value_type template grouped_observable(const grouped_observable& o) : base_type(o) {} // implicit conversion between observables of the same value_type template grouped_observable(grouped_observable&& o) : base_type(std::move(o)) {} /// /// performs type-forgetting conversion to a new grouped_observable /// grouped_observable as_dynamic() const { return *this; } key_type get_key() const { return base_type::source_operator.on_get_key(); } }; } // // support range() >> filter() >> subscribe() syntax // '>>' is spelled 'stream' // template auto operator >> (const rxcpp::grouped_observable& source, OperatorFactory&& of) -> decltype(source.op(std::forward(of))) { return source.op(std::forward(of)); } // // support range() | filter() | subscribe() syntax // '|' is spelled 'pipe' // template auto operator | (const rxcpp::grouped_observable& source, OperatorFactory&& of) -> decltype(source.op(std::forward(of))) { return source.op(std::forward(of)); } #endif