This repository has been archived on 2024-07-15. You can view files and clone it, but cannot push or open issues or pull requests.
hedera/gvn/gvn-param-spec.c

326 lines
7.2 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 * obj = g_new (GvnParamSpec, 1);
obj->gtype = gtype;
obj->editable = editable;
obj->null = null;
obj->def = NULL;
gvn_param_spec_set_default (obj, def);
return obj;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
/**
* gvn_param_spec_get_gtype:
* @obj: a #GvnParamSpec
*
* Gets the type of @obj.
*
* Return value: the type
**/
GType gvn_param_spec_get_gtype (const GvnParamSpec * obj)
{
g_return_val_if_fail (obj, G_TYPE_INVALID);
return obj->gtype;
}
/**
* gvn_param_spec_set_gtype:
* @obj: a #GvnParamSpec
* @gtype: the #GType
**/
void gvn_param_spec_set_gtype (GvnParamSpec * obj, GType gtype)
{
g_return_if_fail (obj);
g_return_if_fail (G_TYPE_IS_VALUE_TYPE (gtype) || gtype == G_TYPE_NONE);
obj->gtype = gtype;
}
/**
* gvn_param_spec_get_editable:
* @obj: a #GvnParamSpec
**/
gboolean gvn_param_spec_get_editable (const GvnParamSpec * obj)
{
g_return_val_if_fail (obj, FALSE);
return obj->editable;
}
/**
* gvn_param_spec_set_editable:
* @obj: 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 * obj, gboolean editable)
{
g_return_if_fail (obj);
obj->editable = editable;
}
/**
* gvn_param_spec_get_null:
* @obj: a #GvnParamSpec
**/
gboolean gvn_param_spec_get_null (const GvnParamSpec * obj)
{
g_return_val_if_fail (obj, FALSE);
return obj->null;
}
/**
* gvn_param_spec_set_null:
* @obj: 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 * obj, gboolean null)
{
g_return_if_fail (obj);
obj->null = null;
}
/**
* gvn_param_spec_get_default:
* @obj: a #GvnParamSpec
*
* Gets the default value.
*
* Return value: the #GValue or %NULL if it havent
**/
const GValue * gvn_param_spec_get_default (const GvnParamSpec * obj)
{
g_return_val_if_fail (obj, NULL);
return obj->def;
}
/**
* gvn_param_spec_set_default:
* @obj: a #GvnParamSpec
* @def: the default #GValue
*
* Sets the default value.
**/
void gvn_param_spec_set_default (GvnParamSpec * obj, const GValue * def)
{
g_return_if_fail (obj);
g_return_if_fail (G_IS_VALUE (def) || !def);
if (obj->def)
{
g_value_unset (obj->def);
g_free (obj->def);
obj->def = NULL;
}
if (def)
{
GValue * value = g_new0 (GValue, 1);
g_value_init (value, G_VALUE_TYPE (def));
g_value_copy (def, value);
obj->def = value;
}
}
/**
* gvn_param_spec_merge:
* @obj: a #GvnParamSpec
* @merge: (allow-none): a #GvnParamSpec
*
* Merges the attributes of @merge into @obj remaining the most restrictive.
**/
void gvn_param_spec_merge (GvnParamSpec * obj, const GvnParamSpec * merge)
{
g_return_if_fail (obj);
if (merge)
{
obj->editable = obj->editable && merge->editable;
obj->null = obj->null && merge->null;
if (obj->gtype == G_TYPE_NONE)
obj->gtype = merge->gtype;
if (!obj->def && merge->def)
gvn_param_spec_set_default (obj, merge->def);
}
}
/**
* gvn_param_spec_ccopy_value:
* @obj: 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 * obj, const GValue * src, GValue * dst)
{
g_return_val_if_fail (obj, 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) != obj->gtype
&& obj->gtype != G_TYPE_NONE)
{
if (gvn_value_is_null (dst))
{
g_value_unset (dst);
g_value_init (dst, obj->gtype);
}
g_value_transform (src, dst);
}
else
return gvn_value_ccopy (src, dst);
return TRUE;
}
/**
* gvn_param_spec_validate:
* @obj: #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 @obj
*
* Return value: gboolean
**/
gboolean gvn_param_spec_validate (const GvnParamSpec * obj, const GValue * value, GError ** err)
{
g_return_val_if_fail (obj, FALSE);
gboolean is_null = gvn_value_is_null (value);
if (!obj->editable)
g_set_error (err
,GVN_PARAM_SPEC_LOG_DOMAIN
,GVN_PARAM_SPEC_ERROR_NOT_EDITABLE
,_("Param not editable"));
else if (is_null && !obj->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 && obj->gtype != G_TYPE_NONE
&& !g_value_type_transformable (obj->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:
* @obj: a #GvnParamSpec
**/
void gvn_param_spec_unset (GvnParamSpec * obj)
{
g_return_if_fail (obj);
obj->null = TRUE;
obj->editable = TRUE;
obj->gtype = G_TYPE_NONE;
gvn_param_spec_set_default (obj, NULL);
}
/**
* gvn_param_spec_copy:
* @obj: a #GvnParamSpec
*
* Makes a copy of @obj with the same attributes.
*
* Return value: the new created #GvnParamSpec
**/
GvnParamSpec * gvn_param_spec_copy (const GvnParamSpec * obj)
{
g_return_val_if_fail (obj, NULL);
return gvn_param_spec_new_with_attrs (obj->gtype,
obj->editable, obj->null, obj->def);
}
/**
* gvn_param_spec_free:
* @obj: a #GvnParamSpec
*
* Frees the memory used by @obj
**/
void gvn_param_spec_free (GvnParamSpec * obj)
{
g_return_if_fail (obj);
gvn_param_spec_set_default (obj, NULL);
g_free (obj);
}
G_DEFINE_BOXED_TYPE (GvnParamSpec, gvn_param_spec, gvn_param_spec_copy, gvn_param_spec_free);