ConfigReader.hpp 4.58 KB
Newer Older
1
2
3
/**
 * @file
 * @brief Provides a reader for configuration files
Simon Spannagel's avatar
Simon Spannagel committed
4
 * @copyright Copyright (c) 2017 CERN and the Corryvreckan authors.
5
6
 * This software is distributed under the terms of the MIT License, copied verbatim in the file "LICENSE.md".
 * In applying this license, CERN does not waive the privileges and immunities granted to it by virtue of its status as an
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * Intergovernmental Organization or submit itself to any jurisdiction.
 */

#ifndef CORRYVRECKAN_CONFIG_READER_H
#define CORRYVRECKAN_CONFIG_READER_H

#include <istream>
#include <list>
#include <map>
#include <string>
#include <vector>

#include "Configuration.hpp"

namespace corryvreckan {

    /**
     * @brief Reader of configuration files
     *
26
     * Read the internal configuration file format used in the framework. The format contains
27
     * - A set of section header between [ and ] brackets
28
     * - Key/value pairs linked to the last defined section (or the empty section if none has been defined yet)
29
30
31
32
33
34
35
36
37
38
     */
    class ConfigReader {
    public:
        /**
         * @brief Constructs a config reader without any attached streams
         */
        ConfigReader();
        /**
         * @brief Constructs a config reader with a single attached stream
         * @param stream Stream to read configuration from
39
         * @param file_name Name of the file related to the stream or empty if not linked to a file
40
41
42
43
         */
        explicit ConfigReader(std::istream& stream, std::string file_name = "");

        /**
44
45
46
         * @brief Parse a line as key-value pair
         * @param line Line to interpret
         * @return Pair of the key and the value
47
         */
48
        static std::pair<std::string, std::string> parseKeyValue(std::string line);
49

50
        /**
51
52
53
         * @brief Adds a configuration stream to read
         * @param stream Stream to read configuration from
         * @param file_name Name of the file related to the stream or empty if not linked to a file
54
         */
55
        void add(std::istream&, std::string file_name = "");
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
        /**
         * @brief Directly add a configuration object to the reader
         * @param config Configuration object to add
         */
        void addConfiguration(Configuration config);

        /// @{
        /**
         * @brief Implement correct copy behaviour
         */
        ConfigReader(const ConfigReader&);
        ConfigReader& operator=(const ConfigReader&);
        /// @}

        /// @{
        /**
         * @brief Use default move behaviour
         */
        ConfigReader(ConfigReader&&) noexcept = default;
        ConfigReader& operator=(ConfigReader&&) noexcept = default;
        /// @}

        /**
         * @brief Removes all streams and all configurations
         */
        void clear();

        /**
         * @brief Check if a configuration exists
         * @param name Name of a configuration header to search for
87
         * @return True if at least a single configuration with this name exists, false otherwise
88
89
90
91
92
93
94
95
96
97
         */
        bool hasConfiguration(std::string name) const;
        /**
         * @brief Count the number of configurations with a particular name
         * @param name Name of a configuration header
         * @return The number of configurations with the given name
         */
        unsigned int countConfigurations(std::string name) const;

        /**
98
         * @brief Get cmobined configuration of all empty sections (usually the header)
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
         * @note Typically this is only the section at the top of the file
         * @return Configuration object for the empty section
         */
        Configuration getHeaderConfiguration() const;

        /**
         * @brief Get all configurations with a particular header
         * @param name Header name of the configurations to return
         * @return List of configurations with the given name
         */
        std::vector<Configuration> getConfigurations(std::string name) const;

        /**
         * @brief Get all configurations
         * @return List of all configurations
         */
        std::vector<Configuration> getConfigurations() const;

117
118
119
120
121
122
        /**
         * @brief Writes all configurations held to the stream provided
         * @param stream Stream to write configuration to
         */
        void write(std::ostream& stream);

123
124
125
126
127
128
129
130
131
132
133
134
    private:
        /**
         * @brief Initialize the configuration map after copy of the class
         */
        void copy_init_map();

        std::map<std::string, std::vector<std::list<Configuration>::iterator>> conf_map_;
        std::list<Configuration> conf_array_;
    };
} // namespace corryvreckan

#endif /* CORRYVRECKAN_CONFIG_MANAGER_H */