Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ArArg.cpp

00001 /*
00002 ActivMedia Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004,2005 ActivMedia Robotics, LLC
00004 
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 ActivMedia Robotics for information about a commercial version of ARIA at 
00022 robots@activmedia.com or 
00023 ActivMedia Robotics, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 
00025 */
00026 
00027 #include "ArExport.h"
00028 #include "ariaOSDef.h"
00029 #include "ArArg.h"
00030 #include "ArLog.h"
00031 #include "ArArgumentBuilder.h"
00032 
00033 ArArg::ArArg()
00034 {
00035   myType = INVALID;
00036   myName = "";
00037   myDescription = "";
00038   clear();
00039 }
00040 
00041 ArArg::ArArg(const char * name, int *pointer, 
00042                       const char * description, int minInt, int maxInt) 
00043 { 
00044   myType = INT;
00045   myName = name; 
00046   myDescription = description;
00047   clear();
00048   myMinInt = minInt;
00049   myMaxInt = maxInt;
00050   myIntPointer = pointer;
00051 }
00052 
00053 ArArg::ArArg(const char * name, double *pointer,
00054                       const char * description, double minDouble, 
00055                       double maxDouble) 
00056 { 
00057   myType = DOUBLE;
00058   myName = name; 
00059   myDescription = description;
00060   clear();
00061   myMinDouble = minDouble;
00062   myMaxDouble = maxDouble;
00063   myDoublePointer = pointer;
00064 }
00065 
00066 ArArg::ArArg(const char * name, bool *pointer, 
00067                       const char * description) 
00068 { 
00069   myType = BOOL;
00070   myName = name; 
00071   myDescription = description;
00072   clear();
00073   myBoolPointer = pointer;
00074 }
00075 
00076 ArArg::ArArg(const char * name, ArPose *pointer, 
00077                       const char * description) 
00078 { 
00079   myType = POSE;
00080   myName = name; 
00081   myDescription = description;
00082   clear();
00083   myPosePointer = pointer;
00084 }
00085 
00086 ArArg::ArArg(const char * name, char *pointer, 
00087                       const char * description, size_t maxStrLen) 
00088 { 
00089   myType = STRING;
00090   myName = name; 
00091   myDescription = description;
00092   clear();
00093   myStringPointer = pointer;
00094   myMaxStrLen = maxStrLen;
00095 }
00096 
00109 ArArg::ArArg(const char *name, 
00110                       ArRetFunctor1<bool, ArArgumentBuilder *> *setFunctor, 
00111               ArRetFunctor<const std::list<ArArgumentBuilder *> *> *getFunctor,
00112                       const char *description)
00113 {
00114   myType = FUNCTOR;
00115   myName = name;
00116   myDescription = description;
00117   clear();
00118   mySetFunctor = setFunctor;
00119   myGetFunctor = getFunctor;
00120 }
00121 
00122 ArArg::ArArg(const char * description)
00123 { 
00124   myType = DESCRIPTION_HOLDER;
00125   myDescription = description;
00126   clear();
00127 }
00128 
00129 ArArg::ArArg(const ArArg & arg) 
00130 {
00131   myType = arg.myType;
00132   myName = arg.myName;
00133   myDescription = arg.myDescription;
00134   myIntPointer = arg.myIntPointer;
00135   myDoublePointer = arg.myDoublePointer;
00136   myPosePointer = arg.myPosePointer;
00137   myBoolPointer = arg.myBoolPointer;
00138   myStringPointer = arg.myStringPointer;
00139   myMinInt = arg.myMinInt;
00140   myMaxInt = arg.myMaxInt;
00141   myMinDouble = arg.myMinDouble;
00142   myMaxDouble = arg.myMaxDouble;
00143   myMaxStrLen = arg.myMaxStrLen;
00144   mySetFunctor = arg.mySetFunctor;
00145   myGetFunctor = arg.myGetFunctor;
00146   myConfigPrioritySet = arg.myConfigPrioritySet;
00147   myConfigPriority = arg.myConfigPriority;
00148 }
00149 
00150 ArArg &ArArg::operator=(const ArArg & arg) 
00151 {
00152         if (this != &arg) {
00153                 myType = arg.myType;
00154                 myName = arg.myName;
00155                 myDescription = arg.myDescription;
00156                 myIntPointer = arg.myIntPointer;
00157                 myDoublePointer = arg.myDoublePointer;
00158                 myPosePointer = arg.myPosePointer;
00159                 myBoolPointer = arg.myBoolPointer;
00160                 myStringPointer = arg.myStringPointer;
00161                 myMinInt = arg.myMinInt;
00162                 myMaxInt = arg.myMaxInt;
00163                 myMinDouble = arg.myMinDouble;
00164                 myMaxDouble = arg.myMaxDouble;
00165                 myMaxStrLen = arg.myMaxStrLen;
00166                 mySetFunctor = arg.mySetFunctor;
00167                 myGetFunctor = arg.myGetFunctor;
00168                 myConfigPrioritySet = arg.myConfigPrioritySet;
00169                 myConfigPriority = arg.myConfigPriority;
00170         }
00171         return *this;
00172 }
00173 
00174 
00175 ArArg::~ArArg()
00176 {
00177 }
00178 
00179 void ArArg::clear(void)
00180 {
00181   myIntPointer = NULL;
00182   myDoublePointer = NULL;
00183   myBoolPointer = NULL;
00184   myPosePointer = NULL;
00185   myStringPointer = NULL;
00186   myMinInt = INT_MIN;
00187   myMaxInt = INT_MAX;
00188   myMinDouble = -HUGE_VAL;
00189   myMaxDouble = HUGE_VAL;
00190   myMaxStrLen = 0;
00191   mySetFunctor = NULL;
00192   myGetFunctor = NULL;  
00193   myConfigPrioritySet = false;
00194   myConfigPriority = ArPriority::NORMAL;
00195 }
00196 
00203 ArArg::Type ArArg::getType(void) const
00204 {
00205   return myType;
00206 }
00207 
00208 int ArArg::getMinInt(void) const
00209 {
00210   return myMinInt;
00211 }
00212 
00213 int ArArg::getMaxInt(void) const
00214 {
00215   return myMaxInt;
00216 }
00217 
00218 double ArArg::getMinDouble(void) const
00219 {
00220   return myMinDouble;
00221 }
00222 
00223 double ArArg::getMaxDouble(void) const
00224 {
00225   return myMaxDouble;
00226 }
00227 
00228 const char *ArArg::getName(void) const
00229 {
00230   return myName.c_str();
00231 }
00232 
00233 const char *ArArg::getDescription(void) const
00234 {
00235   return myDescription.c_str();
00236 }
00237 
00238 int ArArg::getInt(void) const
00239 { 
00240   if (myIntPointer != NULL)
00241     return *myIntPointer;
00242   else
00243     return 0;
00244 }
00245 
00246 double ArArg::getDouble(void) const 
00247 {
00248   if (myDoublePointer != NULL)
00249     return *myDoublePointer; 
00250   else
00251     return 0;
00252 }
00253 
00254 bool ArArg::getBool(void) const
00255 {
00256   if (myBoolPointer != NULL)
00257     return *myBoolPointer;
00258   else
00259     return false;
00260 }
00261 
00262 const char *ArArg::getString(void) const
00263 {
00264   if (myStringPointer != NULL)
00265     return myStringPointer;
00266   else
00267     return NULL;
00268 }
00269 
00270 ArPose ArArg::getPose(void) const
00271 {
00272   ArPose pose;
00273   if (myPosePointer != NULL)
00274     return *myPosePointer;
00275   else
00276     return pose;
00277 }
00278 
00279 const std::list<ArArgumentBuilder *> *ArArg::getArgsWithFunctor(void) const
00280 {
00281   if (myGetFunctor == NULL)
00282     return NULL;
00283   else
00284     return myGetFunctor->invokeR();
00285 }
00286 
00287 bool ArArg::setInt(int val)
00288 {
00289   if (val < myMinInt)
00290   {
00291     ArLog::log(ArLog::Normal, "ArArg of %s: setInt value %d below range [%d, %d]", getName(), val, myMinInt, myMaxInt);
00292     return false;
00293   }
00294   if (val > myMaxInt)
00295   {
00296     ArLog::log(ArLog::Normal, "ArArg of %s: setInt value %d above range [%d, %d]", getName(), val, myMinInt, myMaxInt);
00297     return false;
00298   }
00299   if (myIntPointer == NULL)
00300   {
00301     ArLog::log(ArLog::Normal, "ArArg of %s: setInt called with NULL int pointer.", getName());
00302   }
00303   // if we got to here we're good
00304   *myIntPointer = val;
00305   return true;
00306 }
00307 
00308 bool ArArg::setDouble(double val)
00309 { 
00310   if (val < myMinDouble)
00311   {
00312     ArLog::log(ArLog::Normal, "ArArg of %s: setDouble value %g below range [%g, %g]", getName(), val, myMinDouble, myMaxDouble);
00313     return false;
00314   }
00315   if (val > myMaxDouble)
00316   {
00317     ArLog::log(ArLog::Normal, "ArArg of %s: setDouble value %g above range [%g, %g]", getName(), val, myMinDouble, myMaxDouble);
00318     return false;
00319   }
00320   if (myDoublePointer == NULL)
00321   {
00322     ArLog::log(ArLog::Normal, "ArArg of %s: setDouble called with NULL pointer.", getName());
00323     return false;
00324   }
00325   // if we got to here we're good
00326   *myDoublePointer = val;
00327   return true;
00328 }
00329 
00330 
00331 bool ArArg::setBool(bool val)
00332 {
00333   if (myBoolPointer == NULL)
00334   {
00335     ArLog::log(ArLog::Normal, "ArArg of %s: setBool called with NULL pointer.", getName());
00336     return false;
00337   }
00338   *myBoolPointer = val;
00339   return true;
00340 }
00341 
00342 bool ArArg::setString(const char *str)
00343 {
00344   size_t len;
00345   if (myStringPointer == NULL)
00346   {
00347     ArLog::log(ArLog::Normal, "ArArg of %s: setString called with NULL pointer.", getName());
00348     return false;
00349   }
00350   // this is >= so that if it wouldn't have room with NULL that's
00351   // taken care of too
00352   if ((len = strlen(str)) >= myMaxStrLen)
00353   {
00354     ArLog::log(ArLog::Normal, "ArArg of %s: setString called with argument %d long, when max length is %d.", getName(), len, myMaxStrLen);
00355     return false;
00356   }
00357   strcpy(myStringPointer, str);
00358   return true;
00359 }
00360 
00361 bool ArArg::setPose(ArPose pose)
00362 {
00363   if (myPosePointer == NULL)
00364   {
00365     ArLog::log(ArLog::Normal, "ArArg of %s: setPose called with NULL pointer.", getName());
00366     return false;
00367   }
00368   *myPosePointer = pose;
00369   return true;
00370 
00371 }
00372 
00373 bool ArArg::setArgWithFunctor(ArArgumentBuilder *argument)
00374 {
00375   if (mySetFunctor == NULL)
00376   {
00377     ArLog::log(ArLog::Normal, "ArArg of %s: setArgWithFunctor called with NULL pointer.", getName());
00378     return false;
00379   }
00380   return mySetFunctor->invokeR(argument);
00381 }
00382 
00383 
00384 void ArArg::log(void) const
00385 {
00386   std::list<ArArgumentBuilder *>::const_iterator it;
00387   const std::list<ArArgumentBuilder *> *argList;
00388 
00389   switch (getType()) 
00390   {
00391   case ArArg::INVALID:
00392     ArLog::log(ArLog::Terse, 
00393                "\tType: %10s.  This argument was not created properly.", 
00394                "invalid");
00395   case ArArg::INT:
00396     ArLog::log(ArLog::Terse, "\tType: %10s name: %12s value: %d", "int", 
00397                getName(), getInt());
00398     if (strlen(getDescription()) != 0)
00399       ArLog::log(ArLog::Terse, "\t\tDescription: %s",
00400                  getDescription());
00401     break;
00402   case ArArg::DOUBLE:
00403     ArLog::log(ArLog::Terse, "\tType: %10s name: %12s value: %f", "double",
00404                getName(), getDouble());
00405     if (strlen(getDescription()) != 0)
00406       ArLog::log(ArLog::Terse, "\t\tDescription: %s",
00407                  getDescription());
00408     break; 
00409   case ArArg::STRING:
00410     ArLog::log(ArLog::Terse, "\tType: %10s name: %12s value: %s", "string", 
00411                getName(), getString());
00412     if (strlen(getDescription()) != 0)
00413       ArLog::log(ArLog::Terse, "\t\tDescription: %s",
00414                  getDescription());
00415     break;
00416   case ArArg::BOOL:
00417     ArLog::log(ArLog::Terse, "\tType: %10s name: %12s value: %d", "bool",
00418                getName(), getBool());
00419     if (strlen(getDescription()) != 0)
00420       ArLog::log(ArLog::Terse, "\t\tDescription: %s",
00421                  getDescription());
00422     break;
00423   case ArArg::POSE:
00424     ArLog::log(ArLog::Terse, "\tType: %10s name: %12s value: (%.1f %.1f %.1f)",
00425                "pose", getName(), getPose().getX(), getPose().getY(),
00426                getPose().getTh());
00427     if (strlen(getDescription()) != 0)
00428       ArLog::log(ArLog::Terse, "\t\tDescription: %s",
00429                  getDescription());
00430     break;
00431   case ArArg::FUNCTOR:
00432     ArLog::log(ArLog::Terse, "\tType: %10s name: %12s", 
00433                "functor", getName(), getPose().getX(), getPose().getY(),
00434                getPose().getTh());
00435     if (strlen(getDescription()) != 0)
00436       ArLog::log(ArLog::Terse, "\t\tDescription: %s",
00437                  getDescription());
00438     ArLog::log(ArLog::Terse, "\t\tValues:");
00439     argList = myGetFunctor->invokeR();
00440     for (it = argList->begin(); it != argList->end(); it++)
00441       ArLog::log(ArLog::Terse, "\t\t\t%s", (*it)->getFullString());
00442     break;
00443   case ArArg::DESCRIPTION_HOLDER:
00444     ArLog::log(ArLog::Terse, "\tType: %20s Description: %s", 
00445                "description_holder", getDescription());
00446 
00447   default:
00448     ArLog::log(ArLog::Terse, 
00449                "\tType: %10s.  This type doesn't have a case in ArArg::print.",
00450                "unknown");
00451     break;
00452   }
00453 
00454   if (myConfigPrioritySet)
00455     ArLog::log(ArLog::Terse, "\t\tPriority: %s", 
00456                ArPriority::getPriorityName(myConfigPriority));
00457 }
00458 
00463 bool ArArg::getConfigPrioritySet(void) const
00464 {
00465   return myConfigPrioritySet;
00466 }
00467 
00471 ArPriority::Priority ArArg::getConfigPriority(void) const
00472 {
00473   return myConfigPriority;
00474 }
00475 
00480 void ArArg::setConfigPriority(ArPriority::Priority priority)
00481 {
00482   myConfigPriority = priority;
00483   myConfigPrioritySet = true;
00484 }

Generated on Wed Oct 19 12:56:34 2005 for Aria by  doxygen 1.4.0