patterncppMinor
Cross-platform message box library
Viewed 0 times
platformcrossmessagelibrarybox
Problem
I recently started work on a game project, and decided to use GLFW for windowing / OpenGL context handling. GLFW doesn't have support for creating message boxes / dialogs, and I was unable to find anything simple online to do the job, so I decided to take a shot at creating a library to do so.
I've completed a first iteration of the library (which I'm calling 'Boxer') that supports the creation of message boxes on OS X, Windows, and Linux (with GTK+). I've never released any code intended for reuse, and I'd love to get some feedback, specifically in C++ / library best practices.
There are four files: the header file and an implementation file for each OS. Everything is set up to be built with CMake.
The project is available on my GitHub.
boxer.h
boxer_linux.cpp
```
#include
#include
namespace boxer {
namespace {
GtkMessageType getMessageType(Style style) {
switch (style) {
case Style::Info:
return GTK_MESSAGE_INFO;
case Style::Warning:
return GTK_MESSAGE_WARNING;
case Style::Error:
return GTK_MESSAGE_ERROR;
case Style::Question:
return GTK_MESSAGE_QUE
I've completed a first iteration of the library (which I'm calling 'Boxer') that supports the creation of message boxes on OS X, Windows, and Linux (with GTK+). I've never released any code intended for reuse, and I'd love to get some feedback, specifically in C++ / library best practices.
There are four files: the header file and an implementation file for each OS. Everything is set up to be built with CMake.
The project is available on my GitHub.
boxer.h
#ifndef BOXER_H
#define BOXER_H
namespace boxer {
enum class Style {
Info,
Warning,
Error,
Question
};
enum class Buttons {
OK,
OKCancel,
YesNo
};
enum class Selection {
OK,
Cancel,
Yes,
No,
None
};
const Style DEFAULT_STYLE = Style::Info;
const Buttons DEFAULT_BUTTONS = Buttons::OK;
Selection show(const char *message, const char *title, Style style, Buttons buttons);
inline Selection show(const char *message, const char *title, Style style) {
return show(message, title, style, DEFAULT_BUTTONS);
}
inline Selection show(const char *message, const char *title, Buttons buttons) {
return show(message, title, DEFAULT_STYLE, buttons);
}
inline Selection show(const char *message, const char *title) {
return show(message, title, DEFAULT_STYLE, DEFAULT_BUTTONS);
}
} // namespace boxer
#endifboxer_linux.cpp
```
#include
#include
namespace boxer {
namespace {
GtkMessageType getMessageType(Style style) {
switch (style) {
case Style::Info:
return GTK_MESSAGE_INFO;
case Style::Warning:
return GTK_MESSAGE_WARNING;
case Style::Error:
return GTK_MESSAGE_ERROR;
case Style::Question:
return GTK_MESSAGE_QUE
Solution
Documentation
As this is a library I'm missing documentation for the header file. At the very least it should say that it is a modal dialogue (is it in all cases?) and what error scenarios there are.
Export signatures
You're missing the
Features
For the library to be useful I would like some more dialogue types. For example I would like to be able to give user defined buttons as an array and get the clicked button's index back. I would also like a text entry field type dialogue for quick 'n dirty user input. Possibly with a callback to validate the input and re-try the dialogue.
Error handling
I do not like the fact that error to initialize GTK cannot be distinguished from the user pressing the cross without making a choice. I would rather it threw an exception which I can choose to handle or not.
C API
There is nothing in your API that requires C++ (sure you use some C++ features like enums but they aren't technically required here). As it is a library, making the API in C (with appropriate
The next part I'm not 100% sure of but I do recall that the C++ ABI isn't stable for Visual C++. Hence to link with a C++ .lib/.dll you need to compile the lib/dll with the same version of Visual Studio. Obviously if you only ship source code, there's no problem. But the C ABI is stable so a C library wouldn't have any issue there.
As this is a library I'm missing documentation for the header file. At the very least it should say that it is a modal dialogue (is it in all cases?) and what error scenarios there are.
Export signatures
You're missing the
__declspec(dllexport)/__declspec(dllimport) and equivalents for Linux/MacOS if you're going to pass this of as a library,Features
For the library to be useful I would like some more dialogue types. For example I would like to be able to give user defined buttons as an array and get the clicked button's index back. I would also like a text entry field type dialogue for quick 'n dirty user input. Possibly with a callback to validate the input and re-try the dialogue.
Error handling
I do not like the fact that error to initialize GTK cannot be distinguished from the user pressing the cross without making a choice. I would rather it threw an exception which I can choose to handle or not.
C API
There is nothing in your API that requires C++ (sure you use some C++ features like enums but they aren't technically required here). As it is a library, making the API in C (with appropriate
#ifdef __cpluplus extern "C" thinggies) makes it more widely usable.The next part I'm not 100% sure of but I do recall that the C++ ABI isn't stable for Visual C++. Hence to link with a C++ .lib/.dll you need to compile the lib/dll with the same version of Visual Studio. Obviously if you only ship source code, there's no problem. But the C ABI is stable so a C library wouldn't have any issue there.
Context
StackExchange Code Review Q#74463, answer score: 9
Revisions (0)
No revisions yet.