Fawkes API  Fawkes Development Version
vision_master.cpp
00001 
00002 /***************************************************************************
00003  *  vision_master.cpp - Fawkes VisionMasterAspect initializer/finalizer
00004  *
00005  *  Created: Tue Nov 23 23:06:13 2010
00006  *  Copyright  2006-2010  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 <aspect/inifins/vision_master.h>
00025 #include <aspect/vision_master.h>
00026 #include <fvutils/base/vision_master.h>
00027 #include <core/threading/thread_finalizer.h>
00028 
00029 namespace fawkes {
00030 #if 0 /* just to make Emacs auto-indent happy */
00031 }
00032 #endif
00033 
00034 /** @class VisionMasterAspectIniFin <aspect/inifins/vision_master.h>
00035  * Initializer/finalizer for the VisionMasterAspect.
00036  * @author Tim Niemueller
00037  */
00038 
00039 /** Constructor. */
00040 VisionMasterAspectIniFin::VisionMasterAspectIniFin()
00041   : AspectIniFin("VisionMasterAspect")
00042 {
00043 }
00044 
00045 
00046 void
00047 VisionMasterAspectIniFin::init(Thread *thread)
00048 {
00049   VisionMasterAspect *vision_master_thread;
00050   vision_master_thread = dynamic_cast<VisionMasterAspect *>(thread);
00051   if (vision_master_thread == 0) {
00052     throw CannotInitializeThreadException("Thread '%s' claims to have the "
00053                                           "VisionMasterAspect, but RTTI says it "
00054                                           "has not. ", thread->name());
00055   }
00056 
00057   try {
00058     __vision_dependency.add(vision_master_thread);
00059   } catch (DependencyViolationException &e) {
00060     CannotInitializeThreadException ce("Dependency violation for "
00061                                        "VisionMasterAspect detected");
00062     ce.append(e);
00063     throw ce;
00064   }
00065 }
00066 
00067 
00068 bool
00069 VisionMasterAspectIniFin::prepare_finalize(Thread *thread)
00070 {
00071   VisionMasterAspect *vision_master_thread;
00072   vision_master_thread = dynamic_cast<VisionMasterAspect *>(thread);
00073   if (vision_master_thread == 0) {
00074     return true;
00075   }
00076 
00077   if ( ! __vision_dependency.can_remove(vision_master_thread) ) {
00078     //__logger->log_warn("AspectIniFin", "Cannot remove vision master, there are "
00079     //          "still vision threads that depend on it");
00080     return false;
00081   }
00082 
00083   return true;
00084 }
00085 
00086 void
00087 VisionMasterAspectIniFin::finalize(Thread *thread)
00088 {
00089   VisionMasterAspect *vision_master_thread;
00090   vision_master_thread = dynamic_cast<VisionMasterAspect *>(thread);
00091   if (vision_master_thread == 0) {
00092     throw CannotFinalizeThreadException("Thread '%s' claims to have the "
00093                                         "VisionMasterAspect, but RTTI says it "
00094                                         "has not. ", thread->name());
00095   }
00096 
00097   try {
00098     __vision_dependency.remove(vision_master_thread);
00099   } catch (DependencyViolationException &e) {
00100     CannotFinalizeThreadException ce("Dependency violation for "
00101                                      "VisionMasterAspect detected");
00102     ce.append(e);
00103     throw ce;
00104   }
00105 }
00106 
00107 
00108 /** Get vision master.
00109  * @return vision master
00110  */
00111 firevision::VisionMaster *
00112 VisionMasterAspectIniFin::vision_master()
00113 {
00114   return __vision_dependency.provider()->vision_master();
00115 }
00116 
00117 
00118 /** Add a vision thread.
00119  * @param thread thread to add
00120  */
00121 void
00122 VisionMasterAspectIniFin::add_vision_thread(VisionAspect *thread)
00123 {
00124   __vision_dependency.add(thread);
00125 }
00126 
00127 /** Remove a vision thread.
00128  * @param thread thread to remove
00129  */
00130 void
00131 VisionMasterAspectIniFin::remove_vision_thread(VisionAspect *thread)
00132 {
00133   __vision_dependency.remove(thread);
00134 }
00135 
00136 /** Query if vision thread can be removed.
00137  * @param thread thread to query for
00138  * @return true if the thread can be removed, false otherwise
00139  */
00140 bool
00141 VisionMasterAspectIniFin::can_remove_vision_thread(VisionAspect *thread)
00142 {
00143   return __vision_dependency.can_remove(thread);
00144 }
00145 
00146 
00147 } // end namespace fawkes