Open Lighting Architecture: include/ola/strings/Format.h Source File

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
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
25
26#ifndef INCLUDE_OLA_STRINGS_FORMAT_H_
27#define INCLUDE_OLA_STRINGS_FORMAT_H_
28
29#include <stdint.h>
30#include <ola/strings/FormatPrivate.h>
31#include <iomanip>
32#include <iostream>
33#include <limits>
34#include <ostream>
35#include <sstream>
36#include <string>
37
38namespace ola {
39namespace strings {
40
46std::string IntToString(int i);
47
53std::string IntToString(unsigned int i);
54
67template<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
75template <typename T>
76std::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
101void 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_
ola::strings::operator<<
std::ostream & operator<<(std::ostream &out, const ola::strings::_ToHex< T > &i)
Output the _ToHex type to an ostream.
Definition Format.h:76
ola::strings::ToHex
_ToHex< T > ToHex(T v, bool prefix=true)
Convert a value to a hex string.
Definition Format.h:68
FormatPrivate.h
Private implementations of the formatting functions from Format.h.
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44
ola::FormatData
void FormatData(std::ostream *out, const uint8_t *data, unsigned int length, unsigned int indent=0, unsigned int byte_per_line=8)
Write binary data to an ostream in a human readable form.
Definition StringUtils.h:476
ola::strings::_ToHex
Definition FormatPrivate.h:46