Skip to content

AOptional#

Utility wrapper implementing the stack-allocated (fast) optional idiom.

Header:#include <AUI/Common/AOptional.h>
CMake:aui_link(my_target PUBLIC aui::core)

Examples#

examples/7guis/flight_booker/src/main.cpp

7GUIs Flight Booker - Flight Booker.

constexpr auto REGEX_DATE = ctre::match<"([0-9]+)\\.([0-9]+)\\.([0-9]{4})">;

struct DateTextFieldState {
    AProperty<AOptional<system_clock::time_point>> parsed;
    ASpinlockMutex userChangesText;
};

auto formatDate(system_clock::time_point date) { return "{0:%d}.{0:%m}.{0:%G}"_format(date); }

Public Methods#

constructor#


explicit constexpr AOptional::AOptional(U&& rhs)

Constructs an optional containing the given value (explicit conversion).

Arguments
rhs
Value to store

constexpr AOptional::AOptional(const AOptional& rhs)

Copy constructor.


constexpr AOptional::AOptional(AOptional&& rhs)

Move constructor.


constexpr AOptional::AOptional(const AOptional<U>& rhs)

Converting copy constructor.


constexpr AOptional::AOptional(AOptional<U >&& rhs)

Converting move constructor.

bool#


constexpr explicit operator AOptional::bool()

Boolean conversion operator.

Returns
true if contains value, false otherwise

Examples:

examples/app/minesweeper/src/CellView.cpp

Minesweeper Game - Minesweeper game implementation driven by ass.

        mCellValueCopy = mCell;
    }
    AView::render(context);

    if (bool(mCell & FieldCell::OPEN)) {
        int count = field_cell::getBombCountAround(mCell);
        if (count) {
            AFontStyle fs;
            fs.size = getHeight() * 6 / 7;
            fs.align = ATextAlign::CENTER;
examples/7guis/cells/src/Tokens.cpp

7GUIs Cells - Spreadsheet processor (Excel).

                        continue;
                    }
                    if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') {
                        t->reverseByte();
                        out << token::Identifier { t->readStringWhile([](char c) -> bool { return std::isalnum(c); }) };
                        continue;
                    }
                    throw AException("UNEXPECTED {}"_format(c));
            }
        }

emplace#


template<typename . . . Args >
constexpr AOptional<T>& AOptional::emplace(Args&& . . . args)

Constructs the contained value in-place.

Arguments
args
Arguments to forward to the constructor
Returns
Reference to this optional

hasValue#


bool AOptional::hasValue()

Checks if the optional contains a value.

Returns
true if contains value, false otherwise

Examples:

examples/7guis/flight_booker/src/main.cpp

7GUIs Flight Booker - Flight Booker.

            std::unique_lock lock(state.userChangesText);
            state.parsed = parseDate(s);
        });
        AObject::connect(
            AUI_REACT(ass::PropertyList { BackgroundSolid(!state->parsed->hasValue() ? AColor::RED : AColor::WHITE) }),
            AUI_SLOT(it)::setCustomStyle);
    };
}

struct State {

ptr#


constexpr T* AOptional::ptr()

Get pointer to contained value.

Returns
Pointer to contained value

Examples:

examples/app/game_of_life/src/main.cpp

Game of Life - Game of Life implementation that uses advanced large dynamic data rendering techniques such as ITexture, AImage to be GPU friendly. The computation is performed in AThreadPool.

                  },
            } AUI_OVERRIDE_STYLE { LayoutSpacing { 4_dp } },
          },
          Centered {
            _new<CellsView>(aui::ptr::fake_shared(&mCells)) AUI_OVERRIDE_STYLE {
                  Expanding(),
                  BackgroundSolid(AColor::BLACK),
                },
          },
        } AUI_OVERRIDE_STYLE { LayoutSpacing { 4_dp } });
examples/7guis/circle_drawer/src/main.cpp

7GUIs Circle Drawer - Undo, redo, dialog control.

                              AUI_REACT(mState.history.nextAction != mState.history.end()), AUI_SLOT(it)::setEnabled);
                      },
                },
              },
              _new<CircleDrawArea>(aui::ptr::fake_shared(&mState)),
            } AUI_OVERRIDE_STYLE { LayoutSpacing { 4_dp } });
    }

private:
    State mState;

constexpr const T* AOptional::ptr()

Get const pointer to contained value.

Returns
Const pointer to contained value

Examples:

examples/app/game_of_life/src/main.cpp

Game of Life - Game of Life implementation that uses advanced large dynamic data rendering techniques such as ITexture, AImage to be GPU friendly. The computation is performed in AThreadPool.

                  },
            } AUI_OVERRIDE_STYLE { LayoutSpacing { 4_dp } },
          },
          Centered {
            _new<CellsView>(aui::ptr::fake_shared(&mCells)) AUI_OVERRIDE_STYLE {
                  Expanding(),
                  BackgroundSolid(AColor::BLACK),
                },
          },
        } AUI_OVERRIDE_STYLE { LayoutSpacing { 4_dp } });
examples/7guis/circle_drawer/src/main.cpp

7GUIs Circle Drawer - Undo, redo, dialog control.

                              AUI_REACT(mState.history.nextAction != mState.history.end()), AUI_SLOT(it)::setEnabled);
                      },
                },
              },
              _new<CircleDrawArea>(aui::ptr::fake_shared(&mState)),
            } AUI_OVERRIDE_STYLE { LayoutSpacing { 4_dp } });
    }

private:
    State mState;

value#


constexpr T& AOptional::value()

Access the contained value.

Returns
Reference to the contained value

Examples:

examples/7guis/cells/tests/FormulaTests.cpp

7GUIs Cells - Spreadsheet processor (Excel).

TEST_F(Cells_Formula, ChangePropagation) {
    mSpreadsheet[{1, 0}].expression = "=A0+1";

    mSpreadsheet[{0, 0}].expression = "228";
    EXPECT_EQ(std::get<double>(mSpreadsheet[{1, 0}].value.value()), 229);

    mSpreadsheet[{0, 0}].expression = "0";
    EXPECT_EQ(std::get<double>(mSpreadsheet[{1, 0}].value.value()), 1);
}
examples/7guis/temperature_converter/src/main.cpp

7GUIs Temperature Converter - Fahrenheit to Celsius and vice versa.

    TemperatureConverterWindow() : AWindow("AUI - 7GUIs - TempConv", 300_dp, 50_dp) {
        setContents(Centered {
          Horizontal {
            myPicker() AUI_LET {
                biConnect(it->value(), mCelsius);
                it->focus();
            },
            Label { "°C" },
            Label { "=" } AUI_OVERRIDE_STYLE { Margin { {}, 16_dp } },
            myPicker() AUI_LET { biConnect(it->value(), mFahrenheit); },

constexpr const T& AOptional::value()

Access the contained value.

Returns
Const reference to the contained value

Examples:

examples/7guis/cells/tests/FormulaTests.cpp

7GUIs Cells - Spreadsheet processor (Excel).

TEST_F(Cells_Formula, ChangePropagation) {
    mSpreadsheet[{1, 0}].expression = "=A0+1";

    mSpreadsheet[{0, 0}].expression = "228";
    EXPECT_EQ(std::get<double>(mSpreadsheet[{1, 0}].value.value()), 229);

    mSpreadsheet[{0, 0}].expression = "0";
    EXPECT_EQ(std::get<double>(mSpreadsheet[{1, 0}].value.value()), 1);
}
examples/7guis/temperature_converter/src/main.cpp

7GUIs Temperature Converter - Fahrenheit to Celsius and vice versa.

    TemperatureConverterWindow() : AWindow("AUI - 7GUIs - TempConv", 300_dp, 50_dp) {
        setContents(Centered {
          Horizontal {
            myPicker() AUI_LET {
                biConnect(it->value(), mCelsius);
                it->focus();
            },
            Label { "°C" },
            Label { "=" } AUI_OVERRIDE_STYLE { Margin { {}, 16_dp } },
            myPicker() AUI_LET { biConnect(it->value(), mFahrenheit); },

valueOr#


template<typename F >
constexpr T AOptional::valueOr(F&& alternative)

value or alternative (either value or callback)

Arguments
alternative

valueOrException#


constexpr T& AOptional::valueOrException(const char* message = "empty optional")

value or exception


constexpr const T& AOptional::valueOrException(const char* message = "empty optional")

value or exception