state_machine_event_loop.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (Apache License)
3  *
4  * Copyright (c) 2018 Plus One Robotics
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
19 #include "packml_sm/make_unique.h"
20 
21 #include <chrono>
22 
23 namespace packml_sm
24 {
25 StateMachineEventLoop::StateMachineEventLoop(int interval) : interval_(interval)
26 {
27 }
28 
30 {
31  if (thread_ != nullptr)
32  {
33  stop_thread_ = true;
34  thread_->join();
35  }
36 }
37 
39 {
40  if (thread_ == nullptr)
41  {
42  thread_ = std::make_unique<std::thread>(&StateMachineEventLoop::updateLoop, this);
43  }
44 }
45 
47 {
48  if (thread_ != nullptr)
49  {
50  stop_thread_ = true;
51  thread_->join();
52  thread_ = nullptr;
53  }
54 }
55 
57 {
58  while (!stop_thread_)
59  {
60  std::this_thread::sleep_for(std::chrono::milliseconds(interval_));
61  if (!stop_thread_)
62  {
63  updateTickEvent.invoke(*this, EventArgs::EMPTY);
64  }
65  }
66 }
67 }
EventHandler< StateMachineEventLoop, EventArgs > updateTickEvent
std::unique_ptr< std::thread > thread_
static const EventArgs EMPTY
Definition: event.h:27


packml_sm
Author(s): Shaun Edwards
autogenerated on Fri Jul 12 2019 03:30:55