Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
byte_operation.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 29.05.2013 18:53:20 EDT
3 // File: byte_operation.hpp
4 
5 
6 /* This program is free software. It comes without any warranty, to
7  * the extent permitted by applicable law. You can redistribute it
8  * and/or modify it under the terms of the Do What The Fuck You Want
9  * To Public License, Version 2, as published by Sam Hocevar. See
10  * http://www.wtfpl.net/ or COPYING for more details. */
11 
12 #ifndef __BYTE_OPERATION_HEADER
13 #define __BYTE_OPERATION_HEADER
14 
15 namespace util {
16  namespace detail {
17  typedef uint8_t byte_op_size_type;
18  }
19  template<typename T>
20  inline void clear_byte(T & t, detail::byte_op_size_type const & pos) {
21  uint8_t * p = (uint8_t *) & t;
22  p[pos] = 0;
23  }
24  template<typename T>
25  inline uint8_t read_byte(T const & t, detail::byte_op_size_type const & pos) {
26  uint8_t * p = (uint8_t *) & t;
27  return p[pos];
28  }
29  template<typename T>
30  inline void write_byte(T & t, detail::byte_op_size_type const & pos, uint8_t const & in) {
31  uint8_t * p = (uint8_t *) & t;
32  p[pos] = in;
33  }
34 
35  template<typename T>
36  inline bool read_bit(T const & unit, uint8_t const & pos) {
37  return (unit & (T(1) << pos));
38  }
39  template<typename T>
40  inline void set_bit(T & unit, uint8_t const & pos) {
41  unit |= (T(1) << pos);
42  }
43  template<typename T>
44  inline void clear_bit(T & unit, uint8_t const & pos) {
45  unit &= ~(T(1) << pos);
46  }
47  template<typename T>
48  inline void write_bit(T & unit, uint8_t const & pos, bool state = true) {
49  state ? set_bit(unit, pos) : clear_bit(unit, pos);
50  }
51  template<typename T>
52  inline void flip_bit(T & unit, uint8_t const & pos) {
53  unit ^= (T(1) << pos);
54  }
55 }//end namespace util
56 #endif //__BYTE_OPERATION_HEADER
void clear_byte(T &t, detail::byte_op_size_type const &pos)
Definition: byte_operation.hpp:20
uint8_t read_byte(T const &t, detail::byte_op_size_type const &pos)
Definition: byte_operation.hpp:25
void clear_bit(T &unit, uint8_t const &pos)
Definition: byte_operation.hpp:44
void write_bit(T &unit, uint8_t const &pos, bool state=true)
Definition: byte_operation.hpp:48
void write_byte(T &t, detail::byte_op_size_type const &pos, uint8_t const &in)
Definition: byte_operation.hpp:30
void flip_bit(T &unit, uint8_t const &pos)
Definition: byte_operation.hpp:52
void set_bit(T &unit, uint8_t const &pos)
Definition: byte_operation.hpp:40
bool read_bit(T const &unit, uint8_t const &pos)
Definition: byte_operation.hpp:36