0

我正在尝试在 c (c_glib) 中创建一个 Flume-thrift 客户端,但在创建应发送到服务器的 gobject 时遇到问题。我在 main.c 的行中收到以下错误:

`GLib-GObject-WARNING **: IA__g_object_new_valist: object class `ThriftFlumeEventType' has no property named `timestamp'`

flume_types.h 和 flume_types.c 中的代码是从 Thrift 自动生成的。如果您需要查看更多代码,请告诉我。我很感谢我能得到的所有帮助!

flume_types.h 中的部分代码:

struct _ThriftFlumeEvent
{ 
  ThriftStruct parent;

  /* public */
  gint64 timestamp;
  gboolean __isset_timestamp;
  Priority priority;
  gboolean __isset_priority;
  GByteArray * body;
  gboolean __isset_body;
  gint64 nanos;
  gboolean __isset_nanos;
  gchar * host;
  gboolean __isset_host;
  GHashTable * fields;
  gboolean __isset_fields;
};
typedef struct _ThriftFlumeEvent ThriftFlumeEvent;


GType thrift_flume_event_get_type (void);
#define TYPE_THRIFT_FLUME_EVENT (thrift_flume_event_get_type())

flume_types.c 中的部分代码:

void 
thrift_flume_event_instance_init (ThriftFlumeEvent * object)
{
  printf("thrift_flume_event_instance_init");

  /* satisfy -Wall */
  THRIFT_UNUSED_VAR (object);
  object->timestamp = 0;
  object->__isset_timestamp = FALSE;
  object->__isset_priority = FALSE;
  object->body = NULL;
  object->__isset_body = FALSE;
  object->nanos = 0;
  object->__isset_nanos = FALSE;
  object->host = NULL;
  object->__isset_host = FALSE;
  object->fields = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
  object->__isset_fields = FALSE;
}

GType
thrift_flume_event_get_type (void)
{
  static GType type = 0;

  if (type == 0)
  {
    static const GTypeInfo type_info =
    {
      sizeof (ThriftFlumeEventClass),
      NULL, /* base_init */
      NULL, /* base_finalize */
      (GClassInitFunc) thrift_flume_event_class_init,
      NULL, /* class_finalize */
      NULL, /* class_data */
      sizeof (ThriftFlumeEvent),
      0, /* n_preallocs */
      (GInstanceInitFunc) thrift_flume_event_instance_init,
      NULL, /* value_table */
    };

    type = g_type_register_static (THRIFT_TYPE_STRUCT,
                                   "ThriftFlumeEventType",
                                   &type_info, 0);
    type.timestamp;
  }

  return type;
}

main.c 中的部分代码:

  gpointer eventObj = g_object_new(TYPE_THRIFT_FLUME_EVENT,
                                     "timestamp", 0,
                                     "__isset_timestamp", 0,
                                     "priority", 0,
                                     "__isset_priority", 0,
                                     "body", 0,
                                     "__isset_body", 0,
                                     "nanos", 0,
                                     "__isset_nanos", 0,
                                     "fields", 0,
                                     "__isset_fields", 0,
                                     0);
4

3 回答 3

1

这在 Thrift 0.9.2 中发生了变化:由 C (GLib) 编译器生成的用于表示 Thrift 结构的对象现在将其成员公开为 GObject 属性,从而避免了直接修改对象实例结构的危险(和不良)做法。

从 0.9.2 开始,初始化 struct 对象的首选方式基本上是海报最初所期望的:

ThriftFlumeEvent *event =
  g_object_new (TYPE_THRIFT_FLUME_EVENT,
                "timestamp", (gint64)t_stamp.tv_sec * 1000,
                "priority",  priority,
                ...
                NULL);

请注意,__isset_字段由对象本身管理,不应直接设置。

于 2014-09-18T17:18:02.607 回答
0

GObject属性不是C 结构成员。您需要通过g_object_install_property在您的类init函数 https://developer.gnome.org/gobject/stable/gobject-The-Base-Object-Type.html中安装它们。您还需要从GObject(或从任何从 a 派生的结构GObject)派生对象。

注意:derived在 C 中意味着它必须将父级作为其第一个成员,以便可以无缝转换(不是指向 a 的指针GObject!)

您应该阅读有关该主题的书,它可能会变得非常复杂。

于 2013-07-07T20:50:32.333 回答
0

这是问题的解决方案:

ThriftFlumeEvent *event = g_object_new(TYPE_THRIFT_FLUME_EVENT, 0);
event->timestamp = (gint64)t_stamp.tv_sec * 1000;
event->__isset_timestamp = TRUE;
event->priority = priority;
event->__isset_priority = TRUE;
...
于 2013-08-19T12:19:02.777 回答