882af7cdb182a2f349f3f3c35189e1888468e3a6
[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 "Defaults.hh"
37 #include "../mpl.hh"
38 #include "Stream.hh"
39 #include "Area.hh"
40 #include "Levels.hh"
41 #include "Config.hh"
42
43 ///////////////////////////////ih.p////////////////////////////////////////
44
45 #ifndef _senf_LOG_STREAM
46 #   define _senf_LOG_STREAM std::cerr
47 #endif
48
49 typedef senf::log::Debug       SENFLogDefaultStream;
50 typedef senf::log::DefaultArea SENFLogDefaultArea;
51 typedef senf::log::NONE        SENFLogDefaultLevel;
52
53 namespace senf {
54 namespace log {
55 namespace detail {
56
57     struct AliasBase {};
58
59     template <class Base, class Param, unsigned N>
60     struct Parameters_ {};
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     template <class Base>
89     struct Parameters : public Base
90     {
91
92         static bool const compile_enabled = senf::log::Enabled<
93             typename Base::stream, 
94             typename Base::area, 
95             typename Base::level>::value;
96
97         static bool enabled() { return compile_enabled; }
98         static std::ostream & log_stream() { return _senf_LOG_STREAM; }
99     };
100
101     struct empty {};
102
103     struct Parameters_Merge_
104     {
105         template <class Base, class Param>
106         struct apply {
107             typedef Parameters_<
108                 Base, 
109                 Param, 
110                 SENF_MPL_RV(Parameters_select_(static_cast<Param*>(0)))> type;
111         };
112     };
113
114 }}}
115
116 #define SENF_LOG_MERGE_ARG(r, data, i, elem) BOOST_PP_COMMA_IF(i) elem
117
118 #define SENF_LOG_MERGE_PARAMETERS_I(base, args)                                                   \
119     boost::mpl::fold<                                                                             \
120         boost::mpl::vector< BOOST_PP_SEQ_FOR_EACH_I(SENF_LOG_MERGE_ARG, _, args) >,               \
121         base,                                                                                     \
122         senf::log::detail::Parameters_Merge_ >::type
123
124 #define SENF_LOG_MERGE_PARAMETERS(args)                                                           \
125     senf::log::detail::Parameters< SENF_LOG_MERGE_PARAMETERS_I(                                   \
126         senf::log::detail::empty,                                                                 \
127         (SENFLogDefaultStream)(SENFLogDefaultArea)(SENFLogDefaultLevel)args) >
128
129 #define SENF_LOG_MERGE_PARAMETERS_TPL(args)                                                       \
130     senf::log::detail::Parameters< typename SENF_LOG_MERGE_PARAMETERS_I(                          \
131         senf::log::detail::empty,                                                                 \
132         (SENFLogDefaultStream)(SENFLogDefaultArea)(SENFLogDefaultLevel)args) >
133
134 ///////////////////////////////ih.e////////////////////////////////////////
135 #endif
136
137 \f
138 // Local Variables:
139 // mode: c++
140 // fill-column: 100
141 // comment-column: 40
142 // c-file-style: "senf"
143 // indent-tabs-mode: nil
144 // ispell-local-dictionary: "american"
145 // compile-command: "scons -u test"
146 // End: