Nymph  v1.5.2
Flow-Based Data Processing Framework
KTWriter.hh
Go to the documentation of this file.
1 /*
2  * KTWriter.hh
3  *
4  * Created on: Aug 24, 2012
5  * Author: nsoblath
6  */
7 
8 #ifndef KTWRITER_HH_
9 #define KTWRITER_HH_
10 
11 #include "KTProcessor.hh"
12 
13 #include "KTTIFactory.hh"
14 
15 #include "factory.hh"
16 
17 #include <map>
18 #include <typeinfo>
19 
20 namespace Nymph
21 {
23  {
24  public:
25  KTTypeWriter();
26  virtual ~KTTypeWriter();
27 
28  virtual void RegisterSlots() = 0;
29  };
30 
31 
32  template< class XWriter >
34  {
35  public:
37  virtual ~KTDerivedTypeWriter();
38 
39  void SetWriter(XWriter* writer);
40 
41  protected:
42  XWriter* fWriter;
43  };
44 
45 
46  template< class XWriter >
48  KTTypeWriter(),
49  fWriter(NULL)
50  {
51  }
52 
53  template< class XWriter >
55  {
56  }
57 
58  template< class XWriter >
60  {
61  fWriter = writer;
62  return;
63  }
64 
65 
66 
67 
68  class KTWriter : public KTProcessor
69  {
70  public:
71  KTWriter(const std::string& name = "default-writer-name");
72  virtual ~KTWriter();
73 
74  };
75 
76  template< class XWriter, class XTypist >
78  {
79  protected:
80  typedef std::map< const std::type_info*, XTypist* > TypeWriterMap;
81  public:
82  KTWriterWithTypists(const std::string& name = "default-writer-with-typists-name");
83  virtual ~KTWriterWithTypists();
84 
85  template< class XTypeWriter >
86  XTypeWriter* GetTypeWriter();
87 
88  protected:
89  TypeWriterMap fTypeWriters;
90 
91  };
92 
93 
94  template< class XWriter, class XTypist >
96  KTWriter(name),
97  fTypeWriters()
98  {
100  for (typename KTTIFactory< XTypist >::FactoryCIt factoryIt = twFactory->GetFactoryMapBegin();
101  factoryIt != twFactory->GetFactoryMapEnd();
102  factoryIt++)
103  {
104  XTypist* newTypeWriter = twFactory->Create(factoryIt);
105  newTypeWriter->SetWriter(static_cast< XWriter* >(this));
106  newTypeWriter->RegisterSlots();
107  fTypeWriters.insert(typename TypeWriterMap::value_type(factoryIt->first, newTypeWriter));
108  }
109  }
110 
111  template< class XWriter, class XTypist >
113  {
114  while (! fTypeWriters.empty())
115  {
116  delete fTypeWriters.begin()->second;
117  fTypeWriters.erase(fTypeWriters.begin());
118  }
119  }
120 
121  template< class XWriter, class XTypist >
122  template< class XTypeWriter >
124  {
125  typename TypeWriterMap::const_iterator it = fTypeWriters.find(&typeid(XTypeWriter));
126  if (it == fTypeWriters.end())
127  {
128  return NULL;
129  }
130  return static_cast< XTypeWriter* >(it->second);
131  }
132 
133 
134 #define KT_REGISTER_WRITER(writer_class, writer_name) \
135  static ::scarab::registrar< ::Nymph::KTWriter, writer_class, const std::string& > s##writer_class##WriterRegistrar(writer_name);
136 
137 } /* namespace Nymph */
138 #endif /* KTWRITER_HH_ */
TypeWriterMap fTypeWriters
Definition: KTWriter.hh:89
FactoryMap::const_iterator FactoryCIt
Definition: KTTIFactory.hh:73
void SetWriter(XWriter *writer)
Definition: KTWriter.hh:59
FactoryCIt GetFactoryMapEnd() const
Definition: KTTIFactory.hh:153
KTWriterWithTypists(const std::string &name="default-writer-with-typists-name")
Definition: KTWriter.hh:95
virtual ~KTDerivedTypeWriter()
Definition: KTWriter.hh:54
std::map< const std::type_info *, XTypist *> TypeWriterMap
Definition: KTWriter.hh:80
Contains KTProcessor.
virtual ~KTWriterWithTypists()
Definition: KTWriter.hh:112
FactoryCIt GetFactoryMapBegin() const
Definition: KTTIFactory.hh:147
virtual void RegisterSlots()=0
XBaseType * Create()
Definition: KTTIFactory.hh:100
virtual ~KTTypeWriter()
Definition: KTWriter.cc:28
XTypeWriter * GetTypeWriter()
Definition: KTWriter.hh:123