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

# /Users/fpy/titan-1-5-MBARI/Mers/Models/Base/Io/SaveLoad/VariableSerializer.cpp

    00001
    00002 //
    00003 // Copyright (c) 2005           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
    00012 // This file implements the BaseModelVariable portion of the Serializer
    00013 // class.
    00015
    00016
    00017 #include "Serializer.h"
    00018 #include <Mers/Utils/Io/SaveLoad/ISaveLoad.h>
    00019 #include <Mers/Models/Base/BaseModel.h>
    00020 #include <Mers/Models/Base/BaseModelVariable.h>
    00021 #include <Mers/Utils/Lang/String.h>
    00022 #include <cassert>
    00023
    00025
    00026 using namespace Mers::Models::Base::Io::SaveLoad;
    00027 using Mers::Utils::Io::SaveLoad::ISaveLoad;
    00028 using Mers::Models::Base::BaseModel;
    00029 using Mers::Models::Base::BaseModelVariable;
    00030 using Mers::Utils::Lang::String;
    00031
    00033
    00034 void Serializer::saveLoad( ISaveLoad * sl, const char *,
    00035         BaseModelVariable * &var )
    00036 {
    00037         String name;
    00038         String type;
    00039         String domainName;
    00040
    00041         // Get the data from the variable.
    00042         if( sl->isSaving() ) {
    00043                 assert( var != NULL );
    00044                 assert( var->getName() != NULL );
    00045                 name = var->getName();
    00046                 type = BaseModelVariable::variableTypeToString(
    00047                         var->getVariableType() );
    00048                 assert( var->getDomain()->getName() != NULL );
    00049                 domainName = var->getDomain()->getName();
    00050         }
    00051
    00052         // save/load the data.
    00053         sl->saveLoad( "name", name );
    00054         sl->saveLoad( "variable_type", type );
    00055         sl->saveLoad( "domain_name", domainName );
    00056
    00057         // If loading, find the BaseModelVariable from the data.
    00058         if( sl->isLoading() ) {
    00059                 // We must have a model
    00060                 assert( m_model != NULL );
    00061
    00062                 // Translate our variable type.
    00063                 BaseModelVariable::VariableType varType;
    00064                 if( FAILED( BaseModelVariable::stringToVariableType(
    00065                         type, varType ) ) )
    00066                 {
    00067                         // Default to dependent
    00068                         varType = BaseModelVariable::Dependent;
    00069                         // Log an error.
    00070                         assert( false && "Variable type was unknown." );
    00071                 }
    00072
    00073                 // Get the domain of the variable.
    00074                 BaseModelDomain * dom = m_model->getDomain( domainName );
    00075                 assert( dom != NULL );
    00076
    00077                 // Create our variable
    00078                 m_model->addVariable( name.c_str(), varType, dom );
    00079                 var = m_model->getVariable( name );
    00080                 assert( var != NULL );
    00081         }
    00082 }
    00083
    00085