OpenWalnut  1.5.0dev
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 <memory>
27 #include <string>
28 #include <vector>
29 
30 #include "../common/WLogger.h"
31 #include "../common/WStringUtils.h"
32 #include "WDataHandler.h"
33 #include "WSubject.h"
34 #include "exceptions/WDHNoSuchSubject.h"
35 
36 // instance as singleton
37 std::shared_ptr< WDataHandler > WDataHandler::m_instance = std::shared_ptr< WDataHandler >();
38 
40  m_subjects()
41 {
42  WLogger::getLogger()->addLogMessage( "Initializing Data Handler", "Data Handler", LL_INFO );
43  addSubject( std::shared_ptr< WSubject >( new WSubject( WPersonalInformation::createDummyInformation() ) ) );
44 }
45 
47 {
48 }
49 
50 void WDataHandler::addSubject( std::shared_ptr< WSubject > subject )
51 {
52  WLogger::getLogger()->addLogMessage( "Adding subject with ID \"" +
53  string_utils::toString( subject->getPersonalInformation().getSubjectID() ) + "\" and Name \""
54  + subject->getName() + "\".",
55  "Data Handler", LL_DEBUG );
56 
57  // simply add the new subject
58  m_subjects.push_back( subject );
59 }
60 
61 void WDataHandler::removeSubject( std::shared_ptr< WSubject > subject )
62 {
64 
65  WLogger::getLogger()->addLogMessage( "Removing subject with ID \"" +
66  string_utils::toString( subject->getPersonalInformation().getSubjectID() ) + "\" and Name \""
67  + subject->getName() + "\".",
68  "Data Handler", LL_DEBUG );
69 
70  // iterate and find, remove
71  for( SubjectContainerType::iterator iter = l->get().begin(); iter != l->get().end();
72  ++iter )
73  {
74  if( ( *iter ) == subject )
75  {
76  l->get().erase( iter );
77  break;
78  }
79  }
80 }
81 
83 {
85 
86  WLogger::getLogger()->addLogMessage( "Removing all subjects.", "Data Handler", LL_INFO );
87 
88  for( SubjectContainerType::const_iterator iter = l->get().begin(); iter != l->get().end();
89  ++iter )
90  {
91  WLogger::getLogger()->addLogMessage( "Removing subject \"" +
92  string_utils::toString( ( *iter )->getPersonalInformation().getSubjectID() ) + "\".",
93  "Data Handler", LL_DEBUG );
94 
95  ( *iter )->clear();
96  }
97 
98  l->get().clear();
99 }
100 
102 {
103  return m_subjects.getReadTicket();
104 }
105 
106 std::shared_ptr< WSubject > WDataHandler::getSubjectByID( size_t subjectID )
107 {
109 
110  // search it
111  std::shared_ptr< WSubject > result;
112  try
113  {
114  if( subjectID < l->get().size() )
115  {
116  result = l->get().at( subjectID );
117  }
118  else
119  {
120  result = std::shared_ptr< WSubject >();
121  }
122  }
123  catch( const std::out_of_range& e )
124  {
125  throw WDHNoSuchSubject();
126  }
127 
128  return result;
129 }
130 
131 std::shared_ptr< WDataHandler > WDataHandler::getDataHandler()
132 {
133  if( !m_instance )
134  {
135  m_instance = std::shared_ptr< WDataHandler >( new WDataHandler() );
136  }
137 
138  return m_instance;
139 }
140 
141 std::shared_ptr< WSubject > WDataHandler::getDefaultSubject()
142 {
143  return getDataHandler()->getSubjectByID( WSubject::SUBJECT_UNKNOWN );
144 }
145 
Should be thrown when an invalid index is used to get a WSubject from the WDataHandler.
static std::shared_ptr< WSubject > getDefaultSubject()
Gets the subject with the ID SUBJECT_UNKNOWN.
void clear()
Remove all subjects.
static std::shared_ptr< WDataHandler > getDataHandler()
As WDataHandler is a singleton -> return instance.
virtual ~WDataHandler()
Destructor.
WDataHandler()
Empty standard constructor.
SubjectSharedContainerType::ReadTicket getSubjects() const
Returns read-access to the list of subjects.
void removeSubject(std::shared_ptr< WSubject > subject)
Removes the specified subject if it is in the set.
static std::shared_ptr< WDataHandler > m_instance
Singleton instance of WDataHandler.
Definition: WDataHandler.h:152
SubjectSharedContainerType m_subjects
A container for all WSubjects.
Definition: WDataHandler.h:146
std::shared_ptr< WSubject > getSubjectByID(size_t subjectID)
Returns the subject which corresponds to the specified ID.
void addSubject(std::shared_ptr< WSubject > subject)
Insert a new subject referenced by a pointer.
void addLogMessage(std::string message, std::string source="", LogLevel level=LL_DEBUG)
Appends a log message to the logging queue.
Definition: WLogger.cpp:84
static WLogger * getLogger()
Returns pointer to the currently running logger instance.
Definition: WLogger.cpp:64
static WPersonalInformation createDummyInformation()
Returns an empty dummy WPersonalInformation object.
std::shared_ptr< WSharedObjectTicketRead< T > > ReadTicket
Type for read tickets.
Definition: WSharedObject.h:65
ReadTicket getReadTicket() const
Returns a ticket to get read access to the contained data.
std::shared_ptr< WSharedObjectTicketWrite< T > > WriteTicket
Type for write tickets.
Definition: WSharedObject.h:70
WriteTicket getWriteTicket(bool suppressNotify=false) const
Returns a ticket to get write access to the contained data.
void push_back(const typename S::value_type &x)
Adds a new element at the end of the container.
Container for all WDataSets belonging to one subject or patient.
Definition: WSubject.h:46
std::string toString(const T &value)
Convert a given value to a string.
Definition: WStringUtils.h:120