Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
boolean.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_BOOLEAN_HPP
19#define LIVE_CELLS_BOOLEAN_HPP
20
21#include "observable.hpp"
22#include "constant_cell.hpp"
23#include "computed.hpp"
24#include "store_cell.hpp"
25
26namespace live_cells {
39 template <typename A, typename B>
40 auto operator && (const A &a, const B &b) requires (
43 ) {
44 return make_compute_cell([=] () {
45 return ensure_cell(a).value() && ensure_cell(b).value();
46 }, ensure_cell(a), ensure_cell(b));
47 }
48
61 template <typename A, typename B>
62 auto operator || (const A &a, const B &b) requires (
65 ) {
66 return make_compute_cell([=] () {
67 return a.value() || b.value();
68 }, ensure_cell(a), ensure_cell(b));
69 }
70
79 auto operator ! (const Cell auto &cell) {
80 return computed(cell, [] (auto value) {
81 return !value;
82 });
83 }
84
102 auto select(Cell auto condition, Cell auto if_true, Cell auto if_false) {
103 return make_compute_cell([=] () {
104 return condition.value() ? if_true.value() : if_false.value();
105 }, condition, if_true, if_false);
106 }
107
122 auto select(Cell auto condition, Cell auto if_true) {
123 return store(
124 make_compute_cell([=] () {
125 if (!condition.value()) {
126 none();
127 }
128
129 return if_true.value();
130 }, condition, if_true)
131 );
132 }
133
134 namespace ops {
135
148 auto select(const Cell auto &if_true) {
149 return [&] (const Cell auto &cond) {
150 return live_cells::select(cond, if_true);
151 };
152 }
153
169 auto select(const Cell auto &if_true, const Cell auto &if_false) {
170 return [&] (const Cell auto &cond) {
171 return live_cells::select(cond, if_true, if_false);
172 };
173 }
174
175 } // ops
176
177} // live_cells
178
179#endif /* LIVE_CELLS_BOOLEAN_HPP */
Dynamically typed Cell container.
Definition observable.hpp:133
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
auto select(const Cell auto &if_true)
Create an Operator for conditionally selecting the value of another cell based on the value of the op...
Definition boolean.hpp:148
Definition boolean.hpp:26
auto operator!(const Cell auto &cell)
Create a cell that evaluates to the logical not of the value of a cell.
Definition boolean.hpp:79
constant_cell< T > value(const T &value)
Definition constant_cell.hpp:132
auto store(const C &arg)
Create a Cell that caches the value of another Cell.
Definition store_cell.hpp:206
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 select(Cell auto condition, Cell auto if_true, Cell auto if_false)
Create a cell which selects between the values of two cells based on the value of a condition cell.
Definition boolean.hpp:102
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)
Creates a Cell that evaluates to the logical and of the values of cells a and b.
Definition boolean.hpp:40
auto operator||(const A &a, const B &b)
Create a cell that evaluates to the logical or of the values of cells a and b.
Definition boolean.hpp:62
auto make_compute_cell(std::invocable auto f, auto... args)
Definition compute_cell.hpp:100