MeshLib C++ Docs
Loading...
Searching...
No Matches
MRCommandLoop.h
Go to the documentation of this file.
1#pragma once
2#include "exports.h"
3#include "MRMesh/MRExpected.h"
4#include <queue>
5#include <functional>
6#include <condition_variable>
7#include <mutex>
8#include <thread>
9
10namespace MR
11{
14
15
17class CommandLoop
18{
19public:
20 using CommandFunc = std::function<void()>;
21
31
33 MRVIEWER_API static void setMainThreadId( const std::thread::id& id );
34 MRVIEWER_API static std::thread::id getMainThreadId();
36 MRVIEWER_API static void setState( StartPosition state );
37
42
50 MRVIEWER_API static void runCommandFromGUIThread( CommandFunc func );
51
53 MRVIEWER_API static void processCommands();
54
56 MRVIEWER_API static bool empty();
57
60 MRVIEWER_API static void removeCommands( bool closeLoop );
61
62private:
63 CommandLoop() = default;
64 ~CommandLoop();
65
66 static CommandLoop& instance_();
67
70 static Expected<void> addCommand_( CommandFunc func, bool blockThread, StartPosition state );
71
72 struct Command
73 {
76 std::condition_variable callerThreadCV;
77 std::thread::id threadId;
80 bool started{ false };
83 bool done{ false };
85 bool dropped{ false };
86 };
87
89
91 bool queueClosed_{ false };
92 std::thread::id mainThreadId_;
93 std::queue<std::shared_ptr<Command>> commands_;
94 std::mutex mutex_;
95};
96
101MRVIEWER_API void skipFramesAfterInput();
102
103}
bool done
Definition MRCommandLoop.h:83
StartPosition
Specify execution in specific time of application start.
Definition MRCommandLoop.h:24
static bool empty()
Return true if loop is empty.
std::thread::id threadId
Definition MRCommandLoop.h:77
CommandFunc func
Definition MRCommandLoop.h:74
std::function< void()> CommandFunc
Definition MRCommandLoop.h:20
static void setMainThreadId(const std::thread::id &id)
This function setups main thread id, it should be called before any command.
static void processCommands()
Execute all commands from loop.
void skipFramesAfterInput()
tl::expected< T, E > Expected
Definition MRExpected.h:31
StartPosition state
Definition MRCommandLoop.h:75
static std::thread::id getMainThreadId()
std::condition_variable callerThreadCV
Definition MRCommandLoop.h:76
bool dropped
set under CommandLoop::mutex_ in removeCommands: done, but never executed
Definition MRCommandLoop.h:85
static void runCommandFromGUIThread(CommandFunc func)
static void setState(StartPosition state)
Update state of command loop, only can rise.
static void appendCommand(CommandFunc func, StartPosition state=StartPosition::BeforeWindowAppear)
bool started
Definition MRCommandLoop.h:80
static void removeCommands(bool closeLoop)
@ AfterPluginInit
executes after splash appeared
Definition MRCommandLoop.h:27
@ BeforeWindowAppear
executes during splash, after plugins init)
Definition MRCommandLoop.h:28
@ AfterWindowAppear
executes after splash is going to close, and just before main window is shown and have valid main win...
Definition MRCommandLoop.h:29
@ AfterSplashAppear
executes right after window is initialized
Definition MRCommandLoop.h:26
@ AfterWindowInit
Definition MRCommandLoop.h:25
only for bindings generation
Definition MRCameraOrientationPlugin.h:8