Add 'rt' library to build (needed at least by gentoo)
[senf.git] / 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_Console_Config_
27 #define HH_Console_Config_ 1
28
29 // Custom includes
30 #include <boost/utility.hpp>
31 #include "Parse.hh"
32 #include "Executor.hh"
33
34 //#include "Config.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
36
37 namespace senf {
38 namespace console {
39
40     /** \brief Console node tree based config file parser
41
42         A ConfigFile instance allows flexible parsing of a config file against the console node
43         tree. If you just want to parse a file completely, the senf::console::readConfig() function
44         will do that. ConfigFile however allows to incrementally parse only a subdirectory of the
45         complete configuration file.
46         \code
47         senf::console::ConfigFile cf ("/my/config/file")
48
49         // Parse only statements under the directory of some object. The object 'ob'
50         // must have been registered somewhere in the node tree
51         cf.parse(ob.dir);
52         
53         // Parse rest of the config file
54         cf.parse();
55         \endcode
56       */
57     class ConfigFile
58         : boost::noncopyable
59     {
60     public:
61         ///////////////////////////////////////////////////////////////////////////
62         ///\name Structors and default members
63         ///@{
64
65         explicit ConfigFile(std::string const & filename, DirectoryNode & root = root());
66                                         ///< Create ConfigFile object for \a filename
67                                         /**< The \a filename configuration file will be parsed using
68                                              parse() calls. All configuration statements will be
69                                              interpreted relative to \a root as root node. */
70
71         ///@}
72         ///////////////////////////////////////////////////////////////////////////
73
74         void parse();                   ///< Parse config file
75                                         /**< All nodes already parsed are skipped */
76         void parse(DirectoryNode & restrict); ///< Parse config file under \a restrict
77                                         /**< Only nodes which are children of \a restrict are
78                                              parsed.  */
79
80         bool complete() const;          ///< \c true, if all nodes have been parsed
81         bool parsed(GenericNode & node) const; ///< \c true. if \a node has been parsed
82
83         void reset();                   ///< Reset node parse info state
84                                         /**< After a call to reset(), all information about already
85                                              parsed nodes is cleared. Calling parse() will parse the
86                                              complete config file again. */
87
88     protected:
89
90     private:
91         void policyCallback(DirectoryNode::ptr restrict, DirectoryNode & dir, 
92                             std::string const & item);
93
94         void insertParsedNode(DirectoryNode::ptr node);
95
96         typedef std::vector<DirectoryNode::weak_ptr> ParsedNodes;
97
98         std::string filename_;
99         CommandParser parser_;
100         Executor executor_;
101
102         ParsedNodes parsedNodes_;
103     };
104
105     /** \brief Read configuration file
106
107         The configuration file \a filename will be loaded, interpreting all node's relative to \a
108         root as root node.
109
110         This function uses a local ConfigFile object to perform the parsing.
111
112         \related ConfigFile
113      */
114     void readConfig(std::string const & filename, DirectoryNode & root = root());
115
116 }}
117
118 ///////////////////////////////hh.e////////////////////////////////////////
119 #include "Config.cci"
120 //#include "Config.ct"
121 //#include "Config.cti"
122 #endif
123
124 \f
125 // Local Variables:
126 // mode: c++
127 // fill-column: 100
128 // comment-column: 40
129 // c-file-style: "senf"
130 // indent-tabs-mode: nil
131 // ispell-local-dictionary: "american"
132 // compile-command: "scons -u test"
133 // End: