- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Executives/DeductiveController/Observation/DiscreteVariable.cpp

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00010
    00011 #include "DiscreteVariable.h"
    00012 #include "DiscreteValue.h"
    00013
    00014 #include <Mers/Utils/Exceptions/IllegalArgumentException.h>
    00015
    00017
    00018 using namespace Mers::Executives::DeductiveController::Observation;
    00019 using Mers::Utils::Lang::ReferencedObject;
    00020 using Mers::Utils::Exceptions::IllegalArgumentException;
    00021
    00023
    00024 DiscreteVariable::DiscreteVariable( const char * name )
    00025         : ::Mers::Interfaces::DiscreteVariable( name ), ReferencedObject(),
    00026         m_values( NULL )
    00027 {
    00028 }
    00029
    00031
    00032 DiscreteVariable::~DiscreteVariable()
    00033 {
    00034         // Remove our reference, if we have an object.
    00035         if( m_values != NULL )
    00036                 m_values->removeReference();
    00037 }
    00038
    00040
    00041 void DiscreteVariable::setValues( DiscreteValue * values )
    00042 {
    00043         // Make sure we add a reference before we remove the reference as we
    00044         // might be getting set with the object we already have, and we
    00045         // don't want it to get deleted.
    00046         if( values != NULL )
    00047                 values->addReference();
    00048
    00049         if( m_values != NULL )
    00050                 m_values->removeReference();
    00051
    00052         // Record our new values.
    00053         m_values = values;
    00054
    00055 #ifdef _DEBUG
    00056         // Make sure our invariant that the value's parent is this object hold true.
    00057         for( DiscreteValue * dvValue = m_values; dvValue != NULL;
    00058                 dvValue = dvValue->getNext() ) {
    00059                 // Make sure our invariant holds.
    00060                 if( dvValue->getVariable() != this ) {
    00061                         throw new IllegalArgumentException( "The values set did not all share "
    00062                                 "this object as their parent.", __FILE__, __LINE__ );
    00063                 }
    00064         }
    00065 #endif
    00066 }
    00067
    00069
    00070 DiscreteValue * DiscreteVariable::getValue( double value ) const
    00071 {
    00072         DiscreteValue * dValue;
    00073
    00074         // Search through the list until we find the first match.
    00075         for( dValue = m_values; dValue != NULL; dValue = dValue->getNext() ) {
    00076                 // see if the value is in range for this object.
    00077                 if( dValue->isInRange( value ) )
    00078                         // It's in range, return it.
    00079                         return dValue;
    00080         }
    00081         // We didn't find anything, return NULL.
    00082         return NULL;
    00083 }
    00084