State.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Class which externalizes the state of an Object.
6  *
7  *
8  *
9  * \author O. Krause
10  * \date 2012
11  *
12  *
13  * \par Copyright 1995-2017 Shark Development Team
14  *
15  * <BR><HR>
16  * This file is part of Shark.
17  * <http://shark-ml.org/>
18  *
19  * Shark is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU Lesser General Public License as published
21  * by the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * Shark is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License
30  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
33 //===========================================================================
34 
35 #ifndef SHARK_CORE_STATE_H
36 #define SHARK_CORE_STATE_H
37 
38 #include <boost/cast.hpp>
39 
40 namespace shark{
41 
42 
43 /// \brief Represents the State of an Object.
44 ///
45 /// Often the State of an object is changed during usage.
46 /// This, however, makes it impossible to use the object in a
47 /// multithreaded environment in parallel. The solution is to externalize
48 /// the state, so that every thread can have it's own storage.
49 struct State{
50 
51  ///\brief prevents that this class can be instantiated
52  virtual ~State(){};
53 
54  ///\brief Safely downcast State to it's derived type.
55  ///
56  ///Tries to do a safe cast from State to it's derived type.
57  ///The program is terminated in debug mode, if the wrong Type
58  ///was used.
59  template<class DerivedStateType>
60  DerivedStateType const& toState()const{
61  return *boost::polymorphic_downcast<DerivedStateType const*>(this);
62  }
63 
64  template<class DerivedStateType>
65  DerivedStateType & toState(){
66  return *boost::polymorphic_downcast<DerivedStateType*>(this);
67  }
68 };
69 
70 ///\brief Default State of an Object which does not need a State
71 struct EmptyState:public State{
72 };
73 }
74 #endif