Skip to content

Commit 5307fc4

Browse files
committed
Make AppRunner a singleton and add RunApp helper function
1 parent 5adb439 commit 5307fc4

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

examples/window_example/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ int main() {
159159
std::cout << "Display removed: " << event.GetDisplay().id << std::endl;
160160
});
161161

162-
AppRunner runner;
163-
runner.Run(window_ptr);
162+
RunApp(window_ptr);
164163

165164
return 0;
166165
}

src/app_runner.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
#include "app_runner.h"
22

3-
namespace nativeapi {} // namespace nativeapi
3+
namespace nativeapi {
4+
5+
AppRunner& AppRunner::GetInstance() {
6+
static AppRunner instance;
7+
return instance;
8+
}
9+
10+
int RunApp(std::shared_ptr<Window> window) {
11+
return AppRunner::GetInstance().Run(window);
12+
}
13+
14+
} // namespace nativeapi

src/app_runner.h

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@
88
namespace nativeapi {
99

1010
/**
11-
* AppRunner is a class that manages the application lifecycle and runs the main
11+
* AppRunner is a singleton class that manages the application lifecycle and runs the main
1212
* event loop. It provides a way to run an application with a given window and
1313
* handle application events.
1414
*/
1515
class AppRunner {
1616
public:
17-
AppRunner();
18-
virtual ~AppRunner();
17+
/**
18+
* Gets the singleton instance of AppRunner.
19+
* @return Reference to the singleton AppRunner instance
20+
*/
21+
static AppRunner& GetInstance();
22+
23+
// Delete copy constructor and assignment operator
24+
AppRunner(const AppRunner&) = delete;
25+
AppRunner& operator=(const AppRunner&) = delete;
1926

2027
/**
2128
* Runs the application with the specified window.
@@ -27,9 +34,22 @@ class AppRunner {
2734
*/
2835
int Run(std::shared_ptr<Window> window);
2936

37+
3038
private:
39+
AppRunner();
40+
virtual ~AppRunner();
41+
3142
class Impl;
3243
std::unique_ptr<Impl> pimpl_;
3344
};
3445

35-
} // namespace nativeapi
46+
/**
47+
* Convenience function to run the application with the specified window.
48+
* This is equivalent to calling AppRunner::GetInstance().Run(window).
49+
*
50+
* @param window The window to run the application with
51+
* @return Exit code of the application (0 for success)
52+
*/
53+
int RunApp(std::shared_ptr<Window> window);
54+
55+
} // namespace nativeapi

0 commit comments

Comments
 (0)