source: src/CodePatterns/AtomicInstance.hpp@ 084729c

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 084729c was 084729c, checked in by Frederik Heber <heber@…>, 8 years ago

Squashed 'ThirdParty/CodePatterns/' content from commit c1e1041

git-subtree-dir: ThirdParty/CodePatterns
git-subtree-split: c1e10418c454f98be2f43d93167642b0008428fc

  • Property mode set to 100644
File size: 2.8 KB
Line 
1/*
2 * AtomicInstance.hpp
3 *
4 * Created on: Apr 26, 2016
5 * Author: heber
6 */
7
8
9#ifndef ATOMICINSTANCE_HPP_
10#define ATOMICINSTANCE_HPP_
11
12// include config.h
13#ifdef HAVE_CONFIG_H
14#include <config.h>
15#endif
16
17#include <boost/thread/mutex.hpp>
18
19class AtomicInstanceTest;
20
21/** The AtomicInstance class wraps another class with a mutex that is locked
22 * on construction and unlocked on destruction. Hence, only a single entity
23 * of this instance can be present at any single given time and thus allows
24 * convenient atomic access to the state of the wrapped class.
25 *
26 * This is not perfect. The ref to the internal class may be stored elsewhere
27 * and be used after the scope is left and the wrapping AtomicInstance is
28 * destroyed.
29 *
30 * It is up to the user to maintain access to the class only within the lifetime
31 * of this AtomicInstance object, i.e. to use it only within scopes.
32 *
33 * Note that this instance can be moved but not copied. That is on assignment
34 * the contained pointer is NULL'ed in the right-hand side, regardless of whether
35 * the rhs is const or not.
36 */
37template <class T>
38class AtomicInstance
39{
40 //!> grant unit test access to private parts
41 friend class AtomicInstanceTest;
42public:
43 /** Restrict copy cstor to only moves.
44 *
45 * \warning This resets \a content in \a rhs to mark it as being copied.
46 *
47 * \param rhs right-hand side, its \a content is NULL after this
48 */
49 AtomicInstance(AtomicInstance<T>& rhs);
50
51 /** Restrict copy cstor to only moves.
52 *
53 * \warning This resets \a content in \a rhs to mark it as being copied.
54 *
55 * \param rhs right-hand side, its \a content is NULL after this
56 */
57 AtomicInstance(const AtomicInstance<T>& rhs);
58
59 /** Cstor for wrapping the given instant \a _content.
60 *
61 * \param _content instance to wrap
62 */
63 explicit AtomicInstance(T* _content);
64
65 /** Dstor to unlock the mutex.
66 *
67 */
68 ~AtomicInstance();
69
70 /** Getter for the ref to the internal object.
71 *
72 * \warning This must not be stored outside the scope of the AtomicInstance.
73 * We cannot prevent its copying.
74 */
75 T& operator*();
76
77 /** Getter for the ref to the internal object.
78 *
79 * \warning This must not be stored outside the scope of the AtomicInstance.
80 * We cannot prevent its copying.
81 */
82 const T& operator*() const;
83
84private:
85 /** Disallow empty instance cstor.
86 *
87 */
88 AtomicInstance();
89
90 /** Restrict assignment operator.
91 *
92 * \param rhs right-hand side, its \a content is NULL after this
93 */
94 AtomicInstance<T>& operator=(const AtomicInstance<T>& rhs);
95
96private:
97
98 //!> internal ptr to the protected instance, not (un)locking when NULL
99 mutable T* content;
100
101 //!> a lock for atomic access to the state of the instance
102 static boost::mutex atomicLock;
103};
104
105#include "AtomicInstance_impl.hpp"
106
107#endif /* ATOMICINSTANCE_HPP_ */
Note: See TracBrowser for help on using the repository browser.