50b1e0f1fde6bc5980685ff79c29955f9dbc80ee
[senf.git] / senf / Utils / Console / Config.hh
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Config public header */
25
26 #ifndef HH_SENF_Scheduler_Console_Config_
27 #define HH_SENF_Scheduler_Console_Config_ 1
28
29 // Custom includes
30 #include <boost/utility.hpp>
31 #include <list>
32 #include "Executor.hh"
33
34 //#include "Config.mpp"
35 #include "Config.ih"
36 ///////////////////////////////hh.p////////////////////////////////////////
37
38 namespace senf {
39 namespace console {
40
41     /** \brief Combine multiple configuration sources
42
43         A ConfigBundle combines several sources and parses them together, in the order they were
44         added. Parse restrictions are applied uniformly to all sources.
45         \code
46         // Add three configuration sources: A system configuration file, a user configuration file
47         // and command line options
48         senf::console::ConfigBundle conf;
49         conf.add( senf::console::FileConfig("/etc/some.conf") );
50         conf.add( senf::console::FileConfig("local.conf") );
51         conf.add( senf::console::OptionsConfig(senf::Daemon::instance().argc(),
52                                                senf::Daemon::instance().argv()) );
53
54         conf.parse();
55         \endcode
56
57         This bundle may also be passed to other code which may use restricted parsing to parse
58         partial information from all configuration sources.
59
60         \ingroup console_access
61       */
62     class ConfigBundle
63     {
64     public:
65         ///////////////////////////////////////////////////////////////////////////
66         ///\name Structors and default members
67         ///@{
68
69         ConfigBundle();                     ///< root node is set to console::root()
70         ConfigBundle(DirectoryNode & root); ///< Set custom root node
71
72         ///@}
73         ///////////////////////////////////////////////////////////////////////////
74
75         template <class Source>
76         Source & add(boost::intrusive_ptr<Source> source);
77                                         ///< Add configuration source
78
79         void parse();                   ///< Parse config bundle
80                                         /**< All nodes already parsed are skipped */
81         void parse(DirectoryNode & restrict); ///< Parse config bundle under \a restrict
82                                         /**< Only nodes which are children of \a restrict are
83                                              parsed. */
84
85         bool complete() const;          ///< \c true, if all nodes have been parsed
86         bool parsed(GenericNode & node) const; ///< \c true. if \a node has been parsed
87         void reset();                   ///< Reset node parse info state
88                                         /**< After a call to reset(), all information about already
89                                              parsed nodes is cleared. Calling parse() will parse the
90                                              complete config bundle again. */
91
92         DirectoryNode & root() const;
93         void chroot(DirectoryNode & node);
94
95     protected:
96
97     private:
98         void parseInternal();
99
100         typedef std::list<detail::ConfigSource::ptr> Sources;
101
102         Sources sources_;
103         detail::RestrictedExecutor executor_;
104     };
105
106 namespace detail {
107     // hrmpf ... Can't place this into Config.ih ...
108
109     /** \brief Internal: Provide ConfigBundle facade for a single-source config.
110
111         The BundleMixin is used to define supplementary configuration objects for one specific
112         configuration source. A BundleMixin is \e not a ConfigBundle since it has no public \c add()
113         member.
114      */
115     class BundleMixin
116     {
117     public:
118         BundleMixin();
119         BundleMixin(DirectoryNode & root);
120
121         void parse();                   ///< Parse config file
122                                         /**< All nodes already parsed are skipped */
123         void parse(DirectoryNode & restrict); ///< Parse config file under \a restrict
124                                         /**< Only nodes which are children of \a restrict are
125                                              parsed.  */
126
127         bool complete() const;          ///< \c true, if all nodes have been parsed
128         bool parsed(GenericNode & node) const; ///< \c true. if \a node has been parsed
129         void reset();                   ///< Reset node parse info state
130                                         /**< After a call to reset(), all information about already
131                                              parsed nodes is cleared. Calling parse() will parse the
132                                              complete config file again. */
133
134     protected:
135         template <class Source>
136         Source & add(boost::intrusive_ptr<Source> source);
137
138     private:
139         ConfigBundle bundle_;
140     };
141
142 }}}
143
144 ///////////////////////////////hh.e////////////////////////////////////////
145 #include "Config.cci"
146 //#include "Config.ct"
147 #include "Config.cti"
148 #endif
149
150 \f
151 // Local Variables:
152 // mode: c++
153 // fill-column: 100
154 // comment-column: 40
155 // c-file-style: "senf"
156 // indent-tabs-mode: nil
157 // ispell-local-dictionary: "american"
158 // compile-command: "scons -u test"
159 // End: