~aleteoryx/muditaos

muditaos/module-apps/apps-common/options/Option.hpp -rw-r--r-- 1.3 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#pragma once

#include <string>
#include <utf8/UTF8.hpp>
#include <functional>
#include <memory>
#include <cassert>

#include "OptionBase.hpp"
#include "OptionSimple.hpp"
#include "OptionStyle.hpp"

namespace gui
{
    ///  @brief Option class with various constructor methods (factory).
    ///
    ///  Option container which holds pointer to Base Option GUI element (or derived Options).
    ///

    class Option
    {
      private:
        std::unique_ptr<option::Base> option;

      public:
        explicit Option(std::unique_ptr<option::Base> option) : option(std::move(option))
        {}

        Option(const UTF8 &text, std::function<bool(Item &)> cb, option::Arrow arrow = option::Arrow::Disabled)
            : Option(std::make_unique<option::Simple>(text, std::move(cb), arrow))
        {}

        Option(const Option &o) = delete;

        Option(Option &&o) noexcept : option{std::move(o.option)}
        {}

        [[nodiscard]] auto build() const -> ListItem *
        {
            assert(option);
            return option->build();
        }

        [[nodiscard]] auto str() const -> std::string
        {
            return option->str();
        }
    };
} // namespace gui