Essentia  2.1-beta5-dev
sinkbase.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2016 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Essentia
5  *
6  * Essentia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef ESSENTIA_SINKBASE_H
21 #define ESSENTIA_SINKBASE_H
22 
23 #include "../types.h"
24 #include "../connector.h"
25 
26 namespace essentia {
27 namespace streaming {
28 
29 class SinkBase;
30 class SinkProxyBase;
31 class SourceBase;
32 class Algorithm;
33 
34 
35 void connect(SourceBase& source, SinkBase& sink);
36 void disconnect(SourceBase& source, SinkBase& sink);
37 
38 void attach(SinkProxyBase& proxy, SinkBase& innerSink);
39 void detach(SinkProxyBase& proxy, SinkBase& innerSink);
40 
41 
52 class SinkBase : public Connector {
53  protected:
55  ReaderID _id; // ID to use to identify this reader for the source (to know which reader is requesting tokens, etc...)
56 
58 
59  public:
60  SinkBase(Algorithm* parent = 0, const std::string& name = "unnamed") :
61  Connector(parent, name), _source(0), _sproxy(0) {}
62 
63  SinkBase(const std::string& name) :
64  Connector(name), _source(0), _sproxy(0) {}
65 
67  // NB: this call needs to come before the next one because _source is set by the proxy
68  // even though we're not explicitly connected to a source ourselves
69  E_DEBUG(EMemory, "Deleting SinkBase " << fullName());
70  if (_sproxy) essentia::streaming::detach(*_sproxy, *this);
71  if (_source) essentia::streaming::disconnect(*_source, *this);
72  E_DEBUG(EMemory, "Deleting SinkBase " << fullName() << "ok!");
73  }
74 
75  const SourceBase* source() const { return _source; }
76  SourceBase* source() { return _source; }
77  virtual void setSource(SourceBase* source);
78 
79  ReaderID id() const;
80  virtual void setId(ReaderID id);
81 
82  // should return a vector<TokenType>*
83  virtual const void* getTokens() const = 0;
84 
85  // should return a TokenType*
86  virtual const void* getFirstToken() const = 0;
87 
88  protected:
89  // methods for standard connections
90 
91  // made those protected so that only our friend streaming::{dis}connect() functions can access these
92  virtual void connect(SourceBase& source);
93  virtual void disconnect(SourceBase& source);
94 
95  friend void connect(SourceBase& source, SinkBase& sink);
96  friend void disconnect(SourceBase& source, SinkBase& sink);
97 
98 
99  // methods for proxies
100 
101  friend void attach(SinkProxyBase& proxy, SinkBase& innerSink);
102  friend void detach(SinkProxyBase& proxy, SinkBase& innerSink);
103 
104  // Note: these can't be called attach because they would be shadowed by SourceProxyBase::attach(SourceBase)
105  void attachProxy(SinkProxyBase* sproxy);
106  void detachProxy(SinkProxyBase* sproxy);
107 
108  // for SourceBase destructor
109  friend class SourceBase;
110 
111 };
112 
113 } // namespace streaming
114 } // namespace essentia
115 
116 #endif // ESSENTIA_SINKBASE_H
friend void detach(SinkProxyBase &proxy, SinkBase &innerSink)
Definition: sinkproxy.h:257
virtual void connect(SourceBase &source)
void detach(SinkProxyBase &proxy, SinkBase &innerSink)
Definition: sinkproxy.h:257
const Algorithm * parent() const
Definition: connector.h:53
int ReaderID
Definition: types.h:343
Definition: sourcebase.h:52
virtual void setId(ReaderID id)
const SourceBase * source() const
Definition: sinkbase.h:75
void attach(SinkProxyBase &proxy, SinkBase &innerSink)
Definition: sinkproxy.h:239
void connect(SourceBase &source, DevNullConnector devnull)
virtual const void * getFirstToken() const =0
SourceBase * source()
Definition: sinkbase.h:76
Definition: sinkbase.h:52
virtual void disconnect(SourceBase &source)
Definition: debugging.h:48
SourceBase * _source
Definition: sinkbase.h:54
std::string fullName() const
SinkBase(Algorithm *parent=0, const std::string &name="unnamed")
Definition: sinkbase.h:60
Definition: connector.h:40
Definition: algorithm.h:28
~SinkBase()
Definition: sinkbase.h:66
void detachProxy(SinkProxyBase *sproxy)
SinkBase(const std::string &name)
Definition: sinkbase.h:63
Definition: streamingalgorithm.h:140
void disconnect(SourceBase &source, DevNullConnector devnull)
virtual const void * getTokens() const =0
SinkProxyBase * _sproxy
Definition: sinkbase.h:57
virtual void setSource(SourceBase *source)
const std::string & name() const
Definition: types.h:283
void attachProxy(SinkProxyBase *sproxy)
#define E_DEBUG(module, msg)
Definition: debugging.h:157
Definition: sinkproxy.h:30
friend void attach(SinkProxyBase &proxy, SinkBase &innerSink)
Definition: sinkproxy.h:239
ReaderID _id
Definition: sinkbase.h:55