Open Lighting Architecture  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Format.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * Format.h
17  * Formatting functions for basic types.
18  * Copyright (C) 2005 Simon Newton
19  */
20 
26 #ifndef INCLUDE_OLA_STRINGS_FORMAT_H_
27 #define INCLUDE_OLA_STRINGS_FORMAT_H_
28 
29 #include <stdint.h>
31 #include <iomanip>
32 #include <iostream>
33 #include <limits>
34 #include <ostream>
35 #include <sstream>
36 #include <string>
37 
38 namespace ola {
39 namespace strings {
40 
46 std::string IntToString(int i);
47 
53 std::string IntToString(unsigned int i);
54 
67 template<typename T>
68 _ToHex<T> ToHex(T v, bool prefix = true) {
69  return _ToHex<T>(v, (std::numeric_limits<T>::digits / HEX_BIT_WIDTH), prefix);
70 }
71 
75 template <typename T>
76 std::ostream& operator<<(std::ostream &out, const ola::strings::_ToHex<T> &i) {
77  std::ios::fmtflags flags(out.flags()); // Store the current format flags
78  // In C++, you only get the 0x on non-zero values, so we have to explicitly
79  // add it for all values if we want it
80  if (i.prefix) {
81  out << "0x";
82  }
83  out << std::setw(i.width) << std::hex << std::setfill('0')
84  << ola::strings::_HexCast(i.value);
85  out.flags(flags); // Put the format flags back to normal
86  return out;
87 }
88 
101 void FormatData(std::ostream *out,
102  const uint8_t *data,
103  unsigned int length,
104  unsigned int indent = 0,
105  unsigned int byte_per_line = 8);
106 } // namespace strings
107 } // namespace ola
108 #endif // INCLUDE_OLA_STRINGS_FORMAT_H_