File: option_string_list.hpp

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (146 lines) | stat: -rw-r--r-- 6,721 bytes parent folder | download
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Libdnf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libdnf.  If not, see <https://www.gnu.org/licenses/>.

#ifndef LIBDNF5_CONF_OPTION_STRING_LIST_HPP
#define LIBDNF5_CONF_OPTION_STRING_LIST_HPP

#include "option.hpp"

#include <set>
#include <vector>


namespace libdnf5 {

/// Option that stores a container of strings. The type of the container is a
/// template parameter. Non-type IsAppend template parameter is used to
/// distinguish between regular list-like options (e.g. OptionStringList,
/// OptionStringSet) and append options (e.g. OptionStringAppendList,
/// OptionStringAppendSet).
/// Support default value, and check of an input value using the regular expression.
// @replaces libdnf:conf/OptionStringList.hpp:class:OptionStringList
template <typename T, bool IsAppend = false>
class LIBDNF_API OptionStringContainer : public Option {
public:
    using ValueType = T;

    explicit OptionStringContainer(ValueType default_value);
    OptionStringContainer(ValueType default_value, std::string regex, bool icase);
    explicit OptionStringContainer(const std::string & default_value);
    OptionStringContainer(const std::string & default_value, std::string regex, bool icase);
    OptionStringContainer(ValueType default_value, std::string regex, bool icase, std::string delimiters);

    OptionStringContainer(const OptionStringContainer & src);
    ~OptionStringContainer() override;

    /// Makes copy (clone) of this object.
    // @replaces libdnf:conf/OptionStringList.hpp:method:OptionStringList.clone()
    OptionStringContainer * clone() const override;

    /// Sets new value and priority (source).
    /// The value and priority are stored only if the new priority is equal to or higher than the stored priority.
    // @replaces libdnf:conf/OptionStingList.hpp:method:OptionStringList.set(Priority priority, bool value)
    virtual void set(Priority priority, const ValueType & value);

    /// Sets new value and runtime priority.
    void set(const ValueType & value);

    /// Parses input string and sets new value and priority.
    /// The value and priority are stored only if the new priority is equal to or higher than the stored priority.
    // @replaces libdnf:conf/OptionStringList.hpp:method:OptionStringList.set(Priority priority, const std::string & value)
    void set(Priority priority, const std::string & value) override;

    /// Parses input string and sets new value and runtime priority.
    void set(const std::string & value) override;

    /// Adds items from an another container.
    /// New items are stored in the container value
    void add(Priority priority, const ValueType & items);

    /// Parses input string and adds new values and priority.
    /// The value and priority are stored only if the new priority is equal to or higher than the stored priority.
    void add(Priority priority, const std::string & value);

    /// Adds new item to the container.
    /// New item is stored in the container value
    void add_item(Priority priority, const std::string & item);

    /// Gets the stored value.
    // @replaces libdnf:conf/OptionStringList.hpp:method:OptionStringList.getValue()
    const ValueType & get_value() const;

    /// Gets the default value. Default value is used until it is replaced by set() method.
    // @replaces libdnf:conf/OptionStringList.hpp:method:OptionStringList.getValueString()
    const ValueType & get_default_value() const;

    /// Gets a string representation of the stored value.
    // @replaces libdnf:conf/OptionStringList.hpp:method:OptionStringList.getValueString()
    std::string get_value_string() const override;

    /// Tests input value and throws exception if the value is not allowed.
    // @replaces libdnf:conf/OptionStringList.hpp:method:OptionStringList.test(const std::vector<std::string> & value)
    void test(const ValueType & value) const;

    /// Parses input string and returns a container of items.
    ///
    /// Items in the input string are separated by delimiters (default: space, comma, newline).
    /// Leading and trailing whitespace is trimmed from each item. Backslash '\' can be used
    /// to escape any character, allowing it to be included literally in items.
    ///
    /// Special case: If the string starts with a non-space delimiter, an empty item is
    /// inserted as the first element, which can be used to clear existing option content.
    ///
    /// @param value The input string to parse
    /// @return Container with parsed items
    // @replaces libdnf:conf/OptionStringList.hpp:method:OptionStringList.fromString(const std::string & value)
    ValueType from_string(std::string value) const;

    /// Converts input container of string items to a single string.
    /// Items are joined using the first delimiter character. Backslashes and delimiter characters
    /// within items are escaped with a backslash.
    // @replaces libdnf:conf/OptionStringList.hpp:method:OptionStringList.toString(const ValueType & value)
    std::string to_string(const ValueType & value) const;

    /// Returns the default delimiters
    static const char * get_default_delimiters() noexcept;

    /// Return delimiters of this OptionStringList
    const char * get_delimiters() const noexcept;

protected:
    /// Tests new container item value and throws exception if the item value is not allowed.
    void test_item(const std::string & item) const;

private:
    LIBDNF_LOCAL void init_regex_matcher();
    LIBDNF_LOCAL void test_item_worker(const std::string & item) const;

    class LIBDNF_LOCAL Impl;
    ImplPtr<Impl> p_impl;
};

using OptionStringList = OptionStringContainer<std::vector<std::string>>;
using OptionStringSet = OptionStringContainer<std::set<std::string>>;

using OptionStringAppendList = OptionStringContainer<std::vector<std::string>, true>;
using OptionStringAppendSet = OptionStringContainer<std::set<std::string>, true>;

}  // namespace libdnf5

#endif