Ejemplo n.º 1
0
bool PropertyEnum::is_default_value() const
{
	const GValue* v = g_param_spec_get_default_value(param_spec);
	gint i = g_value_get_enum(v);

	return std::to_string(i) == value;
}
Ejemplo n.º 2
0
void
gom_resource_class_set_primary_key (GomResourceClass *resource_class,
                                    const gchar      *primary_key)
{
   GParamSpec *pspec;
   const GValue *value;

   g_return_if_fail(GOM_IS_RESOURCE_CLASS(resource_class));
   g_return_if_fail(primary_key != NULL);
   g_return_if_fail(strlen(primary_key) <= sizeof(resource_class->primary_key));

   pspec = g_object_class_find_property(G_OBJECT_CLASS(resource_class), primary_key);
   if (!pspec) {
      g_warning("Property for primary key '%s' (class %s) isn't declared yet. Are you running gom_resource_class_set_primary_key() too early?",
                primary_key, G_OBJECT_CLASS_NAME(resource_class));
      return;
   }

   if (pspec->flags & G_PARAM_CONSTRUCT_ONLY) {
      g_warning("Property for primary key '%s' (class %s) is declared as construct-only. This will not work as expected.",
                primary_key, G_OBJECT_CLASS_NAME(resource_class));
      return;
   }

   /* Same check as in has_primary_key() */
   value = g_param_spec_get_default_value (pspec);
   if (value->data[0].v_pointer &&
       *((char *) value->data[0].v_pointer) != '\0') {
      g_warning("Property for primary key '%s' (class %s) has a non-NULL/non-zero default value. This will not work as expected.",
                primary_key, G_OBJECT_CLASS_NAME(resource_class));
      return;
   }

   g_snprintf(resource_class->primary_key,
              sizeof(resource_class->primary_key),
              "%s", primary_key);
}
Ejemplo n.º 3
0
std::string
get_property_with_node_name(
  GParamSpec* pParamSpec, const std::string& strObjectName, const std::string& strNodeName)
{
  std::string strResult;

  // Name and type:
  const std::string strName = g_param_spec_get_name(pParamSpec);
  const std::string strTypeName = G_PARAM_SPEC_TYPE_NAME(pParamSpec);

  const gchar* pchBlurb = g_param_spec_get_blurb(pParamSpec);
  std::string strDocs = (pchBlurb) ? pchBlurb : "";
  // Quick hack to get rid of nested double quotes:
  std::replace(strDocs.begin(), strDocs.end(), '"', '\'');

  strResult += "(" + strNodeName + " " + strName + "\n";
  strResult += "  (of-object \"" + strObjectName + "\")\n";
  strResult += "  (prop-type \"" + strTypeName + "\")\n";
  strResult += "  (docs \"" + strDocs + "\")\n";

  // Flags:
  GParamFlags flags = pParamSpec->flags;
  bool bReadable = (flags & G_PARAM_READABLE) == G_PARAM_READABLE;
  bool bWritable = (flags & G_PARAM_WRITABLE) == G_PARAM_WRITABLE;
  bool bConstructOnly = (flags & G_PARAM_CONSTRUCT_ONLY) == G_PARAM_CONSTRUCT_ONLY;
  bool bDeprecated = (flags & G_PARAM_DEPRECATED) == G_PARAM_DEPRECATED;

  //#t and #f aren't documented, but I guess that it's correct based on the example in the .defs
  // spec.
  const std::string strTrue = "#t";
  const std::string strFalse = "#f";

  strResult += "  (readable " + (bReadable ? strTrue : strFalse) + ")\n";
  strResult += "  (writable " + (bWritable ? strTrue : strFalse) + ")\n";
  strResult += "  (construct-only " + (bConstructOnly ? strTrue : strFalse) + ")\n";
  if (bDeprecated)
    strResult += "  (deprecated #t)\n"; // Default: not deprecated

  // Default value:
  const GValue* defValue = g_param_spec_get_default_value(pParamSpec);
  std::string defString;
  bool defValueExists = false;
  if (G_VALUE_HOLDS_STRING(defValue))
  {
    defValueExists = true;
    const char* defCString = g_value_get_string(defValue);
    if (defCString)
    {
      // Replace newlines with \n.
      // A string default value can contain newline characters.
      // gmmproc removes all newlines when it reads .defs files.
      defString = std::regex_replace(defCString, std::regex("\n"), "\\n");
    }
    else
      defString = ""; // A NULL string pointer becomes an empty string.
  }
  else if (G_VALUE_HOLDS_FLOAT(defValue) || G_VALUE_HOLDS_DOUBLE(defValue))
  {
    // g_value_transform() can transform a floating point value to a terrible
    // string, especially if the value is huge.
    defValueExists = true;
    const double defDouble = G_VALUE_HOLDS_FLOAT(defValue) ?
      g_value_get_float(defValue) : g_value_get_double(defValue);
    std::ostringstream defStringStream;
    defStringStream << defDouble;
    defString = defStringStream.str();
  }
  else
  {
    GValue defStringValue = G_VALUE_INIT;
    g_value_init(&defStringValue, G_TYPE_STRING);

    if (g_value_transform(defValue, &defStringValue))
    {
      const char* defCString = g_value_get_string(&defStringValue);
      if (defCString)
      {
        defValueExists = true;
        defString = defCString;
      }
    }
    g_value_unset(&defStringValue);
  }

  if (defValueExists)
    strResult += "  (default-value \"" + defString + "\")\n";

  strResult += ")\n\n"; // close (strNodeName

  return strResult;
}