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

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

    00001
    00002 //
    00003 // Copyright (c) 2006           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 "ReferenceSerializer.h"
    00012 #include <Mers/Models/Base/BaseModel.h>
    00013 #include <Mers/Utils/Io/SaveLoad/ISaveLoad.h>
    00014 #include <Mers/Utils/Lang/String.h>
    00015 #include <Mers/Utils/Exceptions/RuntimeException.h>
    00016 #include <cassert>
    00017
    00019
    00020 using namespace Mers::Models::Base::Io::SaveLoad;
    00021 using Mers::Models::Base::BaseModel;
    00022 using Mers::Models::Base::BaseModelVariable;
    00023 using Mers::Utils::Io::SaveLoad::ISaveLoad;
    00024 using Mers::Utils::Lang::String;
    00025 using Mers::Utils::Exceptions::RuntimeException;
    00026
    00028
    00029 ReferenceSerializer::ReferenceSerializer( BaseModel * model ) :
    00030         m_model( model )
    00031 {
    00032 }
    00033
    00035
    00036 const char *
    00037 ReferenceSerializer::getTypeName( const BaseModelVariable * )
    00038 {
    00039         return "Ref$Mers::Models::Base::Variable";
    00040 }
    00041
    00043
    00044 namespace {
    00049 BaseModelVariable * saveLoadVariable( ISaveLoad * sl, BaseModel * model,
    00050         const BaseModelVariable * var )
    00051 {
    00052         String variable;
    00053
    00054         // Get the data from the variable.
    00055         if( sl->isSaving() ) {
    00056                 assert( var != NULL );
    00057                 variable = var->getName();
    00058         }
    00059
    00060         // save/load the data.
    00061         sl->saveLoad( "name", variable );
    00062
    00063         // If loading, find the BaseModelVariable from the data.
    00064         if( sl->isLoading() ) {
    00065                 // We must have a model
    00066                 assert( model != NULL );
    00067
    00068                 // Find our variable
    00069                 var = model->getVariable( variable );
    00070
    00071                 // Make sure we found the variable.
    00072                 if( var == NULL )
    00073                         throw RuntimeException( String( "Unable to load variable " ) +
    00074                                 variable, __FILE__, __LINE__ );
    00075         }
    00076
    00077         // Return the variable
    00078         return const_cast< BaseModelVariable * >( var );
    00079 }
    00080 }
    00081
    00083
    00084 void ReferenceSerializer::saveLoad( ISaveLoad * sl, const char *,
    00085         const BaseModelVariable * &var )
    00086 {
    00087         var = saveLoadVariable( sl, m_model, var );
    00088 }
    00089
    00090
    00092
    00093 void ReferenceSerializer::saveLoad( ISaveLoad * sl, const char *,
    00094         BaseModelVariable * &var )
    00095 {
    00096         var = saveLoadVariable( sl, m_model, var );
    00097 }
    00098
    00100