Fawkes API  Fawkes Development Version
vision_master.h
1 
2 /***************************************************************************
3  * vision_master.h - FireVision Vision Master
4  *
5  * Created: Wed May 30 10:28:06 2007
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __FIREVISION_FVUTILS_BASE_VISION_MASTER_H_
25 #define __FIREVISION_FVUTILS_BASE_VISION_MASTER_H_
26 
27 #include <fvutils/color/colorspaces.h>
28 #include <fvcams/control/control.h>
29 #include <core/utils/refptr.h>
30 #include <core/exceptions/software.h>
31 
32 #include <typeinfo>
33 
34 namespace fawkes {
35  class Thread;
36  class TypeMismatchException;
37 }
38 
39 namespace firevision {
40 #if 0 /* just to make Emacs auto-indent happy */
41 }
42 #endif
43 
44 class Camera;
45 
47 {
48  public:
49  virtual ~VisionMaster();
50 
51  virtual Camera * register_for_camera(const char *camera_string,
52  fawkes::Thread *thread,
53  colorspace_t cspace = YUV422_PLANAR) = 0;
54  virtual Camera * register_for_raw_camera(const char *camera_string,
55  fawkes::Thread *thread) = 0;
56  virtual void unregister_thread(fawkes::Thread *thread) = 0;
57 
58  virtual CameraControl *acquire_camctrl(const char *cam_string) = 0;
59  virtual void release_camctrl(CameraControl *cc) = 0;
60 
61 
62  /** Retrieve a typed camera control instance.
63  * Like the non-template method this class will try to instantiate the camera
64  * control based on the camera string (see there for details on the two possible
65  * contents of the string). The camera control will be verified to be of the
66  * desired type.
67  * @param camera_string camera string of camera for the control or the argument
68  * string for a new instance. See documentation of non-template method.
69  * @return typed camera control instance
70  * @exception TypeMismatchException thrown if requested camera control does not
71  * match the requested type.
72  */
73  template <class CC>
74  CC *
75  acquire_camctrl(const char *camera_string);
76 
77  /** Retrieve a typed camera control instance.
78  * Like the non-template method this class will try to instantiate the camera
79  * control based on the camera string (see there for details on the two possible
80  * contents of the string). The camera control will be verified to be of the
81  * desired type.
82  * Unlike the other template method you do not need to explicitly state the type
83  * of the requested type as it is inferred based on the \p cc argument. You can
84  * write something like this:
85  * @code
86  * CameraControlImage *cci = vision_master->acquire_camctrl("camstring...", cci);
87  * @endcode
88  * instead of
89  * @code
90  * CameraControlImage *cci = vision_master->acquire_camctrl<CameraControlImage>("camstring...");
91  * @endcode
92  * @param camera_string camera string of camera for the control or the argument
93  * string for a new instance. See documentation of non-template method.
94  * @param cc reference to pointer of camera control of the intended type, used
95  * to automatically infer the template method. On successful return points to
96  * the camera control instance
97  * @return typed camera control instance (same as \p cc)
98  * @exception TypeMismatchException thrown if requested camera control does not
99  * match the requested type.
100  */
101  template <class CC>
102  CC *
103  acquire_camctrl(const char *camera_string, CC *&cc);
104 
105 
106  /** Get typed raw camera.
107  * Like the non-template variant this method can be used to get access to
108  * the raw camera implementation, without going through a proxy. See the other
109  * method for risks and implication of using the raw device.
110  * @param camera_string camera that can be used by CameraFactory to open a
111  * camera.
112  * @param thread thread to register for this camera
113  * @return typed raw camera
114  */
115  template <class CC>
116  CC *
117  register_for_raw_camera(const char *camera_string, fawkes::Thread *thread);
118 
119 
120  protected:
121  virtual CameraControl *acquire_camctrl(const char *cam_string,
122  const std::type_info &typeinf) = 0;
123 
124 };
125 
126 template <class CC>
127 CC *
128 VisionMaster::acquire_camctrl(const char *camera_string, CC *&cc)
129 {
130  const std::type_info &t = typeid(CC);
131  CameraControl *pcc = acquire_camctrl(camera_string, t);
132  CC *tcc = dynamic_cast<CC *>(pcc);
133  if (tcc) {
134  if (cc) cc = tcc;
135  return tcc;
136  } else {
137  release_camctrl(tcc);
138  throw fawkes::TypeMismatchException("CameraControl defined by string does "
139  "not match desired type");
140  }
141 }
142 
143 
144 template <class CC>
145 CC *
146 VisionMaster::acquire_camctrl(const char *camera_string)
147 {
148  const std::type_info &t = typeid(CC);
149  CameraControl *pcc = acquire_camctrl(camera_string, t);
150  CC *tcc = dynamic_cast<CC *>(pcc);
151  if (tcc) {
152  return tcc;
153  } else {
154  release_camctrl(tcc);
155  throw fawkes::TypeMismatchException("CameraControl defined by string does "
156  "not match desired type");
157  }
158 }
159 
160 template <class CC>
161 CC *
162 VisionMaster::register_for_raw_camera(const char *camera_string, fawkes::Thread *thread)
163 {
164  Camera *camera = register_for_raw_camera(camera_string, thread);
165  CC *tcc = dynamic_cast<CC *>(camera);
166  if (tcc) {
167  return tcc;
168  } else {
169  unregister_thread(thread);
170  throw fawkes::TypeMismatchException("Camera defined by string does "
171  "not match desired type");
172  }
173 }
174 
175 } // end namespace firevision
176 
177 #endif
Camera interface for image aquiring devices in FireVision.
Definition: camera.h:35
Fawkes library namespace.
Thread class encapsulation of pthreads.
Definition: thread.h:42
Camera control interface base class.
Definition: control.h:33