iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImageSourceBase.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_IMAGESOURCE_DETAILS_IMAGESOURCEBASE_H
37 #define INCLUDE_NRT_IMAGEPROC_IO_IMAGESOURCE_DETAILS_IMAGESOURCEBASE_H
38 
43 
44 namespace nrt
45 {
46  //! ImageSourceBase is the base class of all ImageSource implementations.
47  /*! To create a new Image source type, you must create a new class that inherits from ImageSourceBase and do the
48  following:
49 
50  - Create the following methods in your new class.
51 
52  - An overload of the in() method which returns an incoming GenericImage
53 
54  - A <code>static std::string const id()</code> method which returns the string you would like the --in
55  parameter of ImageSource to associate with your class
56 
57  - A <code>static std::string const description()</code> method
58 
59  - In the source file of your new class, register your class using the NRT_REGISTER_SOURCE() macro. This macro
60  will notify the global ImageSourceFactory at load time to let it know that your class exists.
61 
62  - If your class is in a namespace, you must enclose the NRT_REGISTER_SOURCE() method within a namespace block.
63 
64  An Example:
65  \code
66  // /////////////////////////
67  // MyCoolSource.H
68  namespace myCoolNamespace
69  {
70  class MyCoolImageSource : public ImageSourceBase
71  {
72  public:
73  MyCoolImageSource();
74 
75  //! Grab a new Image
76  GenericImage const in();
77 
78  // Get the name associated with our --in type
79  static std::string const id() { return "mycoolsource"; }
80 
81  // Get a description to show for --help
82  static std::string const description() { return "Grabs Images from Antarctica"; }
83  };
84  }
85 
86  // /////////////////////////
87  // MyCoolSource.C
88 
89  // Register this ImageSourceBase
90  namespace myCoolNamespace { NRT_REGISTER_SOURCE(MyCoolImageSource); }
91 
92  nrt::GenericImage const MyCoolNamespace::MyCoolImageSource::in()
93  {
94  GenericImage gImg = grabSomeImage();
95  return gImg;
96  }
97  \endcode
98 */
99  class ImageSourceBase : public Component
100  {
101  public:
102  ImageSourceBase(std::string instanceID="") : Component(instanceID) {};
103  virtual ~ImageSourceBase() { }
104 
105  //! Overload this method to return a new GenericImage from your source implementation
106  virtual GenericImage const in() = 0;
107 
108  virtual bool const ok() const = 0;
109  };
110 
111  //! ImageSource factory
112  typedef Factory<
114  std::string,
115  std::function<std::shared_ptr<ImageSourceBase>()>,
116  std::map<std::string, std::string>
118 
119 } // nrt
120 
121 /*! \def NRT_REGISTER_SOURCE(CLASSNAME) Register an ImageSourceBase with a global Factory. */
122 #define NRT_REGISTER_SOURCE(CLASSNAME) \
123  std::shared_ptr<nrt::ImageSourceBase> create##CLASSNAME() \
124  { return std::shared_ptr<nrt::ImageSourceBase>(new CLASSNAME); } \
125  bool imageSourceRegister##CLASSNAME() \
126  { \
127  std::map<std::string,std::string> metadata; \
128  metadata["id"] = CLASSNAME::id(); \
129  metadata["description"] = CLASSNAME::description(); \
130  nrt::ImageSourceFactory::instance().registerBase( \
131  metadata["id"], \
132  create##CLASSNAME, \
133  metadata \
134  ); \
135  return true; \
136  } \
137  const bool CLASSNAME##Registered __attribute__ ((unused)) = \
138  imageSourceRegister##CLASSNAME(); \
139 
140 
141 #endif // INCLUDE_NRT_IMAGEPROC_IO_IMAGESOURCE_DETAILS_IMAGESOURCEBASE_H
142