Salome HOME
9933e4d946a958746bebae1a95f5bd4b1c772f6f
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingTimeLabel.cxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDCouplingTimeLabel.hxx"
22
23 #include "InterpKernelException.hxx"
24
25 #include <limits>
26
27 using namespace MEDCoupling;
28
29 std::atomic<std::size_t> TimeLabel::GLOBAL_TIME(0);
30
31 TimeLabel::TimeLabel():_time(GLOBAL_TIME++)
32 {
33 }
34
35 TimeLabel::~TimeLabel()
36 {
37 }
38
39 TimeLabel& TimeLabel::operator=(const TimeLabel& other)
40 {
41   _time=GLOBAL_TIME++;
42   return *this;
43 }
44
45 void TimeLabel::declareAsNew() const
46 {
47   _time=GLOBAL_TIME++;
48 }
49
50 void TimeLabel::updateTimeWith(const TimeLabel& other) const
51 {
52   if(_time<other._time)
53     _time=other._time;
54 }
55
56 /*!
57  * This method has to be called with a lot of care. It set aggressively the time in this with the
58  * time in \a other.
59  */
60 void TimeLabel::forceTimeOfThis(const TimeLabel& other) const
61 {
62   _time=other._time;
63 }
64
65 TimeLabelConstOverseer::TimeLabelConstOverseer(const TimeLabel *tl):_tl(tl),_ref_time(std::numeric_limits<std::size_t>::max())
66 {
67   if(!_tl)
68     throw INTERP_KERNEL::Exception("TimeLabelConstOverseer constructor : input instance must be not NULL !");
69   _tl->updateTime();
70   _ref_time=tl->getTimeOfThis();
71 }
72
73 /*!
74  * This method checks that the tracked instance is not NULL and if not NULL that its internal state has not changed.
75  */
76 void TimeLabelConstOverseer::checkConst() const
77 {
78   if(!_tl)
79     throw INTERP_KERNEL::Exception("TimeLabelConstOverseer::checkConst : NULL tracked instance !");
80   _tl->updateTime();
81   if(_ref_time!=_tl->getTimeOfThis())
82     throw INTERP_KERNEL::Exception("TimeLabelConstOverseer::checkConst : the state of the controlled instance of TimeLable has changed !");
83 }
84
85 bool TimeLabelConstOverseer::resetState()
86 {
87   if(_tl)
88     {
89       _tl->updateTime();
90       _ref_time=_tl->getTimeOfThis();
91       return true;
92     }
93   else
94     return false;
95 }
96
97 bool TimeLabelConstOverseer::keepTrackOfNewTL(const TimeLabel *tl)
98 {
99   if(_tl==tl)
100     return false;
101   _tl=tl;
102   if(_tl)
103     {
104       _tl->updateTime();
105       _ref_time=_tl->getTimeOfThis();
106     }
107   else
108     {
109       _ref_time=std::numeric_limits<std::size_t>::max();
110     }
111   return true;
112 }