My Project
printable_to_string.cpp
1 /*
2  * uuid-common - Microcontroller common utilities
3  * Copyright 2019 Simon Arlott
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <uuid/common.h>
20 
21 #include <Arduino.h>
22 
23 #include <string>
24 
25 namespace uuid {
26 
27 class PrintableString: public ::Print {
28 public:
29  explicit PrintableString(std::string &output)
30  : output_(output) {
31 
32  }
33  ~PrintableString() = default;
34 
35  size_t write(uint8_t data) final override {
36  output_.append(1, reinterpret_cast<unsigned char>(data));
37  return 1;
38  }
39 
40  size_t write(const uint8_t *buffer, size_t size) final override {
41  output_.append(reinterpret_cast<const char*>(buffer), size);
42  return size;
43  }
44 
45 private:
46  std::string &output_;
47 };
48 
49 size_t print_to_string(const Printable &printable, std::string &output) {
50  PrintableString pstr{output};
51 
52  return printable.printTo(pstr);
53 }
54 
55 std::string printable_to_string(const Printable &printable) {
56  std::string str;
57 
58  print_to_string(printable, str);
59 
60  return str;
61 }
62 
63 } // namespace uuid
uuid::print_to_string
size_t print_to_string(const Printable &printable, std::string &output)
Append to a std::string by printing a Printable object.
Definition: printable_to_string.cpp:49
uuid::printable_to_string
std::string printable_to_string(const Printable &printable)
Create a std::string from a Printable object.
Definition: printable_to_string.cpp:55
uuid
Common utilities.
Definition: get_uptime_ms.cpp:28