switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / Config.ih
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 internal header */
30
31 #ifndef IH_SENF_Scheduler_Console_Config_
32 #define IH_SENF_Scheduler_Console_Config_ 1
33
34 // Custom includes
35 #include <boost/utility.hpp>
36 #include <boost/intrusive_ptr.hpp>
37 #include "Executor.hh"
38 #include <senf/Utils/intrusive_refcount.hh>
39 #include <senf/Utils/DiscardStream.hh>
40
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 namespace senf {
44 namespace console {
45 namespace detail {
46
47     /** \brief Internal: Executor wrapper implementing restricted execution
48
49         A RestrictedExecutor will only process commands which a re children of a given node. It does
50         \e not follow any links.
51      */
52     class RestrictedExecutor
53         : boost::noncopyable
54     {
55     public:
56         typedef void result_type;
57
58         //-/////////////////////////////////////////////////////////////////////////////////////////
59         //\/name Structors and default members
60         //\{
61
62         RestrictedExecutor(DirectoryNode & root = senf::console::root());
63
64         //\}
65         //-/////////////////////////////////////////////////////////////////////////////////////////
66
67         void execute(std::ostream & output, ParseCommandInfo const & command);
68                                         ///< Execute command
69                                         /**< Output will be written to \a output.
70                                              Same as operator()(). */
71
72         void operator()(std::ostream & output, ParseCommandInfo const & command);
73                                         ///< Execute command
74                                         /**< Output will be written to \a output.
75                                              Same as execute(). */
76
77         GenericNode & getNode(ParseCommandInfo const & command);
78
79         bool complete() const;          ///< \c true, if all nodes have been parsed
80         bool parsed(GenericNode & node) const; ///< \c true. if \a node has been parsed
81         void reset();                   ///< Reset node parse info state
82                                         /**< After a call to reset(), all information about already
83                                              parsed nodes is cleared. Calling parse() will parse the
84                                              complete config file again. */
85
86         DirectoryNode & root() const;
87         void chroot(DirectoryNode & node);
88
89         std::ostream & stream();
90
91         class RestrictGuard;
92
93     protected:
94
95     private:
96         void policyCallback(DirectoryNode & dir, std::string const & item);
97         void insertParsedNode(DirectoryNode & node);
98
99         typedef std::vector<DirectoryNode::weak_ptr> ParsedNodes;
100
101         Executor executor_;
102         ParsedNodes parsedNodes_;
103         DirectoryNode::ptr restrict_;
104         DiscardStream stream_;
105
106         friend class RestrictGuard;
107     };
108
109     /** \brief Internal: Set restricted node of a RestrictedExecutor
110
111         A RestrictGuard will set the node to which to restrict. It will automatically reset the node
112         in it's destructor.
113      */
114     class RestrictedExecutor::RestrictGuard
115     {
116     public:
117         //-/////////////////////////////////////////////////////////////////////////////////////////
118         //\/name Structors and default members
119         //\{
120
121         explicit RestrictGuard(RestrictedExecutor & executor);
122         RestrictGuard(RestrictedExecutor & executor, DirectoryNode & restrict);
123         ~RestrictGuard();
124
125         //\}
126         //-/////////////////////////////////////////////////////////////////////////////////////////
127
128     protected:
129
130     private:
131         RestrictedExecutor & executor_;
132
133     };
134
135     /** \brief Internal: ConfigSource base class
136
137         All configuration sources derive from ConfigSource. A ConigSource somehow reads
138         configuration commands and passes them to a RestrictedExecutor.
139       */
140     class ConfigSource
141         : public senf::intrusive_refcount
142     {
143     public:
144         typedef boost::intrusive_ptr<ConfigSource> ptr;
145         virtual ~ConfigSource();
146
147         void parse(RestrictedExecutor & executor);
148
149     protected:
150
151     private:
152         virtual void v_parse(RestrictedExecutor & executor) = 0;
153     };
154
155 }}}
156
157 //-/////////////////////////////////////////////////////////////////////////////////////////////////
158 #endif
159
160 \f
161 // Local Variables:
162 // mode: c++
163 // fill-column: 100
164 // comment-column: 40
165 // c-file-style: "senf"
166 // indent-tabs-mode: nil
167 // ispell-local-dictionary: "american"
168 // compile-command: "scons -u test"
169 // End: