By moving the base fields to a QObjectBase_, QObject can be a type which also has a 'base' field. This allows writing a generic QOBJECT() macro that will work with any QObject type, including QObject itself. The container_of() macro ensures that the object to cast has a QObjectBase_ base field, giving some type safety guarantees. QObject must have no members but QObjectBase_ base, or else QOBJECT() breaks. QObjectBase_ is not a typedef and uses a trailing underscore to make it obvious it is not for normal use and to avoid potential abuse. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Message-Id: <20180419150145.24795-3-marcandre.lureau@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Markus Armbruster <armbru@redhat.com>
		
			
				
	
	
		
			39 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/*
 | 
						|
 * QString Module
 | 
						|
 *
 | 
						|
 * Copyright (C) 2009 Red Hat Inc.
 | 
						|
 *
 | 
						|
 * Authors:
 | 
						|
 *  Luiz Capitulino <lcapitulino@redhat.com>
 | 
						|
 *
 | 
						|
 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
 | 
						|
 * See the COPYING.LIB file in the top-level directory.
 | 
						|
 */
 | 
						|
 | 
						|
#ifndef QSTRING_H
 | 
						|
#define QSTRING_H
 | 
						|
 | 
						|
#include "qapi/qmp/qobject.h"
 | 
						|
 | 
						|
struct QString {
 | 
						|
    struct QObjectBase_ base;
 | 
						|
    char *string;
 | 
						|
    size_t length;
 | 
						|
    size_t capacity;
 | 
						|
};
 | 
						|
 | 
						|
QString *qstring_new(void);
 | 
						|
QString *qstring_from_str(const char *str);
 | 
						|
QString *qstring_from_substr(const char *str, int start, int end);
 | 
						|
size_t qstring_get_length(const QString *qstring);
 | 
						|
const char *qstring_get_str(const QString *qstring);
 | 
						|
const char *qstring_get_try_str(const QString *qstring);
 | 
						|
const char *qobject_get_try_str(const QObject *qstring);
 | 
						|
void qstring_append_int(QString *qstring, int64_t value);
 | 
						|
void qstring_append(QString *qstring, const char *str);
 | 
						|
void qstring_append_chr(QString *qstring, int c);
 | 
						|
bool qstring_is_equal(const QObject *x, const QObject *y);
 | 
						|
void qstring_destroy_obj(QObject *obj);
 | 
						|
 | 
						|
#endif /* QSTRING_H */
 |