uLib-0.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
BitCode.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 #ifndef U_MATH_BITCODE_H
28 #define U_MATH_BITCODE_H
29 
30 #include <boost/static_assert.hpp>
31 #include <boost/type_traits.hpp>
32 //#include <Core/CommaInitializer.h>
33 #include <Math/Dense.h>
34 
35 #include <Core/Mpl.h>
36 #include <boost/mpl/vector_c.hpp>
37 #include <boost/mpl/set_c.hpp>
38 
39 namespace uLib {
40 
41 
42 template < typename ContainerT, typename ContentT >
43 struct CommaInitializerBitCode
44 {
45  inline explicit CommaInitializerBitCode(ContainerT *container, ContentT s)
46  : container(container)
47  {
48  this->index = 0;
49  this->container->operator()().field1 = s;
50  }
51  inline CommaInitializerBitCode & operator, (ContentT s) {
52  this->index++;
53  if(index < container->size()) {
54  if(index == 1) container->operator()().field2 = s;
55  if(index == 2) container->operator()().field3 = s;
56  if(index == 3) container->operator()().field4 = s;
57  }
58  return *this;
59  }
60 
61  ContainerT *container;
62  unsigned int index;
63 };
64 
65 
66 
67 
68 template <typename T, uint L1, uint L2>
69 class BitCode2
70 {
71 protected:
72  typedef T Type;
73  typedef BitCode2<T,L1,L2> ThisClass;
74  typedef CommaInitializerBitCode< ThisClass, T > CommaInit;
75  BOOST_STATIC_ASSERT_MSG( boost::is_unsigned<T>::value == 1, "CODE TYPE MUST BE UNSIGNED" );
76  BOOST_STATIC_ASSERT( L1+L2 == sizeof(T)*8 );
77 
78 public:
79  struct BitField {
80  T field1 : L1;
81  T field2 : L2;
82  };
83  union CodeSet {
84  BitField bitf;
85  T value;
86  } m_data;
87 
88  BitCode2() {}
89  BitCode2(const T& data) { m_data.value = data; }
90  BitCode2(const Vector2i & data) {
91  (*this) << data(0),data(1);
92  }
93  inline CommaInit operator <<(T scalar) { return CommaInit(this, scalar); }
94  static const int size() { return 2; }
95 
96  BitField & operator()() { return m_data.bitf; }
97  const BitField & operator()() const { return m_data.bitf; }
98  operator Vector2i () { return Vector2i(m_data.bitf.field1,
99  m_data.bitf.field2); }
100 
101  void set(const T data) { m_data.value = data; }
102  T get() const { return m_data.value; }
103 };
104 
105 template <typename T, uint L1, uint L2>
106 std::ostream &
107 operator << (std::ostream &o, const BitCode2<T,L1,L2> &code) {
108  o << code().field1 << "," << code().field2;
109  return o;
110 }
111 
112 
113 
114 
115 
116 
117 
118 template <typename T, uint L1, uint L2, uint L3>
119 class BitCode3
120 {
121 protected:
122  typedef T Type;
123  typedef BitCode3<T,L1,L2,L3> ThisClass;
124  typedef CommaInitializerBitCode< ThisClass, T > CommaInit;
125  BOOST_STATIC_ASSERT_MSG( boost::is_unsigned<T>::value == 1, "CODE TYPE MUST BE UNSIGNED" );
126  BOOST_STATIC_ASSERT( L1+L2+L3 == sizeof(T)*8 );
127 
128 public:
129  struct BitField {
130  T field1 : L1;
131  T field2 : L2;
132  T field3 : L3;
133  };
134  union CodeSet {
135  BitField bitf;
136  T value;
137  } m_data;
138 
139 
140  BitCode3() {}
141  BitCode3(const T& data) { m_data.value = data; }
142  BitCode3(const Vector3i & data) {
143  (*this) << data(0),data(1),data(2);
144  }
145  inline CommaInit operator <<(T scalar) { return CommaInit(this, scalar); }
146  static const int size() { return 3; }
147 
148  BitField & operator()() { return m_data.bitf; }
149  const BitField & operator()() const { return m_data.bitf; }
150  operator Vector3i () { return Vector3i(m_data.bitf.field1,
151  m_data.bitf.field2,
152  m_data.bitf.field3); }
153 
154  void set(const T data) { m_data.value = data; }
155  T get() const { return m_data.value; }
156 };
157 
158 template <typename T, uint L1, uint L2, uint L3>
159 std::ostream &
160 operator << (std::ostream &o, const BitCode3<T,L1,L2,L3> &code) {
161  o << code().field1 << "," << code().field2 << "," << code().field3;
162  return o;
163 }
164 
165 
166 
167 
168 
169 
170 
171 template <typename T, uint L1, uint L2, uint L3, uint L4>
172 class BitCode4
173 {
174 protected:
175  typedef T Type;
176  typedef BitCode4<T,L1,L2,L3,L4> ThisClass;
177  typedef CommaInitializerBitCode< ThisClass, T > CommaInit;
178  BOOST_STATIC_ASSERT_MSG( boost::is_unsigned<T>::value == 1, "CODE TYPE MUST BE UNSIGNED" );
179  BOOST_STATIC_ASSERT( L1+L2+L3+L4 == sizeof(T)*8 );
180 public:
181  struct BitField {
182  T field1 : L1;
183  T field2 : L2;
184  T field3 : L3;
185  T field4 : L4;
186  };
187  union CodeSet {
188  BitField bitf;
189  T value;
190  } m_data;
191 
192  BitCode4() {}
193  BitCode4(const T& data) { m_data.value = data; }
194  BitCode4(const Vector4i & data) {
195  (*this) << data(0),data(1),data(2),data(3);
196  }
197  inline CommaInit operator << (T scalar) { return CommaInit(this, scalar); }
198  static const int size() { return 4; }
199 
200  BitField & operator()() { return m_data.bitf; }
201  const BitField & operator()() const { return m_data.bitf; }
202  operator Vector4i () { return Vector4i(m_data.bitf.field1,
203  m_data.bitf.field2,
204  m_data.bitf.field3,
205  m_data.bitf.field4); }
206 
207  void set(const T data) { m_data.value = data; }
208  T get() const { return m_data.value; }
209 
210 
211 
212 };
213 
214 template <typename T, uint L1, uint L2, uint L3, uint L4>
215 std::ostream &
216 operator << (std::ostream &o, const BitCode4<T,L1,L2,L3,L4> &code) {
217  o << code().field1 << "," << code().field2 << "," << code().field3 << "," << code().field4;
218  return o;
219 }
220 
221 
222 
223 template <typename T, uint L1, uint L2 = 0, uint L3 = 0>
224 class BitCode {
225  typedef boost::mpl::vector_c<T,L1,L2,L3> BitSet;
226  T m_data;
227 
228 public:
229 
230  struct value_printer
231  {
232  template< typename U > void operator()(U x)
233  { std::cout << x << '\n'; }
234  };
235  void PrintSelf( ) {
236 // boost::mpl::for_each<BitSet>( value_printer() );
237 // std::cout << boost::mpl::at_c< BitSet,1 >::type::value << "\n";
238  }
239 
240 // int Get(unsigned short field) const {
241 // return boost::mpl::at_c< BitSet, field >::type::value;
242 // }
243 };
244 
245 
246 
247 } // uLib
248 
249 
250 #endif // BITCODE_H