0

I'm trying to understand what's happening in the following C struct:

/* EXCERPT from LINES 59-90 */
/* parse.h
 * Copyright (c) 2011, Peter Ohler
 * All rights reserved.
 */

typedef struct _ParseInfo {
    // used for the string parser
    const char      *json;
    const char      *cur;
    const char      *end;
    // used for the stream parser
    struct _Reader  rd;

    struct _Err     err;
    struct _Options options;
    VALUE       handler;
    struct _ValStack    stack;
    CircArray       circ_array;
    int         expect_value;
    VALUE       proc;
    VALUE       (*start_hash)(struct _ParseInfo *pi);
    void        (*end_hash)(struct _ParseInfo *pi);
    VALUE       (*hash_key)(struct _ParseInfo *pi, const char *key, size_t klen);
    void        (*hash_set_cstr)(struct _ParseInfo *pi, Val kval, const char *str, size_t len, const char *orig);
    void        (*hash_set_num)(struct _ParseInfo *pi, Val kval, NumInfo ni);
    void        (*hash_set_value)(struct _ParseInfo *pi, Val kval, VALUE value);

    VALUE       (*start_array)(struct _ParseInfo *pi);
    void        (*end_array)(struct _ParseInfo *pi);
    void        (*array_append_cstr)(struct _ParseInfo *pi, const char     *str, size_t len, const char *orig);
    void        (*array_append_num)(struct _ParseInfo *pi, NumInfo ni);
    void        (*array_append_value)(struct _ParseInfo *pi, VALUE value);

    void        (*add_cstr)(struct _ParseInfo *pi, const char *str, size_t len, const char *orig);
    void        (*add_num)(struct _ParseInfo *pi, NumInfo ni);
    void        (*add_value)(struct _ParseInfo *pi, VALUE val);
} *ParseInfo;

from https://github.com/ohler55/oj/blob/master/ext/oj/parse.h#L59:

I don't understand what's going on from lines #74 on (#22 in above listing).

Starting with:

    VALUE       (*start_hash)(struct _ParseInfo *pi);

Can someone explain what these lines are doing?

polarysekt
  • 562
  • 1
  • 5
  • 17
Chamila Wijayarathna
  • 1,815
  • 5
  • 30
  • 54

1 Answers1

2

They are function pointers and the syntax is: return-type (*pointer_name)(args).

However, the developer's using a special technique, denoted by the first argument, which is always a pointer to a _Parser struct. It indeed allows a simple form of Object Oriented Programming, where classes can have methods that specifically act on a certain object instance that have to be manually set up and handled, though. Further information in @paxdiablo's excellent answer here.

Note: there're multiple violations to ISO C because you're not allowed to prefix names with a _ and a capital name. More info here.

edmz
  • 8,220
  • 2
  • 26
  • 45