MeshLib
 
Loading...
Searching...
No Matches
MRCommandLoop.h
Go to the documentation of this file.
1#pragma once
2#include "exports.h"
3#include <queue>
4#include <functional>
5#include <condition_variable>
6#include <mutex>
7#include <thread>
8
9namespace MR
10{
11
12// Additional command loop for external app control
14{
15public:
16 using CommandFunc = std::function<void()>;
17
18 // Specify execution in specific time of application start
19 enum class StartPosition
20 {
21 AfterWindowInit, // executes right after window is initialized
22 AfterSplashAppear, // executes after splash appeared
23 AfterPluginInit, // executes during splash, after plugins init)
24 AfterSplashHide, // executes after splash, to have valid main window context
25 AfterWindowAppear // executes after window appeared to have valid opengl context
26 };
27
28 // This function setups main thread id, it should be called before any command
29 MRVIEWER_API static void setMainThreadId( const std::thread::id& id );
30 MRVIEWER_API static std::thread::id getMainThreadId();
31 // Update state of command loop, only can rise
32 MRVIEWER_API static void setState( StartPosition state );
33
34 // Adds command to the end of command loop, can be performed from any thread
35 // do not block, so be careful with lambda captures
36 // note: state - specify execution in specific time of application start
37 MRVIEWER_API static void appendCommand( CommandFunc func, StartPosition state = StartPosition::AfterSplashHide );
38
39 // If caller thread is main - instantly run command, otherwise add command to the end of loop with
40 // StartPosition state = StartPosition::AfterSplash and blocks caller thread until command is done
41 MRVIEWER_API static void runCommandFromGUIThread( CommandFunc func );
42
43 // Execute all commands from loop
44 MRVIEWER_API static void processCommands();
45
46 // Clears the queue without executing the commands
47 MRVIEWER_API static void removeCommands();
48
49private:
50 CommandLoop() = default;
52
53 static CommandLoop& instance_();
54
55 static void addCommand_( CommandFunc func, bool blockThread, StartPosition state );
56
57 struct Command
58 {
59 CommandFunc func;
61 std::condition_variable callerThreadCV;
62 std::thread::id threadId;
63 };
64
66
67 std::thread::id mainThreadId_;
68 std::queue<std::shared_ptr<Command>> commands_;
69 std::mutex mutex_;
70};
71
72}
Definition MRCommandLoop.h:14
static MRVIEWER_API void runCommandFromGUIThread(CommandFunc func)
StartPosition
Definition MRCommandLoop.h:20
static MRVIEWER_API void processCommands()
std::function< void()> CommandFunc
Definition MRCommandLoop.h:16
static MRVIEWER_API void setMainThreadId(const std::thread::id &id)
static MRVIEWER_API void setState(StartPosition state)
static MRVIEWER_API void appendCommand(CommandFunc func, StartPosition state=StartPosition::AfterSplashHide)
static MRVIEWER_API std::thread::id getMainThreadId()
static MRVIEWER_API void removeCommands()
Definition MRCameraOrientationPlugin.h:7