]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/python/py-param.c
gdb: make string-like set show commands use std::string variable
[thirdparty/binutils-gdb.git] / gdb / python / py-param.c
index 1dd716bba145227977e613e1f66108acd821d888..407db045d930081184cb6d3c80c75611ff062c17 100644 (file)
@@ -64,8 +64,9 @@ union parmpy_variable
   /* Hold an unsigned integer value, for uinteger.  */
   unsigned int uintval;
 
-  /* Hold a string, for the various string types.  */
-  char *stringval;
+  /* Hold a string, for the various string types.  The std::string is
+     new-ed.  */
+  std::string *stringval;
 
   /* Hold a string, for enums.  */
   const char *cstringval;
@@ -95,7 +96,6 @@ struct parmpy_object
 static setting
 make_setting (parmpy_object *s)
 {
-
   if (var_type_uses<bool> (s->type))
     return setting (s->type, &s->value.boolval);
   else if (var_type_uses<int> (s->type))
@@ -104,8 +104,8 @@ make_setting (parmpy_object *s)
     return setting (s->type, &s->value.autoboolval);
   else if (var_type_uses<unsigned int> (s->type))
     return setting (s->type, &s->value.uintval);
-  else if (var_type_uses<char *> (s->type))
-    return setting (s->type, &s->value.stringval);
+  else if (var_type_uses<std::string> (s->type))
+    return setting (s->type, s->value.stringval);
   else if (var_type_uses<const char *> (s->type))
     return setting (s->type, &s->value.cstringval);
   else
@@ -163,13 +163,7 @@ set_parameter_value (parmpy_object *self, PyObject *value)
          return -1;
        }
       if (value == Py_None)
-       {
-         xfree (self->value.stringval);
-         if (self->type == var_optional_filename)
-           self->value.stringval = xstrdup ("");
-         else
-           self->value.stringval = NULL;
-       }
+       self->value.stringval->clear ();
       else
        {
          gdb::unique_xmalloc_ptr<char>
@@ -177,8 +171,7 @@ set_parameter_value (parmpy_object *self, PyObject *value)
          if (string == NULL)
            return -1;
 
-         xfree (self->value.stringval);
-         self->value.stringval = string.release ();
+         *self->value.stringval = string.get ();
        }
       break;
 
@@ -525,14 +518,14 @@ add_setshow_generic (int parmclass, enum command_class cmdclass,
 
     case var_string:
       commands = add_setshow_string_cmd (cmd_name.get (), cmdclass,
-                                        &self->value.stringval, set_doc,
+                                        self->value.stringval, set_doc,
                                         show_doc, help_doc, get_set_value,
                                         get_show_value, set_list, show_list);
       break;
 
     case var_string_noescape:
       commands = add_setshow_string_noescape_cmd (cmd_name.get (), cmdclass,
-                                                 &self->value.stringval,
+                                                 self->value.stringval,
                                                  set_doc, show_doc, help_doc,
                                                  get_set_value, get_show_value,
                                                  set_list, show_list);
@@ -540,7 +533,7 @@ add_setshow_generic (int parmclass, enum command_class cmdclass,
 
     case var_optional_filename:
       commands = add_setshow_optional_filename_cmd (cmd_name.get (), cmdclass,
-                                                   &self->value.stringval,
+                                                   self->value.stringval,
                                                    set_doc, show_doc, help_doc,
                                                    get_set_value,
                                                    get_show_value, set_list,
@@ -549,7 +542,7 @@ add_setshow_generic (int parmclass, enum command_class cmdclass,
 
     case var_filename:
       commands = add_setshow_filename_cmd (cmd_name.get (), cmdclass,
-                                          &self->value.stringval, set_doc,
+                                          self->value.stringval, set_doc,
                                           show_doc, help_doc, get_set_value,
                                           get_show_value, set_list, show_list);
       break;
@@ -732,6 +725,9 @@ parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
   obj->type = (enum var_types) parmclass;
   memset (&obj->value, 0, sizeof (obj->value));
 
+  if (var_type_uses<std::string> (obj->type))
+    obj->value.stringval = new std::string;
+
   gdb::unique_xmalloc_ptr<char> cmd_name
     = gdbpy_parse_command_name (name, &set_list, &setlist);
   if (cmd_name == nullptr)
@@ -764,7 +760,16 @@ parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
   return 0;
 }
 
-\f
+/* Deallocate function for a gdb.Parameter.  */
+
+static void
+parmpy_dealloc (PyObject *obj)
+{
+  parmpy_object *parm_obj = (parmpy_object *) obj;
+
+  if (var_type_uses<std::string> (parm_obj->type))
+    delete parm_obj->value.stringval;
+}
 
 /* Initialize the 'parameters' module.  */
 int
@@ -803,7 +808,7 @@ PyTypeObject parmpy_object_type =
   "gdb.Parameter",               /*tp_name*/
   sizeof (parmpy_object),        /*tp_basicsize*/
   0,                             /*tp_itemsize*/
-  0,                             /*tp_dealloc*/
+  parmpy_dealloc,                /*tp_dealloc*/
   0,                             /*tp_print*/
   0,                             /*tp_getattr*/
   0,                             /*tp_setattr*/