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/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));
            }
        }
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;

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:

ptr#


constexpr T* AOptional::ptr()

Get pointer to contained value.

Returns
Pointer to contained value

Examples:

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;
examples/7guis/crud/src/main.cpp

7GUIs CRUD — Create/Read/Update/Delete example.

    APropertyPrecomputed<bool> mDeleteEnabled = [this] { return mSelectedUser != nullptr; };
    APropertyPrecomputed<bool> mUpdateEnabled = [this] { return mCreateEnabled && mDeleteEnabled; };

    void createClicked() {
        mUsers.writeScope() << aui::ptr::manage_shared(new User {
          .name = std::exchange(mEditedUser.name, {}), .surname = std::exchange(mEditedUser.surname, {}) });
    }

    void updateClicked() {
        (*mSelectedUser)->name = std::exchange(mEditedUser.name, {});
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/app/notes/src/main.cpp

Notes App — Note taking app that demonstrates usage of AListModel, AProperty, user data saving and loading.

    /// [save]

    /// [newNote]
    void newNote() {
        auto note = aui::ptr::manage_shared(new Note { .title = "Untitled" });
        mNotes.writeScope()->push_back(note);
        mCurrentNote = std::move(note);
    }
    /// [newNote]
examples/ui/infinite_lazy_list/src/main.cpp

Infinite Lazy List — Usage of AUI_DECLARATIVE_FOR to make an infinite lazy list.

            AThread::sleep(500ms); // imitate hard work here

            // aka "loaded" from backend storage of some kind
            auto loadedItems = AVector<_<Item>>::generate(20, [&](size_t i) {
                return aui::ptr::manage_shared(new Item { .value = "Item {}"_format(loadFrom + i) });
            });

            AUI_UI_THREAD { // back to main thread.
                state->items.writeScope()->insertAll(loadedItems);
                state->needMore = false;

constexpr const T* AOptional::ptr()

Get const pointer to contained value.

Returns
Const pointer to contained value

Examples:

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:

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