Skip to content

Commit 84c2c5f

Browse files
committed
Added BufferCount operator
1 parent e48e6b8 commit 84c2c5f

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/***************************************************
2+
Copyright (c) 2019 Luis Llamas
3+
(www.luisllamas.es)
4+
5+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License
8+
****************************************************/
9+
10+
#ifndef _REACTIVEOPERATORBUFFERCOUNT_h
11+
#define _REACTIVEOPERATORBUFFERCOUNT_h
12+
13+
template <typename T>
14+
class OperatorBufferCount: public Operator<T, T[]>
15+
{
16+
public:
17+
OperatorBufferCount(size_t N);
18+
19+
void OnNext(T value) override;
20+
21+
void Reset() override;
22+
private:
23+
std::vector<T> _buffer;
24+
size_t _num_elements = 0;
25+
};
26+
27+
template <typename T>
28+
OperatorBufferCount<T>::OperatorBufferCount(size_t N)
29+
{
30+
_num_elements = N;
31+
}
32+
33+
template <typename T>
34+
void OperatorBufferCount<T>::Reset()
35+
{
36+
_buffer.clear();
37+
}
38+
39+
template <typename T>
40+
void OperatorBufferCount<T>::OnNext(T value)
41+
{
42+
_buffer.push_back(value);
43+
if (_buffer.size() >= _num_elements)
44+
{
45+
this->_childObservers.OnNext(_buffer.data());
46+
_buffer.clear();
47+
}
48+
}
49+
50+
#endif

src/Operators/Operators.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Unless required by applicable law or agreed to in writing, software distributed
2020
#include "OperatorTake.h"
2121
#include "OperatorSkip.h"
2222
#include "OperatorBatch.h"
23+
#include "OperatorBufferCount.h"
2324

2425
#include "OperatorTakeAt.h"
2526
#include "OperatorTakeFirst.h"

src/ReactiveArduinoCore.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ template <typename T> class OperatorFirst;
5656
template <typename T> class OperatorTake;
5757
template <typename T> class OperatorSkip;
5858
template <typename T> class OperatorBatch;
59+
template <typename T> class OperatorBufferCount;
5960
template <typename T> class OperatorTakeAt;
6061
template <typename T> class OperatorTakeFirst;
6162
template <typename T> class OperatorTakeLast;
@@ -168,6 +169,7 @@ class Observable : IObservable<T>, IResetable<T>
168169
OperatorTakeUntil<T>& TakeUntil(ReactivePredicate<T> condition);
169170
OperatorTakeWhile<T>& TakeWhile(ReactivePredicate<T> condition);
170171
OperatorBatch<T>& Batch(size_t num);
172+
OperatorBufferCount<T>& BufferCount(size_t num);
171173
OperatorIf<T>& If(ReactivePredicate<T> condition, ReactiveAction<T> action);
172174
OperatorForEach<T>& ForEach(ReactiveAction<T> action);
173175
OperatorTimeoutMillis<T>& TimeoutMillis(ReactiveAction<T> action);
@@ -378,6 +380,14 @@ auto Observable<T>::Batch(size_t num) -> OperatorBatch<T>&
378380
return *newOp;
379381
}
380382

383+
template <typename T>
384+
auto Observable<T>::BufferCount(size_t num) -> OperatorBufferCount<T>&
385+
{
386+
auto newOp = new OperatorBufferCount<T>(num);
387+
Compound(*this, *newOp);
388+
return *newOp;
389+
}
390+
381391
template <typename T>
382392
auto Observable<T>::If(ReactivePredicate<T> condition, ReactiveAction<T> action) -> OperatorIf<T>&
383393
{

src/ReactiveArduinoLib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Unless required by applicable law or agreed to in writing, software distributed
1919
#endif
2020

2121
#include <set>
22+
#include <vector>
2223

2324
namespace Reactive
2425
{

0 commit comments

Comments
 (0)