grim/gplate

I thought I already added this.. my bad
trying_to_get_includes_working
2008-02-20, grim
545b7214a0de
I thought I already added this.. my bad
/*
* Copyright (C) 2007-2008 Gary Kramlich <grim@reaperworld.com>
*
* 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 2 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, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <gplate/gplate-variable.h>
#define GPLATE_VARIABLE_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE((obj), GPLATE_TYPE_VARIABLE, GPlateVariablePrivate))
/******************************************************************************
* Enums
*****************************************************************************/
enum {
PROP_ZERO,
PROP_NAME,
PROP_VALUE,
PROP_LAST,
};
/******************************************************************************
* Structs
*****************************************************************************/
typedef struct {
gchar *name;
gchar *value;
} GPlateVariablePrivate;
/******************************************************************************
* Globals
*****************************************************************************/
static GObjectClass *parent_class = NULL;
/******************************************************************************
* Private stuff
*****************************************************************************/
static void
gplate_variable_set_name(GPlateVariable *variable, const gchar *name) {
GPlateVariablePrivate *priv = GPLATE_VARIABLE_GET_PRIVATE(variable);
priv->name = (name) ? g_strdup(name) : NULL;
}
static void
gplate_variable_set_value(GPlateVariable *variable, const gchar *value) {
GPlateVariablePrivate *priv = GPLATE_VARIABLE_GET_PRIVATE(variable);
priv->value = (value) ? g_strdup(value) : NULL;
}
/******************************************************************************
* Variable stuff
*****************************************************************************/
static const gchar *
gplate_variable_real_get_name(const GPlateVariable *variable) {
GPlateVariablePrivate *priv = GPLATE_VARIABLE_GET_PRIVATE(variable);
return priv->name;
}
static const gchar *
gplate_variable_real_get_value(const GPlateVariable *variable) {
GPlateVariablePrivate *priv = GPLATE_VARIABLE_GET_PRIVATE(variable);
return priv->value;
}
/******************************************************************************
* Object Stuff
*****************************************************************************/
static void
gplate_variable_get_property(GObject *obj, guint param_id, GValue *value,
GParamSpec *pspec)
{
GPlateVariable *variable = GPLATE_VARIABLE(obj);
switch(param_id) {
case PROP_NAME:
g_value_set_string(value, gplate_variable_get_name(variable));
break;
case PROP_VALUE:
g_value_set_string(value, gplate_variable_get_value(variable));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
break;
}
}
static void
gplate_variable_set_property(GObject *obj, guint param_id, const GValue *value,
GParamSpec *pspec)
{
GPlateVariable *variable = GPLATE_VARIABLE(obj);
switch(param_id) {
case PROP_NAME:
gplate_variable_set_name(variable, g_value_get_string(value));
break;
case PROP_VALUE:
gplate_variable_set_value(variable, g_value_get_string(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
break;
}
}
static void
gplate_variable_finalize(GObject *obj) {
GPlateVariablePrivate *priv = GPLATE_VARIABLE_GET_PRIVATE(obj);
g_free(priv->name);
g_free(priv->value);
G_OBJECT_CLASS(parent_class)->finalize(obj);
}
static void
gplate_variable_class_init(GPlateVariableClass *klass) {
GObjectClass *obj_class = G_OBJECT_CLASS(klass);
GParamSpec *pspec = NULL;
g_type_class_add_private(klass, sizeof(GPlateVariablePrivate));
parent_class = g_type_class_peek_parent(klass);
obj_class->get_property = gplate_variable_get_property;
obj_class->set_property = gplate_variable_set_property;
obj_class->finalize = gplate_variable_finalize;
klass->get_name = gplate_variable_real_get_name;
klass->get_value = gplate_variable_real_get_value;
pspec = g_param_spec_string("name", "name",
"The name of the variable",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
g_object_class_install_property(obj_class, PROP_NAME, pspec);
pspec = g_param_spec_string("value", "value",
"The value of the variable",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
g_object_class_install_property(obj_class, PROP_VALUE, pspec);
}
/******************************************************************************
* GPlateVariable API
*****************************************************************************/
GType
gplate_variable_get_gtype(void) {
static GType type = 0;
if(type == 0) {
static const GTypeInfo info = {
sizeof(GPlateVariableClass),
NULL,
NULL,
(GClassInitFunc)gplate_variable_class_init,
NULL,
NULL,
sizeof(GPlateVariable),
0,
NULL,
};
type = g_type_register_static(G_TYPE_OBJECT,
"GPlateVariable",
&info, 0);
}
return type;
}
/**
* gplate_variable_new_from_string:
* @name: The name of the #GPlateVariable.
* @value: The string value.
*
* Creates a new #GPlateVariable named @name containing @value.
*
* Return Value: The new #GPlateVariable.
*/
GPlateVariable *
gplate_variable_new_from_string(const gchar *name, const gchar *value) {
return g_object_new(GPLATE_TYPE_VARIABLE,
"name", name,
"value", value,
NULL);
}
/**
* gplate_variable_new_from_boolean:
* @name: The name of the #GPlateVariable.
* @value: The boolean value.
*
* Creates a new #GPlateVariable named @name containing @value.
*
* Return Value: The new #GPlateVariable.
*/
GPlateVariable *
gplate_variable_new_from_boolean(const gchar *name, gboolean value) {
return gplate_variable_new_from_string(name, (value) ? "TRUE" : "FALSE");
}
/**
* gplate_variable_new_from_integer:
* @name: The name of the #GPlateVariable.
* @value: The integer value.
*
* Creates a new #GPlateVariable named @name containing @value.
*
* Return Value: The new #GPlateVariable.
*/
GPlateVariable *
gplate_variable_new_from_integer(const gchar *name, gint value) {
GPlateVariable *ret = NULL;
gchar *v = g_strdup_printf("%d", value);
ret = gplate_variable_new_from_string(name, v);
g_free(v);
return ret;
}
/**
* gplate_variable_new_from_float:
* @name: The name of the #GPlateVariable.
* @value: The float value.
*
* Creates a new #GPlateVariable named @name containing @value.
*
* Return Value: The new #GPlateVariable.
*/
GPlateVariable *
gplate_variable_new_from_float(const gchar *name, gfloat value) {
GPlateVariable *ret = NULL;
gchar *v = g_strdup_printf("%f", value);
ret = gplate_variable_new_from_string(name, v);
g_free(v);
return ret;
}
/**
* gplate_variable_new_from_double:
* @name: The name of the #GPlateVariable.
* @value: The double value.
*
* Creates a new #GPlateVariable named @name containing @value.
*
* Return Value: The new #GPlateVariable.
*/
GPlateVariable *
gplate_variable_new_from_double(const gchar *name, gdouble value) {
GPlateVariable *ret = NULL;
gchar *v = g_strdup_printf("%g", value);
ret = gplate_variable_new_from_string(name, v);
g_free(v);
return ret;
}
/**
* gplate_variable_get_name:
* @variable: The #GPlateVariable.
*
* Gets the name of @variable.
*
* Return Value: The name of @variable or NULL.
*/
const gchar *
gplate_variable_get_name(const GPlateVariable *variable) {
GPlateVariableClass *klass = NULL;
g_return_val_if_fail(GPLATE_IS_VARIABLE(variable), NULL);
klass = GPLATE_VARIABLE_GET_CLASS(variable);
if(klass && klass->get_name)
return klass->get_name(variable);
return NULL;
}
/**
* gplate_variable_get_value:
* @variable: The #GPlateVariable.
*
* Gets the value of @variable.
*
* Return Value: The value of @variable or NULL.
*/
const gchar *
gplate_variable_get_value(const GPlateVariable *variable) {
GPlateVariableClass *klass = NULL;
g_return_val_if_fail(GPLATE_IS_VARIABLE(variable), NULL);
klass = GPLATE_VARIABLE_GET_CLASS(variable);
if(klass && klass->get_value)
return klass->get_value(variable);
return NULL;
}