Rpi-hw  0.7.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
utils.hpp
1 /*
2  Title --- utils.hpp
3 
4  Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com
5 
6  This file is part of Rpi-hw.
7 
8  Rpi-hw is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation version 3 of the License.
11 
12  Rpi-hw is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with Rpi-hw. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #ifndef _RPI_HW_UTILS_HPP_
23 #define _RPI_HW_UTILS_HPP_
24 
25 #include <cstdio>
26 #include <cstdarg>
27 
28 #include <string>
29 
30 #include <rpi-hw/types.hpp>
31 
32 namespace rpihw { // Begin main namespace
33 
34 /*!
35  @namespace rpihw::utils
36  @brief Namespace of the utils functions.
37 */
38 
39 namespace utils { // Begin utils namespace
40 
41 //! Parameters of text.
42 enum TextFlags {
43 
44  ALIGN_LEFT = 0x01,
45  ALIGN_CENTER = 0x02,
46  ALIGN_RIGHT = 0x04,
47  WORD_WRAP = 0x08,
48  WORD_BREAK = 0x10
49 };
50 
51 
52 /*!
53  @brief Sets a bit in a variable.
54  @param[in] buffer The buffer containing the data.
55  @param[in] offset The offset position in the buffer.
56  @param[in] index The bit position.
57  @param[in] value The bit value.
58 */
59 template < typename T >
60 void set_bit( T &buffer, size_t offset, uint8_t index, bool value = 1 );
61 
62 /*!
63  @brief Clears a bit in a variable.
64  @param[in] buffer The buffer containing the data.
65  @param[in] offset The offset position in the buffer.
66  @param[in] index The bit position.
67 */
68 template < typename T >
69 void clear_bit( T &buffer, size_t offset, uint8_t index );
70 
71 /*!
72  @brief Flips a bit value in a variable.
73  @param[in] buffer The buffer containing the data.
74  @param[in] offset The offset position in the buffer.
75  @param[in] index The bit position.
76 */
77 template < typename T >
78 void flip_bit( T &buffer, size_t offset, uint8_t index );
79 
80 /*!
81  @brief Retuns a bit value in a variable.
82  @param[in] buffer The buffer containing the data.
83  @param[in] offset The offset position in the buffer.
84  @param[in] index The bit position.
85  @return The bit value.
86 */
87 template < typename T >
88 bool get_bit( T &buffer, size_t offset, uint8_t index );
89 
90 
91 //! Returns a formatted string like `printf`.
92 std::string format( const char *format, ... );
93 
94 /*!
95  @brief Aligns a text.
96  @param[in] text The string to align.
97  @param[in] width The width of the text.
98  @param[in] flags The parameters of the text.
99  @return The aligned text.
100 */
101 template < typename T >
102 std::basic_string< T > align( const std::basic_string< T > &text, size_t width, uint8_t flags = ALIGN_LEFT );
103 
104 } // End of utils namespace
105 
106 } // End of main namespace
107 
108 
109 // Include inline methods
110 #include <rpi-hw/utils-inl.hpp>
111 
112 #endif /* _RPI_HW_UTILS_HPP_ */
std::string format(const char *format,...)
Returns a formatted string like printf.
bool get_bit(T &buffer, size_t offset, uint8_t index)
Retuns a bit value in a variable.
Definition: utils-inl.hpp:58
void flip_bit(T &buffer, size_t offset, uint8_t index)
Flips a bit value in a variable.
Definition: utils-inl.hpp:50
std::basic_string< T > align(const std::basic_string< T > &text, size_t width, uint8_t flags)
Aligns a text.
Definition: utils-inl.hpp:66
void set_bit(T &buffer, size_t offset, uint8_t index, bool value)
Sets a bit in a variable.
Definition: utils-inl.hpp:31
void clear_bit(T &buffer, size_t offset, uint8_t index)
Clears a bit in a variable.
Definition: utils-inl.hpp:42
TextFlags
Parameters of text.
Definition: utils.hpp:42