Commit 4e5221c3 authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum

[CHANGE] - polished revived WDataHandler

parent 86f5db4f
......@@ -32,8 +32,8 @@
/**
* This class provides a common interface for thread-safe access to sequence containers (list, vector, dequeue ).
*/
template < typename T >
class WSharedSequenceContainer: public WSharedObject< T >
template < typename T, typename S >
class WSharedSequenceContainer: public WSharedObject< S >
{
public:
......@@ -47,9 +47,92 @@ public:
*/
virtual ~WSharedSequenceContainer();
//////////////////////////////////////////////////////////////////////////////////////////
// These methods implement common methods of all sequence containers. The list is not
// complete but should be enough for now.
// \NOTE: all methods using or returning iterators are NOT implemented here. Use the access
// Object (getAccessObject) to iterate.
//////////////////////////////////////////////////////////////////////////////////////////
/**
* Adds a new element at the end of the container.
*
* \param x the new element.
*/
void push_back( const T& x );
/**
* Removes an element from the end.
*/
void pop_back();
/**
* Clears the container.
*/
void clear();
/**
* The size of the container.
*
* \return the size.
*
* \note: be aware that the size can change at every moment after getting the size, since the read lock got freed. Better use
* access objects to lock the container and use size() on the container directly.
*/
size_t size();
protected:
private:
};
template < typename T, typename S >
WSharedSequenceContainer< T, S >::WSharedSequenceContainer():
WSharedObject< S >()
{
// init members
}
template < typename T, typename S >
WSharedSequenceContainer< T, S >::~WSharedSequenceContainer()
{
// clean up
}
template < typename T, typename S >
void WSharedSequenceContainer< T, S >::push_back( const T& x )
{
typename WSharedObject< S >::WSharedAccess a = WSharedObject< S >::getAccessObject();
a->beginWrite();
a->get().push_back( x );
}
template < typename T, typename S >
void WSharedSequenceContainer< T, S >::pop_back()
{
typename WSharedObject< S >::WSharedAccess a = WSharedObject< S >::getAccessObject();
a->beginWrite();
a->get().pop_back();
}
template < typename T, typename S >
void WSharedSequenceContainer< T, S >::clear()
{
typename WSharedObject< S >::WSharedAccess a = WSharedObject< S >::getAccessObject();
a->beginWrite();
a->get().clear();
}
template < typename T, typename S >
size_t WSharedSequenceContainer< T, S >::size()
{
typename WSharedObject< S >::WSharedAccess a = WSharedObject< S >::getAccessObject();
a->beginRead();
size_t size = a->get().size();
return size;
// NOTE: the lock in access object a is freed automatically
}
#endif // WSHAREDSEQUENCECONTAINER_H
......@@ -22,32 +22,91 @@
//
//---------------------------------------------------------------------------
#include <algorithm>
#include <string>
#include <vector>
#include "WDataHandler.h"
#include <boost/lexical_cast.hpp>
#include "WSubject.h"
#include "exceptions/WDHNoSuchDataSet.h"
#include "exceptions/WDHNoSuchSubject.h"
#include "../common/WLogger.h"
#include "WDataHandler.h"
// instance as singleton
boost::shared_ptr< WDataHandler > WDataHandler::m_instance = boost::shared_ptr< WDataHandler >();
WDataHandler::WDataHandler():
m_subjects(),
m_subjectAccess( m_subjects.getAccessObject() )
{
WLogger::getLogger()->addLogMessage( "Initializing Data Handler", "Data Handler", LL_INFO );
}
WDataHandler::~WDataHandler()
{
}
void WDataHandler::addSubject( boost::shared_ptr< WSubject > subject )
{
WLogger::getLogger()->addLogMessage( "Adding subject with ID \"" +
boost::lexical_cast< std::string >( subject->getPersonalInformation().getSubjectID() ) + "\" and Name \""
+ subject->getName() + "\".",
"Data Handler", LL_INFO );
// simply add the new subject
m_subjectAccess->beginWrite();
m_subjectAccess->get().insert( subject );
m_subjectAccess->endWrite();
m_subjects.push_back( subject );
}
void WDataHandler::removeSubject( boost::shared_ptr< WSubject > subject )
{
// simply add the new subject
m_subjectAccess->beginWrite();
m_subjectAccess->get().erase( subject );
WLogger::getLogger()->addLogMessage( "Removing subject with ID \"" +
boost::lexical_cast< std::string >( subject->getPersonalInformation().getSubjectID() ) + "\" and Name \""
+ subject->getName() + "\".",
"Data Handler", LL_INFO );
// iterate and find, remove
remove( m_subjectAccess->get().begin(), m_subjectAccess->get().end(), subject );
m_subjectAccess->endWrite();
}
WDataHandler::SubjectSharedContainerType::WSharedAccess WDataHandler::getAccessObject()
{
return m_subjects.getAccessObject();
}
boost::shared_ptr< WSubject > WDataHandler::getSubjectByID( size_t subjectID )
{
m_subjectAccess->beginRead();
// search it
boost::shared_ptr< WSubject > result;
try
{
result = m_subjectAccess->get().at( subjectID );
}
catch( std::out_of_range& e )
{
throw WDHNoSuchSubject();
}
m_subjectAccess->endWrite();
return result;
}
boost::shared_ptr< WDataHandler > WDataHandler::getDataHandler()
{
if ( !m_instance )
{
m_instance = boost::shared_ptr< WDataHandler >( new WDataHandler() );
}
return m_instance;
}
......@@ -26,13 +26,14 @@
#define WDATAHANDLER_H
#include <string>
#include <set>
#include <vector>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include "../common/WSharedObject.h"
#include "../common/WSharedSequenceContainer.h"
#include "WDataSet.h"
......@@ -53,17 +54,33 @@ class WDataHandler
friend class WDataHandlerTest;
public:
/**
* For shortening: a type defining a shared vector of WSubject pointers.
*/
typedef std::set< boost::shared_ptr< WSubject > > SubjectContainerType;
typedef std::vector< boost::shared_ptr< WSubject > > SubjectContainerType;
/**
* The alias for a shared container.
*/
typedef WSharedSequenceContainer< boost::shared_ptr< WSubject >, SubjectContainerType > SubjectSharedContainerType;
/**
* Empty standard constructor.
*/
WDataHandler();
/**
* Destructor.
*/
virtual ~WDataHandler();
/**
* As WDataHandler is a singleton -> return instance.
*
* \return the instance.
*/
static boost::shared_ptr< WDataHandler > getDataHandler();
/**
* Insert a new subject referenced by a pointer.
*
......@@ -78,24 +95,43 @@ public:
*/
void removeSubject( boost::shared_ptr< WSubject > subject );
/**
* Returns the subject which corresponds to the specified ID. It throws an exception, if the subject does not exists anymore.
*
* \param subjectID the ID to search the subject for
*
* \return the subject.
*
* \throw WNoSuchSubject in case the subject can't be found.
*/
boost::shared_ptr< WSubject > getSubjectByID( size_t subjectID );
/**
* Gets an access object which allows thread save iteration over the subjects.
*
* \return the access object.
*/
SubjectSharedContainerType::WSharedAccess getAccessObject();
protected:
/**
* A container for all WSubjects.
*/
WSharedObject< SubjectContainerType > m_subjects;
SubjectSharedContainerType m_subjects;
/**
* The access object used for thread safe access.
*/
WSharedObject< SubjectContainerType >::WSharedAccess m_subjectAccess;
SubjectSharedContainerType::WSharedAccess m_subjectAccess;
private:
/**
* The lock used for avoiding mutual write to the subjects list
* Singleton instance of WDataHandler.
*/
boost::shared_mutex m_subjectsLock;
static boost::shared_ptr< WDataHandler > m_instance;
private:
};
/**
......
......@@ -45,6 +45,11 @@ std::string WSubject::getName() const
return m_personalInfo.getLastName() + ", " + m_personalInfo.getFirstName() + " " + m_personalInfo.getMiddleName();
}
WPersonalInformation WSubject::getPersonalInformation() const
{
return m_personalInfo;
}
boost::shared_ptr< WDataSet > WSubject::getDataSet( const unsigned int dataSetId ) const
{
if( dataSetId >= m_dataSets.size() )
......
......@@ -45,22 +45,34 @@ class WSubject
friend class WSubjectTest;
public:
/**
* Constructs a dummy subject.
*/
WSubject();
/**
* Allows to give the subject information the person during construction
* Allows to give the subject information the person during construction.
*
* \param personInfo personal information object
*/
explicit WSubject( WPersonalInformation personInfo );
/**
* Returns the name of the subject. See WSubject::m_name for details on the name.
* Returns the name of the subject. See WPersonalInformation for details on the name.
*
* \return the name of the subject extracted from this subject's WPersonalInformation.
*/
std::string getName() const;
/**
* Gives the personal information of a subject.
*
* \return
*/
WPersonalInformation getPersonalInformation() const;
/**
* Get the pointer to the i'th WDataSet. The return type is const since we
* want to ensure that each DataSet cannot modified after retrival.
......@@ -75,19 +87,15 @@ public:
*/
boost::shared_ptr< const WDataSet > operator[]( const unsigned int dataSetId ) const;
/**
* Insert a new DataSet referenced by a pointer.
*
* \param newDataSet This WDataSet will be added
*/
void addDataSet( boost::shared_ptr< WDataSet > newDataSet );
/**
* Get the number of DataSets which are actually handled by our subject.
*/
unsigned int getNumberOfDataSets() const;
protected:
private:
WPersonalInformation m_personalInfo; //!< Information on the person represented by this WSubject.
/**
......
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <string>
#include "WDHNoSuchSubject.h"
WDHNoSuchSubject::WDHNoSuchSubject( const std::string& msg )
: WDHException( msg )
{
// initialize members
}
WDHNoSuchSubject::~WDHNoSuchSubject() throw()
{
// cleanup
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WDHNOSUCHSUBJECT_H
#define WDHNOSUCHSUBJECT_H
#include <string>
#include "WDHException.h"
/**
* Should be thrown when an invalid index is used to get a WSubject from the
* WDataHandler. An index is invalid if it's greater or equal than the number
* of WSubjects in WDataHandler.
*
* \ingroup dataHandler
*/
class WDHNoSuchSubject : public WDHException
{
public:
/**
* Constructs new exception.
* \param msg the reason for this exception.
*/
explicit WDHNoSuchSubject( const std::string& msg = "DataHandler Exception: Requested subject does not exist." );
/**
* Destroys this exception
*/
virtual ~WDHNoSuchSubject() throw();
protected:
private:
};
#endif // WDHNOSUCHSUBJECT_H
......@@ -38,6 +38,7 @@
#include <boost/filesystem.hpp>
#include <boost/thread/xtime.hpp>
#include "../dataHandler/WDataHandler.h"
#include "../common/WPreferences.h"
#include "../common/WStringUtils.h"
#include "WBatchLoader.h"
......@@ -96,10 +97,12 @@ void WKernel::init()
findAppPath();
m_roiManager = boost::shared_ptr< WROIManagerFibers >( new WROIManagerFibers() );
// get module factory
m_moduleFactory = WModuleFactory::getModuleFactory();
// init data handler
WDataHandler::getDataHandler();
// initialize module container
m_moduleContainer = boost::shared_ptr< WModuleContainer >( new WModuleContainer( "KernelRootContainer",
"Root module container in Kernel." ) );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment