• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #include "rxcpp/rx-trace.hpp"
3 
4 struct trace_calls : rxcpp::trace_noop
5 {
trace_callstrace_calls6     trace_calls()
7         : onnexts(0)
8         , onerrors(0)
9         , oncompleteds(0)
10         , subscribes(0)
11         , lifts(0)
12         , unsubscribes(0)
13         , adds(0)
14         , removes(0)
15         , actions(0)
16         , recurses(0)
17         , schedules(0)
18         , schedulewhens(0)
19     {}
20 
21     template<class Worker>
schedule_returntrace_calls22     inline void schedule_return(const Worker&) {
23         schedules++;
24     }
25 
26     template<class Worker>
schedule_when_returntrace_calls27     inline void schedule_when_return(const Worker&) {
28         schedulewhens++;
29     }
30 
31     template<class Schedulable>
action_returntrace_calls32     inline void action_return(const Schedulable&) {
33         actions++;
34     }
35 
36     template<class Schedulable>
action_recursetrace_calls37     inline void action_recurse(const Schedulable&) {
38         recurses++;
39     }
40 
41     template<class Observable>
subscribe_returntrace_calls42     inline void subscribe_return(const Observable&) {
43         subscribes++;
44     }
45 
46     template<class OperatorSource, class OperatorChain>
lift_returntrace_calls47     inline void lift_return(const OperatorSource&, const OperatorChain&) {
48         lifts++;
49     }
50 
51     template<class SubscriptionState>
unsubscribe_returntrace_calls52     inline void unsubscribe_return(const SubscriptionState&) {
53         unsubscribes++;
54     }
55 
56     template<class SubscriptionState>
subscription_add_returntrace_calls57     inline void subscription_add_return(const SubscriptionState&) {
58         adds++;
59     }
60 
61     template<class SubscriptionState>
subscription_remove_returntrace_calls62     inline void subscription_remove_return(const SubscriptionState&) {
63         removes++;
64     }
65 
66     template<class Observer>
on_next_returntrace_calls67     inline void on_next_return(const Observer&) {
68         onnexts++;
69     }
70 
71     template<class Observer>
on_error_returntrace_calls72     inline void on_error_return(const Observer&) {
73         onerrors++;
74     }
75 
76     template<class Observer>
on_completed_returntrace_calls77     inline void on_completed_return(const Observer&) {
78         oncompleteds++;
79     }
80 
81     int onnexts;
82     int onerrors;
83     int oncompleteds;
84     int subscribes;
85     int lifts;
86     int unsubscribes;
87     int adds;
88     int removes;
89     int actions;
90     int recurses;
91     int schedules;
92     int schedulewhens;
93 };
94 
95 auto rxcpp_trace_activity(rxcpp::trace_tag) -> trace_calls;
96 
97 #include "rxcpp/rx.hpp"
98 // create alias' to simplify code
99 // these are owned by the user so that
100 // conflicts can be managed by the user.
101 namespace rx=rxcpp;
102 namespace rxu=rxcpp::util;
103 
104 // At this time, RxCpp will fail to compile if the contents
105 // of the std namespace are merged into the global namespace
106 // DO NOT USE: 'using namespace std;'
107 
108 #ifdef UNICODE
wmain()109 int wmain()
110 #else
111 int main()
112 #endif
113 {
114     int c = 0;
115 
116     auto triples =
117         rx::observable<>::range(1)
118             .concat_map(
119                 [&c](int z){
120                     return rx::observable<>::range(1, z)
121                         .concat_map(
122                             [=, &c](int x){
123                                 return rx::observable<>::range(x, z)
124                                     .filter([=, &c](int y){++c; return x*x + y*y == z*z;})
125                                     .map([=](int y){return std::make_tuple(x, y, z);})
126                                     // forget type to workaround lambda deduction bug on msvc 2013
127                                     .as_dynamic();},
128                             [](int /*x*/, std::tuple<int,int,int> triplet){return triplet;})
129                         // forget type to workaround lambda deduction bug on msvc 2013
130                         .as_dynamic();},
131                 [](int /*z*/, std::tuple<int,int,int> triplet){return triplet;});
132 
133     int ct = 0;
134 
135     triples
136         .take(100)
137         .subscribe(rxu::apply_to([&ct](int /*x*/,int /*y*/,int /*z*/){
138             ++ct;
139         }));
140 
141     std::cout << "concat_map pythagorian range : " << c << " filtered to, " << ct << " triplets." << std::endl;
142     std::cout << "onnexts: " << rxcpp::trace_activity().onnexts << ", onerrors: " << rxcpp::trace_activity().onerrors << ", oncompleteds: " << rxcpp::trace_activity().oncompleteds << std::endl;
143     std::cout << "subscribes: " << rxcpp::trace_activity().subscribes << ", lifts: " << rxcpp::trace_activity().lifts << std::endl;
144     std::cout << "unsubscribes: " << rxcpp::trace_activity().unsubscribes << ", adds: " << rxcpp::trace_activity().adds << ", removes: " << rxcpp::trace_activity().removes << std::endl;
145     std::cout << "schedules: " << rxcpp::trace_activity().schedules << ", schedulewhens: " << rxcpp::trace_activity().schedulewhens << std::endl;
146     std::cout << "actions: " << rxcpp::trace_activity().actions << ", recurses: " << rxcpp::trace_activity().recurses << std::endl;
147 
148     return 0;
149 }
150