factory.cpp

00001
00002 /***************************************************************************
00003  *  factory.cpp - Camera factory
00004  *
00005  *  Created: Wed Apr 11 15:37:45 2007
00006  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #include <cams/factory.h>
00025 #include <fvutils/system/camargp.h>
00026
00027 #ifdef HAVE_FIREWIRE_CAM
00028 #include <cams/firewire.h>
00029 #endif
00030 #ifdef HAVE_LEUTRON_CAM
00031 #include <cams/leutron.h>
00032 #endif
00033 #ifdef HAVE_FILELOADER_CAM
00034 #include <cams/fileloader.h>
00035 #endif
00036 #ifdef HAVE_SHMEM_CAM
00037 #include <cams/shmem.h>
00038 #endif
00039 #ifdef HAVE_NETWORK_CAM
00040 #include <cams/net.h>
00041 #endif
00042 #ifdef HAVE_V4L_CAM
00043 #include <cams/v4l.h>
00044 #endif
00045 #ifdef HAVE_V4L1_CAM
00046 #include <cams/v4l1.h>
00047 #endif
00048 #ifdef HAVE_V4L2_CAM
00049 #include <cams/v4l2.h>
00050 #endif
00051 #ifdef HAVE_NAO_CAM
00052 #include <cams/nao.h>
00053 #endif
00054 #ifdef HAVE_BUMBLEBEE2_CAM
00055 #include <cams/bumblebee2.h>
00056 #endif
00057 
00058 using namespace std;
00059 
00060 /** @class CameraFactory factory.h <cams/factory.h>
00061  * Camera factory.
00062  * This camera factory provides access to all cameras in a unified way. You just
00063  * supply a camera argument string and depending on the camera ID and compile-time
00064  * support of camera types an instance of the desired camera is returned or otherwise
00065  * an exception is thrown. See instance() for a list of supported camera types.
00066  *
00067  * @author Tim Niemueller
00068  */
00069 
00070 /** Get camera instance with parameters from given camera argument parser.
00071  * This is a convenience method and works like instace(const char *as).
00072  * @param cap camera argument parser
00073  * @return camera instance
00074  * @exception UnknownCameraTypeException thrown if camera type is not known or
00075  * was not available at build time.
00076  */
00077 Camera *
00078 CameraFactory::instance(const CameraArgumentParser *cap)
00079 {
00080   Camera *c = NULL;
00081
00082   // ######
00083   if ( cap->cam_type() == "firewire" ) {
00084 #ifdef HAVE_FIREWIRE_CAM
00085     c = new FirewireCamera(cap);
00086 #else
00087     throw UnknownCameraTypeException("No firewire support at compile time");
00088 #endif
00089   }
00090
00091   // ######
00092   if ( cap->cam_type() == "leutron" ) {
00093 #ifdef HAVE_LEUTRON_CAM
00094     c = new LeutronCamera();
00095 #else
00096     throw UnknownCameraTypeException("No Leutron support at compile time");
00097 #endif
00098   }
00099
00100   // ######
00101   if ( cap->cam_type() == "file" ) {
00102 #ifdef HAVE_FILELOADER_CAM
00103     c = new FileLoader(cap);
00104 #else
00105     throw UnknownCameraTypeException("No file loader support at compile time");
00106 #endif
00107   }
00108
00109   // ######
00110   if ( cap->cam_type() == "shmem" ) {
00111 #ifdef HAVE_SHMEM_CAM
00112     c = new SharedMemoryCamera(cap);
00113 #else
00114     throw UnknownCameraTypeException("No shared memory support at compile time");
00115 #endif
00116   }
00117
00118   // ######
00119   if ( cap->cam_type() == "net" ) {
00120 #ifdef HAVE_NETWORK_CAM
00121     c = new NetworkCamera(cap);
00122 #else
00123     throw UnknownCameraTypeException("No network support at compile time");
00124 #endif
00125   }
00126
00127   // ######
00128   if ( cap->cam_type() == "v4l" ) {
00129 #ifdef HAVE_V4L_CAM
00130     c = new V4LCamera(cap);
00131 #else
00132     throw UnknownCameraTypeException("No video4linux support at compile time");
00133 #endif
00134   }
00135
00136   // ######
00137   if ( cap->cam_type() == "v4l1" ) {
00138 #ifdef HAVE_V4L1_CAM
00139     c = new V4L1Camera(cap);
00140 #else
00141     throw UnknownCameraTypeException("No video4linux1 support at compile time");
00142 #endif
00143   }
00144
00145   // ######
00146   if ( cap->cam_type() == "v4l2" ) {
00147 #ifdef HAVE_V4L2_CAM
00148     c = new V4L2Camera(cap);
00149 #else
00150     throw UnknownCameraTypeException("No video4linux2 support at compile time");
00151 #endif
00152   }
00153
00154   // ######
00155   if ( cap->cam_type() == "nao" ) {
00156 #ifdef HAVE_NAO_CAM
00157     c = new NaoCamera(cap);
00158 #else
00159     throw UnknownCameraTypeException("No nao camera support at compile time");
00160 #endif
00161   }
00162
00163   // ######
00164   if ( cap->cam_type() == "bumblebee2" ) {
00165 #ifdef HAVE_BUMBLEBEE2_CAM
00166     c = new Bumblebee2Camera(cap);
00167 #else
00168     throw UnknownCameraTypeException("No Bumblebee 2 support at compile time");
00169 #endif
00170   }
00171
00172   if ( c == NULL ) {
00173     throw UnknownCameraTypeException();
00174   }
00175
00176   return c;
00177 }
00178
00179 
00180 /** Get camera instance.
00181  * Get an instance of a camera of the given type. The argument string determines
00182  * the type of camera to open.
00183  * Supported camera types:
00184  * - firewire, FirewireCamera, compiled if HAVE_FIREWIRE_CAM is defined in fvconf.mk
00185  * - leutron, LeutronCamera, compiled if HAVE_LEUTRON_CAM is defined in fvconf.mk
00186  * - file, FileLoader, compiled if HAVE_FILELOADER_CAM is defined in fvconf.mk
00187  * - shmem, SharedMemoryCamera, compiled if HAVE_SHMEM_CAM is defined in fvconf.mk
00188  * - net, NetworkCamera, compiled if HAVE_NETWORK_CAM is defined in fvconf.mk
00189  * - v4l, V4LCamera, compiled if HAVE_V4L_CAM is defined in fvconf.mk
00190  * @param as camera argument string
00191  * @return camera instance of requested type
00192  * @exception UnknownCameraTypeException thrown, if the desired camera could
00193  * not be instantiated. This could be either to a misspelled camera ID, generally
00194  * missing support or unset definition due to configuration in fvconf.mk or missing
00195  * libraries and camera support compile-time autodetection.
00196  */
00197 Camera *
00198 CameraFactory::instance(const char *as)
00199 {
00200   CameraArgumentParser *cap = new CameraArgumentParser(as);
00201   try {
00202     Camera *cam = instance(cap);
00203     delete cap;
00204     return cam;
00205   } catch (UnknownCameraTypeException &e) {
00206     delete cap;
00207     throw;
00208   }
00209 }