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