Embedded Template Library  1.0
frame_check_sequence.h
Go to the documentation of this file.
1 
3 
4 /******************************************************************************
5 The MIT License(MIT)
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 Copyright(c) 2014 jwellbelove
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 The above copyright notice and this permission notice shall be included in all
17 copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 ******************************************************************************/
26 
27 #ifndef ETL_FRAME_CHECK_SEQUENCE_INCLUDED
28 #define ETL_FRAME_CHECK_SEQUENCE_INCLUDED
29 
30 #include <stdint.h>
31 
32 #include "platform.h"
33 #include "static_assert.h"
34 #include "type_traits.h"
35 #include "binary.h"
36 
37 #include "iterator.h"
38 
39 ETL_STATIC_ASSERT(ETL_8BIT_SUPPORT, "This file does not currently support targets with no 8bit type");
40 
43 
44 namespace etl
45 {
46  namespace private_frame_check_sequence
47  {
48  //***************************************************
51  //***************************************************
52  template <typename TFCS>
53  class add_insert_iterator : public etl::iterator<ETL_OR_STD::output_iterator_tag, void, void, void, void>
54  {
55  public:
56 
57  //***********************************
58  explicit add_insert_iterator(TFCS& fcs) ETL_NOEXCEPT
59  : p_fcs(&fcs)
60  {
61  }
62 
63  //***********************************
64  add_insert_iterator& operator*() ETL_NOEXCEPT
65  {
66  return *this;
67  }
68 
69  //***********************************
70  add_insert_iterator& operator++() ETL_NOEXCEPT
71  {
72  return *this;
73  }
74 
75  //***********************************
76  add_insert_iterator& operator++(int) ETL_NOEXCEPT
77  {
78  return *this;
79  }
80 
81  //***********************************
82  add_insert_iterator& operator =(uint8_t value)
83  {
84  p_fcs->add(value);
85  return *this;
86  }
87 
88  private:
89 
90  TFCS* p_fcs;
91  };
92  }
93 
94  //***************************************************************************
98  //***************************************************************************
99  template <typename TPolicy>
101  {
102  public:
103 
104  typedef TPolicy policy_type;
105  typedef typename policy_type::value_type value_type;
107 
108  ETL_STATIC_ASSERT(etl::is_unsigned<value_type>::value, "Signed frame check type not supported");
109 
110  //*************************************************************************
112  //*************************************************************************
114  {
115  reset();
116  }
117 
118  //*************************************************************************
122  //*************************************************************************
123  template<typename TIterator>
124  frame_check_sequence(TIterator begin, const TIterator end)
125  {
126  ETL_STATIC_ASSERT(sizeof(typename etl::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
127 
128  reset();
129  add(begin, end);
130  }
131 
132  //*************************************************************************
134  //*************************************************************************
135  void reset()
136  {
137  frame_check = policy.initial();
138  }
139 
140  //*************************************************************************
144  //*************************************************************************
145  template<typename TIterator>
146  void add(TIterator begin, const TIterator end)
147  {
148  ETL_STATIC_ASSERT(sizeof(typename etl::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
149 
150  while (begin != end)
151  {
152  frame_check = policy.add(frame_check, *begin++);
153  }
154  }
155 
156  //*************************************************************************
158  //*************************************************************************
159  void add(uint8_t value_)
160  {
161  frame_check = policy.add(frame_check, value_);
162  }
163 
164  //*************************************************************************
166  //*************************************************************************
167  value_type value() const
168  {
169  return policy.final(frame_check);
170  }
171 
172  //*************************************************************************
174  //*************************************************************************
175  operator value_type () const
176  {
177  return policy.final(frame_check);
178  }
179 
180  //*************************************************************************
182  //*************************************************************************
184  {
185  return add_insert_iterator(*this);
186  }
187 
188  private:
189 
190  value_type frame_check;
191  policy_type policy;
192  };
193 }
194 
195 #endif
Definition: frame_check_sequence.h:54
ETL_CONSTEXPR TContainer::iterator begin(TContainer &container)
Definition: container.h:49
ETL_CONSTEXPR TContainer::iterator end(TContainer &container)
Definition: container.h:99
void reset()
Resets the FCS to the initial state.
Definition: frame_check_sequence.h:135
add_insert_iterator input()
Gets an add_insert_iterator for input.
Definition: frame_check_sequence.h:183
void add(uint8_t value_)
Definition: frame_check_sequence.h:159
frame_check_sequence(TIterator begin, const TIterator end)
Definition: frame_check_sequence.h:124
void add(TIterator begin, const TIterator end)
Definition: frame_check_sequence.h:146
frame_check_sequence()
Default constructor.
Definition: frame_check_sequence.h:113
value_type value() const
Gets the FCS value.
Definition: frame_check_sequence.h:167
Definition: frame_check_sequence.h:101
is_unsigned
Definition: type_traits_generator.h:961
Definition: absolute.h:37
iterator
Definition: iterator.h:422