OpenWalnut  1.4.0
WDataHandler.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <algorithm>
26 #include <string>
27 #include <vector>
28 
29 
30 #include "WSubject.h"
31 #include "exceptions/WDHNoSuchSubject.h"
32 
33 #include "../common/WLogger.h"
34 #include "../common/WStringUtils.h"
35 
36 #include "WDataHandler.h"
37 
38 // instance as singleton
39 boost::shared_ptr< WDataHandler > WDataHandler::m_instance = boost::shared_ptr< WDataHandler >();
40 
42  m_subjects()
43 {
44  WLogger::getLogger()->addLogMessage( "Initializing Data Handler", "Data Handler", LL_INFO );
45  addSubject( boost::shared_ptr< WSubject >( new WSubject( WPersonalInformation::createDummyInformation() ) ) );
46 }
47 
49 {
50 }
51 
52 void WDataHandler::addSubject( boost::shared_ptr< WSubject > subject )
53 {
54  WLogger::getLogger()->addLogMessage( "Adding subject with ID \"" +
55  string_utils::toString( subject->getPersonalInformation().getSubjectID() ) + "\" and Name \""
56  + subject->getName() + "\".",
57  "Data Handler", LL_DEBUG );
58 
59  // simply add the new subject
60  m_subjects.push_back( subject );
61 }
62 
63 void WDataHandler::removeSubject( boost::shared_ptr< WSubject > subject )
64 {
66 
67  WLogger::getLogger()->addLogMessage( "Removing subject with ID \"" +
68  string_utils::toString( subject->getPersonalInformation().getSubjectID() ) + "\" and Name \""
69  + subject->getName() + "\".",
70  "Data Handler", LL_DEBUG );
71 
72  // iterate and find, remove
73  for( SubjectContainerType::iterator iter = l->get().begin(); iter != l->get().end();
74  ++iter )
75  {
76  if( ( *iter ) == subject )
77  {
78  l->get().erase( iter );
79  break;
80  }
81  }
82 }
83 
85 {
87 
88  WLogger::getLogger()->addLogMessage( "Removing all subjects.", "Data Handler", LL_INFO );
89 
90  for( SubjectContainerType::const_iterator iter = l->get().begin(); iter != l->get().end();
91  ++iter )
92  {
93  WLogger::getLogger()->addLogMessage( "Removing subject \"" +
94  string_utils::toString( ( *iter )->getPersonalInformation().getSubjectID() ) + "\".",
95  "Data Handler", LL_DEBUG );
96 
97  ( *iter )->clear();
98  }
99 
100  l->get().clear();
101 }
102 
104 {
105  return m_subjects.getReadTicket();
106 }
107 
108 boost::shared_ptr< WSubject > WDataHandler::getSubjectByID( size_t subjectID )
109 {
111 
112  // search it
113  boost::shared_ptr< WSubject > result;
114  try
115  {
116  if( subjectID < l->get().size() )
117  {
118  result = l->get().at( subjectID );
119  }
120  else
121  {
122  result = boost::shared_ptr< WSubject >();
123  }
124  }
125  catch( const std::out_of_range& e )
126  {
127  throw WDHNoSuchSubject();
128  }
129 
130  return result;
131 }
132 
133 boost::shared_ptr< WDataHandler > WDataHandler::getDataHandler()
134 {
135  if( !m_instance )
136  {
137  m_instance = boost::shared_ptr< WDataHandler >( new WDataHandler() );
138  }
139 
140  return m_instance;
141 }
142 
143 boost::shared_ptr< WSubject > WDataHandler::getDefaultSubject()
144 {
145  return getDataHandler()->getSubjectByID( WSubject::SUBJECT_UNKNOWN );
146 }
147 
boost::shared_ptr< WSharedObjectTicketWrite< SubjectContainerType > > WriteTicket
Type for write tickets.
Definition: WSharedObject.h:69
static WPersonalInformation createDummyInformation()
Returns an empty dummy WPersonalInformation object.
void push_back(const typename S::value_type &x)
Adds a new element at the end of the container.
Should be thrown when an invalid index is used to get a WSubject from the WDataHandler.
Container for all WDataSets belonging to one subject or patient.
Definition: WSubject.h:47
virtual ~WDataHandler()
Destructor.
static WLogger * getLogger()
Returns pointer to the currently running logger instance.
Definition: WLogger.cpp:64
SubjectSharedContainerType::ReadTicket getSubjects() const
Returns read-access to the list of subjects.
void addLogMessage(std::string message, std::string source="", LogLevel level=LL_DEBUG)
Appends a log message to the logging queue.
Definition: WLogger.cpp:84
ReadTicket getReadTicket() const
Returns a ticket to get read access to the contained data.
WriteTicket getWriteTicket(bool suppressNotify=false) const
Returns a ticket to get write access to the contained data.
void removeSubject(boost::shared_ptr< WSubject > subject)
Removes the specified subject if it is in the set.
SubjectSharedContainerType m_subjects
A container for all WSubjects.
Definition: WDataHandler.h:154
WDataHandler()
Empty standard constructor.
std::string toString(const T &value)
Convert a given value to a string.
Definition: WStringUtils.h:120
static boost::shared_ptr< WSubject > getDefaultSubject()
Gets the subject with the ID SUBJECT_UNKNOWN.
void clear()
Remove all subjects.
void addSubject(boost::shared_ptr< WSubject > subject)
Insert a new subject referenced by a pointer.
static boost::shared_ptr< WDataHandler > getDataHandler()
As WDataHandler is a singleton -> return instance.
boost::shared_ptr< WSubject > getSubjectByID(size_t subjectID)
Returns the subject which corresponds to the specified ID.
boost::shared_ptr< WSharedObjectTicketRead< SubjectContainerType > > ReadTicket
Type for read tickets.
Definition: WSharedObject.h:64
static boost::shared_ptr< WDataHandler > m_instance
Singleton instance of WDataHandler.
Definition: WDataHandler.h:160