Packets: Fix VariantParser invalid parser access bug
[senf.git] / Utils / Logger / Target.ih
1 // $Id$
2 //
3 // Copyright (C) 2007
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 Target internal header */
25
26 #ifndef IH_SENF_Utils_Logger_Target_
27 #define IH_SENF_Utils_Logger_Target_ 1
28
29 // Custom includes
30 #include <memory>
31 #include <boost/type_traits/is_same.hpp>
32 #include <boost/static_assert.hpp>
33
34 ///////////////////////////////ih.p////////////////////////////////////////
35
36 namespace senf {
37 namespace log {
38 namespace detail {
39
40     /** \brief Internal: Target registry */
41     class TargetRegistry
42         : public senf::singleton<TargetRegistry>
43     {
44     public:
45         using senf::singleton<TargetRegistry>::instance;
46
47         void write(StreamBase const & stream, AreaBase const & area, unsigned level, 
48                    std::string msg);
49
50         void routed();
51         bool fallbackRouting();
52
53     private:
54         TargetRegistry();
55         
56         void registerTarget(Target * target);
57         void unregisterTarget(Target * target);
58
59         typedef std::set<Target *> Targets;
60         Targets targets_;
61
62         bool fallbackRouting_;
63         
64         friend class senf::log::Target;
65         friend class senf::singleton<TargetRegistry>;
66     };
67
68     /** \brief Internal: Write log message */
69     template <class Stream, class Area, class Level>
70     void write(std::string msg);
71
72 #ifndef DOXYGEN
73
74     // This code takes the routing target template arguments in any order and sorts them 
75     // by type (Stream, Area and Level).
76
77     senf::mpl::rv<0u> RouteParameterCheck_(...);
78     senf::mpl::rv<1u> RouteParameterCheck_(StreamBase *);
79     senf::mpl::rv<2u> RouteParameterCheck_(AreaBase *);
80     template <class T> senf::mpl::rv<3u> RouteParameterCheck_(T*, typename T::SENFLogArea * = 0);
81     senf::mpl::rv<4u> RouteParameterCheck_(LevelBase *);
82
83     // For g++ 4.0 (at least) we need to provide the fully scoped name for this default value.
84     // no idea why. It works without the scope in 4.1
85     template < class T, class A2, class A1,
86                unsigned type = SENF_MPL_RV( senf::log::detail::RouteParameterCheck_(static_cast<T*>(0)) ) >
87     struct RouteParameters
88     {};
89
90     template <class A2, class A1>
91     struct RouteParameters<mpl::nil,A2,A1,0u>
92         : public RouteParameters<A2,A1,mpl::nil>
93     {};
94
95     struct NilLevel {
96         static unsigned const value = NONE::value;
97     };
98
99     template <>
100     struct RouteParameters<mpl::nil,mpl::nil,mpl::nil,0u>
101     {
102         typedef mpl::nil Stream;
103         typedef mpl::nil Area;
104         typedef NilLevel Level;
105     };
106
107     template <class T, class A2, class A1>
108     struct RouteParameters<T,A2,A1,1u>
109         : public RouteParameters<A2,A1,mpl::nil>
110     {
111         typedef RouteParameters<A2,A1,mpl::nil> base;
112         BOOST_STATIC_ASSERT(( boost::is_same<typename base::Stream, mpl::nil>::value ));
113         typedef T Stream;
114     };
115
116     template <class T, class A2, class A1>
117     struct RouteParameters<T,A2,A1,2u>
118         : public RouteParameters<A2,A1,mpl::nil>
119     {
120         typedef RouteParameters<A2,A1,mpl::nil> base;
121         BOOST_STATIC_ASSERT(( boost::is_same<typename base::Area, mpl::nil>::value ));
122         typedef T Area;
123     };
124
125     template <class T, class A2, class A1>
126     struct RouteParameters<T,A2,A1,3u>
127         : public RouteParameters<A2,A1,mpl::nil>
128     {
129         typedef RouteParameters<A2,A1,mpl::nil> base;
130         BOOST_STATIC_ASSERT(( boost::is_same<typename base::Area, mpl::nil>::value ));
131         typedef typename T::SENFLogArea Area;
132     };
133
134     template <class T, class A2, class A1>
135     struct RouteParameters<T,A2,A1,4u>
136         : public RouteParameters<A2,A1,mpl::nil>
137     {
138         typedef RouteParameters<A2,A1,mpl::nil> base;
139         BOOST_STATIC_ASSERT(( boost::is_same<typename base::Level, NilLevel>::value ));
140         typedef T Level;
141     };
142
143     template <class T, class RV>
144     struct InstanceP
145     {
146         static RV * value() { return & T::instance(); }
147     };
148
149     template <class RV>
150     struct InstanceP<mpl::nil, RV>
151     {
152         static RV * value() { return 0; }
153     };
154
155 #endif
156
157 }}}
158
159 ///////////////////////////////ih.e////////////////////////////////////////
160 #endif
161
162 \f
163 // Local Variables:
164 // mode: c++
165 // fill-column: 100
166 // comment-column: 40
167 // c-file-style: "senf"
168 // indent-tabs-mode: nil
169 // ispell-local-dictionary: "american"
170 // compile-command: "scons -u test"
171 // End: