Templates -- Meow  1.1.2
不能,也不應該先編譯成obj-file的templates
Pipeline.h
Go to the documentation of this file.
1 #ifndef Pipeline_H__
2 #define Pipeline_H__
3 
4 #include "Self.h"
5 
6 #include <list>
7 
8 namespace meow {
9 
13 template<class Input, class Output, class Medium>
14 class Pipeline {
15 protected:
16  typedef std::list<Medium const*>::const_iterator MediumPointerIteratorK;
17 private:
18  struct Myself {
19  struct MediumInfo {
20  bool autoDel_;
21  int counter_;
22 
23  MediumInfo(bool ad): autoDel_(ad), counter_(1) {
24  }
26  }
27  };
28 
29  std::list<Medium const*> pipe_;
30  std::list<MediumInfo* > info_;
31 
32  Myself() {
33  }
34  Myself(Myself const& b): pipe_(b.pipe_), info_(b.info_) {
35  for (std::list<MediumInfo*>::iterator
36  it = info_.begin(); it != info_.end(); ++it) {
37  (*it).counter_ += 1;
38  }
39  }
40  ~Myself() {
41  std::list<MediumInfo* >::iterator it = info_.begin();
42  std::list<Medium const*>::iterator ip = pipe_.begin();
43  for ( ; it != info_.end(); ++it, ++ip) {
44  if ((*it).counter_ <= 0 && (*it).autoDel_ == true) {
45  delete *ip;
46  }
47  }
48  }
49 
50  bool frontAdd(Medium const* ptr, bool auto_delete) {
51  pipe_.push_front(ptr);
52  info_.push_front(new MediumInfo(auto_delete));
53  return true;
54  }
55 
56  bool backAdd(Medium const* ptr, bool auto_delete) {
57  pipe_.push_back(ptr);
58  info_.push_back(new MediumInfo(auto_delete));
59  return true;
60  }
61 
62  bool frontDel() {
63  if (pipe_.empty()) return false;
64  info_.front().counter_ -= 1;
65  if (info_.front().autoDel_ && info_.front().counter_ <= 0) {
66  delete pipe_.front();
67  }
68  pipe_.pop_front();
69  return true;
70  }
71 
72  bool backDel() {
73  if (pipe_.empty()) return false;
74  info_.back().counter_ -= 1;
75  if (info_.back().autoDel_ && info_.back().counter_ <= 0) {
76  delete pipe_.back();
77  }
78  pipe_.pop_back();
79  return true;
80  }
81  };
82 
83  Self<Myself> const self;
84 protected:
88  Pipeline(): self() {
89  }
90 
94  Pipeline(Pipeline const& p): self(p.self(), COPY_FROM) {
95  }
96 
104  self().copyFrom(p.self);
105  return *this;
106  }
107 
115  self().referenceFrom(p.self);
116  }
117 
122  return copyFrom(p);
123  }
124 
128  std::list<Medium const*> const& pipe() const {
129  return self->pipe_;
130  }
131 
132 public:
136  virtual ~Pipeline() {
137  }
138 
145  virtual Output goThrough(Input const& in) const = 0;
146 
152  virtual bool inversable() {
153  return false;
154  }
155 
162  virtual Input goThroughInv(Output const& in) const {
163  return Input();
164  }
165 
173  virtual bool frontAdd(Medium const* ptr, bool auto_delete) {
174  return self()->frontAdd(ptr, auto_delete);
175  }
176 
183  virtual bool backAdd(Medium const* ptr, bool auto_delete) {
184  return self()->backAdd(ptr, auto_delete);
185  }
186 
192  virtual bool frontDel() {
193  return self()->frontDel();
194  }
195 
201  virtual bool backDel() {
202  return self()->backDel();
203  }
204 
208  virtual size_t size() const {
209  return self->pipe_.size();
210  }
211 
215  virtual bool empty() const {
216  return (size() == 0);
217  }
218 
222  virtual Medium const* first() const {
223  return self->pipe_.front().pointer_;
224  }
225 
229  virtual Medium const* last() const {
230  return self->pipe_.end().pointer_;
231  }
232 };
233 
234 } // meow
235 
236 #endif // Pipeline_H__