Autowiring  1.0.3
A C++ Inversion of Control Framework
auto_out.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
2 #pragma once
3 #include "AutoPacket.h"
4 #include "autowiring_error.h"
5 #include MEMORY_HEADER
6 
7 class AutoPacket;
8 
9 namespace autowiring {
10 
11 template<class T>
12 class auto_arg;
13 
39 template<class T>
40 class auto_out
41 {
42 public:
43  auto_out(void) = default;
44  auto_out(const auto_out& ao) = default;
45  auto_out(AutoPacket& packet) :
46  m_auto_out_impl(std::make_shared<auto_out_impl>(packet))
47  {}
48 
49  auto_out(auto_out&& rhs) :
50  m_auto_out_impl(std::move(rhs.m_auto_out_impl))
51  {}
52 
53  struct auto_out_impl {
54  public:
55  auto_out_impl(void) = delete;
56  auto_out_impl(const auto_out_impl &aoi) = default;
57 
58  auto_out_impl(auto_out_impl&& rhs) :
59  m_packet(std::move(rhs.m_packet)),
60  m_decoration(std::move(rhs.m_decoration))
61  {}
62 
63  auto_out_impl(AutoPacket& packet) :
64  m_packet(packet.shared_from_this())
65  {}
66 
67  ~auto_out_impl(void) {
68  if (m_decoration)
69  m_packet->Decorate<T>(std::move(m_decoration));
70  else
71  m_packet->MarkUnsatisfiable<T>();
72  }
73 
74  private:
75  std::shared_ptr<AutoPacket> m_packet;
76  std::shared_ptr<T> m_decoration;
77 
78  public:
79  T &operator*(void) const {
80  return *m_decoration;
81  }
82  T *operator->(void) const {
83  return m_decoration.get();
84  }
85  void operator=(const T& t) {
86  m_decoration = std::make_shared<T>(t);
87  }
88  void operator=(T&& t) {
89  m_decoration = std::make_shared<T>(std::forward<T&&>(t));
90  }
91  void operator=(const std::shared_ptr<T>& t) {
92  m_decoration = t;
93  }
94  };
95 
96 private:
97  std::shared_ptr<auto_out_impl> m_auto_out_impl;
98 
99 public:
100  // Returns true iff this auto_out object is nonempty (i.e. refers to a packet).
101  explicit operator bool(void) const { return bool(m_auto_out_impl); }
102 
110  void reset(void) {
111  m_auto_out_impl.reset();
112  }
113 
114  auto_out_impl& operator*(void) const { return *m_auto_out_impl; }
115  void operator=(auto_out&& rhs) { m_auto_out_impl = std::move(rhs.m_auto_out_impl); }
116 };
117 
118 template<class T>
119 class auto_arg<auto_out<T>>
120 {
121 public:
122 
123  typedef auto_out<T> type;
124  typedef auto_out<T> arg_type;
125  typedef auto_id_t<T> id_type;
126  static const bool is_input = false;
127  static const bool is_output = true;
128  static const bool is_rvalue = false;
129  static const bool is_shared = false;
130  static const bool is_multi = false;
131  static const int tshift = 0;
132 
133  static auto_out<T> arg(AutoPacket& packet) {
134  return auto_out<T>(packet);
135  }
136 
137  static void Commit(AutoPacket&, const type&) {
138  // Do nothing -- auto_out does its own deferred decoration.
139  }
140 };
141 
142 }
A decorator-style processing packet
Definition: AutoPacket.h:49
Definition: auto_arg.h:8
void reset(void)
Releases the internal implementatoin object
Definition: auto_out.h:110
The auto_out class provides a way to implement deferred-output autofilters.
Definition: auto_out.h:40