feedbackmap.h

Go to the documentation of this file.
00001 
00004 
00005 //
00006 // Copyright (c) 2001-2004 California Institute of Technology
00007 // Copyright (c) 2004-2007 University of Southern California
00008 // Rob Peters <rjpeters at usc dot edu>
00009 //
00010 // created: Thu Jul 19 16:58:49 2001
00011 // commit: $Id: feedbackmap.h 10065 2007-04-12 05:54:56Z rjpeters $
00012 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/visx/feedbackmap.h $
00013 //
00014 // --------------------------------------------------------------------
00015 //
00016 // This file is part of GroovX.
00017 //   [http://ilab.usc.edu/rjpeters/groovx/]
00018 //
00019 // GroovX is free software; you can redistribute it and/or modify it
00020 // under the terms of the GNU General Public License as published by
00021 // the Free Software Foundation; either version 2 of the License, or
00022 // (at your option) any later version.
00023 //
00024 // GroovX is distributed in the hope that it will be useful, but
00025 // WITHOUT ANY WARRANTY; without even the implied warranty of
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00027 // General Public License for more details.
00028 //
00029 // You should have received a copy of the GNU General Public License
00030 // along with GroovX; if not, write to the Free Software Foundation,
00031 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00032 //
00034 
00035 #ifndef GROOVX_VISX_FEEDBACKMAP_H_UTC20050626084017_DEFINED
00036 #define GROOVX_VISX_FEEDBACKMAP_H_UTC20050626084017_DEFINED
00037 
00038 #include "tcl/list.h"
00039 #include "tcl/obj.h"
00040 #include "tcl/interp.h"
00041 
00042 #include "rutz/error.h"
00043 #include "rutz/fstring.h"
00044 
00045 #include <vector>
00046 
00048 class FeedbackMap
00049 {
00050 public:
00052   FeedbackMap() :
00053     itsUseFeedback(true), itsRep(), itsItems(), isItDirty(true) {}
00054 
00056   const rutz::fstring& rep() const { return itsRep; }
00057 
00059   void set(const rutz::fstring& new_rep)
00060   {
00061     itsRep = new_rep;
00062     isItDirty = true;
00063     update();
00064   }
00065 
00067   void giveFeedback(tcl::interpreter& intp, int response) const
00068   {
00069     if (!itsUseFeedback) return;
00070 
00071     update();
00072 
00073     intp.set_global_var("resp_val", tcl::convert_from(response));
00074 
00075     bool feedbackGiven = false;
00076     for (size_t i = 0; i<itsItems.size() && !feedbackGiven; ++i)
00077       {
00078         feedbackGiven = itsItems[i].invokeIfTrue(intp);
00079       }
00080 
00081     intp.unset_global_var("resp_val");
00082   }
00083 
00084 private:
00085 
00087   void update() const
00088   {
00089     if (!isItDirty) return;
00090 
00091     tcl::list pairs_list(tcl::convert_from(itsRep));
00092 
00093     itsItems.clear();
00094 
00095     for (unsigned int i = 0; i < pairs_list.length(); ++i)
00096       {
00097         tcl::list current_pair(pairs_list[i]);
00098 
00099         if (current_pair.length() != 2)
00100           {
00101             throw rutz::error("\"pair\" did not have length 2 "
00102                               "in FeedbackMap::update", SRC_POS);
00103           }
00104 
00105         itsItems.push_back(Item(current_pair[0], current_pair[1]));
00106       }
00107 
00108     isItDirty = false;
00109   }
00110 
00112   class Item
00113   {
00114   public:
00116     Item(Tcl_Obj* cond, Tcl_Obj* res) :
00117       itsCondition(cond),
00118       itsResultCmd(res)
00119     {}
00120 
00122     bool invokeIfTrue(tcl::interpreter& interp)
00123     {
00124       if (interp.eval_boolean_expr(itsCondition))
00125         {
00126           interp.eval(itsResultCmd);
00127           return true;
00128         }
00129       return false;
00130     }
00131 
00132   private:
00133     tcl::obj itsCondition;
00134     tcl::obj itsResultCmd;
00135   };
00136 
00137 public:
00138   bool itsUseFeedback; 
00139 
00140 private:
00141   rutz::fstring itsRep;
00142   mutable std::vector<Item> itsItems;
00143   mutable bool isItDirty;
00144 };
00145 
00146 static const char __attribute__((used)) vcid_groovx_visx_feedbackmap_h_utc20050626084017[] = "$Id: feedbackmap.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00147 #endif // !GROOVX_VISX_FEEDBACKMAP_H_UTC20050626084017_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:42 2008 by Doxygen version 1.5.5.