• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/libs/core/program_options/include/hpx/program_options/variables_map.hpp
1
//  Copyright Vladimir Prus 2002-2004.
2
//
3
//  SPDX-License-Identifier: BSL-1.0
4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
#pragma once
8

9
#include <hpx/program_options/config.hpp>
10
#include <hpx/modules/datastructures.hpp>
11

12
#include <map>
13
#include <memory>
14
#include <set>
15
#include <string>
16

17
#include <hpx/config/warnings_prefix.hpp>
18

19
namespace hpx::program_options {
20

21
    HPX_CXX_EXPORT template <typename Char>
22
    class basic_parsed_options;
23

24
    HPX_CXX_EXPORT class value_semantic;
25
    HPX_CXX_EXPORT class variables_map;
26

27
    // forward declaration
28

29
    /** Stores in 'm' all options that are defined in 'options'.
30
        If 'm' already has a non-defaulted value of an option, that value
31
        is not changed, even if 'options' specify some value.
32
    */
33
    HPX_CXX_EXPORT HPX_CORE_EXPORT void store(
34
        basic_parsed_options<char> const& options, variables_map& m,
35
        bool utf8 = false);
36

37
    /** Stores in 'm' all options that are defined in 'options'.
38
        If 'm' already has a non-defaulted value of an option, that value
39
        is not changed, even if 'options' specify some value.
40
        This is wide character variant.
41
    */
42
    HPX_CXX_EXPORT HPX_CORE_EXPORT void store(
43
        basic_parsed_options<wchar_t> const& options, variables_map& m);
44

45
    /** Runs all 'notify' function for options in 'm'. */
46
    HPX_CXX_EXPORT HPX_CORE_EXPORT void notify(variables_map& m);
47

48
    /** Class holding value of option. Contains details about how the
49
        value is set and allows to conveniently obtain the value.
50
    */
51
    HPX_CXX_EXPORT class HPX_CORE_EXPORT variable_value
52
    {
53
    public:
54
        variable_value() = default;
×
55
        variable_value(hpx::any_nonser const& xv, bool xdefaulted)
56
          : v(xv)
325✔
57
          , m_defaulted(xdefaulted)
325✔
58
        {
59
        }
60

61
        /** If stored value if of type T, returns that value. Otherwise,
62
            throws boost::bad_any_cast exception. */
63
        template <class T>
64
        T const& as() const
65
        {
66
            return hpx::any_cast<T const&>(v);
440✔
67
        }
68
        /** @overload */
69
        template <class T>
70
        T& as()
71
        {
72
            return hpx::any_cast<T&>(v);
73
        }
74

75
        /// Returns true if no value is stored.
76
        bool empty() const noexcept;
77
        /** Returns true if the value was not explicitly
78
            given, but has default value. */
79
        bool defaulted() const noexcept;
80
        /** Returns the contained value. */
81
        hpx::any_nonser const& value() const noexcept;
82

83
        /** Returns the contained value. */
84
        hpx::any_nonser& value() noexcept;
85

86
    private:
87
        hpx::any_nonser v;
88
        bool m_defaulted = false;
89
        // Internal reference to value semantic. We need to run
90
        // notifications when *final* values of options are known, and
91
        // they are known only after all sources are stored. By that
92
        // time options_description for the first source might not
93
        // be easily accessible, so we need to store semantic here.
94
        std::shared_ptr<value_semantic const> m_value_semantic;
95

96
        friend HPX_CORE_EXPORT void store(
97
            basic_parsed_options<char> const& options, variables_map& m, bool);
98

99
        friend class HPX_CORE_EXPORT variables_map;
100
    };
101

102
    /** Implements string->string mapping with convenient value casting
103
        facilities. */
104
    HPX_CXX_EXPORT class HPX_CORE_EXPORT abstract_variables_map
105
    {
106
    public:
107
        abstract_variables_map();
108
        explicit abstract_variables_map(abstract_variables_map const* next);
109

110
        virtual ~abstract_variables_map() = default;
111

112
        /** Obtains the value of variable 'name', from *this and
113
            possibly from the chain of variable maps.
114

115
            - if there's no value in *this.
116
                - if there's next variable map, returns value from it
117
                - otherwise, returns empty value
118

119
            - if there's defaulted value
120
                - if there's next variable map, which has a non-defaulted
121
                  value, return that
122
                - otherwise, return value from *this
123

124
            - if there's a non-defaulted value, returns it.
125
        */
126
        virtual variable_value const& operator[](std::string const& name) const;
127

128
        /** Sets next variable map, which will be used to find
129
           variables not found in *this. */
130
        void next(abstract_variables_map* next);
131

132
    private:
133
        /** Returns value of variable 'name' stored in *this, or
134
            empty value otherwise. */
135
        virtual variable_value const& get(std::string const& name) const = 0;
136

137
        abstract_variables_map const* m_next;
138
    };
139

140
    /** Concrete variables map which store variables in real map.
141

142
        This class is derived from std::map<std::string, variable_value>,
143
        so you can use all map operators to examine its content.
144
    */
145
    HPX_CXX_EXPORT class HPX_CORE_EXPORT variables_map
146
      : public abstract_variables_map
147
      , public std::map<std::string, variable_value>
148
    {
149
    public:
150
        variables_map();
151
        explicit variables_map(abstract_variables_map const* next);
152

153
        // Resolve conflict between inherited operators.
154
        variable_value const& operator[](std::string const& name) const override
436✔
155
        {
156
            return abstract_variables_map::operator[](name);
440✔
157
        }
158

159
        // Override to clear some extra fields.
160
        void clear();
161

162
        void notify();
163

164
    private:
165
        /** Implementation of abstract_variables_map::get
166
            which does 'find' in *this. */
167
        variable_value const& get(std::string const& name) const override;
168

169
        /** Names of option with 'final' values \-- which should not
170
            be changed by subsequence assignments. */
171
        std::set<std::string> m_final;
172

173
        friend HPX_CORE_EXPORT void store(
174
            basic_parsed_options<char> const& options, variables_map& xm,
175
            bool utf8);
176

177
        /** Names of required options, filled by parser which has
178
            access to options_description.
179
            The map values are the "canonical" names for each corresponding option.
180
            This is useful in creating diagnostic messages when the option is absent. */
181
        std::map<std::string, std::string> m_required;
182
    };
183

184
    /*
185
     * Templates/inlines
186
     */
187
    [[nodiscard]] inline bool variable_value::empty() const noexcept
188
    {
189
        return !v.has_value();
440✔
190
    }
191

192
    [[nodiscard]] inline bool variable_value::defaulted() const noexcept
193
    {
194
        return m_defaulted;
923✔
195
    }
196

197
    [[nodiscard]] inline hpx::any_nonser const& variable_value::value()
198
        const noexcept
199
    {
200
        return v;
238✔
201
    }
202

203
    [[nodiscard]] inline hpx::any_nonser& variable_value::value() noexcept
204
    {
205
        return v;
1,261✔
206
    }
207
}    // namespace hpx::program_options
208

209
#include <hpx/config/warnings_suffix.hpp>
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc