Platform Independent Resource Management

How to write a platform independent wrapper function in C++

I would not write it like that:

void WrapperSprintf( char buffer, const char *format, ... )
{
#if defined(_WIN32)
_snprintf_s(buffer, sizeof(buffer), format,...);
#else
snprintf(buffer, sizeof(buffer), format, ...);
#endif
}

You don't need a function here. What you want is to select the correct function.

Rather do this:

#if defined(_WIN32)
#define WrapperSprintf _snprintf_s
#else
#define WrapperSprintf snprintf
#endif

If you want to get complex and have the ability rearrange argumetns then we can take it one step further:

#if defined(_WIN32)
#define WrapperSprintf(buffer, size, ...) _snprintf_s(buffer, size, __VA_ARGS__)
#elif defined(_WIERDYSTEM)
#define WrapperSprintf(buffer, size, ...) wierdsnprintf(size, buffer, __VA_ARGS__)
#else
#define WrapperSprintf(buffer, size, ...) snprintf(buffer, size, __VA_ARGS__)
#endif

Note: __VA_ARGS__ must match at least one argument (i.e. it can not match zero). So use if for "format and format-arguments" as potentially there may be zero "format-arguments" if there are no % markers in the format string.

What's the best cross platform approach (Windows/Mac) to deploy a simple service/daemon (with simple UI)

I've written cross-platform C++ code and this was the kind of decision we were faced with.

Apologies in advance, but I'm not aware of any cross platform libraries that you can use for this, the systems are sufficiently different that they will require different deployment strategies.

Suggestion 1:

  • build your application in Mono
  • build two deployment strategies, separating the deployment issues into the "installers"
  • on windows deploy with some scripting language or installer application that inserts your app into the task scheduler, or write some simple C# code (see below)
  • on the Mac use the built-in UNIX cron demon to periodically call your app.

I think this strategy is fairly simple. The platform-specific effort is centered on the deployment. The app uses no resources until it runs, and it uses simple mechanisms to activate it. Logging and error handling can be done using the file system.

Suggestion #2:

  • write the platform independent code as an assembly
  • write the platform-specific code for each platform as necessary:

    • on windows that's a windows service or a scheduled app installed with click-once
    • I've been away from a mac long enough that I don;t know what the strategy is there
  • again, additional effort for the deployment

This solution has the overhead of writing more specific code to interface with the particular services that the OS Supports. The benefit is that it should co-exist better with the OS with the additional effort (ie: hook into OS-level resource management, reporting, logging and error management).

Note: C# interface to the Windows task scheduler here Unfortunately it's probably not Mono-compatible.

Is there a Linux equivalent of Windows' resource files?

It's actually quite simple on Linux and other ELF systems: http://www.linuxjournal.com/content/embedding-file-executable-aka-hello-world-version-5967

OS X has bundles, so you just build your library as a framework and put the file in the bundle.

Can you reference an item in qt resource file in the qt project file?

No, you have to include the file path outside the Qt Resource System (you can include the same file in both App.qrc and in RC_ICONS).

win32:RC_ICONS += icon.ico
  • RC_ICONS works by generating a .rc file that includes the icons specified. That means the icons will be stored in the result .exe file using windows resource management (and only then explorer.exe will be able to recognize the icon of the .exe file).

  • While, when you add the icon to App.qrc, it will be using Qt Resource System to store the icon in the result .exe. This is Qt's platform-independent mechanism for storing binary files in the application's executable. This can't be read by explorer.exe (it can be read only from Qt classes that depend on the QFile class).

Clearly, these are two different system to get your icon (or other resource) stored within the result .exe file, and they use different ways to do that, so you can't specify an icon from the Qt Resource system to be included in the generated RC file.

python system idle/inactive without using ctypes - platform independent as well

I managed to get ctypes to work (well...without crashing python that is :-) ) and so now I am able to use this solution from SO

And thanks to FogleBird for the above solution

In the process of tackling this problem I learnt about psutil which I think can benefit those who are looking for system monitoring, profiling and limiting process resources and management of running processes. And it's cross platform...yay!

Cheers,
M&M

Is closing the resources always important?

I guess all the resources I opened will be closed/released when the application/Virtual machine exits.

What happens with a resource which was not regurarly released is out of your control. It may do no harm, or it may do some. It is also highly platform-dependent, so testing on just one won't help.

why should I care about closing these resources in such small, short working application?

The size of the application shouldn't matter. First, applications usually grow; second, if you don't practice doing it the right way, you won't know how to do it when it matters.



Related Topics



Leave a reply



Submit