switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / Config.hh
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Config public header */
30
31 #ifndef HH_SENF_Scheduler_Console_Config_
32 #define HH_SENF_Scheduler_Console_Config_ 1
33
34 // Custom includes
35 #include <boost/utility.hpp>
36 #include <list>
37 #include "Executor.hh"
38
39 //#include "Config.mpp"
40 #include "Config.ih"
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 namespace senf {
44 namespace console {
45
46     /** \brief Combine multiple configuration sources
47
48         A ConfigBundle combines several sources and parses them together, in the order they were
49         added. Parse restrictions are applied uniformly to all sources.
50         \code
51         // Add three configuration sources: A system configuration file, a user configuration file
52         // and command line options
53         senf::console::ConfigBundle conf;
54         conf.add( senf::console::FileConfig("/etc/some.conf") );
55         conf.add( senf::console::FileConfig("local.conf") );
56         conf.add( senf::console::OptionsConfig(senf::Daemon::instance().argc(),
57                                                senf::Daemon::instance().argv()) );
58
59         conf.parse();
60         \endcode
61
62         This bundle may also be passed to other code which may use restricted parsing to parse
63         partial information from all configuration sources.
64
65         \ingroup console_access
66       */
67     class ConfigBundle
68     {
69     public:
70         //-////////////////////////////////////////////////////////////////////////
71         ///\name Structors and default members
72         //\{
73
74         ConfigBundle();                     ///< root node is set to console::root()
75         ConfigBundle(DirectoryNode & root); ///< Set custom root node
76
77         //\}
78         //-////////////////////////////////////////////////////////////////////////
79
80         template <class Source>
81         Source & add(boost::intrusive_ptr<Source> source);
82                                         ///< Add configuration source
83
84         void parse();                   ///< Parse config bundle
85                                         /**< All nodes already parsed are skipped */
86         void parse(DirectoryNode & restrict); ///< Parse config bundle under \a restrict
87                                         /**< Only nodes which are children of \a restrict are
88                                              parsed. */
89
90         bool complete() const;          ///< \c true, if all nodes have been parsed
91         bool parsed(GenericNode & node) const; ///< \c true. if \a node has been parsed
92         void reset();                   ///< Reset node parse info state
93                                         /**< After a call to reset(), all information about already
94                                              parsed nodes is cleared. Calling parse() will parse the
95                                              complete config bundle again. */
96
97         DirectoryNode & root() const;
98         void chroot(DirectoryNode & node);
99
100     protected:
101
102     private:
103         void parseInternal();
104
105         typedef std::list<detail::ConfigSource::ptr> Sources;
106
107         Sources sources_;
108         detail::RestrictedExecutor executor_;
109     };
110
111 namespace detail {
112     // hrmpf ... Can't place this into Config.ih ...
113
114     /** \brief Internal: Provide ConfigBundle facade for a single-source config.
115
116         The BundleMixin is used to define supplementary configuration objects for one specific
117         configuration source. A BundleMixin is \e not a ConfigBundle since it has no public \c add()
118         member.
119      */
120     class BundleMixin
121     {
122     public:
123         BundleMixin();
124         BundleMixin(DirectoryNode & root);
125
126         void parse();                   ///< Parse config file
127                                         /**< All nodes already parsed are skipped */
128         void parse(DirectoryNode & restrict); ///< Parse config file under \a restrict
129                                         /**< Only nodes which are children of \a restrict are
130                                              parsed.  */
131
132         bool complete() const;          ///< \c true, if all nodes have been parsed
133         bool parsed(GenericNode & node) const; ///< \c true. if \a node has been parsed
134         void reset();                   ///< Reset node parse info state
135                                         /**< After a call to reset(), all information about already
136                                              parsed nodes is cleared. Calling parse() will parse the
137                                              complete config file again. */
138
139     protected:
140         template <class Source>
141         Source & add(boost::intrusive_ptr<Source> source);
142
143     private:
144         ConfigBundle bundle_;
145     };
146
147 }}}
148
149 //-/////////////////////////////////////////////////////////////////////////////////////////////////
150 #include "Config.cci"
151 //#include "Config.ct"
152 #include "Config.cti"
153 #endif
154
155 \f
156 // Local Variables:
157 // mode: c++
158 // fill-column: 100
159 // comment-column: 40
160 // c-file-style: "senf"
161 // indent-tabs-mode: nil
162 // ispell-local-dictionary: "american"
163 // compile-command: "scons -u test"
164 // End: