Skip to content

aui::small_pimpl#

Small buffer optimized PIMPL implementation.

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

Detailed Description#

small_pimpl behaves similarly to std::unique_ptr but stores the implementation object inline when its size is less than or equal to StackSize. When the object is larger, a heap allocation is used.

The class provides a pointer‑like interface to the underlying implementation, supporting construction, copy/move semantics, and destruction. It is useful for the pImpl idiom where the concrete type may be small enough to avoid dynamic allocation.

It is is a good compromise for most small objects and matches the size used by boost::function or std::any.

T must implement interface Interface.

If small_pimpl is holding a non-move-constructible object, it will attempt to use copy constructor instead.

If small_pimpl is holding a non-copy-constructible object, small_pimpl will throw an exception in runtime on a copy attempt.

Public Methods#

constructor#


explicit small_pimpl::small_pimpl(T t)

Construct from a concrete type.

If T is neither copy-constructible nor move-constructible, you can use inplace constructor of small_pimpl only. In such a scenario, you are likely to get a runtime error when attempting to copy/move a small_pimpl.


explicit small_pimpl::small_pimpl(std::in_place_type_t<T >, Args&& . . . args)

In place construct from concrete type.

ptr#


Interface* small_pimpl::ptr()

Get the stored pointer to interface.

Examples:

examples/7guis/cells/src/AST.cpp

7GUIs Cells — Spreadsheet processor (Excel).

                if (o.priority < currentPriority && o.op->right) {
                    // steal rhs
                    auto currentOperator = std::make_unique<T>();
                    currentOperator->left = std::move(o.op->right);
                    auto ptr = currentOperator.get();
                    o.op->right = std::move(currentOperator);
                    binaryOperators << BinaryOperatorAndItsPriority {
                        .op = ptr,
                        .priority = currentPriority,
                    };
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;