2
0

qfloat.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * QFloat Module
  3. *
  4. * Copyright IBM, Corp. 2009
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.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. */
  13. #include "qapi/qmp/qfloat.h"
  14. #include "qapi/qmp/qobject.h"
  15. #include "qemu-common.h"
  16. static void qfloat_destroy_obj(QObject *obj);
  17. static const QType qfloat_type = {
  18. .code = QTYPE_QFLOAT,
  19. .destroy = qfloat_destroy_obj,
  20. };
  21. /**
  22. * qfloat_from_int(): Create a new QFloat from a float
  23. *
  24. * Return strong reference.
  25. */
  26. QFloat *qfloat_from_double(double value)
  27. {
  28. QFloat *qf;
  29. qf = g_malloc(sizeof(*qf));
  30. qf->value = value;
  31. QOBJECT_INIT(qf, &qfloat_type);
  32. return qf;
  33. }
  34. /**
  35. * qfloat_get_double(): Get the stored float
  36. */
  37. double qfloat_get_double(const QFloat *qf)
  38. {
  39. return qf->value;
  40. }
  41. /**
  42. * qobject_to_qfloat(): Convert a QObject into a QFloat
  43. */
  44. QFloat *qobject_to_qfloat(const QObject *obj)
  45. {
  46. if (qobject_type(obj) != QTYPE_QFLOAT)
  47. return NULL;
  48. return container_of(obj, QFloat, base);
  49. }
  50. /**
  51. * qfloat_destroy_obj(): Free all memory allocated by a
  52. * QFloat object
  53. */
  54. static void qfloat_destroy_obj(QObject *obj)
  55. {
  56. assert(obj != NULL);
  57. g_free(qobject_to_qfloat(obj));
  58. }