Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
numeric.hpp
1/*
2 * live_cells_cpp
3 * Copyright (C) 2024 Alexander Gutev <alex.gutev@gmail.com>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you
6 * may not use this file except in compliance with the License. You
7 * may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 */
17
18#ifndef LIVE_CELLS_NUMERIC_HPP
19#define LIVE_CELLS_NUMERIC_HPP
20
21#include "constant_cell.hpp"
22#include "observable.hpp"
23#include "computed.hpp"
24
25namespace live_cells {
35 template <typename A, typename B>
36 auto operator +(const A &a, const B &b) requires (
39 ) {
40 return computed(ensure_cell(a), ensure_cell(b), [] (auto a, auto b) {
41 return a + b;
42 });
43 }
44
54 template <typename A, typename B>
55 auto operator -(const A &a, const B &b) requires (
58 ) {
59 return computed(ensure_cell(a), ensure_cell(b), [] (auto a, auto b) {
60 return a - b;
61 });
62 }
63
72 auto operator -(const Cell auto &cell) {
73 return computed(cell, [] (auto value) {
74 return -value;
75 });
76 }
77
87 template <typename A, typename B>
88 auto operator *(const A &a, const B &b) requires (
91 ) {
92 return computed(ensure_cell(a), ensure_cell(b), [] (auto a, auto b) {
93 return a * b;
94 });
95 }
96
106 template <typename A, typename B>
107 auto operator /(const A &a, const B &b) requires (
108 (Cell<A> && CellOrValue<B>) ||
110 ) {
111 return computed(ensure_cell(a), ensure_cell(b), [] (auto a, auto b) {
112 return a / b;
113 });
114 }
115
125 template <typename A, typename B>
126 auto operator %(const A &a, const B &b) requires (
127 (Cell<A> && CellOrValue<B>) ||
129 ) {
130 return computed(ensure_cell(a), ensure_cell(b), [] (auto a, auto b) {
131 return a % b;
132 });
133 }
134
144 template <typename A, typename B>
145 auto operator <(const A &a, const B &b) requires (
146 (Cell<A> && CellOrValue<B>) ||
148 ) {
149 return computed(ensure_cell(a), ensure_cell(b), [] (auto a, auto b) {
150 return a < b;
151 });
152 }
153
164 template <typename A, typename B>
165 auto operator <=(const A &a, const B &b) requires (
166 (Cell<A> && CellOrValue<B>) ||
168 ) {
169 return computed(ensure_cell(a), ensure_cell(b), [] (auto a, auto b) {
170 return a <= b;
171 });
172 }
173
183 template <typename A, typename B>
184 auto operator >(const A &a, const B &b) requires (
185 (Cell<A> && CellOrValue<B>) ||
187 ) {
188 return computed(ensure_cell(a), ensure_cell(b), [] (auto a, auto b) {
189 return a > b;
190 });
191 }
192
203 template <typename A, typename B>
204 auto operator >=(const A &a, const B &b) requires (
205 (Cell<A> && CellOrValue<B>) ||
207 ) {
208 return computed(ensure_cell(a), ensure_cell(b), [] (auto a, auto b) {
209 return a >= b;
210 });
211 }
212
213} // live_cells
214
215#endif /* LIVE_CELLS_NUMERIC_HPP */
Dynamically typed Cell container.
Definition observable.hpp:133
A computed cell which determines its argument cells at runtime.
Definition dynamic_compute_cell.hpp:153
Concept specifying that T is either a Cell or a value that can be converted to a constant_cell.
Definition constant_cell.hpp:141
Defines the cell protocol.
Definition types.hpp:128
Definition boolean.hpp:26
auto operator-(const A &a, const B &b)
Create a Cell that computes the difference (using the - operator) of cells and .
Definition numeric.hpp:55
auto operator+(const A &a, const B &b)
Create a Cell that computes the sum (using the + operator) of cells and .
Definition numeric.hpp:36
auto operator*(const A &a, const B &b)
Create a Cell that computes the product (using the .
Definition numeric.hpp:88
constant_cell< T > value(const T &value)
Definition constant_cell.hpp:132
auto computed(F &&compute)
Create a cell with a value that is a function of one or more argument cells.
Definition computed.hpp:45
auto operator%(const A &a, const B &b)
Create a Cell that computes the remainder (using the % operator) of cell divided by .
Definition numeric.hpp:126
auto operator<=(const A &a, const B &b)
Create a Cell that is true if the value of cell is less than or equal to (by the <= operator) the val...
Definition numeric.hpp:165
auto operator>(const A &a, const B &b)
Create a Cell that is true if the value of cell is greater than (by the > operator) the value of cell...
Definition numeric.hpp:184
auto operator>=(const A &a, const B &b)
Create a Cell that is true if the value of cell is greater than or equal to (by the >= operator) the ...
Definition numeric.hpp:204
auto ensure_cell(const Cell auto &thing)
Ensure that thing is a Cell.
Definition constant_cell.hpp:155
auto operator<(const A &a, const B &b)
Create a Cell that is true if the value of cell is less than (by the < operator) the value of cell .
Definition numeric.hpp:145
auto operator/(const A &a, const B &b)
Create a Cell that computes the quotient (using the / operator) of cells and .
Definition numeric.hpp:107