object.h 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058
  1. /*
  2. * QEMU Object Model
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. *
  12. */
  13. #ifndef QEMU_OBJECT_H
  14. #define QEMU_OBJECT_H
  15. #include "qapi/qapi-builtin-types.h"
  16. #include "qemu/module.h"
  17. struct TypeImpl;
  18. typedef struct TypeImpl *Type;
  19. typedef struct TypeInfo TypeInfo;
  20. typedef struct InterfaceClass InterfaceClass;
  21. typedef struct InterfaceInfo InterfaceInfo;
  22. #define TYPE_OBJECT "object"
  23. typedef struct ObjectProperty ObjectProperty;
  24. /**
  25. * typedef ObjectPropertyAccessor:
  26. * @obj: the object that owns the property
  27. * @v: the visitor that contains the property data
  28. * @name: the name of the property
  29. * @opaque: the object property opaque
  30. * @errp: a pointer to an Error that is filled if getting/setting fails.
  31. *
  32. * Called when trying to get/set a property.
  33. */
  34. typedef void (ObjectPropertyAccessor)(Object *obj,
  35. Visitor *v,
  36. const char *name,
  37. void *opaque,
  38. Error **errp);
  39. /**
  40. * typedef ObjectPropertyResolve:
  41. * @obj: the object that owns the property
  42. * @opaque: the opaque registered with the property
  43. * @part: the name of the property
  44. *
  45. * Resolves the #Object corresponding to property @part.
  46. *
  47. * The returned object can also be used as a starting point
  48. * to resolve a relative path starting with "@part".
  49. *
  50. * Returns: If @path is the path that led to @obj, the function
  51. * returns the #Object corresponding to "@path/@part".
  52. * If "@path/@part" is not a valid object path, it returns #NULL.
  53. */
  54. typedef Object *(ObjectPropertyResolve)(Object *obj,
  55. void *opaque,
  56. const char *part);
  57. /**
  58. * typedef ObjectPropertyRelease:
  59. * @obj: the object that owns the property
  60. * @name: the name of the property
  61. * @opaque: the opaque registered with the property
  62. *
  63. * Called when a property is removed from a object.
  64. */
  65. typedef void (ObjectPropertyRelease)(Object *obj,
  66. const char *name,
  67. void *opaque);
  68. /**
  69. * typedef ObjectPropertyInit:
  70. * @obj: the object that owns the property
  71. * @prop: the property to set
  72. *
  73. * Called when a property is initialized.
  74. */
  75. typedef void (ObjectPropertyInit)(Object *obj, ObjectProperty *prop);
  76. struct ObjectProperty
  77. {
  78. char *name;
  79. char *type;
  80. char *description;
  81. ObjectPropertyAccessor *get;
  82. ObjectPropertyAccessor *set;
  83. ObjectPropertyResolve *resolve;
  84. ObjectPropertyRelease *release;
  85. ObjectPropertyInit *init;
  86. void *opaque;
  87. QObject *defval;
  88. };
  89. /**
  90. * typedef ObjectUnparent:
  91. * @obj: the object that is being removed from the composition tree
  92. *
  93. * Called when an object is being removed from the QOM composition tree.
  94. * The function should remove any backlinks from children objects to @obj.
  95. */
  96. typedef void (ObjectUnparent)(Object *obj);
  97. /**
  98. * typedef ObjectFree:
  99. * @obj: the object being freed
  100. *
  101. * Called when an object's last reference is removed.
  102. */
  103. typedef void (ObjectFree)(void *obj);
  104. #define OBJECT_CLASS_CAST_CACHE 4
  105. /**
  106. * struct ObjectClass:
  107. *
  108. * The base for all classes. The only thing that #ObjectClass contains is an
  109. * integer type handle.
  110. */
  111. struct ObjectClass
  112. {
  113. /* private: */
  114. Type type;
  115. GSList *interfaces;
  116. const char *object_cast_cache[OBJECT_CLASS_CAST_CACHE];
  117. const char *class_cast_cache[OBJECT_CLASS_CAST_CACHE];
  118. ObjectUnparent *unparent;
  119. GHashTable *properties;
  120. };
  121. /**
  122. * struct Object:
  123. *
  124. * The base for all objects. The first member of this object is a pointer to
  125. * a #ObjectClass. Since C guarantees that the first member of a structure
  126. * always begins at byte 0 of that structure, as long as any sub-object places
  127. * its parent as the first member, we can cast directly to a #Object.
  128. *
  129. * As a result, #Object contains a reference to the objects type as its
  130. * first member. This allows identification of the real type of the object at
  131. * run time.
  132. */
  133. struct Object
  134. {
  135. /* private: */
  136. ObjectClass *class;
  137. ObjectFree *free;
  138. GHashTable *properties;
  139. uint32_t ref;
  140. Object *parent;
  141. };
  142. /**
  143. * DECLARE_INSTANCE_CHECKER:
  144. * @InstanceType: instance struct name
  145. * @OBJ_NAME: the object name in uppercase with underscore separators
  146. * @TYPENAME: type name
  147. *
  148. * Direct usage of this macro should be avoided, and the complete
  149. * OBJECT_DECLARE_TYPE macro is recommended instead.
  150. *
  151. * This macro will provide the instance type cast functions for a
  152. * QOM type.
  153. */
  154. #define DECLARE_INSTANCE_CHECKER(InstanceType, OBJ_NAME, TYPENAME) \
  155. static inline G_GNUC_UNUSED InstanceType * \
  156. OBJ_NAME(const void *obj) \
  157. { return OBJECT_CHECK(InstanceType, obj, TYPENAME); }
  158. /**
  159. * DECLARE_CLASS_CHECKERS:
  160. * @ClassType: class struct name
  161. * @OBJ_NAME: the object name in uppercase with underscore separators
  162. * @TYPENAME: type name
  163. *
  164. * Direct usage of this macro should be avoided, and the complete
  165. * OBJECT_DECLARE_TYPE macro is recommended instead.
  166. *
  167. * This macro will provide the class type cast functions for a
  168. * QOM type.
  169. */
  170. #define DECLARE_CLASS_CHECKERS(ClassType, OBJ_NAME, TYPENAME) \
  171. static inline G_GNUC_UNUSED ClassType * \
  172. OBJ_NAME##_GET_CLASS(const void *obj) \
  173. { return OBJECT_GET_CLASS(ClassType, obj, TYPENAME); } \
  174. \
  175. static inline G_GNUC_UNUSED ClassType * \
  176. OBJ_NAME##_CLASS(const void *klass) \
  177. { return OBJECT_CLASS_CHECK(ClassType, klass, TYPENAME); }
  178. /**
  179. * DECLARE_OBJ_CHECKERS:
  180. * @InstanceType: instance struct name
  181. * @ClassType: class struct name
  182. * @OBJ_NAME: the object name in uppercase with underscore separators
  183. * @TYPENAME: type name
  184. *
  185. * Direct usage of this macro should be avoided, and the complete
  186. * OBJECT_DECLARE_TYPE macro is recommended instead.
  187. *
  188. * This macro will provide the three standard type cast functions for a
  189. * QOM type.
  190. */
  191. #define DECLARE_OBJ_CHECKERS(InstanceType, ClassType, OBJ_NAME, TYPENAME) \
  192. DECLARE_INSTANCE_CHECKER(InstanceType, OBJ_NAME, TYPENAME) \
  193. \
  194. DECLARE_CLASS_CHECKERS(ClassType, OBJ_NAME, TYPENAME)
  195. /**
  196. * OBJECT_DECLARE_TYPE:
  197. * @InstanceType: instance struct name
  198. * @ClassType: class struct name
  199. * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
  200. *
  201. * This macro is typically used in a header file, and will:
  202. *
  203. * - create the typedefs for the object and class structs
  204. * - register the type for use with g_autoptr
  205. * - provide three standard type cast functions
  206. *
  207. * The object struct and class struct need to be declared manually.
  208. */
  209. #define OBJECT_DECLARE_TYPE(InstanceType, ClassType, MODULE_OBJ_NAME) \
  210. typedef struct InstanceType InstanceType; \
  211. typedef struct ClassType ClassType; \
  212. \
  213. G_DEFINE_AUTOPTR_CLEANUP_FUNC(InstanceType, object_unref) \
  214. \
  215. DECLARE_OBJ_CHECKERS(InstanceType, ClassType, \
  216. MODULE_OBJ_NAME, TYPE_##MODULE_OBJ_NAME)
  217. /**
  218. * OBJECT_DECLARE_SIMPLE_TYPE:
  219. * @InstanceType: instance struct name
  220. * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
  221. *
  222. * This does the same as OBJECT_DECLARE_TYPE(), but with no class struct
  223. * declared.
  224. *
  225. * This macro should be used unless the class struct needs to have
  226. * virtual methods declared.
  227. */
  228. #define OBJECT_DECLARE_SIMPLE_TYPE(InstanceType, MODULE_OBJ_NAME) \
  229. typedef struct InstanceType InstanceType; \
  230. \
  231. G_DEFINE_AUTOPTR_CLEANUP_FUNC(InstanceType, object_unref) \
  232. \
  233. DECLARE_INSTANCE_CHECKER(InstanceType, MODULE_OBJ_NAME, TYPE_##MODULE_OBJ_NAME)
  234. /**
  235. * DO_OBJECT_DEFINE_TYPE_EXTENDED:
  236. * @ModuleObjName: the object name with initial caps
  237. * @module_obj_name: the object name in lowercase with underscore separators
  238. * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
  239. * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
  240. * separators
  241. * @ABSTRACT: boolean flag to indicate whether the object can be instantiated
  242. * @CLASS_SIZE: size of the type's class
  243. * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces
  244. *
  245. * This is the base macro used to implement all the OBJECT_DEFINE_*
  246. * macros. It should never be used directly in a source file.
  247. */
  248. #define DO_OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
  249. MODULE_OBJ_NAME, \
  250. PARENT_MODULE_OBJ_NAME, \
  251. ABSTRACT, CLASS_SIZE, ...) \
  252. static void \
  253. module_obj_name##_finalize(Object *obj); \
  254. static void \
  255. module_obj_name##_class_init(ObjectClass *oc, void *data); \
  256. static void \
  257. module_obj_name##_init(Object *obj); \
  258. \
  259. static const TypeInfo module_obj_name##_info = { \
  260. .parent = TYPE_##PARENT_MODULE_OBJ_NAME, \
  261. .name = TYPE_##MODULE_OBJ_NAME, \
  262. .instance_size = sizeof(ModuleObjName), \
  263. .instance_align = __alignof__(ModuleObjName), \
  264. .instance_init = module_obj_name##_init, \
  265. .instance_finalize = module_obj_name##_finalize, \
  266. .class_size = CLASS_SIZE, \
  267. .class_init = module_obj_name##_class_init, \
  268. .abstract = ABSTRACT, \
  269. .interfaces = (InterfaceInfo[]) { __VA_ARGS__ } , \
  270. }; \
  271. \
  272. static void \
  273. module_obj_name##_register_types(void) \
  274. { \
  275. type_register_static(&module_obj_name##_info); \
  276. } \
  277. type_init(module_obj_name##_register_types);
  278. /**
  279. * OBJECT_DEFINE_TYPE_EXTENDED:
  280. * @ModuleObjName: the object name with initial caps
  281. * @module_obj_name: the object name in lowercase with underscore separators
  282. * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
  283. * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
  284. * separators
  285. * @ABSTRACT: boolean flag to indicate whether the object can be instantiated
  286. * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces
  287. *
  288. * This macro is typically used in a source file, and will:
  289. *
  290. * - declare prototypes for _finalize, _class_init and _init methods
  291. * - declare the TypeInfo struct instance
  292. * - provide the constructor to register the type
  293. *
  294. * After using this macro, implementations of the _finalize, _class_init,
  295. * and _init methods need to be written. Any of these can be zero-line
  296. * no-op impls if no special logic is required for a given type.
  297. *
  298. * This macro should rarely be used, instead one of the more specialized
  299. * macros is usually a better choice.
  300. */
  301. #define OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
  302. MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
  303. ABSTRACT, ...) \
  304. DO_OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
  305. MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
  306. ABSTRACT, sizeof(ModuleObjName##Class), \
  307. __VA_ARGS__)
  308. /**
  309. * OBJECT_DEFINE_TYPE:
  310. * @ModuleObjName: the object name with initial caps
  311. * @module_obj_name: the object name in lowercase with underscore separators
  312. * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
  313. * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
  314. * separators
  315. *
  316. * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
  317. * for the common case of a non-abstract type, without any interfaces.
  318. */
  319. #define OBJECT_DEFINE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME, \
  320. PARENT_MODULE_OBJ_NAME) \
  321. OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
  322. MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
  323. false, { NULL })
  324. /**
  325. * OBJECT_DEFINE_TYPE_WITH_INTERFACES:
  326. * @ModuleObjName: the object name with initial caps
  327. * @module_obj_name: the object name in lowercase with underscore separators
  328. * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
  329. * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
  330. * separators
  331. * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces
  332. *
  333. * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
  334. * for the common case of a non-abstract type, with one or more implemented
  335. * interfaces.
  336. *
  337. * Note when passing the list of interfaces, be sure to include the final
  338. * NULL entry, e.g. { TYPE_USER_CREATABLE }, { NULL }
  339. */
  340. #define OBJECT_DEFINE_TYPE_WITH_INTERFACES(ModuleObjName, module_obj_name, \
  341. MODULE_OBJ_NAME, \
  342. PARENT_MODULE_OBJ_NAME, ...) \
  343. OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
  344. MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
  345. false, __VA_ARGS__)
  346. /**
  347. * OBJECT_DEFINE_ABSTRACT_TYPE:
  348. * @ModuleObjName: the object name with initial caps
  349. * @module_obj_name: the object name in lowercase with underscore separators
  350. * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
  351. * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
  352. * separators
  353. *
  354. * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
  355. * for defining an abstract type, without any interfaces.
  356. */
  357. #define OBJECT_DEFINE_ABSTRACT_TYPE(ModuleObjName, module_obj_name, \
  358. MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME) \
  359. OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
  360. MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
  361. true, { NULL })
  362. /**
  363. * OBJECT_DEFINE_SIMPLE_TYPE_WITH_INTERFACES:
  364. * @ModuleObjName: the object name with initial caps
  365. * @module_obj_name: the object name in lowercase with underscore separators
  366. * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
  367. * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
  368. * separators
  369. *
  370. * This is a variant of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable for
  371. * the case of a non-abstract type, with interfaces, and with no requirement
  372. * for a class struct.
  373. */
  374. #define OBJECT_DEFINE_SIMPLE_TYPE_WITH_INTERFACES(ModuleObjName, \
  375. module_obj_name, \
  376. MODULE_OBJ_NAME, \
  377. PARENT_MODULE_OBJ_NAME, ...) \
  378. DO_OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
  379. MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
  380. false, 0, __VA_ARGS__)
  381. /**
  382. * OBJECT_DEFINE_SIMPLE_TYPE:
  383. * @ModuleObjName: the object name with initial caps
  384. * @module_obj_name: the object name in lowercase with underscore separators
  385. * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
  386. * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
  387. * separators
  388. *
  389. * This is a variant of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable for
  390. * the common case of a non-abstract type, without any interfaces, and with
  391. * no requirement for a class struct. If you declared your type with
  392. * OBJECT_DECLARE_SIMPLE_TYPE then this is probably the right choice for
  393. * defining it.
  394. */
  395. #define OBJECT_DEFINE_SIMPLE_TYPE(ModuleObjName, module_obj_name, \
  396. MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME) \
  397. OBJECT_DEFINE_SIMPLE_TYPE_WITH_INTERFACES(ModuleObjName, module_obj_name, \
  398. MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, { NULL })
  399. /**
  400. * struct TypeInfo:
  401. * @name: The name of the type.
  402. * @parent: The name of the parent type.
  403. * @instance_size: The size of the object (derivative of #Object). If
  404. * @instance_size is 0, then the size of the object will be the size of the
  405. * parent object.
  406. * @instance_align: The required alignment of the object. If @instance_align
  407. * is 0, then normal malloc alignment is sufficient; if non-zero, then we
  408. * must use qemu_memalign for allocation.
  409. * @instance_init: This function is called to initialize an object. The parent
  410. * class will have already been initialized so the type is only responsible
  411. * for initializing its own members.
  412. * @instance_post_init: This function is called to finish initialization of
  413. * an object, after all @instance_init functions were called.
  414. * @instance_finalize: This function is called during object destruction. This
  415. * is called before the parent @instance_finalize function has been called.
  416. * An object should only free the members that are unique to its type in this
  417. * function.
  418. * @abstract: If this field is true, then the class is considered abstract and
  419. * cannot be directly instantiated.
  420. * @class_size: The size of the class object (derivative of #ObjectClass)
  421. * for this object. If @class_size is 0, then the size of the class will be
  422. * assumed to be the size of the parent class. This allows a type to avoid
  423. * implementing an explicit class type if they are not adding additional
  424. * virtual functions.
  425. * @class_init: This function is called after all parent class initialization
  426. * has occurred to allow a class to set its default virtual method pointers.
  427. * This is also the function to use to override virtual methods from a parent
  428. * class.
  429. * @class_base_init: This function is called for all base classes after all
  430. * parent class initialization has occurred, but before the class itself
  431. * is initialized. This is the function to use to undo the effects of
  432. * memcpy from the parent class to the descendants.
  433. * @class_data: Data to pass to the @class_init,
  434. * @class_base_init. This can be useful when building dynamic
  435. * classes.
  436. * @interfaces: The list of interfaces associated with this type. This
  437. * should point to a static array that's terminated with a zero filled
  438. * element.
  439. */
  440. struct TypeInfo
  441. {
  442. const char *name;
  443. const char *parent;
  444. size_t instance_size;
  445. size_t instance_align;
  446. void (*instance_init)(Object *obj);
  447. void (*instance_post_init)(Object *obj);
  448. void (*instance_finalize)(Object *obj);
  449. bool abstract;
  450. size_t class_size;
  451. void (*class_init)(ObjectClass *klass, void *data);
  452. void (*class_base_init)(ObjectClass *klass, void *data);
  453. void *class_data;
  454. InterfaceInfo *interfaces;
  455. };
  456. /**
  457. * OBJECT:
  458. * @obj: A derivative of #Object
  459. *
  460. * Converts an object to a #Object. Since all objects are #Objects,
  461. * this function will always succeed.
  462. */
  463. #define OBJECT(obj) \
  464. ((Object *)(obj))
  465. /**
  466. * OBJECT_CLASS:
  467. * @class: A derivative of #ObjectClass.
  468. *
  469. * Converts a class to an #ObjectClass. Since all objects are #Objects,
  470. * this function will always succeed.
  471. */
  472. #define OBJECT_CLASS(class) \
  473. ((ObjectClass *)(class))
  474. /**
  475. * OBJECT_CHECK:
  476. * @type: The C type to use for the return value.
  477. * @obj: A derivative of @type to cast.
  478. * @name: The QOM typename of @type
  479. *
  480. * A type safe version of @object_dynamic_cast_assert. Typically each class
  481. * will define a macro based on this type to perform type safe dynamic_casts to
  482. * this object type.
  483. *
  484. * If an invalid object is passed to this function, a run time assert will be
  485. * generated.
  486. */
  487. #define OBJECT_CHECK(type, obj, name) \
  488. ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \
  489. __FILE__, __LINE__, __func__))
  490. /**
  491. * OBJECT_CLASS_CHECK:
  492. * @class_type: The C type to use for the return value.
  493. * @class: A derivative class of @class_type to cast.
  494. * @name: the QOM typename of @class_type.
  495. *
  496. * A type safe version of @object_class_dynamic_cast_assert. This macro is
  497. * typically wrapped by each type to perform type safe casts of a class to a
  498. * specific class type.
  499. */
  500. #define OBJECT_CLASS_CHECK(class_type, class, name) \
  501. ((class_type *)object_class_dynamic_cast_assert(OBJECT_CLASS(class), (name), \
  502. __FILE__, __LINE__, __func__))
  503. /**
  504. * OBJECT_GET_CLASS:
  505. * @class: The C type to use for the return value.
  506. * @obj: The object to obtain the class for.
  507. * @name: The QOM typename of @obj.
  508. *
  509. * This function will return a specific class for a given object. Its generally
  510. * used by each type to provide a type safe macro to get a specific class type
  511. * from an object.
  512. */
  513. #define OBJECT_GET_CLASS(class, obj, name) \
  514. OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
  515. /**
  516. * struct InterfaceInfo:
  517. * @type: The name of the interface.
  518. *
  519. * The information associated with an interface.
  520. */
  521. struct InterfaceInfo {
  522. const char *type;
  523. };
  524. /**
  525. * struct InterfaceClass:
  526. * @parent_class: the base class
  527. *
  528. * The class for all interfaces. Subclasses of this class should only add
  529. * virtual methods.
  530. */
  531. struct InterfaceClass
  532. {
  533. ObjectClass parent_class;
  534. /* private: */
  535. ObjectClass *concrete_class;
  536. Type interface_type;
  537. };
  538. #define TYPE_INTERFACE "interface"
  539. /**
  540. * INTERFACE_CLASS:
  541. * @klass: class to cast from
  542. * Returns: An #InterfaceClass or raise an error if cast is invalid
  543. */
  544. #define INTERFACE_CLASS(klass) \
  545. OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
  546. /**
  547. * INTERFACE_CHECK:
  548. * @interface: the type to return
  549. * @obj: the object to convert to an interface
  550. * @name: the interface type name
  551. *
  552. * Returns: @obj casted to @interface if cast is valid, otherwise raise error.
  553. */
  554. #define INTERFACE_CHECK(interface, obj, name) \
  555. ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \
  556. __FILE__, __LINE__, __func__))
  557. /**
  558. * object_new_with_class:
  559. * @klass: The class to instantiate.
  560. *
  561. * This function will initialize a new object using heap allocated memory.
  562. * The returned object has a reference count of 1, and will be freed when
  563. * the last reference is dropped.
  564. *
  565. * Returns: The newly allocated and instantiated object.
  566. */
  567. Object *object_new_with_class(ObjectClass *klass);
  568. /**
  569. * object_new:
  570. * @typename: The name of the type of the object to instantiate.
  571. *
  572. * This function will initialize a new object using heap allocated memory.
  573. * The returned object has a reference count of 1, and will be freed when
  574. * the last reference is dropped.
  575. *
  576. * Returns: The newly allocated and instantiated object.
  577. */
  578. Object *object_new(const char *typename);
  579. /**
  580. * object_new_with_props:
  581. * @typename: The name of the type of the object to instantiate.
  582. * @parent: the parent object
  583. * @id: The unique ID of the object
  584. * @errp: pointer to error object
  585. * @...: list of property names and values
  586. *
  587. * This function will initialize a new object using heap allocated memory.
  588. * The returned object has a reference count of 1, and will be freed when
  589. * the last reference is dropped.
  590. *
  591. * The @id parameter will be used when registering the object as a
  592. * child of @parent in the composition tree.
  593. *
  594. * The variadic parameters are a list of pairs of (propname, propvalue)
  595. * strings. The propname of %NULL indicates the end of the property
  596. * list. If the object implements the user creatable interface, the
  597. * object will be marked complete once all the properties have been
  598. * processed.
  599. *
  600. * .. code-block:: c
  601. * :caption: Creating an object with properties
  602. *
  603. * Error *err = NULL;
  604. * Object *obj;
  605. *
  606. * obj = object_new_with_props(TYPE_MEMORY_BACKEND_FILE,
  607. * object_get_objects_root(),
  608. * "hostmem0",
  609. * &err,
  610. * "share", "yes",
  611. * "mem-path", "/dev/shm/somefile",
  612. * "prealloc", "yes",
  613. * "size", "1048576",
  614. * NULL);
  615. *
  616. * if (!obj) {
  617. * error_reportf_err(err, "Cannot create memory backend: ");
  618. * }
  619. *
  620. * The returned object will have one stable reference maintained
  621. * for as long as it is present in the object hierarchy.
  622. *
  623. * Returns: The newly allocated, instantiated & initialized object.
  624. */
  625. Object *object_new_with_props(const char *typename,
  626. Object *parent,
  627. const char *id,
  628. Error **errp,
  629. ...) G_GNUC_NULL_TERMINATED;
  630. /**
  631. * object_new_with_propv:
  632. * @typename: The name of the type of the object to instantiate.
  633. * @parent: the parent object
  634. * @id: The unique ID of the object
  635. * @errp: pointer to error object
  636. * @vargs: list of property names and values
  637. *
  638. * See object_new_with_props() for documentation.
  639. */
  640. Object *object_new_with_propv(const char *typename,
  641. Object *parent,
  642. const char *id,
  643. Error **errp,
  644. va_list vargs);
  645. bool object_apply_global_props(Object *obj, const GPtrArray *props,
  646. Error **errp);
  647. void object_set_machine_compat_props(GPtrArray *compat_props);
  648. void object_set_accelerator_compat_props(GPtrArray *compat_props);
  649. void object_register_sugar_prop(const char *driver, const char *prop,
  650. const char *value, bool optional);
  651. void object_apply_compat_props(Object *obj);
  652. /**
  653. * object_set_props:
  654. * @obj: the object instance to set properties on
  655. * @errp: pointer to error object
  656. * @...: list of property names and values
  657. *
  658. * This function will set a list of properties on an existing object
  659. * instance.
  660. *
  661. * The variadic parameters are a list of pairs of (propname, propvalue)
  662. * strings. The propname of %NULL indicates the end of the property
  663. * list.
  664. *
  665. * .. code-block:: c
  666. * :caption: Update an object's properties
  667. *
  668. * Error *err = NULL;
  669. * Object *obj = ...get / create object...;
  670. *
  671. * if (!object_set_props(obj,
  672. * &err,
  673. * "share", "yes",
  674. * "mem-path", "/dev/shm/somefile",
  675. * "prealloc", "yes",
  676. * "size", "1048576",
  677. * NULL)) {
  678. * error_reportf_err(err, "Cannot set properties: ");
  679. * }
  680. *
  681. * The returned object will have one stable reference maintained
  682. * for as long as it is present in the object hierarchy.
  683. *
  684. * Returns: %true on success, %false on error.
  685. */
  686. bool object_set_props(Object *obj, Error **errp, ...) G_GNUC_NULL_TERMINATED;
  687. /**
  688. * object_set_propv:
  689. * @obj: the object instance to set properties on
  690. * @errp: pointer to error object
  691. * @vargs: list of property names and values
  692. *
  693. * See object_set_props() for documentation.
  694. *
  695. * Returns: %true on success, %false on error.
  696. */
  697. bool object_set_propv(Object *obj, Error **errp, va_list vargs);
  698. /**
  699. * object_initialize:
  700. * @obj: A pointer to the memory to be used for the object.
  701. * @size: The maximum size available at @obj for the object.
  702. * @typename: The name of the type of the object to instantiate.
  703. *
  704. * This function will initialize an object. The memory for the object should
  705. * have already been allocated. The returned object has a reference count of 1,
  706. * and will be finalized when the last reference is dropped.
  707. */
  708. void object_initialize(void *obj, size_t size, const char *typename);
  709. /**
  710. * object_initialize_child_with_props:
  711. * @parentobj: The parent object to add a property to
  712. * @propname: The name of the property
  713. * @childobj: A pointer to the memory to be used for the object.
  714. * @size: The maximum size available at @childobj for the object.
  715. * @type: The name of the type of the object to instantiate.
  716. * @errp: If an error occurs, a pointer to an area to store the error
  717. * @...: list of property names and values
  718. *
  719. * This function will initialize an object. The memory for the object should
  720. * have already been allocated. The object will then be added as child property
  721. * to a parent with object_property_add_child() function. The returned object
  722. * has a reference count of 1 (for the "child<...>" property from the parent),
  723. * so the object will be finalized automatically when the parent gets removed.
  724. *
  725. * The variadic parameters are a list of pairs of (propname, propvalue)
  726. * strings. The propname of %NULL indicates the end of the property list.
  727. * If the object implements the user creatable interface, the object will
  728. * be marked complete once all the properties have been processed.
  729. *
  730. * Returns: %true on success, %false on failure.
  731. */
  732. bool object_initialize_child_with_props(Object *parentobj,
  733. const char *propname,
  734. void *childobj, size_t size, const char *type,
  735. Error **errp, ...) G_GNUC_NULL_TERMINATED;
  736. /**
  737. * object_initialize_child_with_propsv:
  738. * @parentobj: The parent object to add a property to
  739. * @propname: The name of the property
  740. * @childobj: A pointer to the memory to be used for the object.
  741. * @size: The maximum size available at @childobj for the object.
  742. * @type: The name of the type of the object to instantiate.
  743. * @errp: If an error occurs, a pointer to an area to store the error
  744. * @vargs: list of property names and values
  745. *
  746. * See object_initialize_child() for documentation.
  747. *
  748. * Returns: %true on success, %false on failure.
  749. */
  750. bool object_initialize_child_with_propsv(Object *parentobj,
  751. const char *propname,
  752. void *childobj, size_t size, const char *type,
  753. Error **errp, va_list vargs);
  754. /**
  755. * object_initialize_child:
  756. * @parent: The parent object to add a property to
  757. * @propname: The name of the property
  758. * @child: A precisely typed pointer to the memory to be used for the
  759. * object.
  760. * @type: The name of the type of the object to instantiate.
  761. *
  762. * This is like::
  763. *
  764. * object_initialize_child_with_props(parent, propname,
  765. * child, sizeof(*child), type,
  766. * &error_abort, NULL)
  767. */
  768. #define object_initialize_child(parent, propname, child, type) \
  769. object_initialize_child_internal((parent), (propname), \
  770. (child), sizeof(*(child)), (type))
  771. void object_initialize_child_internal(Object *parent, const char *propname,
  772. void *child, size_t size,
  773. const char *type);
  774. /**
  775. * object_dynamic_cast:
  776. * @obj: The object to cast.
  777. * @typename: The @typename to cast to.
  778. *
  779. * This function will determine if @obj is-a @typename. @obj can refer to an
  780. * object or an interface associated with an object.
  781. *
  782. * Returns: This function returns @obj on success or #NULL on failure.
  783. */
  784. Object *object_dynamic_cast(Object *obj, const char *typename);
  785. /**
  786. * object_dynamic_cast_assert:
  787. * @obj: The object to cast.
  788. * @typename: The @typename to cast to.
  789. * @file: Source code file where function was called
  790. * @line: Source code line where function was called
  791. * @func: Name of function where this function was called
  792. *
  793. * See object_dynamic_cast() for a description of the parameters of this
  794. * function. The only difference in behavior is that this function asserts
  795. * instead of returning #NULL on failure if QOM cast debugging is enabled.
  796. * This function is not meant to be called directly, but only through
  797. * the wrapper macro OBJECT_CHECK.
  798. */
  799. Object *object_dynamic_cast_assert(Object *obj, const char *typename,
  800. const char *file, int line, const char *func);
  801. /**
  802. * object_get_class:
  803. * @obj: A derivative of #Object
  804. *
  805. * Returns: The #ObjectClass of the type associated with @obj.
  806. */
  807. ObjectClass *object_get_class(Object *obj);
  808. /**
  809. * object_get_typename:
  810. * @obj: A derivative of #Object.
  811. *
  812. * Returns: The QOM typename of @obj.
  813. */
  814. const char *object_get_typename(const Object *obj);
  815. /**
  816. * type_register_static:
  817. * @info: The #TypeInfo of the new type.
  818. *
  819. * @info and all of the strings it points to should exist for the life time
  820. * that the type is registered.
  821. *
  822. * Returns: the new #Type.
  823. */
  824. Type type_register_static(const TypeInfo *info);
  825. /**
  826. * type_register:
  827. * @info: The #TypeInfo of the new type
  828. *
  829. * Unlike type_register_static(), this call does not require @info or its
  830. * string members to continue to exist after the call returns.
  831. *
  832. * Returns: the new #Type.
  833. */
  834. Type type_register(const TypeInfo *info);
  835. /**
  836. * type_register_static_array:
  837. * @infos: The array of the new type #TypeInfo structures.
  838. * @nr_infos: number of entries in @infos
  839. *
  840. * @infos and all of the strings it points to should exist for the life time
  841. * that the type is registered.
  842. */
  843. void type_register_static_array(const TypeInfo *infos, int nr_infos);
  844. /**
  845. * DEFINE_TYPES:
  846. * @type_array: The array containing #TypeInfo structures to register
  847. *
  848. * @type_array should be static constant that exists for the life time
  849. * that the type is registered.
  850. */
  851. #define DEFINE_TYPES(type_array) \
  852. static void do_qemu_init_ ## type_array(void) \
  853. { \
  854. type_register_static_array(type_array, ARRAY_SIZE(type_array)); \
  855. } \
  856. type_init(do_qemu_init_ ## type_array)
  857. /**
  858. * type_print_class_properties:
  859. * @type: a QOM class name
  860. *
  861. * Print the object's class properties to stdout or the monitor.
  862. * Return whether an object was found.
  863. */
  864. bool type_print_class_properties(const char *type);
  865. /**
  866. * object_set_properties_from_keyval:
  867. * @obj: a QOM object
  868. * @qdict: a dictionary with the properties to be set
  869. * @from_json: true if leaf values of @qdict are typed, false if they
  870. * are strings
  871. * @errp: pointer to error object
  872. *
  873. * For each key in the dictionary, parse the value string if needed,
  874. * then set the corresponding property in @obj.
  875. */
  876. void object_set_properties_from_keyval(Object *obj, const QDict *qdict,
  877. bool from_json, Error **errp);
  878. /**
  879. * object_class_dynamic_cast_assert:
  880. * @klass: The #ObjectClass to attempt to cast.
  881. * @typename: The QOM typename of the class to cast to.
  882. * @file: Source code file where function was called
  883. * @line: Source code line where function was called
  884. * @func: Name of function where this function was called
  885. *
  886. * See object_class_dynamic_cast() for a description of the parameters
  887. * of this function. The only difference in behavior is that this function
  888. * asserts instead of returning #NULL on failure if QOM cast debugging is
  889. * enabled. This function is not meant to be called directly, but only through
  890. * the wrapper macro OBJECT_CLASS_CHECK.
  891. */
  892. ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass,
  893. const char *typename,
  894. const char *file, int line,
  895. const char *func);
  896. /**
  897. * object_class_dynamic_cast:
  898. * @klass: The #ObjectClass to attempt to cast.
  899. * @typename: The QOM typename of the class to cast to.
  900. *
  901. * Returns: If @typename is a class, this function returns @klass if
  902. * @typename is a subtype of @klass, else returns #NULL.
  903. *
  904. * If @typename is an interface, this function returns the interface
  905. * definition for @klass if @klass implements it unambiguously; #NULL
  906. * is returned if @klass does not implement the interface or if multiple
  907. * classes or interfaces on the hierarchy leading to @klass implement
  908. * it. (FIXME: perhaps this can be detected at type definition time?)
  909. */
  910. ObjectClass *object_class_dynamic_cast(ObjectClass *klass,
  911. const char *typename);
  912. /**
  913. * object_class_get_parent:
  914. * @klass: The class to obtain the parent for.
  915. *
  916. * Returns: The parent for @klass or %NULL if none.
  917. */
  918. ObjectClass *object_class_get_parent(ObjectClass *klass);
  919. /**
  920. * object_class_get_name:
  921. * @klass: The class to obtain the QOM typename for.
  922. *
  923. * Returns: The QOM typename for @klass.
  924. */
  925. const char *object_class_get_name(ObjectClass *klass);
  926. /**
  927. * object_class_is_abstract:
  928. * @klass: The class to obtain the abstractness for.
  929. *
  930. * Returns: %true if @klass is abstract, %false otherwise.
  931. */
  932. bool object_class_is_abstract(ObjectClass *klass);
  933. /**
  934. * object_class_by_name:
  935. * @typename: The QOM typename to obtain the class for.
  936. *
  937. * Returns: The class for @typename or %NULL if not found.
  938. */
  939. ObjectClass *object_class_by_name(const char *typename);
  940. /**
  941. * module_object_class_by_name:
  942. * @typename: The QOM typename to obtain the class for.
  943. *
  944. * For objects which might be provided by a module. Behaves like
  945. * object_class_by_name, but additionally tries to load the module
  946. * needed in case the class is not available.
  947. *
  948. * Returns: The class for @typename or %NULL if not found.
  949. */
  950. ObjectClass *module_object_class_by_name(const char *typename);
  951. void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
  952. const char *implements_type, bool include_abstract,
  953. void *opaque);
  954. /**
  955. * object_class_get_list:
  956. * @implements_type: The type to filter for, including its derivatives.
  957. * @include_abstract: Whether to include abstract classes.
  958. *
  959. * Returns: A singly-linked list of the classes in reverse hashtable order.
  960. */
  961. GSList *object_class_get_list(const char *implements_type,
  962. bool include_abstract);
  963. /**
  964. * object_class_get_list_sorted:
  965. * @implements_type: The type to filter for, including its derivatives.
  966. * @include_abstract: Whether to include abstract classes.
  967. *
  968. * Returns: A singly-linked list of the classes in alphabetical
  969. * case-insensitive order.
  970. */
  971. GSList *object_class_get_list_sorted(const char *implements_type,
  972. bool include_abstract);
  973. /**
  974. * object_ref:
  975. * @obj: the object
  976. *
  977. * Increase the reference count of a object. A object cannot be freed as long
  978. * as its reference count is greater than zero.
  979. * Returns: @obj
  980. */
  981. Object *object_ref(void *obj);
  982. /**
  983. * object_unref:
  984. * @obj: the object
  985. *
  986. * Decrease the reference count of a object. A object cannot be freed as long
  987. * as its reference count is greater than zero.
  988. */
  989. void object_unref(void *obj);
  990. /**
  991. * object_property_try_add:
  992. * @obj: the object to add a property to
  993. * @name: the name of the property. This can contain any character except for
  994. * a forward slash. In general, you should use hyphens '-' instead of
  995. * underscores '_' when naming properties.
  996. * @type: the type name of the property. This namespace is pretty loosely
  997. * defined. Sub namespaces are constructed by using a prefix and then
  998. * to angle brackets. For instance, the type 'virtio-net-pci' in the
  999. * 'link' namespace would be 'link<virtio-net-pci>'.
  1000. * @get: The getter to be called to read a property. If this is NULL, then
  1001. * the property cannot be read.
  1002. * @set: the setter to be called to write a property. If this is NULL,
  1003. * then the property cannot be written.
  1004. * @release: called when the property is removed from the object. This is
  1005. * meant to allow a property to free its opaque upon object
  1006. * destruction. This may be NULL.
  1007. * @opaque: an opaque pointer to pass to the callbacks for the property
  1008. * @errp: pointer to error object
  1009. *
  1010. * Returns: The #ObjectProperty; this can be used to set the @resolve
  1011. * callback for child and link properties.
  1012. */
  1013. ObjectProperty *object_property_try_add(Object *obj, const char *name,
  1014. const char *type,
  1015. ObjectPropertyAccessor *get,
  1016. ObjectPropertyAccessor *set,
  1017. ObjectPropertyRelease *release,
  1018. void *opaque, Error **errp);
  1019. /**
  1020. * object_property_add:
  1021. * Same as object_property_try_add() with @errp hardcoded to
  1022. * &error_abort.
  1023. *
  1024. * @obj: the object to add a property to
  1025. * @name: the name of the property. This can contain any character except for
  1026. * a forward slash. In general, you should use hyphens '-' instead of
  1027. * underscores '_' when naming properties.
  1028. * @type: the type name of the property. This namespace is pretty loosely
  1029. * defined. Sub namespaces are constructed by using a prefix and then
  1030. * to angle brackets. For instance, the type 'virtio-net-pci' in the
  1031. * 'link' namespace would be 'link<virtio-net-pci>'.
  1032. * @get: The getter to be called to read a property. If this is NULL, then
  1033. * the property cannot be read.
  1034. * @set: the setter to be called to write a property. If this is NULL,
  1035. * then the property cannot be written.
  1036. * @release: called when the property is removed from the object. This is
  1037. * meant to allow a property to free its opaque upon object
  1038. * destruction. This may be NULL.
  1039. * @opaque: an opaque pointer to pass to the callbacks for the property
  1040. */
  1041. ObjectProperty *object_property_add(Object *obj, const char *name,
  1042. const char *type,
  1043. ObjectPropertyAccessor *get,
  1044. ObjectPropertyAccessor *set,
  1045. ObjectPropertyRelease *release,
  1046. void *opaque);
  1047. void object_property_del(Object *obj, const char *name);
  1048. ObjectProperty *object_class_property_add(ObjectClass *klass, const char *name,
  1049. const char *type,
  1050. ObjectPropertyAccessor *get,
  1051. ObjectPropertyAccessor *set,
  1052. ObjectPropertyRelease *release,
  1053. void *opaque);
  1054. /**
  1055. * object_property_set_default_bool:
  1056. * @prop: the property to set
  1057. * @value: the value to be written to the property
  1058. *
  1059. * Set the property default value.
  1060. */
  1061. void object_property_set_default_bool(ObjectProperty *prop, bool value);
  1062. /**
  1063. * object_property_set_default_str:
  1064. * @prop: the property to set
  1065. * @value: the value to be written to the property
  1066. *
  1067. * Set the property default value.
  1068. */
  1069. void object_property_set_default_str(ObjectProperty *prop, const char *value);
  1070. /**
  1071. * object_property_set_default_list:
  1072. * @prop: the property to set
  1073. *
  1074. * Set the property default value to be an empty list.
  1075. */
  1076. void object_property_set_default_list(ObjectProperty *prop);
  1077. /**
  1078. * object_property_set_default_int:
  1079. * @prop: the property to set
  1080. * @value: the value to be written to the property
  1081. *
  1082. * Set the property default value.
  1083. */
  1084. void object_property_set_default_int(ObjectProperty *prop, int64_t value);
  1085. /**
  1086. * object_property_set_default_uint:
  1087. * @prop: the property to set
  1088. * @value: the value to be written to the property
  1089. *
  1090. * Set the property default value.
  1091. */
  1092. void object_property_set_default_uint(ObjectProperty *prop, uint64_t value);
  1093. /**
  1094. * object_property_find:
  1095. * @obj: the object
  1096. * @name: the name of the property
  1097. *
  1098. * Look up a property for an object.
  1099. *
  1100. * Return its #ObjectProperty if found, or NULL.
  1101. */
  1102. ObjectProperty *object_property_find(Object *obj, const char *name);
  1103. /**
  1104. * object_property_find_err:
  1105. * @obj: the object
  1106. * @name: the name of the property
  1107. * @errp: returns an error if this function fails
  1108. *
  1109. * Look up a property for an object.
  1110. *
  1111. * Return its #ObjectProperty if found, or NULL.
  1112. */
  1113. ObjectProperty *object_property_find_err(Object *obj,
  1114. const char *name,
  1115. Error **errp);
  1116. /**
  1117. * object_class_property_find:
  1118. * @klass: the object class
  1119. * @name: the name of the property
  1120. *
  1121. * Look up a property for an object class.
  1122. *
  1123. * Return its #ObjectProperty if found, or NULL.
  1124. */
  1125. ObjectProperty *object_class_property_find(ObjectClass *klass,
  1126. const char *name);
  1127. /**
  1128. * object_class_property_find_err:
  1129. * @klass: the object class
  1130. * @name: the name of the property
  1131. * @errp: returns an error if this function fails
  1132. *
  1133. * Look up a property for an object class.
  1134. *
  1135. * Return its #ObjectProperty if found, or NULL.
  1136. */
  1137. ObjectProperty *object_class_property_find_err(ObjectClass *klass,
  1138. const char *name,
  1139. Error **errp);
  1140. typedef struct ObjectPropertyIterator {
  1141. ObjectClass *nextclass;
  1142. GHashTableIter iter;
  1143. } ObjectPropertyIterator;
  1144. /**
  1145. * object_property_iter_init:
  1146. * @iter: the iterator instance
  1147. * @obj: the object
  1148. *
  1149. * Initializes an iterator for traversing all properties
  1150. * registered against an object instance, its class and all parent classes.
  1151. *
  1152. * It is forbidden to modify the property list while iterating,
  1153. * whether removing or adding properties.
  1154. *
  1155. * Typical usage pattern would be
  1156. *
  1157. * .. code-block:: c
  1158. * :caption: Using object property iterators
  1159. *
  1160. * ObjectProperty *prop;
  1161. * ObjectPropertyIterator iter;
  1162. *
  1163. * object_property_iter_init(&iter, obj);
  1164. * while ((prop = object_property_iter_next(&iter))) {
  1165. * ... do something with prop ...
  1166. * }
  1167. */
  1168. void object_property_iter_init(ObjectPropertyIterator *iter,
  1169. Object *obj);
  1170. /**
  1171. * object_class_property_iter_init:
  1172. * @iter: the iterator instance
  1173. * @klass: the class
  1174. *
  1175. * Initializes an iterator for traversing all properties
  1176. * registered against an object class and all parent classes.
  1177. *
  1178. * It is forbidden to modify the property list while iterating,
  1179. * whether removing or adding properties.
  1180. *
  1181. * This can be used on abstract classes as it does not create a temporary
  1182. * instance.
  1183. */
  1184. void object_class_property_iter_init(ObjectPropertyIterator *iter,
  1185. ObjectClass *klass);
  1186. /**
  1187. * object_property_iter_next:
  1188. * @iter: the iterator instance
  1189. *
  1190. * Return the next available property. If no further properties
  1191. * are available, a %NULL value will be returned and the @iter
  1192. * pointer should not be used again after this point without
  1193. * re-initializing it.
  1194. *
  1195. * Returns: the next property, or %NULL when all properties
  1196. * have been traversed.
  1197. */
  1198. ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter);
  1199. void object_unparent(Object *obj);
  1200. /**
  1201. * object_property_get:
  1202. * @obj: the object
  1203. * @name: the name of the property
  1204. * @v: the visitor that will receive the property value. This should be an
  1205. * Output visitor and the data will be written with @name as the name.
  1206. * @errp: returns an error if this function fails
  1207. *
  1208. * Reads a property from a object.
  1209. *
  1210. * Returns: %true on success, %false on failure.
  1211. */
  1212. bool object_property_get(Object *obj, const char *name, Visitor *v,
  1213. Error **errp);
  1214. /**
  1215. * object_property_set_str:
  1216. * @obj: the object
  1217. * @name: the name of the property
  1218. * @value: the value to be written to the property
  1219. * @errp: returns an error if this function fails
  1220. *
  1221. * Writes a string value to a property.
  1222. *
  1223. * Returns: %true on success, %false on failure.
  1224. */
  1225. bool object_property_set_str(Object *obj, const char *name,
  1226. const char *value, Error **errp);
  1227. /**
  1228. * object_property_get_str:
  1229. * @obj: the object
  1230. * @name: the name of the property
  1231. * @errp: returns an error if this function fails
  1232. *
  1233. * Returns: the value of the property, converted to a C string, or NULL if
  1234. * an error occurs (including when the property value is not a string).
  1235. * The caller should free the string.
  1236. */
  1237. char *object_property_get_str(Object *obj, const char *name,
  1238. Error **errp);
  1239. /**
  1240. * object_property_set_link:
  1241. * @obj: the object
  1242. * @name: the name of the property
  1243. * @value: the value to be written to the property
  1244. * @errp: returns an error if this function fails
  1245. *
  1246. * Writes an object's canonical path to a property.
  1247. *
  1248. * If the link property was created with
  1249. * %OBJ_PROP_LINK_STRONG bit, the old target object is
  1250. * unreferenced, and a reference is added to the new target object.
  1251. *
  1252. * Returns: %true on success, %false on failure.
  1253. */
  1254. bool object_property_set_link(Object *obj, const char *name,
  1255. Object *value, Error **errp);
  1256. /**
  1257. * object_property_get_link:
  1258. * @obj: the object
  1259. * @name: the name of the property
  1260. * @errp: returns an error if this function fails
  1261. *
  1262. * Returns: the value of the property, resolved from a path to an Object,
  1263. * or NULL if an error occurs (including when the property value is not a
  1264. * string or not a valid object path).
  1265. */
  1266. Object *object_property_get_link(Object *obj, const char *name,
  1267. Error **errp);
  1268. /**
  1269. * object_property_set_bool:
  1270. * @obj: the object
  1271. * @name: the name of the property
  1272. * @value: the value to be written to the property
  1273. * @errp: returns an error if this function fails
  1274. *
  1275. * Writes a bool value to a property.
  1276. *
  1277. * Returns: %true on success, %false on failure.
  1278. */
  1279. bool object_property_set_bool(Object *obj, const char *name,
  1280. bool value, Error **errp);
  1281. /**
  1282. * object_property_get_bool:
  1283. * @obj: the object
  1284. * @name: the name of the property
  1285. * @errp: returns an error if this function fails
  1286. *
  1287. * Returns: the value of the property, converted to a boolean, or false if
  1288. * an error occurs (including when the property value is not a bool).
  1289. */
  1290. bool object_property_get_bool(Object *obj, const char *name,
  1291. Error **errp);
  1292. /**
  1293. * object_property_set_int:
  1294. * @obj: the object
  1295. * @name: the name of the property
  1296. * @value: the value to be written to the property
  1297. * @errp: returns an error if this function fails
  1298. *
  1299. * Writes an integer value to a property.
  1300. *
  1301. * Returns: %true on success, %false on failure.
  1302. */
  1303. bool object_property_set_int(Object *obj, const char *name,
  1304. int64_t value, Error **errp);
  1305. /**
  1306. * object_property_get_int:
  1307. * @obj: the object
  1308. * @name: the name of the property
  1309. * @errp: returns an error if this function fails
  1310. *
  1311. * Returns: the value of the property, converted to an integer, or -1 if
  1312. * an error occurs (including when the property value is not an integer).
  1313. */
  1314. int64_t object_property_get_int(Object *obj, const char *name,
  1315. Error **errp);
  1316. /**
  1317. * object_property_set_uint:
  1318. * @obj: the object
  1319. * @name: the name of the property
  1320. * @value: the value to be written to the property
  1321. * @errp: returns an error if this function fails
  1322. *
  1323. * Writes an unsigned integer value to a property.
  1324. *
  1325. * Returns: %true on success, %false on failure.
  1326. */
  1327. bool object_property_set_uint(Object *obj, const char *name,
  1328. uint64_t value, Error **errp);
  1329. /**
  1330. * object_property_get_uint:
  1331. * @obj: the object
  1332. * @name: the name of the property
  1333. * @errp: returns an error if this function fails
  1334. *
  1335. * Returns: the value of the property, converted to an unsigned integer, or 0
  1336. * an error occurs (including when the property value is not an integer).
  1337. */
  1338. uint64_t object_property_get_uint(Object *obj, const char *name,
  1339. Error **errp);
  1340. /**
  1341. * object_property_get_enum:
  1342. * @obj: the object
  1343. * @name: the name of the property
  1344. * @typename: the name of the enum data type
  1345. * @errp: returns an error if this function fails
  1346. *
  1347. * Returns: the value of the property, converted to an integer (which
  1348. * can't be negative), or -1 on error (including when the property
  1349. * value is not an enum).
  1350. */
  1351. int object_property_get_enum(Object *obj, const char *name,
  1352. const char *typename, Error **errp);
  1353. /**
  1354. * object_property_set:
  1355. * @obj: the object
  1356. * @name: the name of the property
  1357. * @v: the visitor that will be used to write the property value. This should
  1358. * be an Input visitor and the data will be first read with @name as the
  1359. * name and then written as the property value.
  1360. * @errp: returns an error if this function fails
  1361. *
  1362. * Writes a property to a object.
  1363. *
  1364. * Returns: %true on success, %false on failure.
  1365. */
  1366. bool object_property_set(Object *obj, const char *name, Visitor *v,
  1367. Error **errp);
  1368. /**
  1369. * object_property_parse:
  1370. * @obj: the object
  1371. * @name: the name of the property
  1372. * @string: the string that will be used to parse the property value.
  1373. * @errp: returns an error if this function fails
  1374. *
  1375. * Parses a string and writes the result into a property of an object.
  1376. *
  1377. * Returns: %true on success, %false on failure.
  1378. */
  1379. bool object_property_parse(Object *obj, const char *name,
  1380. const char *string, Error **errp);
  1381. /**
  1382. * object_property_print:
  1383. * @obj: the object
  1384. * @name: the name of the property
  1385. * @human: if true, print for human consumption
  1386. * @errp: returns an error if this function fails
  1387. *
  1388. * Returns a string representation of the value of the property. The
  1389. * caller shall free the string.
  1390. */
  1391. char *object_property_print(Object *obj, const char *name, bool human,
  1392. Error **errp);
  1393. /**
  1394. * object_property_get_type:
  1395. * @obj: the object
  1396. * @name: the name of the property
  1397. * @errp: returns an error if this function fails
  1398. *
  1399. * Returns: The type name of the property.
  1400. */
  1401. const char *object_property_get_type(Object *obj, const char *name,
  1402. Error **errp);
  1403. /**
  1404. * object_get_root:
  1405. *
  1406. * Returns: the root object of the composition tree
  1407. */
  1408. Object *object_get_root(void);
  1409. /**
  1410. * object_get_objects_root:
  1411. *
  1412. * Get the container object that holds user created
  1413. * object instances. This is the object at path
  1414. * "/objects"
  1415. *
  1416. * Returns: the user object container
  1417. */
  1418. Object *object_get_objects_root(void);
  1419. /**
  1420. * object_get_internal_root:
  1421. *
  1422. * Get the container object that holds internally used object
  1423. * instances. Any object which is put into this container must not be
  1424. * user visible, and it will not be exposed in the QOM tree.
  1425. *
  1426. * Returns: the internal object container
  1427. */
  1428. Object *object_get_internal_root(void);
  1429. /**
  1430. * object_get_canonical_path_component:
  1431. * @obj: the object
  1432. *
  1433. * Returns: The final component in the object's canonical path. The canonical
  1434. * path is the path within the composition tree starting from the root.
  1435. * %NULL if the object doesn't have a parent (and thus a canonical path).
  1436. */
  1437. const char *object_get_canonical_path_component(const Object *obj);
  1438. /**
  1439. * object_get_canonical_path:
  1440. * @obj: the object
  1441. *
  1442. * Returns: The canonical path for a object, newly allocated. This is
  1443. * the path within the composition tree starting from the root. Use
  1444. * g_free() to free it.
  1445. */
  1446. char *object_get_canonical_path(const Object *obj);
  1447. /**
  1448. * object_resolve_path:
  1449. * @path: the path to resolve
  1450. * @ambiguous: returns true if the path resolution failed because of an
  1451. * ambiguous match
  1452. *
  1453. * There are two types of supported paths--absolute paths and partial paths.
  1454. *
  1455. * Absolute paths are derived from the root object and can follow child<> or
  1456. * link<> properties. Since they can follow link<> properties, they can be
  1457. * arbitrarily long. Absolute paths look like absolute filenames and are
  1458. * prefixed with a leading slash.
  1459. *
  1460. * Partial paths look like relative filenames. They do not begin with a
  1461. * prefix. The matching rules for partial paths are subtle but designed to make
  1462. * specifying objects easy. At each level of the composition tree, the partial
  1463. * path is matched as an absolute path. The first match is not returned. At
  1464. * least two matches are searched for. A successful result is only returned if
  1465. * only one match is found. If more than one match is found, a flag is
  1466. * returned to indicate that the match was ambiguous.
  1467. *
  1468. * Returns: The matched object or NULL on path lookup failure.
  1469. */
  1470. Object *object_resolve_path(const char *path, bool *ambiguous);
  1471. /**
  1472. * object_resolve_path_type:
  1473. * @path: the path to resolve
  1474. * @typename: the type to look for.
  1475. * @ambiguous: returns true if the path resolution failed because of an
  1476. * ambiguous match
  1477. *
  1478. * This is similar to object_resolve_path. However, when looking for a
  1479. * partial path only matches that implement the given type are considered.
  1480. * This restricts the search and avoids spuriously flagging matches as
  1481. * ambiguous.
  1482. *
  1483. * For both partial and absolute paths, the return value goes through
  1484. * a dynamic cast to @typename. This is important if either the link,
  1485. * or the typename itself are of interface types.
  1486. *
  1487. * Returns: The matched object or NULL on path lookup failure.
  1488. */
  1489. Object *object_resolve_path_type(const char *path, const char *typename,
  1490. bool *ambiguous);
  1491. /**
  1492. * object_resolve_type_unambiguous:
  1493. * @typename: the type to look for
  1494. * @errp: pointer to error object
  1495. *
  1496. * Return the only object in the QOM tree of type @typename.
  1497. * If no match or more than one match is found, an error is
  1498. * returned.
  1499. *
  1500. * Returns: The matched object or NULL on path lookup failure.
  1501. */
  1502. Object *object_resolve_type_unambiguous(const char *typename, Error **errp);
  1503. /**
  1504. * object_resolve_path_at:
  1505. * @parent: the object in which to resolve the path
  1506. * @path: the path to resolve
  1507. *
  1508. * This is like object_resolve_path(), except paths not starting with
  1509. * a slash are relative to @parent.
  1510. *
  1511. * Returns: The resolved object or NULL on path lookup failure.
  1512. */
  1513. Object *object_resolve_path_at(Object *parent, const char *path);
  1514. /**
  1515. * object_resolve_path_component:
  1516. * @parent: the object in which to resolve the path
  1517. * @part: the component to resolve.
  1518. *
  1519. * This is similar to object_resolve_path with an absolute path, but it
  1520. * only resolves one element (@part) and takes the others from @parent.
  1521. *
  1522. * Returns: The resolved object or NULL on path lookup failure.
  1523. */
  1524. Object *object_resolve_path_component(Object *parent, const char *part);
  1525. /**
  1526. * object_property_try_add_child:
  1527. * @obj: the object to add a property to
  1528. * @name: the name of the property
  1529. * @child: the child object
  1530. * @errp: pointer to error object
  1531. *
  1532. * Child properties form the composition tree. All objects need to be a child
  1533. * of another object. Objects can only be a child of one object.
  1534. *
  1535. * There is no way for a child to determine what its parent is. It is not
  1536. * a bidirectional relationship. This is by design.
  1537. *
  1538. * The value of a child property as a C string will be the child object's
  1539. * canonical path. It can be retrieved using object_property_get_str().
  1540. * The child object itself can be retrieved using object_property_get_link().
  1541. *
  1542. * Returns: The newly added property on success, or %NULL on failure.
  1543. */
  1544. ObjectProperty *object_property_try_add_child(Object *obj, const char *name,
  1545. Object *child, Error **errp);
  1546. /**
  1547. * object_property_add_child:
  1548. * @obj: the object to add a property to
  1549. * @name: the name of the property
  1550. * @child: the child object
  1551. *
  1552. * Same as object_property_try_add_child() with @errp hardcoded to
  1553. * &error_abort
  1554. */
  1555. ObjectProperty *object_property_add_child(Object *obj, const char *name,
  1556. Object *child);
  1557. typedef enum {
  1558. /* Unref the link pointer when the property is deleted */
  1559. OBJ_PROP_LINK_STRONG = 0x1,
  1560. /* private */
  1561. OBJ_PROP_LINK_DIRECT = 0x2,
  1562. OBJ_PROP_LINK_CLASS = 0x4,
  1563. } ObjectPropertyLinkFlags;
  1564. /**
  1565. * object_property_allow_set_link:
  1566. * @obj: the object to add a property to
  1567. * @name: the name of the property
  1568. * @child: the child object
  1569. * @errp: pointer to error object
  1570. *
  1571. * The default implementation of the object_property_add_link() check()
  1572. * callback function. It allows the link property to be set and never returns
  1573. * an error.
  1574. */
  1575. void object_property_allow_set_link(const Object *obj, const char *name,
  1576. Object *child, Error **errp);
  1577. /**
  1578. * object_property_add_link:
  1579. * @obj: the object to add a property to
  1580. * @name: the name of the property
  1581. * @type: the qobj type of the link
  1582. * @targetp: a pointer to where the link object reference is stored
  1583. * @check: callback to veto setting or NULL if the property is read-only
  1584. * @flags: additional options for the link
  1585. *
  1586. * Links establish relationships between objects. Links are unidirectional
  1587. * although two links can be combined to form a bidirectional relationship
  1588. * between objects.
  1589. *
  1590. * Links form the graph in the object model.
  1591. *
  1592. * The @check() callback is invoked when
  1593. * object_property_set_link() is called and can raise an error to prevent the
  1594. * link being set. If @check is NULL, the property is read-only
  1595. * and cannot be set.
  1596. *
  1597. * Ownership of the pointer that @child points to is transferred to the
  1598. * link property. The reference count for *@child is
  1599. * managed by the property from after the function returns till the
  1600. * property is deleted with object_property_del(). If the
  1601. * @flags %OBJ_PROP_LINK_STRONG bit is set,
  1602. * the reference count is decremented when the property is deleted or
  1603. * modified.
  1604. *
  1605. * Returns: The newly added property on success, or %NULL on failure.
  1606. */
  1607. ObjectProperty *object_property_add_link(Object *obj, const char *name,
  1608. const char *type, Object **targetp,
  1609. void (*check)(const Object *obj, const char *name,
  1610. Object *val, Error **errp),
  1611. ObjectPropertyLinkFlags flags);
  1612. ObjectProperty *object_class_property_add_link(ObjectClass *oc,
  1613. const char *name,
  1614. const char *type, ptrdiff_t offset,
  1615. void (*check)(const Object *obj, const char *name,
  1616. Object *val, Error **errp),
  1617. ObjectPropertyLinkFlags flags);
  1618. /**
  1619. * object_property_add_str:
  1620. * @obj: the object to add a property to
  1621. * @name: the name of the property
  1622. * @get: the getter or NULL if the property is write-only. This function must
  1623. * return a string to be freed by g_free().
  1624. * @set: the setter or NULL if the property is read-only
  1625. *
  1626. * Add a string property using getters/setters. This function will add a
  1627. * property of type 'string'.
  1628. *
  1629. * Returns: The newly added property on success, or %NULL on failure.
  1630. */
  1631. ObjectProperty *object_property_add_str(Object *obj, const char *name,
  1632. char *(*get)(Object *, Error **),
  1633. void (*set)(Object *, const char *, Error **));
  1634. ObjectProperty *object_class_property_add_str(ObjectClass *klass,
  1635. const char *name,
  1636. char *(*get)(Object *, Error **),
  1637. void (*set)(Object *, const char *,
  1638. Error **));
  1639. /**
  1640. * object_property_add_bool:
  1641. * @obj: the object to add a property to
  1642. * @name: the name of the property
  1643. * @get: the getter or NULL if the property is write-only.
  1644. * @set: the setter or NULL if the property is read-only
  1645. *
  1646. * Add a bool property using getters/setters. This function will add a
  1647. * property of type 'bool'.
  1648. *
  1649. * Returns: The newly added property on success, or %NULL on failure.
  1650. */
  1651. ObjectProperty *object_property_add_bool(Object *obj, const char *name,
  1652. bool (*get)(Object *, Error **),
  1653. void (*set)(Object *, bool, Error **));
  1654. ObjectProperty *object_class_property_add_bool(ObjectClass *klass,
  1655. const char *name,
  1656. bool (*get)(Object *, Error **),
  1657. void (*set)(Object *, bool, Error **));
  1658. /**
  1659. * object_property_add_enum:
  1660. * @obj: the object to add a property to
  1661. * @name: the name of the property
  1662. * @typename: the name of the enum data type
  1663. * @lookup: enum value namelookup table
  1664. * @get: the getter or %NULL if the property is write-only.
  1665. * @set: the setter or %NULL if the property is read-only
  1666. *
  1667. * Add an enum property using getters/setters. This function will add a
  1668. * property of type '@typename'.
  1669. *
  1670. * Returns: The newly added property on success, or %NULL on failure.
  1671. */
  1672. ObjectProperty *object_property_add_enum(Object *obj, const char *name,
  1673. const char *typename,
  1674. const QEnumLookup *lookup,
  1675. int (*get)(Object *, Error **),
  1676. void (*set)(Object *, int, Error **));
  1677. ObjectProperty *object_class_property_add_enum(ObjectClass *klass,
  1678. const char *name,
  1679. const char *typename,
  1680. const QEnumLookup *lookup,
  1681. int (*get)(Object *, Error **),
  1682. void (*set)(Object *, int, Error **));
  1683. /**
  1684. * object_property_add_tm:
  1685. * @obj: the object to add a property to
  1686. * @name: the name of the property
  1687. * @get: the getter or NULL if the property is write-only.
  1688. *
  1689. * Add a read-only struct tm valued property using a getter function.
  1690. * This function will add a property of type 'struct tm'.
  1691. *
  1692. * Returns: The newly added property on success, or %NULL on failure.
  1693. */
  1694. ObjectProperty *object_property_add_tm(Object *obj, const char *name,
  1695. void (*get)(Object *, struct tm *, Error **));
  1696. ObjectProperty *object_class_property_add_tm(ObjectClass *klass,
  1697. const char *name,
  1698. void (*get)(Object *, struct tm *, Error **));
  1699. typedef enum {
  1700. /* Automatically add a getter to the property */
  1701. OBJ_PROP_FLAG_READ = 1 << 0,
  1702. /* Automatically add a setter to the property */
  1703. OBJ_PROP_FLAG_WRITE = 1 << 1,
  1704. /* Automatically add a getter and a setter to the property */
  1705. OBJ_PROP_FLAG_READWRITE = (OBJ_PROP_FLAG_READ | OBJ_PROP_FLAG_WRITE),
  1706. } ObjectPropertyFlags;
  1707. /**
  1708. * object_property_add_uint8_ptr:
  1709. * @obj: the object to add a property to
  1710. * @name: the name of the property
  1711. * @v: pointer to value
  1712. * @flags: bitwise-or'd ObjectPropertyFlags
  1713. *
  1714. * Add an integer property in memory. This function will add a
  1715. * property of type 'uint8'.
  1716. *
  1717. * Returns: The newly added property on success, or %NULL on failure.
  1718. */
  1719. ObjectProperty *object_property_add_uint8_ptr(Object *obj, const char *name,
  1720. const uint8_t *v,
  1721. ObjectPropertyFlags flags);
  1722. ObjectProperty *object_class_property_add_uint8_ptr(ObjectClass *klass,
  1723. const char *name,
  1724. const uint8_t *v,
  1725. ObjectPropertyFlags flags);
  1726. /**
  1727. * object_property_add_uint16_ptr:
  1728. * @obj: the object to add a property to
  1729. * @name: the name of the property
  1730. * @v: pointer to value
  1731. * @flags: bitwise-or'd ObjectPropertyFlags
  1732. *
  1733. * Add an integer property in memory. This function will add a
  1734. * property of type 'uint16'.
  1735. *
  1736. * Returns: The newly added property on success, or %NULL on failure.
  1737. */
  1738. ObjectProperty *object_property_add_uint16_ptr(Object *obj, const char *name,
  1739. const uint16_t *v,
  1740. ObjectPropertyFlags flags);
  1741. ObjectProperty *object_class_property_add_uint16_ptr(ObjectClass *klass,
  1742. const char *name,
  1743. const uint16_t *v,
  1744. ObjectPropertyFlags flags);
  1745. /**
  1746. * object_property_add_uint32_ptr:
  1747. * @obj: the object to add a property to
  1748. * @name: the name of the property
  1749. * @v: pointer to value
  1750. * @flags: bitwise-or'd ObjectPropertyFlags
  1751. *
  1752. * Add an integer property in memory. This function will add a
  1753. * property of type 'uint32'.
  1754. *
  1755. * Returns: The newly added property on success, or %NULL on failure.
  1756. */
  1757. ObjectProperty *object_property_add_uint32_ptr(Object *obj, const char *name,
  1758. const uint32_t *v,
  1759. ObjectPropertyFlags flags);
  1760. ObjectProperty *object_class_property_add_uint32_ptr(ObjectClass *klass,
  1761. const char *name,
  1762. const uint32_t *v,
  1763. ObjectPropertyFlags flags);
  1764. /**
  1765. * object_property_add_uint64_ptr:
  1766. * @obj: the object to add a property to
  1767. * @name: the name of the property
  1768. * @v: pointer to value
  1769. * @flags: bitwise-or'd ObjectPropertyFlags
  1770. *
  1771. * Add an integer property in memory. This function will add a
  1772. * property of type 'uint64'.
  1773. *
  1774. * Returns: The newly added property on success, or %NULL on failure.
  1775. */
  1776. ObjectProperty *object_property_add_uint64_ptr(Object *obj, const char *name,
  1777. const uint64_t *v,
  1778. ObjectPropertyFlags flags);
  1779. ObjectProperty *object_class_property_add_uint64_ptr(ObjectClass *klass,
  1780. const char *name,
  1781. const uint64_t *v,
  1782. ObjectPropertyFlags flags);
  1783. /**
  1784. * object_property_add_alias:
  1785. * @obj: the object to add a property to
  1786. * @name: the name of the property
  1787. * @target_obj: the object to forward property access to
  1788. * @target_name: the name of the property on the forwarded object
  1789. *
  1790. * Add an alias for a property on an object. This function will add a property
  1791. * of the same type as the forwarded property.
  1792. *
  1793. * The caller must ensure that @target_obj stays alive as long as
  1794. * this property exists. In the case of a child object or an alias on the same
  1795. * object this will be the case. For aliases to other objects the caller is
  1796. * responsible for taking a reference.
  1797. *
  1798. * Returns: The newly added property on success, or %NULL on failure.
  1799. */
  1800. ObjectProperty *object_property_add_alias(Object *obj, const char *name,
  1801. Object *target_obj, const char *target_name);
  1802. /**
  1803. * object_property_add_const_link:
  1804. * @obj: the object to add a property to
  1805. * @name: the name of the property
  1806. * @target: the object to be referred by the link
  1807. *
  1808. * Add an unmodifiable link for a property on an object. This function will
  1809. * add a property of type link<TYPE> where TYPE is the type of @target.
  1810. *
  1811. * The caller must ensure that @target stays alive as long as
  1812. * this property exists. In the case @target is a child of @obj,
  1813. * this will be the case. Otherwise, the caller is responsible for
  1814. * taking a reference.
  1815. *
  1816. * Returns: The newly added property on success, or %NULL on failure.
  1817. */
  1818. ObjectProperty *object_property_add_const_link(Object *obj, const char *name,
  1819. Object *target);
  1820. /**
  1821. * object_property_set_description:
  1822. * @obj: the object owning the property
  1823. * @name: the name of the property
  1824. * @description: the description of the property on the object
  1825. *
  1826. * Set an object property's description.
  1827. *
  1828. * Returns: %true on success, %false on failure.
  1829. */
  1830. void object_property_set_description(Object *obj, const char *name,
  1831. const char *description);
  1832. void object_class_property_set_description(ObjectClass *klass, const char *name,
  1833. const char *description);
  1834. /**
  1835. * object_child_foreach:
  1836. * @obj: the object whose children will be navigated
  1837. * @fn: the iterator function to be called
  1838. * @opaque: an opaque value that will be passed to the iterator
  1839. *
  1840. * Call @fn passing each child of @obj and @opaque to it, until @fn returns
  1841. * non-zero.
  1842. *
  1843. * It is forbidden to add or remove children from @obj from the @fn
  1844. * callback.
  1845. *
  1846. * Returns: The last value returned by @fn, or 0 if there is no child.
  1847. */
  1848. int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
  1849. void *opaque);
  1850. /**
  1851. * object_child_foreach_recursive:
  1852. * @obj: the object whose children will be navigated
  1853. * @fn: the iterator function to be called
  1854. * @opaque: an opaque value that will be passed to the iterator
  1855. *
  1856. * Call @fn passing each child of @obj and @opaque to it, until @fn returns
  1857. * non-zero. Calls recursively, all child nodes of @obj will also be passed
  1858. * all the way down to the leaf nodes of the tree. Depth first ordering.
  1859. *
  1860. * It is forbidden to add or remove children from @obj (or its
  1861. * child nodes) from the @fn callback.
  1862. *
  1863. * Returns: The last value returned by @fn, or 0 if there is no child.
  1864. */
  1865. int object_child_foreach_recursive(Object *obj,
  1866. int (*fn)(Object *child, void *opaque),
  1867. void *opaque);
  1868. /**
  1869. * container_get:
  1870. * @root: root of the #path, e.g., object_get_root()
  1871. * @path: path to the container
  1872. *
  1873. * Return a container object whose path is @path. Create more containers
  1874. * along the path if necessary.
  1875. *
  1876. * Returns: the container object.
  1877. */
  1878. Object *container_get(Object *root, const char *path);
  1879. /**
  1880. * object_type_get_instance_size:
  1881. * @typename: Name of the Type whose instance_size is required
  1882. *
  1883. * Returns the instance_size of the given @typename.
  1884. */
  1885. size_t object_type_get_instance_size(const char *typename);
  1886. /**
  1887. * object_property_help:
  1888. * @name: the name of the property
  1889. * @type: the type of the property
  1890. * @defval: the default value
  1891. * @description: description of the property
  1892. *
  1893. * Returns: a user-friendly formatted string describing the property
  1894. * for help purposes.
  1895. */
  1896. char *object_property_help(const char *name, const char *type,
  1897. QObject *defval, const char *description);
  1898. G_DEFINE_AUTOPTR_CLEANUP_FUNC(Object, object_unref)
  1899. #endif