Fawkes API  Fawkes Development Version
vision.cpp
00001 
00002 /***************************************************************************
00003  *  vision.cpp - Fawkes VisionAspect initializer/finalizer
00004  *
00005  *  Created: Wed Nov 24 00:13:36 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.h>
00025 #include <aspect/inifins/vision_master.h>
00026 #include <aspect/vision.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 VisionAspectIniFin <aspect/inifins/vision.h>
00035  * Initializer/finalizer for the VisionAspect.
00036  * @author Tim Niemueller
00037  */
00038 
00039 /** Constructor.
00040  * @param master_inifin vision master aspect inifin to get master from
00041  */
00042 VisionAspectIniFin::VisionAspectIniFin(VisionMasterAspectIniFin *master_inifin)
00043   : AspectIniFin("VisionAspect")
00044 {
00045   __master_inifin = master_inifin;
00046 }
00047 
00048 
00049 void
00050 VisionAspectIniFin::init(Thread *thread)
00051 {
00052   VisionAspect *vision_thread;
00053   vision_thread = dynamic_cast<VisionAspect *>(thread);
00054   if (vision_thread == 0) {
00055     throw CannotInitializeThreadException("Thread '%s' claims to have the "
00056                                           "VisionAspect, but RTTI says it "
00057                                           "has not. ", thread->name());
00058   }
00059 
00060   try {
00061     if ( (vision_thread->vision_thread_mode() == VisionAspect::CONTINUOUS) &&
00062          (thread->opmode() != Thread::OPMODE_CONTINUOUS) ) {
00063       throw CannotInitializeThreadException("Vision thread '%s' operates in "
00064                                             "continuous mode but thread does not",
00065                                             thread->name());
00066     }
00067     if ( (vision_thread->vision_thread_mode() == VisionAspect::CYCLIC) &&
00068          (thread->opmode() != Thread::OPMODE_WAITFORWAKEUP) ) {
00069       throw CannotInitializeThreadException("Vision thread '%s' operates in "
00070                                             "cyclic mode but thread does not "
00071                                             "operate in wait-for-wakeup mode.",
00072                                             thread->name());
00073     }
00074 
00075     __master_inifin->add_vision_thread(vision_thread);
00076     vision_thread->init_VisionAspect(__master_inifin->vision_master());
00077   } catch (DependencyViolationException &e) {
00078     CannotInitializeThreadException ce("Dependency violation for "
00079                                        "VisionAspect detected");
00080     ce.append(e);
00081     throw ce;
00082   }
00083 
00084 }
00085 
00086 
00087 bool
00088 VisionAspectIniFin::prepare_finalize(Thread *thread)
00089 {
00090   VisionAspect *vision_thread;
00091   vision_thread = dynamic_cast<VisionAspect *>(thread);
00092   if (vision_thread == 0) {
00093     return true;
00094   }
00095 
00096   if ( ! __master_inifin->can_remove_vision_thread(vision_thread) ) {
00097     //__logger->log_warn("AspectIniFin", "Cannot remove vision master, there are "
00098     //          "still vision threads that depend on it");
00099     return false;
00100   }
00101 
00102   return true;
00103 }
00104 
00105 void
00106 VisionAspectIniFin::finalize(Thread *thread)
00107 {
00108   VisionAspect *vision_thread;
00109   vision_thread = dynamic_cast<VisionAspect *>(thread);
00110   if (vision_thread == 0) {
00111     throw CannotFinalizeThreadException("Thread '%s' claims to have the "
00112                                         "VisionAspect, but RTTI says it "
00113                                         "has not. ", thread->name());
00114   }
00115 
00116   try {
00117     __master_inifin->remove_vision_thread(vision_thread);
00118   } catch (DependencyViolationException &e) {
00119     CannotFinalizeThreadException ce("Dependency violation for "
00120                                      "VisionAspect detected");
00121     ce.append(e);
00122     throw ce;
00123   }
00124 }
00125 
00126 } // end namespace fawkes