2
0

qint.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * QInt Module
  3. *
  4. * Copyright (C) 2009 Red Hat Inc.
  5. *
  6. * Authors:
  7. * Luiz Capitulino <lcapitulino@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  10. * See the COPYING.LIB file in the top-level directory.
  11. */
  12. #include "qapi/qmp/qint.h"
  13. #include "qapi/qmp/qobject.h"
  14. #include "qemu-common.h"
  15. static void qint_destroy_obj(QObject *obj);
  16. static const QType qint_type = {
  17. .code = QTYPE_QINT,
  18. .destroy = qint_destroy_obj,
  19. };
  20. /**
  21. * qint_from_int(): Create a new QInt from an int64_t
  22. *
  23. * Return strong reference.
  24. */
  25. QInt *qint_from_int(int64_t value)
  26. {
  27. QInt *qi;
  28. qi = g_malloc(sizeof(*qi));
  29. qi->value = value;
  30. QOBJECT_INIT(qi, &qint_type);
  31. return qi;
  32. }
  33. /**
  34. * qint_get_int(): Get the stored integer
  35. */
  36. int64_t qint_get_int(const QInt *qi)
  37. {
  38. return qi->value;
  39. }
  40. /**
  41. * qobject_to_qint(): Convert a QObject into a QInt
  42. */
  43. QInt *qobject_to_qint(const QObject *obj)
  44. {
  45. if (qobject_type(obj) != QTYPE_QINT)
  46. return NULL;
  47. return container_of(obj, QInt, base);
  48. }
  49. /**
  50. * qint_destroy_obj(): Free all memory allocated by a
  51. * QInt object
  52. */
  53. static void qint_destroy_obj(QObject *obj)
  54. {
  55. assert(obj != NULL);
  56. g_free(qobject_to_qint(obj));
  57. }