326 lines
7.3 KiB
C
326 lines
7.3 KiB
C
/*
|
|
* Copyright (C) 2012 - Juan Ferrer Toribio
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "gvn-param-spec.h"
|
|
#include "gvn-misc.h"
|
|
#include "gvn-value.h"
|
|
|
|
/**
|
|
* SECTION: gvn-param-spec
|
|
* @Short_description:
|
|
* @Title: GvnParamSpec
|
|
**/
|
|
|
|
/**
|
|
* gvn_param_spec_new:
|
|
*
|
|
* Creates a new #GvnParamSpec.
|
|
*
|
|
* Return value: the new #GvnParamSpec
|
|
**/
|
|
GvnParamSpec * gvn_param_spec_new ()
|
|
{
|
|
return gvn_param_spec_new_with_attrs (G_TYPE_NONE, TRUE, TRUE, NULL);
|
|
}
|
|
|
|
/**
|
|
* gvn_param_spec_new_with_attrs:
|
|
* @gtype: the #GType
|
|
* @editable: a %gboolean indicating whether it will be editable
|
|
* @null: a %gboolean indicating whether it can be null
|
|
* @def: the default #GValue
|
|
*
|
|
* Creates a new #GvnParamSpec.
|
|
*
|
|
* Return value: the new #GvnParamSpec
|
|
**/
|
|
GvnParamSpec * gvn_param_spec_new_with_attrs (GType gtype,
|
|
gboolean editable, gboolean null, const GValue * def)
|
|
{
|
|
g_return_val_if_fail (G_IS_VALUE (def) || !def, NULL);
|
|
|
|
GvnParamSpec * self = g_new (GvnParamSpec, 1);
|
|
self->gtype = gtype;
|
|
self->editable = editable;
|
|
self->null = null;
|
|
self->def = NULL;
|
|
gvn_param_spec_set_default (self, def);
|
|
return self;
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
|
|
|
|
/**
|
|
* gvn_param_spec_get_gtype:
|
|
* @self: a #GvnParamSpec
|
|
*
|
|
* Gets the type of @self.
|
|
*
|
|
* Return value: the type
|
|
**/
|
|
GType gvn_param_spec_get_gtype (const GvnParamSpec * self)
|
|
{
|
|
g_return_val_if_fail (self, G_TYPE_INVALID);
|
|
|
|
return self->gtype;
|
|
}
|
|
|
|
/**
|
|
* gvn_param_spec_set_gtype:
|
|
* @self: a #GvnParamSpec
|
|
* @gtype: the #GType
|
|
**/
|
|
void gvn_param_spec_set_gtype (GvnParamSpec * self, GType gtype)
|
|
{
|
|
g_return_if_fail (self);
|
|
g_return_if_fail (G_TYPE_IS_VALUE_TYPE (gtype) || gtype == G_TYPE_NONE);
|
|
|
|
self->gtype = gtype;
|
|
}
|
|
|
|
/**
|
|
* gvn_param_spec_get_editable:
|
|
* @self: a #GvnParamSpec
|
|
**/
|
|
gboolean gvn_param_spec_get_editable (const GvnParamSpec * self)
|
|
{
|
|
g_return_val_if_fail (self, FALSE);
|
|
|
|
return self->editable;
|
|
}
|
|
|
|
/**
|
|
* gvn_param_spec_set_editable:
|
|
* @self: a #GvnParamSpec
|
|
* @editable: a %gboolean indicating whether it will be editable
|
|
*
|
|
* Sets if the value can be edited.
|
|
**/
|
|
void gvn_param_spec_set_editable (GvnParamSpec * self, gboolean editable)
|
|
{
|
|
g_return_if_fail (self);
|
|
|
|
self->editable = editable;
|
|
}
|
|
|
|
/**
|
|
* gvn_param_spec_get_null:
|
|
* @self: a #GvnParamSpec
|
|
**/
|
|
gboolean gvn_param_spec_get_null (const GvnParamSpec * self)
|
|
{
|
|
g_return_val_if_fail (self, FALSE);
|
|
|
|
return self->null;
|
|
}
|
|
|
|
/**
|
|
* gvn_param_spec_set_null:
|
|
* @self: a #GvnParamSpec
|
|
* @null: a %gboolean indicating whether it can be null
|
|
*
|
|
* Set if the value can be null.
|
|
**/
|
|
void gvn_param_spec_set_null (GvnParamSpec * self, gboolean null)
|
|
{
|
|
g_return_if_fail (self);
|
|
|
|
self->null = null;
|
|
}
|
|
|
|
/**
|
|
* gvn_param_spec_get_default:
|
|
* @self: a #GvnParamSpec
|
|
*
|
|
* Gets the default value.
|
|
*
|
|
* Return value: the #GValue or %NULL if it havent
|
|
**/
|
|
const GValue * gvn_param_spec_get_default (const GvnParamSpec * self)
|
|
{
|
|
g_return_val_if_fail (self, NULL);
|
|
|
|
return self->def;
|
|
}
|
|
|
|
/**
|
|
* gvn_param_spec_set_default:
|
|
* @self: a #GvnParamSpec
|
|
* @def: the default #GValue
|
|
*
|
|
* Sets the default value.
|
|
**/
|
|
void gvn_param_spec_set_default (GvnParamSpec * self, const GValue * def)
|
|
{
|
|
g_return_if_fail (self);
|
|
g_return_if_fail (G_IS_VALUE (def) || !def);
|
|
|
|
if (self->def)
|
|
{
|
|
g_value_unset (self->def);
|
|
g_free (self->def);
|
|
self->def = NULL;
|
|
}
|
|
if (def)
|
|
{
|
|
GValue * value = g_new0 (GValue, 1);
|
|
g_value_init (value, G_VALUE_TYPE (def));
|
|
g_value_copy (def, value);
|
|
self->def = value;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* gvn_param_spec_merge:
|
|
* @self: a #GvnParamSpec
|
|
* @merge: (allow-none): a #GvnParamSpec
|
|
*
|
|
* Merges the attributes of @merge into @self remaining the most restrictive.
|
|
**/
|
|
void gvn_param_spec_merge (GvnParamSpec * self, const GvnParamSpec * merge)
|
|
{
|
|
g_return_if_fail (self);
|
|
|
|
if (merge)
|
|
{
|
|
self->editable = self->editable && merge->editable;
|
|
self->null = self->null && merge->null;
|
|
|
|
if (self->gtype == G_TYPE_NONE)
|
|
self->gtype = merge->gtype;
|
|
if (!self->def && merge->def)
|
|
gvn_param_spec_set_default (self, merge->def);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* gvn_param_spec_ccopy_value:
|
|
* @self: a #GvnParamSpec
|
|
* @src: source value to be copied
|
|
* @dst: destination for the copied value
|
|
*
|
|
* Return value: %TRUE if the value has been copied, %FALSE otherwise
|
|
**/
|
|
gboolean gvn_param_spec_ccopy_value (const GvnParamSpec * self, const GValue * src, GValue * dst)
|
|
{
|
|
g_return_val_if_fail (self, FALSE);
|
|
g_return_val_if_fail (G_IS_VALUE (src), FALSE);
|
|
g_return_val_if_fail (G_IS_VALUE (dst), FALSE);
|
|
|
|
if (!gvn_value_is_null (src)
|
|
&& G_VALUE_TYPE (src) != self->gtype
|
|
&& self->gtype != G_TYPE_NONE)
|
|
{
|
|
if (gvn_value_is_null (dst))
|
|
{
|
|
g_value_unset (dst);
|
|
g_value_init (dst, self->gtype);
|
|
}
|
|
|
|
g_value_transform (src, dst);
|
|
}
|
|
else
|
|
return gvn_value_ccopy (src, dst);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
/**
|
|
* gvn_param_spec_validate:
|
|
* @self: #GvnParamSpec object where @GValue wants to be validated
|
|
* @value: new value
|
|
* @err: (out) (allow-none): the return location for an allocated @GError, or
|
|
* %NULL to ignore errors.
|
|
*
|
|
* It checks if the value is valid to be saved into @self
|
|
*
|
|
* Return value: gboolean
|
|
**/
|
|
gboolean gvn_param_spec_validate (const GvnParamSpec * self, const GValue * value, GError ** err)
|
|
{
|
|
g_return_val_if_fail (self, FALSE);
|
|
|
|
gboolean is_null = gvn_value_is_null (value);
|
|
|
|
if (!self->editable)
|
|
g_set_error (err
|
|
,GVN_PARAM_SPEC_LOG_DOMAIN
|
|
,GVN_PARAM_SPEC_ERROR_NOT_EDITABLE
|
|
,_("Param not editable"));
|
|
else if (is_null && !self->null)
|
|
g_set_error (err
|
|
,GVN_PARAM_SPEC_LOG_DOMAIN
|
|
,GVN_PARAM_SPEC_ERROR_NOT_NULL
|
|
,_("Param can't be NULL"));
|
|
else if (!is_null && self->gtype != G_TYPE_NONE
|
|
&& !g_value_type_transformable (self->gtype, G_VALUE_TYPE (value)))
|
|
g_set_error (err
|
|
,GVN_PARAM_SPEC_LOG_DOMAIN
|
|
,GVN_PARAM_SPEC_ERROR_WRONG_TYPE
|
|
,_("Incompatible type for this param"));
|
|
else
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
/**
|
|
* gvn_param_spec_unset:
|
|
* @self: a #GvnParamSpec
|
|
**/
|
|
void gvn_param_spec_unset (GvnParamSpec * self)
|
|
{
|
|
g_return_if_fail (self);
|
|
|
|
self->null = TRUE;
|
|
self->editable = TRUE;
|
|
self->gtype = G_TYPE_NONE;
|
|
gvn_param_spec_set_default (self, NULL);
|
|
}
|
|
|
|
/**
|
|
* gvn_param_spec_copy:
|
|
* @self: a #GvnParamSpec
|
|
*
|
|
* Makes a copy of @self with the same attributes.
|
|
*
|
|
* Return value: the new created #GvnParamSpec
|
|
**/
|
|
GvnParamSpec * gvn_param_spec_copy (const GvnParamSpec * self)
|
|
{
|
|
g_return_val_if_fail (self, NULL);
|
|
|
|
return gvn_param_spec_new_with_attrs (self->gtype,
|
|
self->editable, self->null, self->def);
|
|
}
|
|
|
|
/**
|
|
* gvn_param_spec_free:
|
|
* @self: a #GvnParamSpec
|
|
*
|
|
* Frees the memory used by @self
|
|
**/
|
|
void gvn_param_spec_free (GvnParamSpec * self)
|
|
{
|
|
g_return_if_fail (self);
|
|
|
|
gvn_param_spec_set_default (self, NULL);
|
|
g_free (self);
|
|
}
|
|
|
|
G_DEFINE_BOXED_TYPE (GvnParamSpec, gvn_param_spec, gvn_param_spec_copy, gvn_param_spec_free);
|