Skip to content

Instantly share code, notes, and snippets.

@wi7a1ian
Last active March 18, 2019 10:52
Show Gist options
  • Save wi7a1ian/5a3519dd04e0f9d0d9668ac720659218 to your computer and use it in GitHub Desktop.
Save wi7a1ian/5a3519dd04e0f9d0d9668ac720659218 to your computer and use it in GitHub Desktop.
Base for C++ API #cpp
#include "FooAPIDefs.h"
#define FOOINTERFACES_VERSION 7
extern "C" FOOINTERFACES_API IFooFactory* GetFooFactoryPtr(const uchar* options = nullptr, uint32_t version = FOOINTERFACES_VERSION) noexcept;
// API entry point
inline IFooFactory::Ptr GetFooFactory(const uchar* options = nullptr, uint32_t version = FOOINTERFACES_VERSION) {
return IFooFactory::Ptr(GetFooFactoryPtr(options, version));
}
#ifdef _WIN32
# ifdef FOOINTERFACES_EXPORTS
# define FOOINTERFACES_API __declspec(dllexport)
# else
# define FOOINTERFACES_API __declspec(dllimport)
# endif
#else
# define FOOINTERFACES_API
#endif
namespace FooImplementation
{
extern "C" FOOINTERFACES_API IFooFactory* GetFooFactoryPtr(const uchar* options, uint32_t version) noexcept
{
assert(options != nullptr);
assert(version == FOOINTERFACES_VERSION);
IFooFactory* outPtr = nullptr;
try
{
if (version == FOOINTERFACES_VERSION)
{
outPtr = new SomeFooFactoryImplementation(options);
}
}
catch (...)
{
assert(false && "Failed to create factory object");
}
return outPtr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment