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 <core/exceptions/software.h>
28 #include <core/utils/refptr.h>
29 #include <fvcams/control/control.h>
30 #include <fvutils/color/colorspaces.h>
31 
32 #include <typeinfo>
33 
34 namespace fawkes {
35 class Thread;
36 class TypeMismatchException;
37 } // namespace fawkes
38 
39 namespace firevision {
40 
41 class Camera;
42 
44 {
45 public:
46  virtual ~VisionMaster();
47 
48  virtual Camera *register_for_camera(const char * camera_string,
49  fawkes::Thread *thread,
50  colorspace_t cspace = YUV422_PLANAR) = 0;
51  virtual Camera *register_for_raw_camera(const char *camera_string, fawkes::Thread *thread) = 0;
52  virtual void unregister_thread(fawkes::Thread *thread) = 0;
53 
54  virtual CameraControl *acquire_camctrl(const char *cam_string) = 0;
55  virtual void release_camctrl(CameraControl *cc) = 0;
56 
57  /** Retrieve a typed camera control instance.
58  * Like the non-template method this class will try to instantiate the camera
59  * control based on the camera string (see there for details on the two possible
60  * contents of the string). The camera control will be verified to be of the
61  * desired type.
62  * @param camera_string camera string of camera for the control or the argument
63  * string for a new instance. See documentation of non-template method.
64  * @return typed camera control instance
65  * @exception TypeMismatchException thrown if requested camera control does not
66  * match the requested type.
67  */
68  template <class CC>
69  CC *acquire_camctrl(const char *camera_string);
70 
71  /** Retrieve a typed camera control instance.
72  * Like the non-template method this class will try to instantiate the camera
73  * control based on the camera string (see there for details on the two possible
74  * contents of the string). The camera control will be verified to be of the
75  * desired type.
76  * Unlike the other template method you do not need to explicitly state the type
77  * of the requested type as it is inferred based on the \p cc argument. You can
78  * write something like this:
79  * @code
80  * CameraControlImage *cci = vision_master->acquire_camctrl("camstring...", cci);
81  * @endcode
82  * instead of
83  * @code
84  * CameraControlImage *cci = vision_master->acquire_camctrl<CameraControlImage>("camstring...");
85  * @endcode
86  * @param camera_string camera string of camera for the control or the argument
87  * string for a new instance. See documentation of non-template method.
88  * @param cc reference to pointer of camera control of the intended type, used
89  * to automatically infer the template method. On successful return points to
90  * the camera control instance
91  * @return typed camera control instance (same as \p cc)
92  * @exception TypeMismatchException thrown if requested camera control does not
93  * match the requested type.
94  */
95  template <class CC>
96  CC *acquire_camctrl(const char *camera_string, CC *&cc);
97 
98  /** Get typed raw camera.
99  * Like the non-template variant this method can be used to get access to
100  * the raw camera implementation, without going through a proxy. See the other
101  * method for risks and implication of using the raw device.
102  * @param camera_string camera that can be used by CameraFactory to open a
103  * camera.
104  * @param thread thread to register for this camera
105  * @return typed raw camera
106  */
107  template <class CC>
108  CC *register_for_raw_camera(const char *camera_string, fawkes::Thread *thread);
109 
110 protected:
111  virtual CameraControl *acquire_camctrl(const char *cam_string, const std::type_info &typeinf) = 0;
112 };
113 
114 template <class CC>
115 CC *
116 VisionMaster::acquire_camctrl(const char *camera_string, CC *&cc)
117 {
118  const std::type_info &t = typeid(CC);
119  CameraControl * pcc = acquire_camctrl(camera_string, t);
120  CC * tcc = dynamic_cast<CC *>(pcc);
121  if (tcc) {
122  if (cc)
123  cc = tcc;
124  return tcc;
125  } else {
126  release_camctrl(tcc);
127  throw fawkes::TypeMismatchException("CameraControl defined by string does "
128  "not match desired type");
129  }
130 }
131 
132 template <class CC>
133 CC *
134 VisionMaster::acquire_camctrl(const char *camera_string)
135 {
136  const std::type_info &t = typeid(CC);
137  CameraControl * pcc = acquire_camctrl(camera_string, t);
138  CC * tcc = dynamic_cast<CC *>(pcc);
139  if (tcc) {
140  return tcc;
141  } else {
142  release_camctrl(tcc);
143  throw fawkes::TypeMismatchException("CameraControl defined by string does "
144  "not match desired type");
145  }
146 }
147 
148 template <class CC>
149 CC *
150 VisionMaster::register_for_raw_camera(const char *camera_string, fawkes::Thread *thread)
151 {
152  Camera *camera = register_for_raw_camera(camera_string, thread);
153  CC * tcc = dynamic_cast<CC *>(camera);
154  if (tcc) {
155  return tcc;
156  } else {
157  unregister_thread(thread);
158  throw fawkes::TypeMismatchException("Camera defined by string does "
159  "not match desired type");
160  }
161 }
162 
163 } // end namespace firevision
164 
165 #endif
firevision::VisionMaster::release_camctrl
virtual void release_camctrl(CameraControl *cc)=0
firevision::VisionMaster::unregister_thread
virtual void unregister_thread(fawkes::Thread *thread)=0
firevision::CameraControl
Definition: control.h:37
firevision::VisionMaster::acquire_camctrl
virtual CameraControl * acquire_camctrl(const char *cam_string)=0
fawkes::TypeMismatchException
Definition: software.h:49
fawkes
firevision::VisionMaster::register_for_raw_camera
virtual Camera * register_for_raw_camera(const char *camera_string, fawkes::Thread *thread)=0
firevision::VisionMaster::register_for_camera
virtual Camera * register_for_camera(const char *camera_string, fawkes::Thread *thread, colorspace_t cspace=YUV422_PLANAR)=0
fawkes::Thread
Definition: thread.h:45
firevision::Camera
Definition: camera.h:38
firevision::VisionMaster
Definition: vision_master.h:43
firevision::VisionMaster::~VisionMaster
virtual ~VisionMaster()
Virtual empty destructor.
Definition: vision_master.cpp:129