uLib-0.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Debug.h
Go to the documentation of this file.
1 /*//////////////////////////////////////////////////////////////////////////////
2 // CMT Cosmic Muon Tomography project //////////////////////////////////////////
4 
5  Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
6  All rights reserved
7 
8  Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
9 
10  ------------------------------------------------------------------
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 3.0 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library.
23 
25 
26 
27 
28 #ifndef U_CORE_DEBUG_H
29 #define U_CORE_DEBUG_H
30 
31 
32 #include <vector>
33 #include "Macros.h"
34 #include "Types.h"
35 #include "Mpl.h"
36 #include "SmartPointer.h"
37 
38 #include <boost/any.hpp>
39 #include <TObject.h>
40 
41 namespace uLib {
42 
43 
44 namespace detail {
45 
46 struct DebugAdapterInterface {
47  virtual ~DebugAdapterInterface() {}
48 
49  virtual void operator()(char val) {}
50  virtual void operator()(unsigned char val) {}
51  virtual void operator()(short val) {}
52  virtual void operator()(unsigned short val) {}
53  virtual void operator()(int val) {}
54  virtual void operator()(unsigned int val) {}
55  virtual void operator()(long val) {}
56  virtual void operator()(unsigned long val) {}
57 
58  virtual void operator()(float val) {}
59  virtual void operator()(double val) {}
60 
61  virtual void operator()(std::string val) {}
62 };
63 
64 
65 struct DebugAdapter {
66  struct AnyCastAdapterBase {
67  virtual ~AnyCastAdapterBase(){}
68  virtual void operator()(SmartPointer<DebugAdapterInterface> &ad, boost::any &val) {}
69  };
70 
71  template < typename T >
72  struct AnyCastAdapter : AnyCastAdapterBase {
73  void operator()(SmartPointer<DebugAdapterInterface> &ad, boost::any &val) { ad->operator()(boost::any_cast<T>(val)); }
74  };
75 
76  struct DItem {
77  DItem(){}
78  template <typename T> DItem(std::string str, T &t) :
79  m_adapter(new AnyCastAdapter<T>()),
80  m_name(str),
81  m_value(t) { }
82 
83  SmartPointer<AnyCastAdapterBase> m_adapter;
84  std::string m_name;
85  boost::any m_value;
86  };
87 
88 };
89 
90 
91 } // detail
92 
93 
94 
97 // text ADAPTER
98 
99 class DebugAdapterText : public detail::DebugAdapterInterface {
100  std::ostream &m_out;
101 public:
102  DebugAdapterText(std::ostream &o) : m_out(o) {}
103  void operator()(int val) { m_out << "debug: " << val << "\n"; }
104  void operator()(std::string val) { m_out << "debug: " << val << "\n"; }
105 };
106 
107 
108 
109 
110 
111 class Debug {
112  typedef detail::DebugAdapterInterface AdapterInterface;
113  typedef SmartPointer<detail::DebugAdapterInterface> Adapter;
114  typedef detail::DebugAdapter::DItem DItem;
115 public:
116 
117  template <typename T> void operator() (std::string str, T &t) { m_v.push_back(DItem(str,t)); }
118 
119  void AddAdapter(AdapterInterface &ad) { m_a.push_back(Adapter(ad)); }
120 
121  void Update() {
122  foreach(Adapter &ad, m_a) {
123  foreach(DItem &item, m_v) {
124  item.m_adapter->operator()(ad, item.m_value);
125  }
126  }
127  }
128 
129 private:
130  std::vector<DItem> m_v;
131  std::vector<Adapter> m_a;
132 };
133 
134 
135 
136 
137 
138 
139 } // uLib
140 
141 
142 
143 
144 
145 
146 
147 #endif // DEBUG_H