7c16f3d3bda1d0cffe7d63e1baf0bba7288c6b36
[senf.git] / senf / Utils / Logger / Parameters.ih
1 // $Id$
2 //
3 // Copyright (C) 2007
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 Parameters internal header */
30
31 #ifndef IH_SENF_Utils_Logger_Parameters_
32 #define IH_SENF_Utils_Logger_Parameters_ 1
33
34 // Custom includes
35 #include <iostream>
36 #include <boost/preprocessor/seq/for_each_i.hpp>
37 #include <boost/preprocessor/facilities/apply.hpp>
38 #include <boost/preprocessor/punctuation/comma_if.hpp>
39 #include <boost/mpl/vector.hpp>
40 #include <boost/mpl/fold.hpp>
41 #include <boost/mpl/if.hpp>
42 #include <boost/utility.hpp>
43 #include <boost/type_traits/is_convertible.hpp>
44 #include <senf/Utils/mpl.hh>
45 #include "Config.hh"
46 #include "Target.hh"
47
48 //-/////////////////////////////////////////////////////////////////////////////////////////////////
49
50 namespace senf {
51 namespace log {
52
53     class DefaultArea;
54     class Debug;
55     class NONE;
56
57 namespace detail {
58
59     class StreamBase;
60     class AreaBase;
61     class LevelBase;
62     class AliasBase;
63
64     /// Internal: Parameter extractor
65     template <class Base, class Param, unsigned N>
66     struct Parameters_ {};
67
68 #ifndef DOXYGEN
69
70     senf::mpl::rv<1> Parameters_select_(StreamBase *);
71     template <class Base, class Param>
72     struct Parameters_<Base,Param,1> : public Base
73     { typedef Param stream; };
74
75     senf::mpl::rv<2> Parameters_select_(AreaBase *);
76     template <class Base, class Param>
77     struct Parameters_<Base,Param,2> : public Base
78     { typedef Param area; typedef Param area_base; };
79
80     senf::mpl::rv<3> Parameters_select_(LevelBase *);
81     template <class Base, class Param>
82     struct Parameters_<Base,Param,3> : public Base
83     { typedef Param level; };
84
85     senf::mpl::rv<4> Parameters_select_(void *);
86     template <class Base>
87     struct Parameters_<Base,void,4> : public Base
88     {};
89
90     senf::mpl::rv<5> Parameters_select_(AliasBase *);
91     template <class Base, class Param>
92     struct Parameters_<Base,Param,5>
93         : public Param::template apply<Base>::type
94     {};
95
96     // This trick makes any class with a SENFLogArea typedef member usable as area. A typedef of
97     // this name is created by SENF_LOG_CLASS_AREA()
98     template <class T>
99     senf::mpl::rv<6> Parameters_select_(
100         T *,
101         typename boost::disable_if< boost::is_convertible<T*,StreamBase*> >::type * = 0,
102         typename boost::disable_if< boost::is_convertible<T*,AreaBase*> >::type * = 0,
103         typename boost::disable_if< boost::is_convertible<T*,LevelBase*> >::type * = 0,
104         typename boost::disable_if< boost::is_convertible<T*,AliasBase*> >::type * = 0);
105     template <class Base, class Param>
106     struct Parameters_<Base,Param,6> : public Base
107     { typedef typename Param::SENFLogArea area; typedef Param area_base; };
108
109 #endif
110
111     /// Internal: Log message parameter collection
112     template <class Base>
113     struct Parameters : public Base
114     {
115         typedef typename boost::mpl::if_c< Base::level::value == NONE::value,
116                                            typename Base::stream::defaultLevel,
117                                            typename Base::level >::type level;
118
119         static bool const compileEnabled = senf::log::Enabled<
120             typename Base::stream,
121             typename Base::area_base,
122             level>::value;
123
124         static bool enabled() {
125             return compileEnabled
126                 && ( senf::log::detail::TargetRegistry::instance().fallbackRouting() ||
127                      Base::area::instance().limit(Base::stream::instance()) <= level::value );
128         }
129     };
130
131     /// Internal: Empty base class
132     struct empty {};
133
134     /// Internal: Merge log message parameter list
135     struct Parameters_Merge
136     {
137         /// Internal: Embedded mpl template meta-function
138         template <class Base, class Param>
139         struct apply {
140             typedef Parameters_<
141                 Base,
142                 Param,
143                 SENF_MPL_RV(Parameters_select_(static_cast<Param*>(0)))> type;
144         };
145     };
146
147 }}}
148
149 typedef senf::log::Debug       SENFLogDefaultStream;
150 typedef senf::log::DefaultArea SENFLogDefaultArea;
151 typedef senf::log::NONE        SENFLogDefaultLevel;
152
153 #define SENF_LOG_MERGE_ARG(r, data, i, elem) BOOST_PP_COMMA_IF(i) elem
154
155 #define SENF_LOG_MERGE_PARAMETERS_I(base, args)                                                   \
156     boost::mpl::fold<                                                                             \
157         boost::mpl::vector< BOOST_PP_SEQ_FOR_EACH_I(SENF_LOG_MERGE_ARG, _, args) >,               \
158         base,                                                                                     \
159         senf::log::detail::Parameters_Merge >::type
160
161 #define SENF_LOG_MERGE_PARAMETERS(args)                                                           \
162     senf::log::detail::Parameters< SENF_LOG_MERGE_PARAMETERS_I(                                   \
163         senf::log::detail::empty,                                                                 \
164         (SENFLogDefaultStream)(SENFLogDefaultArea)(SENFLogDefaultLevel)args) >
165
166 #define SENF_LOG_MERGE_PARAMETERS_TPL(args)                                                       \
167     senf::log::detail::Parameters< typename SENF_LOG_MERGE_PARAMETERS_I(                          \
168         senf::log::detail::empty,                                                                 \
169         (SENFLogDefaultStream)(SENFLogDefaultArea)(SENFLogDefaultLevel)args) >
170
171 //-/////////////////////////////////////////////////////////////////////////////////////////////////
172 #endif
173
174 \f
175 // Local Variables:
176 // mode: c++
177 // fill-column: 100
178 // comment-column: 40
179 // c-file-style: "senf"
180 // indent-tabs-mode: nil
181 // ispell-local-dictionary: "american"
182 // compile-command: "scons -u test"
183 // End: