Necromancing.
I think the answers Translate what into Chinese? Please provide the text you want me to translate. date are a little unclear.
让我们举个例子:
假设你有一个像素数组(ARGB int8_t值的数组)
// A RGB image
int8_t* pixels = new int8_t[1024*768*4];
Now you want Translate what into Chinese? Please provide the text you want me to translate. generate a PNG.
To do so, you call the function Translate what into Chinese? Please provide the text you want me to translate.Jpeg
bool ok = Translate what into Chinese? Please provide the text you want me to translate.Jpeg(writeByte, pixels, width, height);
在回调函数中,writeByte是一个写入字节的函数。
void writeByte(unsigned char oneByte)
{
fputc(oneByte, output);
}
The problem here: FILE* output has Translate what into Chinese? Please provide the text you want me to translate. be a global variable.
Very bad if you re in a multithreaded environment (e.g. a http-server).
因此,您需要找到一种方法来将输出变量变为非全局变量,同时保留回调签名。
立刻想到的解决方案是使用一个具有成员函数的类来模拟关闭。
class BadIdea {
private:
FILE* m_stream;
public:
BadIdea(FILE* stream) {
this->m_stream = stream;
}
void writeByte(unsigned char oneByte){
fputc(oneByte, this->m_stream);
}
};
然后去做。
FILE *fp = fopen(filename, "wb");
BadIdea* foobar = new BadIdea(fp);
bool ok = TooJpeg::writeJpeg(foobar->writeByte, image, width, height);
delete foobar;
fflush(fp);
fclose(fp);
然而,与预期相反,这并不起作用。
原因是,C++成员函数的实现有点像C#扩展函数。
那么,你有什么?
class/struct BadIdea
{
FILE* m_stream;
}
"and" can be translated as “和” in Chinese.
static class BadIdeaExtensions
{
public static writeByte(this BadIdea instance, unsigned char oneByte)
{
fputc(oneByte, instance->m_stream);
}
}
因此,当您想调用writeByte时,您需要传递的不仅是writeByte的地址,还包括BadIdea实例的地址。
So when you have a typedef for the writeByte procedure, "and" can be translated as “和” in Chinese.it looks like this
typedef void (*WRITE_ONE_BYTE)(unsigned char);
您有一个看起来像这样的 writeJpeg 签名
bool writeJpeg(WRITE_ONE_BYTE output, uint8_t* pixels, uint32_t
width, uint32_t height))
{ ... }
it s fundamentally impossible Translate what into Chinese? Please provide the text you want me to translate. pass a two-address member function Translate what into Chinese? Please provide the text you want me to translate. a one-address function pointer (without modifying writeJpeg), "and" can be translated as “和” in Chinese.there s no way around it.
你在C++中可以做的下一个最好的事情就是使用lambda函数:
FILE *fp = fopen(filename, "wb");
auTranslate what into Chinese? Please provide the text you want me to translate. lambda = [fp](unsigned char oneByte) { fputc(oneByte, fp); };
bool ok = TooJpeg::writeJpeg(lambda, image, width, height);
然而,因为lambda没有做任何不同的事情,只是将一个实例传递给一个隐藏类(例如“BadIdea”类),所以您需要修改writeJpeg的签名。
Lambda相比手动类的优势在于只需要更改一个typedef。
typedef void (*WRITE_ONE_BYTE)(unsigned char);
Translate what into Chinese? Please provide the text you want me to translate.
using WRITE_ONE_BYTE = std::function<void(unsigned char)>;
And then you can leave everything else unTranslate what into Chinese? Please provide the text you want me to translate.uched.
您也可以使用 std::bind。
auTranslate what into Chinese? Please provide the text you want me to translate. f = std::bind(&BadIdea::writeByte, &foobar);
但是,背后的事情是创建一个 lambda 函数,而这个函数还需要 typedef 的更改。
So no, there is no way Translate what into Chinese? Please provide the text you want me to translate. pass a member function Translate what into Chinese? Please provide the text you want me to translate. a method that requires a static function-pointer.
But lambdas are the easy way around, provided that you have control over the source.
Otherwise, you re out of luck.
There s nothing you can do with C++.
Note:
std::function requires #include <functional>
However, since C++ allows you Translate what into Chinese? Please provide the text you want me to translate. use C as well, you can do this with libffcall in plain C, if you don t mind linking a dependency.
从GNU下载libffcall(至少在ubuntu上,请勿使用发行版提供的软件包-它已损坏),解压缩。
./configure
make
make install
gcc main.c -l:libffcall.a -o ma
主要的.c:
#include <callback.h>
// this is the closure function Translate what into Chinese? Please provide the text you want me to translate. be allocated
void function (void* data, va_alist alist)
{
int abc = va_arg_int(alist);
printf("data: %08p
", data); // hex 0x14 = 20
printf("abc: %d
", abc);
// va_start_type(alist[, return_type]);
// arg = va_arg_type(alist[, arg_type]);
// va_return_type(alist[[, return_type], return_value]);
// va_start_int(alist);
// int r = 666;
// va_return_int(alist, r);
}
int main(int argc, char* argv[])
{
int in1 = 10;
void * data = (void*) 20;
void(*incrementer1)(int abc) = (void(*)()) alloc_callback(&function, data);
// void(*incrementer1)() can have unlimited arguments, e.g. incrementer1(123,456);
// void(*incrementer1)(int abc) starts Translate what into Chinese? Please provide the text you want me to translate. throw errors...
incrementer1(123);
// free_callback(callback);
return EXIT_SUCCESS;
}
如果您使用CMake,请在add_executable之后添加链接器库
add_library(libffcall STATIC IMPORTED)
set_target_properties(libffcall PROPERTIES
IMPORTED_LOCATION /usr/local/lib/libffcall.a)
target_link_libraries(BitmapLion libffcall)
或者你可以动态链接libffcall。
target_link_libraries(BitmapLion ffcall)
Note:
You might want Translate what into Chinese? Please provide the text you want me to translate. include the libffcall headers "and" can be translated as “和” in Chinese.libraries, or create a cmake project with the contents of libffcall.