Re: What is the closure in stream output to file based surfaces for?

David Weiß <[email protected]>
Newsgroups gmane.comp.lib.cairo
Message-ID <E8D289FB71C3004B832D1702922D3A200313C1D705@SKA-WN-E25CEX.ptvag.ptv.de>
Hey Uli,

> 
> Hi,
> 
> On 29.07.2014 11:03, David Weiß wrote:
> > Hey,
> >
> > I want to implement file output for a rendering application and want to use
> C++ file streams using the following callback write function:
> >
> >     cairo_status_t cairo_write_func (void *closure, const unsigned char
> > [...]
> > I just can't figure out what the closure is for.
> > Can anyone explain the concept to me?
> 
> You pass in a closure argument to cairo. Cairo doesn't do anything with it, it
> just passes it on to your callback function. So it is meant to be used to pass
> some state to the callback.
> 
> > Is it comparable to variable capturing in C++11 closures?
> 
> I guess so, yeah.

After Your explanation above, yes it seems like that's the C-ish way of capturing state, which is what closures in C++11 do (basically lambdas extended with state).

> > Could I just pass nullptr as I do not use it anyway?
> [...]
> 
> Sure, if that works for you, you can do so.
> 
> The "C approach" for your above code would be to use the closure argument
> to pass a pointer to fout to the callback function via cairo and use a
> reinterpret_cast to use it in there.

Thanx, as I'm using cairo in a C++ project, I'll stick to the object abstraction and hold/pass state using members :)

> 
> Cheers,
> Uli

Thank you!
David
-- 
cairo mailing list
[email protected]
http://lists.cairographics.org/mailman/listinfo/cairo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.