需要帮助来理解这段代码。

时间:2022-04-06 02:02:18

There some code in one gstreamer-plugin:

在gstreamer-plugin中有一些代码:

static GstFlowReturn
gst_ebml_peek_id_full (GstEbmlRead * ebml, guint32 * id, guint64 * length,
    guint * prefix)
{
  GstFlowReturn ret;

  ret = gst_ebml_peek_id_length (id, length, prefix,
      (GstPeekData) gst_ebml_read_peek, (gpointer) gst_ebml_read_br (ebml),
      ebml->el, gst_ebml_read_get_pos (ebml));
  if (ret != GST_FLOW_OK)
    return ret;

  GST_LOG_OBJECT (ebml->el, "id 0x%x at offset 0x%" G_GINT64_MODIFIER "x"
      " of length %" G_GUINT64_FORMAT ", prefix %d", *id,
      gst_ebml_read_get_pos (ebml), *length, *prefix);

Now see the 4:th argument to gst_ebml_peek_id_length () is

现在看到gst_ebml_peek_id_length()的第4:th参数是

(GstPeekData) gst_ebml_read_peek

where gst_ebml_read_peek is another function whose definition is:

其中gst_ebml_read_peek是另一个定义为:

static const guint8 *
gst_ebml_read_peek (GstByteReader * br, guint peek)
{
  const guint8 *data = NULL;

  if (G_LIKELY (gst_byte_reader_peek_data (br, peek, &data)))
    return data;
  else
    return NULL;
}

Now I want to ask you is: gst_ebml_read_peek has two input argument in definition, so how can it be called (in the upper code) without arguments?

现在我想问您的是:gst_ebml_read_peek在定义中有两个输入参数,那么如何在没有参数的情况下调用(在上面的代码中)呢?

Edit: You can find this code at http://gstreamer.freedesktop.org/data/coverage/lcov/gst-plugins-good/gst/matroska/ebml-read.c.gcov.html

编辑:您可以在http://gstreamer.freedesktop.org/data/coverage/lcov/gst-plugins-good/gst/matroska/ebml-read.c.gcov.html找到这段代码

From around line 194.

从第194行。

3 个解决方案

#1


3  

Here is a simpler example doing a similar thing:

这里有一个更简单的例子:

#include <stdio.h>

typedef int (*binaryop)(int, int);

static int add(int a, int b) {
  return a + b;
}

static int mul(int a, int b) {
  return a * b;
}

static void print_result(binaryop op, int a, int b) {
  printf("%d\n", op(a, b));
}

int main() {
  print_result(add, 2, 3);
  print_result(mul, 5, 7);
  return 0;
}

The print_result function takes another function as a parameter, which it then calls. When you just write add without the parentheses, the function is not yet called. It is only called in a function call expression, and that looks like this: function_name(arguments).

print_result函数将另一个函数作为参数,然后调用它。当你只写不带括号的add时,函数还没有被调用。它只在函数调用表达式中调用,它看起来是这样的:function_name(参数)。

#2


8  

Because it's a function pointer. The function is not actually called at that point, it's just passed to gst_ebml_peek_id_length (which will likely call it later with the correct arguments).

因为它是一个函数指针。函数在这一点上实际上并没有被调用,它只是被传递给了gst_ebml_peek_peek_id_length(稍后可能会用正确的参数调用它)。

#3


1  

Argument passed to gst_ebml_peek_id_full() is function pointer of gst_ebml_read_peek() or in other terms its just passing the handle of the function so that gst_ebml_peek_id_full() can call passed in function with correct parameter.

传递给gst_ebml_peek_id_full()的参数是gst_ebml_read_peek()的函数指针,或者在其他条件下,它只是传递函数的句柄,以便gst_ebml_peek_id_full()可以调用具有正确参数的函数。

Gives flexibility of passing your own/different implementation of function which implements same prototype of gst_ebml_read_peek().

提供了传递您自己/不同的函数实现的灵活性,它实现了gst_ebml_read_peek()的相同原型。

#1


3  

Here is a simpler example doing a similar thing:

这里有一个更简单的例子:

#include <stdio.h>

typedef int (*binaryop)(int, int);

static int add(int a, int b) {
  return a + b;
}

static int mul(int a, int b) {
  return a * b;
}

static void print_result(binaryop op, int a, int b) {
  printf("%d\n", op(a, b));
}

int main() {
  print_result(add, 2, 3);
  print_result(mul, 5, 7);
  return 0;
}

The print_result function takes another function as a parameter, which it then calls. When you just write add without the parentheses, the function is not yet called. It is only called in a function call expression, and that looks like this: function_name(arguments).

print_result函数将另一个函数作为参数,然后调用它。当你只写不带括号的add时,函数还没有被调用。它只在函数调用表达式中调用,它看起来是这样的:function_name(参数)。

#2


8  

Because it's a function pointer. The function is not actually called at that point, it's just passed to gst_ebml_peek_id_length (which will likely call it later with the correct arguments).

因为它是一个函数指针。函数在这一点上实际上并没有被调用,它只是被传递给了gst_ebml_peek_peek_id_length(稍后可能会用正确的参数调用它)。

#3


1  

Argument passed to gst_ebml_peek_id_full() is function pointer of gst_ebml_read_peek() or in other terms its just passing the handle of the function so that gst_ebml_peek_id_full() can call passed in function with correct parameter.

传递给gst_ebml_peek_id_full()的参数是gst_ebml_read_peek()的函数指针,或者在其他条件下,它只是传递函数的句柄,以便gst_ebml_peek_id_full()可以调用具有正确参数的函数。

Gives flexibility of passing your own/different implementation of function which implements same prototype of gst_ebml_read_peek().

提供了传递您自己/不同的函数实现的灵活性,它实现了gst_ebml_read_peek()的相同原型。