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