Commit e4c4b1c7 authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum

[CHANGE] - WSubject now also uses new SharedObject class to handle its list

parent 5bf2c027
......@@ -54,7 +54,7 @@ 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 );
"Data Handler", LL_DEBUG );
// simply add the new subject
m_subjects.push_back( subject );
......@@ -67,7 +67,7 @@ void WDataHandler::removeSubject( boost::shared_ptr< WSubject > subject )
WLogger::getLogger()->addLogMessage( "Removing subject with ID \"" +
boost::lexical_cast< std::string >( subject->getPersonalInformation().getSubjectID() ) + "\" and Name \""
+ subject->getName() + "\".",
"Data Handler", LL_INFO );
"Data Handler", LL_DEBUG );
// iterate and find, remove
remove( m_subjectAccess->get().begin(), m_subjectAccess->get().end(), subject );
......@@ -75,6 +75,27 @@ void WDataHandler::removeSubject( boost::shared_ptr< WSubject > subject )
m_subjectAccess->endWrite();
}
void WDataHandler::clear()
{
m_subjectAccess->beginWrite();
WLogger::getLogger()->addLogMessage( "Removing all subjects.", "Data Handler", LL_INFO );
for ( SubjectContainerType::const_iterator iter = m_subjectAccess->get().begin(); iter != m_subjectAccess->get().end();
++iter )
{
WLogger::getLogger()->addLogMessage( "Removing subject \"" +
boost::lexical_cast< std::string >( ( *iter )->getPersonalInformation().getSubjectID() ) + "\".",
"Data Handler", LL_DEBUG );
( *iter )->clear();
}
m_subjectAccess->get().clear();
m_subjectAccess->endWrite();
}
WDataHandler::SubjectSharedContainerType::WSharedAccess WDataHandler::getAccessObject()
{
return m_subjects.getAccessObject();
......
......@@ -54,6 +54,7 @@ class WDataHandler
friend class WDataHandlerTest;
public:
/**
* For shortening: a type defining a shared vector of WSubject pointers.
*/
......@@ -95,6 +96,11 @@ public:
*/
void removeSubject( boost::shared_ptr< WSubject > subject );
/**
* Remove all subjects.
*/
void clear();
/**
* Returns the subject which corresponds to the specified ID. It throws an exception, if the subject does not exists anymore.
*
......
......@@ -24,19 +24,25 @@
#include <string>
#include <boost/lexical_cast.hpp>
#include "../common/WLogger.h"
#include "WSubject.h"
#include "exceptions/WDHNoSuchDataSet.h"
WSubject::WSubject()
: m_personalInfo( WPersonalInformation::createDummyInformation() ),
m_dataSets( 0 )
WSubject::WSubject():
m_datasets(),
m_datasetAccess( m_datasets.getAccessObject() ),
m_personalInfo( WPersonalInformation::createDummyInformation() )
{
}
WSubject::WSubject( WPersonalInformation personInfo )
: m_personalInfo( personInfo ),
m_dataSets( 0 )
WSubject::WSubject( WPersonalInformation personInfo ):
m_datasets(),
m_datasetAccess( m_datasets.getAccessObject() ),
m_personalInfo( personInfo )
{
}
......@@ -50,24 +56,54 @@ WPersonalInformation WSubject::getPersonalInformation() const
return m_personalInfo;
}
boost::shared_ptr< WDataSet > WSubject::getDataSet( const unsigned int dataSetId ) const
void WSubject::addDataSet( boost::shared_ptr< WDataSet > dataset )
{
if( dataSetId >= m_dataSets.size() )
throw WDHNoSuchDataSet( "Index too large." );
return m_dataSets.at( dataSetId );
// simply add the new dataset
m_datasets.push_back( dataset );
}
void WSubject::removeDataSet( boost::shared_ptr< WDataSet > dataset )
{
m_datasetAccess->beginWrite();
// iterate and find, remove
remove( m_datasetAccess->get().begin(), m_datasetAccess->get().end(), dataset );
m_datasetAccess->endWrite();
}
boost::shared_ptr< const WDataSet > WSubject::operator[]( const unsigned int dataSetId ) const
void WSubject::clear()
{
return getDataSet( dataSetId );
m_datasetAccess->beginWrite();
// iterate and find, remove
m_datasetAccess->get().clear();
m_datasetAccess->endWrite();
}
/*void WSubject::addDataSet( boost::shared_ptr< WDataSet > newDataSet )
boost::shared_ptr< WDataSet > WSubject::getDataSetByID( size_t datasetID )
{
m_dataSets.push_back( newDataSet );
}*/
m_datasetAccess->beginRead();
// search it
boost::shared_ptr< WDataSet > result;
try
{
result = m_datasetAccess->get().at( datasetID );
}
catch( std::out_of_range& e )
{
throw WDHNoSuchDataSet();
}
m_datasetAccess->endWrite();
unsigned int WSubject::getNumberOfDataSets() const
return result;
}
WSubject::DatasetSharedContainerType::WSharedAccess WSubject::getAccessObject()
{
return m_dataSets.size();
return m_datasets.getAccessObject();
}
......@@ -29,6 +29,9 @@
#include <vector>
#include <boost/shared_ptr.hpp>
#include "../common/WSharedObject.h"
#include "../common/WSharedSequenceContainer.h"
#include "WPersonalInformation.h"
class WDataSet;
......@@ -46,6 +49,16 @@ class WSubject
public:
/**
* For shortening: a type defining a shared vector of WSubject pointers.
*/
typedef std::vector< boost::shared_ptr< WDataSet > > DatasetContainerType;
/**
* The alias for a shared container.
*/
typedef WSharedSequenceContainer< boost::shared_ptr< WDataSet >, DatasetContainerType > DatasetSharedContainerType;
/**
* Constructs a dummy subject.
*/
......@@ -73,34 +86,59 @@ public:
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.
* \param dataSetId the number of the data set to retrieve
* Insert a new dataset referenced by a pointer.
*
* \param dataset a pointer to the dataset that will be added
*/
boost::shared_ptr< WDataSet > getDataSet( const unsigned int dataSetId ) const;
void addDataSet( boost::shared_ptr< WDataSet > dataset );
/**
* Returns a shared_ptr to the i'th WSubject. The return type is const since we
* want to ensure that a subject cannot be modified after retrieval.
* \param dataSetId the number of the data set to retrieve
* Removes the specified dataset if it is in the set.
*
* \param dataset the dataset to remove.
*/
boost::shared_ptr< const WDataSet > operator[]( const unsigned int dataSetId ) const;
void removeDataSet( boost::shared_ptr< WDataSet > dataset );
/**
* Get the number of DataSets which are actually handled by our subject.
* Remove all datasets from the subjects.
*/
unsigned int getNumberOfDataSets() const;
void clear();
protected:
/**
* Returns the dataset which corresponds to the specified ID. It throws an exception, if the dataset does not exists anymore.
*
* \param datasetID the ID to search the dataset for
*
* \return the dataset.
*
* \throw WNoSuchDataSet in case the dataset can't be found.
*
* \note you should avoid this function. Do NOT store ID's. They may change.
*/
boost::shared_ptr< WDataSet > getDataSetByID( size_t datasetID );
private:
/**
* Gets an access object which allows thread save iteration over the datasets.
*
* \return the access object.
*/
DatasetSharedContainerType::WSharedAccess getAccessObject();
WPersonalInformation m_personalInfo; //!< Information on the person represented by this WSubject.
protected:
/**
* A container for all WDataSets belonging to the subject.
* A container for all WDataSet.
*/
std::vector< boost::shared_ptr< WDataSet > > m_dataSets;
DatasetSharedContainerType m_datasets;
/**
* The access object used for thread safe access.
*/
DatasetSharedContainerType::WSharedAccess m_datasetAccess;
private:
WPersonalInformation m_personalInfo; //!< Information on the person represented by this WSubject.
};
#endif // WSUBJECT_H
......@@ -141,6 +141,9 @@ void WKernel::finalize()
// NOTE: stopping a container erases all modules inside.
getRootContainer()->stop();
WLogger::getLogger()->addLogMessage( "Stopping Data Handler", "Kernel", LL_INFO );
WDataHandler::getDataHandler()->clear();
}
void WKernel::threadMain()
......
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