esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
printing.hpp
Go to the documentation of this file.
1 #ifndef NDNPH_CORE_PRINTING_HPP
2 #define NDNPH_CORE_PRINTING_HPP
3 
4 #include "common.hpp"
5 
6 #ifdef ARDUINO
7 #define NDNPH_PRINT_ARDUINO
8 #include <Print.h>
9 #include <Printable.h>
10 #else
11 #define NDNPH_PRINT_OSTREAM
12 #include <ostream>
13 #endif
14 
15 namespace ndnph {
16 
17 #if defined(ARDUINO_ARCH_ESP32)
18 
19 // On ESP32, ::Printable type is not trivially destructible because it has a virtual destructor.
20 // Consequently, it cannot serve as a base class in a type that needs to be allocated from Region.
21 // This Printable class is a workaround that relies on implicit conversion to Esp32Printable.
22 class Printable {
23 private:
24  class Esp32Printable : public ::Printable {
25  public:
26  Esp32Printable(const ndnph::Printable* obj)
27  : m_obj(obj) {}
28 
29  size_t printTo(::Print& p) const final {
30  return m_obj->printTo(p);
31  }
32 
33  private:
34  const ndnph::Printable* m_obj;
35  };
36 
37 public:
38  operator Esp32Printable() const {
39  return Esp32Printable(this);
40  }
41 
42  virtual size_t printTo(::Print& p) const = 0;
43 };
44 
45 #elif defined(ARDUINO)
46 
47 using Printable = ::Printable;
48 
49 #else
50 
51 // In non-Arduino environment, declare Printable as an empty class, so that implementers
52 // do not have to ifdef away the inheritance. It's still necessary to surround printTo function
53 // with ifdef, because Arduino ::Print class is absent.
54 
55 class Printable {
56 protected:
57  ~Printable() = default;
58 };
59 
60 #endif
61 
62 } // namespace ndnph
63 
64 #endif // NDNPH_CORE_PRINTING_HPP
Definition: printing.hpp:22
virtual size_t printTo(::Print &p) const =0
Definition: fs.hpp:33