Revision 7b7b7d18

b/include/qemu/object.h
622 622
                         struct Error **errp);
623 623

  
624 624
/**
625
 * object_property_set_str:
626
 * @value: the value to be written to the property
627
 * @name: the name of the property
628
 * @errp: returns an error if this function fails
629
 *
630
 * Writes a string value to a property.
631
 */
632
void object_property_set_str(Object *obj, const char *value,
633
                             const char *name, struct Error **errp);
634

  
635
/**
636
 * object_property_get_str:
637
 * @obj: the object
638
 * @name: the name of the property
639
 * @errp: returns an error if this function fails
640
 *
641
 * Returns: the value of the property, converted to a C string, or NULL if
642
 * an error occurs (including when the property value is not a string).
643
 * The caller should free the string.
644
 */
645
char *object_property_get_str(Object *obj, const char *name,
646
                              struct Error **errp);
647

  
648
/**
649
 * object_property_set_bool:
650
 * @value: the value to be written to the property
651
 * @name: the name of the property
652
 * @errp: returns an error if this function fails
653
 *
654
 * Writes a bool value to a property.
655
 */
656
void object_property_set_bool(Object *obj, bool value,
657
                              const char *name, struct Error **errp);
658

  
659
/**
660
 * object_property_get_bool:
661
 * @obj: the object
662
 * @name: the name of the property
663
 * @errp: returns an error if this function fails
664
 *
665
 * Returns: the value of the property, converted to a boolean, or NULL if
666
 * an error occurs (including when the property value is not a bool).
667
 */
668
bool object_property_get_bool(Object *obj, const char *name,
669
                              struct Error **errp);
670

  
671
/**
672
 * object_property_set_int:
673
 * @value: the value to be written to the property
674
 * @name: the name of the property
675
 * @errp: returns an error if this function fails
676
 *
677
 * Writes an integer value to a property.
678
 */
679
void object_property_set_int(Object *obj, int64_t value,
680
                             const char *name, struct Error **errp);
681

  
682
/**
683
 * object_property_get_int:
684
 * @obj: the object
685
 * @name: the name of the property
686
 * @errp: returns an error if this function fails
687
 *
688
 * Returns: the value of the property, converted to an integer, or NULL if
689
 * an error occurs (including when the property value is not an integer).
690
 */
691
int64_t object_property_get_int(Object *obj, const char *name,
692
                                struct Error **errp);
693

  
694
/**
625 695
 * object_property_set:
626 696
 * @obj: the object
627 697
 * @v: the visitor that will be used to write the property value.  This should
b/qom/object.c
14 14
#include "qemu-common.h"
15 15
#include "qapi/qapi-visit-core.h"
16 16

  
17
/* TODO: replace QObject with a simpler visitor to avoid a dependency
18
 * of the QOM core on QObject?  */
19
#include "qemu/qom-qobject.h"
20
#include "qobject.h"
21
#include "qbool.h"
22
#include "qint.h"
23
#include "qstring.h"
24

  
17 25
#define MAX_INTERFACES 32
18 26

  
19 27
typedef struct InterfaceImpl InterfaceImpl;
......
657 665
    }
658 666
}
659 667

  
668
void object_property_set_str(Object *obj, const char *value,
669
                             const char *name, Error **errp)
670
{
671
    QString *qstr = qstring_from_str(value);
672
    object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
673

  
674
    QDECREF(qstr);
675
}
676

  
677
char *object_property_get_str(Object *obj, const char *name,
678
                              Error **errp)
679
{
680
    QObject *ret = object_property_get_qobject(obj, name, errp);
681
    QString *qstring;
682
    char *retval;
683

  
684
    if (!ret) {
685
        return NULL;
686
    }
687
    qstring = qobject_to_qstring(ret);
688
    if (!qstring) {
689
        error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
690
        retval = NULL;
691
    } else {
692
        retval = g_strdup(qstring_get_str(qstring));
693
    }
694

  
695
    QDECREF(qstring);
696
    return retval;
697
}
698

  
699
void object_property_set_bool(Object *obj, bool value,
700
                              const char *name, Error **errp)
701
{
702
    QBool *qbool = qbool_from_int(value);
703
    object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
704

  
705
    QDECREF(qbool);
706
}
707

  
708
bool object_property_get_bool(Object *obj, const char *name,
709
                              Error **errp)
710
{
711
    QObject *ret = object_property_get_qobject(obj, name, errp);
712
    QBool *qbool;
713
    bool retval;
714

  
715
    if (!ret) {
716
        return false;
717
    }
718
    qbool = qobject_to_qbool(ret);
719
    if (!qbool) {
720
        error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
721
        retval = false;
722
    } else {
723
        retval = qbool_get_int(qbool);
724
    }
725

  
726
    QDECREF(qbool);
727
    return retval;
728
}
729

  
730
void object_property_set_int(Object *obj, int64_t value,
731
                             const char *name, Error **errp)
732
{
733
    QInt *qint = qint_from_int(value);
734
    object_property_set_qobject(obj, QOBJECT(qint), name, errp);
735

  
736
    QDECREF(qint);
737
}
738

  
739
int64_t object_property_get_int(Object *obj, const char *name,
740
                                Error **errp)
741
{
742
    QObject *ret = object_property_get_qobject(obj, name, errp);
743
    QInt *qint;
744
    int64_t retval;
745

  
746
    if (!ret) {
747
        return -1;
748
    }
749
    qint = qobject_to_qint(ret);
750
    if (!qint) {
751
        error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
752
        retval = -1;
753
    } else {
754
        retval = qint_get_int(qint);
755
    }
756

  
757
    QDECREF(qint);
758
    return retval;
759
}
760

  
660 761
const char *object_property_get_type(Object *obj, const char *name, Error **errp)
661 762
{
662 763
    ObjectProperty *prop = object_property_find(obj, name);
......
938 1039
    void (*set)(Object *, const char *, Error **);
939 1040
} StringProperty;
940 1041

  
941
static void object_property_get_str(Object *obj, Visitor *v, void *opaque,
942
                                    const char *name, Error **errp)
1042
static void property_get_str(Object *obj, Visitor *v, void *opaque,
1043
                             const char *name, Error **errp)
943 1044
{
944 1045
    StringProperty *prop = opaque;
945 1046
    char *value;
......
951 1052
    }
952 1053
}
953 1054

  
954
static void object_property_set_str(Object *obj, Visitor *v, void *opaque,
955
                                  const char *name, Error **errp)
1055
static void property_set_str(Object *obj, Visitor *v, void *opaque,
1056
                             const char *name, Error **errp)
956 1057
{
957 1058
    StringProperty *prop = opaque;
958 1059
    char *value;
......
968 1069
    g_free(value);
969 1070
}
970 1071

  
971
static void object_property_release_str(Object *obj, const char *name,
972
                                      void *opaque)
1072
static void property_release_str(Object *obj, const char *name,
1073
                                 void *opaque)
973 1074
{
974 1075
    StringProperty *prop = opaque;
975 1076
    g_free(prop);
......
986 1087
    prop->set = set;
987 1088

  
988 1089
    object_property_add(obj, name, "string",
989
                        get ? object_property_get_str : NULL,
990
                        set ? object_property_set_str : NULL,
991
                        object_property_release_str,
1090
                        get ? property_get_str : NULL,
1091
                        set ? property_set_str : NULL,
1092
                        property_release_str,
992 1093
                        prop, errp);
993 1094
}

Also available in: Unified diff