WBatchLoader.cpp

00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <string>
00026 #include <vector>
00027 
00028 #include "WModule.h"
00029 #include "WDataModule.h"
00030 #include "WModuleContainer.h"
00031 #include "WModuleFactory.h"
00032 
00033 #include "WBatchLoader.h"
00034 
00035 WBatchLoader::WBatchLoader( std::vector< std::string > fileNames, boost::shared_ptr< WModuleContainer > targetContainer ):
00036     WThreadedRunner(),
00037     boost::enable_shared_from_this< WBatchLoader >(),
00038     m_fileNamesToLoad( fileNames ),
00039     m_targetContainer( targetContainer )
00040 {
00041     // initialize members
00042 }
00043 
00044 WBatchLoader::~WBatchLoader()
00045 {
00046     // cleanup
00047 }
00048 
00049 void WBatchLoader::run()
00050 {
00051     // the module needs to be added here, as it else could be freed before the thread finishes ( remember: it is a shared_ptr).
00052     m_targetContainer->addPendingThread( shared_from_this() );
00053 
00054     // actually run
00055     WThreadedRunner::run();
00056 }
00057 
00058 void WBatchLoader::threadMain()
00059 {
00060     // add a new data module for each file to load
00061     for( std::vector< std::string >::iterator iter = m_fileNamesToLoad.begin(); iter != m_fileNamesToLoad.end(); ++iter )
00062     {
00063         boost::shared_ptr< WModule > mod = WModuleFactory::getModuleFactory()->create(
00064                 WModuleFactory::getModuleFactory()->getPrototypeByName( "Data Module" )
00065         );
00066 
00067         // set the filename
00068         boost::shared_static_cast< WDataModule >( mod )->setFilename( *iter );
00069 
00070         m_targetContainer->add( mod );
00071         // serialize loading of a couple of data sets
00072         // ignore the case where isCrashed is true
00073         mod->isReadyOrCrashed().wait();
00074     }
00075 
00076     m_targetContainer->finishedPendingThread( shared_from_this() );
00077 }
00078 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends