iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImageSinkType.H
Go to the documentation of this file.
1 /*! @file
2  @author Randolph Voorhies
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_IMAGEPROC_IO_IMAGESINK_IMAGESINKTYPE_H
37 #define INCLUDE_NRT_IMAGEPROC_IO_IMAGESINK_IMAGESINKTYPE_H
38 
43 
44 namespace nrt
45 {
46  //! ImageSinkType is the base class of all ImageSink implementations.
47 
48  /*! To create a new Image sink type, you must create a new class that inherits
49  from ImageSinkType and do the following:
50  - Create the following methods in your new class.
51  - An overload of the out() method to handle outgoing Images
52  - A <code>static std::string id()</code> method which returns the string you would like
53  the --out parameter of ImageSink to associate with your class
54  - A <code>static std::string description()</code> method
55  - In the source file of your new class, register your class using the NRT_REGISTER_SINK() macro.
56  This macro will notify the global ImageSinkFactory at load time to let it know that your class exists.
57  - If your class is in a namespace, you must enclose the NRT_REGISTER_SINK() method within a namespace block.
58 
59  An Example:
60  \code
61  // /////////////////////////
62  // MyCoolSink.H
63  namespace myCoolNamespace
64  {
65  class MyCoolImageSink : public ImageSinkType
66  {
67  public:
68  MyCoolImageSink();
69 
70  //! Do something cool with the Image
71  void out(GenericImage img, std::string label="");
72 
73  // Get the name associated with our --out type
74  static std::string id()
75  { return "mycoolsink"; }
76 
77  // Get a description to show for --help
78  static std::string description()
79  { return "Does something really cool with Images"; }
80  };
81  }
82 
83  // /////////////////////////
84  // MyCoolSink.C
85 
86  // Register this ImageSinkType
87  namespace myCoolNamespace { NRT_REGISTER_SINK(MyCoolImageSink); }
88 
89  void MyCoolNamespace::MyCoolImageSink::out(GenericImage img,
90  std::string label)
91  {
92  // Do something with the output image here...
93  }
94  \endcode
95  */
97  {
98  public:
99  //! Constructor is inherited from Component
100  using Component::Component;
101 
102  //! The input of ImageSink::out() is forwarded directly to here when your ImageSinkType is selected
103  virtual void out(GenericImage img, std::string label="") = 0;
104  };
105 
106  typedef ComponentFactory<
107  std::string,
108  std::function<std::shared_ptr<nrt::ImageSinkType>(nrt::Component * parent, std::string const & instanceName)>,
109  std::map<std::string, std::string>
111 }
112 
113 /*! \def REGISTER_SINK(CLASSNAME)
114  Register an ImageSinkType with a global Factory. */
115 #define NRT_REGISTER_SINK(CLASSNAME) \
116  std::shared_ptr<nrt::ImageSinkType> create##CLASSNAME(nrt::Component * parent, std::string const & instanceName) \
117  { return parent->addSubComponent<CLASSNAME>(instanceName); } \
118  \
119  bool imageSinkRegister##CLASSNAME() \
120  { \
121  std::map<std::string,std::string> metadata; \
122  metadata["id"] = CLASSNAME::id(); \
123  metadata["description"] = CLASSNAME::description(); \
124  nrt::ImageSinkFactory::instance(). \
125  registerType(metadata["id"], create##CLASSNAME, metadata); \
126  return true; \
127  } \
128  \
129  bool const CLASSNAME##Registered __attribute__ ((unused)) = \
130  imageSinkRegister##CLASSNAME(); \
131 
132 
133 #endif // INCLUDE_NRT_IMAGEPROC_IO_IMAGESINK_IMAGESINKTYPE_H