MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Core/MOOSLIB/MOOSVariable.cpp
Go to the documentation of this file.
00001 
00002 //
00003 //   MOOS - Mission Oriented Operating Suite 
00004 //  
00005 //   A suit of Applications and Libraries for Mobile Robotics Research 
00006 //   Copyright (C) 2001-2005 Massachusetts Institute of Technology and 
00007 //   Oxford University. 
00008 //    
00009 //   This software was written by Paul Newman at MIT 2001-2002 and Oxford 
00010 //   University 2003-2005. email: pnewman@robots.ox.ac.uk. 
00011 //      
00012 //   This file is part of a  MOOS Core Component. 
00013 //        
00014 //   This program is free software; you can redistribute it and/or 
00015 //   modify it under the terms of the GNU General Public License as 
00016 //   published by the Free Software Foundation; either version 2 of the 
00017 //   License, or (at your option) any later version. 
00018 //          
00019 //   This program is distributed in the hope that it will be useful, 
00020 //   but WITHOUT ANY WARRANTY; without even the implied warranty of 
00021 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
00022 //   General Public License for more details. 
00023 //            
00024 //   You should have received a copy of the GNU General Public License 
00025 //   along with this program; if not, write to the Free Software 
00026 //   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
00027 //   02111-1307, USA. 
00028 //
00030 // MOOSVariable.cpp: implementation of the CMOOSVariable class.
00031 //
00033 
00034 #include "MOOSVariable.h"
00035 //#include <sstream>
00036 #include <sstream>
00037 #include <iomanip>
00038 
00039 using namespace std;
00041 // Construction/Destruction
00043 
00044 CMOOSVariable::CMOOSVariable()
00045 {
00046     m_bDouble = false;//true;
00047     m_bFresh = false;
00048     m_dfCommsTime = DEFAULT_MOOS_VAR_COMMS_TIME;
00049     m_dfTimeWritten = -1;
00050     m_dfVal = 0;
00051   
00052 }
00053 
00054 CMOOSVariable::CMOOSVariable(std::string sName,std::string sSubscribe, std::string  sPublish,double dfCommsTime)
00055 {
00056     m_bDouble = false;//true;
00057     m_bFresh = false;
00058     m_sName = sName;
00059     m_sSubscribeName =  sSubscribe;
00060     m_sPublishName = sPublish;
00061     m_dfCommsTime = dfCommsTime;
00062     m_dfTimeWritten = -1;
00063     m_dfVal = 0;
00064   
00065 }
00066 
00067 CMOOSVariable::~CMOOSVariable()
00068 {
00069 
00070 }
00071 bool CMOOSVariable::Set(double dfVal,double dfTime)
00072 {
00073     m_dfVal = dfVal;
00074     m_dfTimeWritten = dfTime;
00075     m_bDouble = true;
00076     
00077     SetFresh(true);
00078 
00079     return true;
00080 }
00081 
00082 bool CMOOSVariable::Set(const std::string & sVal,double dfTime)
00083 {
00084     m_sVal = sVal;
00085     m_dfTimeWritten = dfTime;
00086     m_bDouble = false;
00087     SetFresh(true);
00088 
00089     return true;
00090 
00091 }
00092 
00093 bool CMOOSVariable::Set(const CMOOSMsg &Msg)
00094 {
00095     switch(Msg.m_cDataType)
00096     {
00097     case MOOS_DOUBLE:
00098         m_bDouble = true;
00099         m_dfVal = Msg.m_dfVal;
00100     
00101         break;
00102     case MOOS_STRING:
00103         case MOOS_BINARY_STRING:
00104         m_bDouble = false;
00105         m_sVal = Msg.m_sVal;
00106         break;
00107     }
00108 
00109     m_dfTimeWritten = Msg.m_dfTime;
00110 
00111     m_sName = Msg.m_sKey;
00112 
00113     m_sSrc = Msg.m_sSrc;
00114 
00115     m_bFresh = true;
00116 
00117     return true;
00118 
00119 }
00120 
00121 bool CMOOSVariable::SetFresh(bool bFresh) 
00122 {
00123     m_bFresh = bFresh;
00124 
00125     return true;
00126 }
00127 
00128 std::string CMOOSVariable::GetSubscribeName() const
00129 {
00130     return  m_sSubscribeName;
00131 }
00132 
00133 bool CMOOSVariable::IsFresh() const
00134 {
00135     return m_bFresh;
00136 }
00137 
00138 std::string CMOOSVariable::GetName() const
00139 {
00140     return m_sName;
00141 }
00142 
00143 std::string CMOOSVariable::GetPublishName() const
00144 {
00145     return m_sPublishName;
00146 }
00147 
00148 bool CMOOSVariable::IsDouble() const
00149 {
00150     return m_bDouble;
00151 }
00152 
00153 double CMOOSVariable::GetDoubleVal() const
00154 {
00155     return m_dfVal;
00156 }
00157 
00158 double CMOOSVariable::GetTime() const
00159 {
00160     return m_dfTimeWritten;
00161 }
00162 
00163 std::string CMOOSVariable::GetStringVal()  const
00164 {
00165     return m_sVal;
00166 }
00167 
00168 std::string CMOOSVariable::GetAsString(int nFieldWidth) const
00169 {
00170     ostringstream os;
00171 
00172     os.setf(ios::left);
00173 
00174     if(GetTime()!=-1)
00175     {        
00176         if(IsDouble())
00177         {
00178             os<<setw(nFieldWidth)<<m_dfVal<<ends;       
00179         }
00180         else
00181         {
00182             os<<m_sVal.c_str()<<ends;
00183         }
00184 
00185     }
00186     else
00187     {
00188         os<<setw(nFieldWidth)<<"NaN"<<ends;       
00189     }
00190 
00191     std::string sResult = os.str();
00192 
00193     return sResult;
00194 }
00195 
00196 double CMOOSVariable::GetAge(double dfTimeNow) const
00197 {
00198     return dfTimeNow-m_dfTimeWritten;
00199 }
00200 
00201 std::string CMOOSVariable::GetWriter() const
00202 {
00203     return m_sSrc;
00204 }
00205 
00206 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines