Utils/Logger: Implement targets
[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; };
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; };
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, 
114             level>::value;
115
116         static bool enabled() { 
117             return compileEnabled
118                 && Base::area::instance().streamLimit(Base::stream::instance()) 
119                 <= Base::level::value;
120         }
121     };
122
123     /// Internal: Empty base class
124     struct empty {};
125
126     /// Internal: Merge log message parameter list
127     struct Parameters_Merge
128     {
129         /// Internal: Embedded mpl template meta-function
130         template <class Base, class Param>
131         struct apply {
132             typedef Parameters_<
133                 Base, 
134                 Param, 
135                 SENF_MPL_RV(Parameters_select_(static_cast<Param*>(0)))> type;
136         };
137     };
138
139 }}}
140
141 typedef senf::log::Debug       SENFLogDefaultStream;
142 typedef senf::log::DefaultArea SENFLogDefaultArea;
143 typedef senf::log::NONE        SENFLogDefaultLevel;
144
145 #define SENF_LOG_MERGE_ARG(r, data, i, elem) BOOST_PP_COMMA_IF(i) elem
146
147 #define SENF_LOG_MERGE_PARAMETERS_I(base, args)                                                   \
148     boost::mpl::fold<                                                                             \
149         boost::mpl::vector< BOOST_PP_SEQ_FOR_EACH_I(SENF_LOG_MERGE_ARG, _, args) >,               \
150         base,                                                                                     \
151         senf::log::detail::Parameters_Merge >::type
152
153 #define SENF_LOG_MERGE_PARAMETERS(args)                                                           \
154     senf::log::detail::Parameters< SENF_LOG_MERGE_PARAMETERS_I(                                   \
155         senf::log::detail::empty,                                                                 \
156         (SENFLogDefaultStream)(SENFLogDefaultArea)(SENFLogDefaultLevel)args) >
157
158 #define SENF_LOG_MERGE_PARAMETERS_TPL(args)                                                       \
159     senf::log::detail::Parameters< typename SENF_LOG_MERGE_PARAMETERS_I(                          \
160         senf::log::detail::empty,                                                                 \
161         (SENFLogDefaultStream)(SENFLogDefaultArea)(SENFLogDefaultLevel)args) >
162
163 ///////////////////////////////ih.e////////////////////////////////////////
164 #endif
165
166 \f
167 // Local Variables:
168 // mode: c++
169 // fill-column: 100
170 // comment-column: 40
171 // c-file-style: "senf"
172 // indent-tabs-mode: nil
173 // ispell-local-dictionary: "american"
174 // compile-command: "scons -u test"
175 // End: