Alchemy  1.0
A framework to robustly process network messages and structured data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
packed_bits.h
1 // @file meta/BitField/basic_list.h
2 //
3 // Defines a collector for bit-fields.
4 //
5 // The MIT License(MIT)
6 // @copyright 2014 Paul M Watt
7 // ****************************************************************************
8 #ifndef META_BIT_FIELD_BASIC_LIST_H_INCLUDED
9 #define META_BIT_FIELD_BASIC_LIST_H_INCLUDED
10 // Includes *******************************************************************
11 #include <Pb/bit_field/bit_field.h>
12 #include <Pb/integer_sequence.h>
13 
14 namespace Hg
15 {
16 
17 // ****************************************************************************
18 // A trait-type to define constants and common parameterized typedefs.
19 //
20 template< size_t IndexT,
21  typename HostT,
22  size_t CountT
23  >
24 struct FieldIndex
25 {
26  typedef HostT host_type;
27  typedef FieldIndex<IndexT,HostT,CountT> this_type;
28 
29  enum { k_index = IndexT};
30  enum { k_size = CountT};
31 };
32 
33 // ****************************************************************************
34 // A specialization for a bit-field representing the empty type.
35 //
36 template<>
37 struct FieldIndex<0,MT,0>
38 {
39  typedef MT host_type;
40  typedef FieldIndex<0,MT,0> this_type;
41 
42  enum { k_index = 0};
43  enum { k_size = 0};
44 };
45 
46 // ****************************************************************************
47 typedef FieldIndex<0,MT,0> idx_empty_t;
48 
49 // ****************************************************************************
50 // The base container definition for a list of bit-fields.
51 // This object contains type definitions
52 //
53 template <typename T>
54 struct PackedBits
55  : public container_trait
56  , public packed_trait
57 
58 {
59  // Define each of these types for the base.
60  // This will ensure the types exist if less than
61  // the maximum number of bit-fields are defined.
62  typedef idx_empty_t idx_0;
63  typedef idx_empty_t idx_1;
64  typedef idx_empty_t idx_2;
65  typedef idx_empty_t idx_3;
66  typedef idx_empty_t idx_4;
67  typedef idx_empty_t idx_5;
68  typedef idx_empty_t idx_6;
69  typedef idx_empty_t idx_7;
70 
71  typedef idx_empty_t idx_8;
72  typedef idx_empty_t idx_9;
73  typedef idx_empty_t idx_10;
74  typedef idx_empty_t idx_11;
75  typedef idx_empty_t idx_12;
76  typedef idx_empty_t idx_13;
77  typedef idx_empty_t idx_14;
78  typedef idx_empty_t idx_15;
79 
80  typedef idx_empty_t idx_16;
81  typedef idx_empty_t idx_17;
82  typedef idx_empty_t idx_18;
83  typedef idx_empty_t idx_19;
84  typedef idx_empty_t idx_20;
85  typedef idx_empty_t idx_21;
86  typedef idx_empty_t idx_22;
87  typedef idx_empty_t idx_23;
88 
89  typedef idx_empty_t idx_24;
90  typedef idx_empty_t idx_25;
91  typedef idx_empty_t idx_26;
92  typedef idx_empty_t idx_27;
93  typedef idx_empty_t idx_28;
94  typedef idx_empty_t idx_29;
95  typedef idx_empty_t idx_30;
96  typedef idx_empty_t idx_31;
97 
98  typedef PackedBits<T> this_type;
99  typedef T value_type;
100 
101  enum { k_offset_0 = 0 };
102 
103  // **************************************************************************
104  // Default Constructor
105  //
106  PackedBits()
107  : m_data(0)
108  { }
109 
110  // **************************************************************************
111  // Value Constructor.
112  //
113  PackedBits(value_type &data_field)
114  : m_data(data_field)
115  { }
116 
117  // **************************************************************************
118  // Value conversion operator.
119  //
120  operator value_type() const
121  {
122  return m_data;
123  }
124 
125  // **************************************************************************
126  /// (Value) Assignment operator
127  ///
128  PackedBits& operator=(const value_type &rhs)
129  {
130  m_data = rhs;
131  return *this;
132  }
133 
134  // **************************************************************************
135  // Returns the value of the packed bits.
136  //
137  const value_type& value() const
138  {
139  return m_data;
140  }
141 
142  // **************************************************************************
143  // Returns a reference to the storage buffer.
144  //
145  value_type& value()
146  {
147  return m_data;
148  }
149 
150  // **************************************************************************
151  // Sets the value of the packed bits.
152  //
153  void value(value_type value)
154  {
155  m_data = value;
156  }
157 
158  // Methods *******************************************************************
159  // ***************************************************************************
160  /// Returns the size of the base integer type used in the bit list.
161  ///
162  static
163  size_t size()
164  {
165  return sizeof(value_type);
166  }
167 
168 protected:
169  // Member Data **************************************************************
170 
171  value_type m_data; // Stores the values for the proxy.
172 };
173 
174 
175 
176 // ****************************************************************************
177 template< size_t Idx,
178  typename format_type
179  >
180 struct DeduceBitFieldList
181 {
182  typedef
183  typename TypeAt < Idx,
184  format_type
185  >::type base_t;
186 
187  typedef base_t type;
188 };
189 
190 
191 } // namespace Hg
192 
193 
194 #endif