News for hackers. Stuff that matters.
Re:Hourglass Interfaces for C++ APIs by jbandela1 on Friday September 12, 2014 @02:54PM (#8309713)
Another way to accomplish the same goal(abi stability) is to use cppcomponents (www.github.com/jbandela/cppcomponents/), which is a c++11 library that does this internally while presenting you with a high-level interface where you can use all your favorite standard types (string,vector,tuple,etc) and have them work even across different compilers and standard libraries.

If interested, take a look at my slides are at https://github.com/boostcon/cppnow_presentations_2014/blob/m...

and my C++Now talk at http://www.youtube.com/watch?v=a4iFJuNBx7U&list=UU5e__RG9K3c...

Re:Hourglass Interfaces for C++ APIs by ericfrederich on Friday September 12, 2014 @04:13PM (#8310176)
Its funny that a C++ expert / brain surgeon struggles so much with Windows 8
Re:Hourglass Interfaces for C++ APIs by captainmuon on Friday September 12, 2014 @05:20PM (#8310529)
Wow, that seems really useful! I've been wondering for a while how to do C++ libraries best. One thing I never figured out were how to expose lists/vectors/containers, I never thought of using callbacks for that.

By the way, the restriction to a minimal subset of C++ like in this technique explains a lot of why COM is the wierd way it is. E.g. only simple types for parameters; no exceptions, instead you return HRESULTS, and so on. COM does one thing that goes beyond plain C: Interestingly, it uses the one part of the C++ ABI that is stable (whether per standard or per accident I don't know): that vtables always have a certain simple layout, and given a pointer and an interface you can predict where the virtual function pointer will sit.

Re:Hourglass Interfaces for C++ APIs by blt on Friday September 12, 2014 @05:04PM (#8310458)
I've seen a lot of tutorials in this style, but they all fall short in one major way: the user is forced to let the library take care of memory allocation.

What if I want to create and destroy the library's objects over and over using the same memory?

I'm surprised we don't see more libraries like this: (of course, in real life the function bodies would not be inline in the header.)

    size_t hairpoll_size() { return sizeof(Poll); }
    void hairpoll_construct(void *mem, char const *person) { new (mem) Poll(person); }
    void hairpoll_destruct(void *mem) { ((Poll *)mem)->Poll::~Poll(); }

    void *poll_reusable = malloc(hairpoll_size());
    while (a long time) {
        char const *person = get_person_name();
        hairpoll_construct(poll_reusable, person);
        // use the hairpoll
        hairpoll_destruct(poll_reusable);
    }
    free(poll_reusable);
Yeah, it's complex and it loses type safety, but if you're already wrapping in a client-side C++ library that will be hidden from the user.
Re:Hourglass Interfaces for C++ APIs by adamjs on Friday September 12, 2014 @05:45PM (#8310627)
There's actually a few other alternatives if you want to interop with straight-up C++ interfaces:

1. Use SWIG [1] to generate the C-API from the C++ headers (you are limited to a subset of C++: no STL, no templates, all interfaces must be pure-virtual).

2. The guys behind Mono created an amazing tool [2] that allowed you to call C++ symbols directly from managed C# code via transparent translation of the underlying ABI scheme (automatically maps the object layout, vtable layout, and name mangling scheme and emits code at runtime). They finished the GCC ABI and added partial support for MSVC ABI. Sadly the project seems to be abandoned in recent months.

[1] http://www.swig.org/

[2] http://tirania.org/blog/archive/2011/Dec-19.html

Re:Hourglass Interfaces for C++ APIs by tbrock on Friday September 12, 2014 @12:19PM (#8308855)
This seems like a great presentation. Is there a video available?
Re:Hourglass Interfaces for C++ APIs by Nican on Friday September 12, 2014 @01:03PM (#8309087)
Videos for http://cppcon.org/ should be coming out soon
Re:Hourglass Interfaces for C++ APIs by unfamiliar on Friday September 12, 2014 @02:05PM (#8309443)
Isn't the corollary that I have to do a bunch more on the implementation side because the library being exported is the bare minimal?
Re:Hourglass Interfaces for C++ APIs by sonyandy on Friday September 12, 2014 @02:24PM (#8309556)
Just because it's C doesn't mean it's the minimal library. Additionally, just because it's header-only C++ doesn't mean it's the minimal interface.