Commit 09ea1908 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Move variable type assertions into the core

parent 4dbd738a
...@@ -25,8 +25,6 @@ ...@@ -25,8 +25,6 @@
#ifndef VLC_VARIABLES_H #ifndef VLC_VARIABLES_H
#define VLC_VARIABLES_H 1 #define VLC_VARIABLES_H 1
#include <assert.h>
/** /**
* \file * \file
* This file defines functions and structures for dynamic variables in vlc * This file defines functions and structures for dynamic variables in vlc
...@@ -129,6 +127,8 @@ VLC_EXPORT( int, __var_Change, ( vlc_object_t *, const char *, int, vlc_value_t ...@@ -129,6 +127,8 @@ VLC_EXPORT( int, __var_Change, ( vlc_object_t *, const char *, int, vlc_value_t
VLC_EXPORT( int, __var_Type, ( vlc_object_t *, const char * ) LIBVLC_USED ); VLC_EXPORT( int, __var_Type, ( vlc_object_t *, const char * ) LIBVLC_USED );
VLC_EXPORT( int, __var_Set, ( vlc_object_t *, const char *, vlc_value_t ) ); VLC_EXPORT( int, __var_Set, ( vlc_object_t *, const char *, vlc_value_t ) );
VLC_EXPORT( int, __var_Get, ( vlc_object_t *, const char *, vlc_value_t * ) ); VLC_EXPORT( int, __var_Get, ( vlc_object_t *, const char *, vlc_value_t * ) );
VLC_EXPORT( int, var_SetChecked, ( vlc_object_t *, const char *, int, vlc_value_t ) );
VLC_EXPORT( int, var_GetChecked, ( vlc_object_t *, const char *, int, vlc_value_t * ) );
#define var_Command(a,b,c,d,e) __var_Command( VLC_OBJECT( a ), b, c, d, e ) #define var_Command(a,b,c,d,e) __var_Command( VLC_OBJECT( a ), b, c, d, e )
VLC_EXPORT( int, __var_Command, ( vlc_object_t *, const char *, const char *, const char *, char ** ) ); VLC_EXPORT( int, __var_Command, ( vlc_object_t *, const char *, const char *, const char *, char ** ) );
...@@ -192,21 +192,6 @@ VLC_EXPORT( int, __var_TriggerCallback, ( vlc_object_t *, const char * ) ); ...@@ -192,21 +192,6 @@ VLC_EXPORT( int, __var_TriggerCallback, ( vlc_object_t *, const char * ) );
* helpers functions * helpers functions
*****************************************************************************/ *****************************************************************************/
#ifndef NDEBUG
/**
* This function assert the variable is of the expected type or it
* is not defined
*/
static inline void __var_AssertType( vlc_object_t *p_obj, const char *psz_name,
int i_expected )
{
const int i_type = __var_Type( p_obj, psz_name ) & VLC_VAR_CLASS;
assert( i_type == 0 || i_type == (i_expected&VLC_VAR_CLASS) );
}
#else
# define __var_AssertType( o, n, e ) (void)(o, n, e)
#endif
/** /**
* Set the value of an integer variable * Set the value of an integer variable
* *
...@@ -218,8 +203,7 @@ static inline int __var_SetInteger( vlc_object_t *p_obj, const char *psz_name, i ...@@ -218,8 +203,7 @@ static inline int __var_SetInteger( vlc_object_t *p_obj, const char *psz_name, i
{ {
vlc_value_t val; vlc_value_t val;
val.i_int = i; val.i_int = i;
__var_AssertType( p_obj, psz_name, VLC_VAR_INTEGER ); return var_SetChecked( p_obj, psz_name, VLC_VAR_INTEGER, val );
return __var_Set( p_obj, psz_name, val );
} }
#define var_SetInteger(a,b,c) __var_SetInteger( VLC_OBJECT(a),b,c) #define var_SetInteger(a,b,c) __var_SetInteger( VLC_OBJECT(a),b,c)
/** /**
...@@ -233,8 +217,7 @@ static inline int __var_SetBool( vlc_object_t *p_obj, const char *psz_name, bool ...@@ -233,8 +217,7 @@ static inline int __var_SetBool( vlc_object_t *p_obj, const char *psz_name, bool
{ {
vlc_value_t val; vlc_value_t val;
val.b_bool = b; val.b_bool = b;
__var_AssertType( p_obj, psz_name, VLC_VAR_BOOL ); return var_SetChecked( p_obj, psz_name, VLC_VAR_BOOL, val );
return __var_Set( p_obj, psz_name, val );
} }
/** /**
...@@ -248,8 +231,7 @@ static inline int __var_SetTime( vlc_object_t *p_obj, const char *psz_name, int6 ...@@ -248,8 +231,7 @@ static inline int __var_SetTime( vlc_object_t *p_obj, const char *psz_name, int6
{ {
vlc_value_t val; vlc_value_t val;
val.i_time = i; val.i_time = i;
__var_AssertType( p_obj, psz_name, VLC_VAR_TIME ); return var_SetChecked( p_obj, psz_name, VLC_VAR_TIME, val );
return __var_Set( p_obj, psz_name, val );
} }
/** /**
...@@ -263,8 +245,7 @@ static inline int __var_SetFloat( vlc_object_t *p_obj, const char *psz_name, flo ...@@ -263,8 +245,7 @@ static inline int __var_SetFloat( vlc_object_t *p_obj, const char *psz_name, flo
{ {
vlc_value_t val; vlc_value_t val;
val.f_float = f; val.f_float = f;
__var_AssertType( p_obj, psz_name, VLC_VAR_FLOAT ); return var_SetChecked( p_obj, psz_name, VLC_VAR_FLOAT, val );
return __var_Set( p_obj, psz_name, val );
} }
/** /**
...@@ -278,8 +259,7 @@ static inline int __var_SetString( vlc_object_t *p_obj, const char *psz_name, co ...@@ -278,8 +259,7 @@ static inline int __var_SetString( vlc_object_t *p_obj, const char *psz_name, co
{ {
vlc_value_t val; vlc_value_t val;
val.psz_string = (char *)psz_string; val.psz_string = (char *)psz_string;
__var_AssertType( p_obj, psz_name, VLC_VAR_STRING ); return var_SetChecked( p_obj, psz_name, VLC_VAR_STRING, val );
return __var_Set( p_obj, psz_name, val );
} }
/** /**
...@@ -292,8 +272,7 @@ static inline int __var_SetVoid( vlc_object_t *p_obj, const char *psz_name ) ...@@ -292,8 +272,7 @@ static inline int __var_SetVoid( vlc_object_t *p_obj, const char *psz_name )
{ {
vlc_value_t val; vlc_value_t val;
val.b_bool = true; val.b_bool = true;
__var_AssertType( p_obj, psz_name, VLC_VAR_VOID ); return var_SetChecked( p_obj, psz_name, VLC_VAR_VOID, val );
return __var_Set( p_obj, psz_name, val );
} }
#define var_SetVoid(a,b) __var_SetVoid( VLC_OBJECT(a),b) #define var_SetVoid(a,b) __var_SetVoid( VLC_OBJECT(a),b)
...@@ -324,9 +303,8 @@ static inline int __var_SetVoid( vlc_object_t *p_obj, const char *psz_name ) ...@@ -324,9 +303,8 @@ static inline int __var_SetVoid( vlc_object_t *p_obj, const char *psz_name )
LIBVLC_USED LIBVLC_USED
static inline int __var_GetInteger( vlc_object_t *p_obj, const char *psz_name ) static inline int __var_GetInteger( vlc_object_t *p_obj, const char *psz_name )
{ {
vlc_value_t val;val.i_int = 0; vlc_value_t val;
__var_AssertType( p_obj, psz_name, VLC_VAR_INTEGER ); if( !var_GetChecked( p_obj, psz_name, VLC_VAR_INTEGER, &val ) )
if( !__var_Get( p_obj, psz_name, &val ) )
return val.i_int; return val.i_int;
else else
return 0; return 0;
...@@ -343,8 +321,7 @@ static inline int __var_GetBool( vlc_object_t *p_obj, const char *psz_name ) ...@@ -343,8 +321,7 @@ static inline int __var_GetBool( vlc_object_t *p_obj, const char *psz_name )
{ {
vlc_value_t val; val.b_bool = false; vlc_value_t val; val.b_bool = false;
__var_AssertType( p_obj, psz_name, VLC_VAR_BOOL ); if( !var_GetChecked( p_obj, psz_name, VLC_VAR_BOOL, &val ) )
if( !__var_Get( p_obj, psz_name, &val ) )
return val.b_bool; return val.b_bool;
else else
return false; return false;
...@@ -360,8 +337,7 @@ LIBVLC_USED ...@@ -360,8 +337,7 @@ LIBVLC_USED
static inline int64_t __var_GetTime( vlc_object_t *p_obj, const char *psz_name ) static inline int64_t __var_GetTime( vlc_object_t *p_obj, const char *psz_name )
{ {
vlc_value_t val; val.i_time = 0L; vlc_value_t val; val.i_time = 0L;
__var_AssertType( p_obj, psz_name, VLC_VAR_TIME ); if( !var_GetChecked( p_obj, psz_name, VLC_VAR_TIME, &val ) )
if( !__var_Get( p_obj, psz_name, &val ) )
return val.i_time; return val.i_time;
else else
return 0; return 0;
...@@ -377,8 +353,7 @@ LIBVLC_USED ...@@ -377,8 +353,7 @@ LIBVLC_USED
static inline float __var_GetFloat( vlc_object_t *p_obj, const char *psz_name ) static inline float __var_GetFloat( vlc_object_t *p_obj, const char *psz_name )
{ {
vlc_value_t val; val.f_float = 0.0; vlc_value_t val; val.f_float = 0.0;
__var_AssertType( p_obj, psz_name, VLC_VAR_FLOAT ); if( !var_GetChecked( p_obj, psz_name, VLC_VAR_FLOAT, &val ) )
if( !__var_Get( p_obj, psz_name, &val ) )
return val.f_float; return val.f_float;
else else
return 0.0; return 0.0;
...@@ -394,8 +369,7 @@ LIBVLC_USED ...@@ -394,8 +369,7 @@ LIBVLC_USED
static inline char *__var_GetString( vlc_object_t *p_obj, const char *psz_name ) static inline char *__var_GetString( vlc_object_t *p_obj, const char *psz_name )
{ {
vlc_value_t val; val.psz_string = NULL; vlc_value_t val; val.psz_string = NULL;
__var_AssertType( p_obj, psz_name, VLC_VAR_STRING ); if( var_GetChecked( p_obj, psz_name, VLC_VAR_STRING, &val ) )
if( __var_Get( p_obj, psz_name, &val ) )
return NULL; return NULL;
else else
return val.psz_string; return val.psz_string;
...@@ -405,8 +379,7 @@ LIBVLC_USED ...@@ -405,8 +379,7 @@ LIBVLC_USED
static inline char *__var_GetNonEmptyString( vlc_object_t *p_obj, const char *psz_name ) static inline char *__var_GetNonEmptyString( vlc_object_t *p_obj, const char *psz_name )
{ {
vlc_value_t val; vlc_value_t val;
__var_AssertType( p_obj, psz_name, VLC_VAR_STRING ); if( var_GetChecked( p_obj, psz_name, VLC_VAR_STRING, &val ) )
if( __var_Get( p_obj, psz_name, &val ) )
return NULL; return NULL;
if( *val.psz_string ) if( *val.psz_string )
return val.psz_string; return val.psz_string;
......
...@@ -417,7 +417,9 @@ __var_Create ...@@ -417,7 +417,9 @@ __var_Create
__var_DelCallback __var_DelCallback
__var_Destroy __var_Destroy
__var_Get __var_Get
var_GetChecked
__var_Set __var_Set
var_SetChecked
__var_TriggerCallback __var_TriggerCallback
__var_Type __var_Type
video_format_FixRgb video_format_FixRgb
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "libvlc.h" #include "libvlc.h"
#include "vlc_interface.h" #include "vlc_interface.h"
#include <assert.h>
/***************************************************************************** /*****************************************************************************
* Private types * Private types
...@@ -734,14 +735,8 @@ int __var_Type( vlc_object_t *p_this, const char *psz_name ) ...@@ -734,14 +735,8 @@ int __var_Type( vlc_object_t *p_this, const char *psz_name )
return i_type; return i_type;
} }
/** int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
* Set a variable's value int expected_type, vlc_value_t val )
*
* \param p_this The object that hold the variable
* \param psz_name The name of the variable
* \param val the value to set
*/
int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
{ {
int i_var; int i_var;
variable_t *p_var; variable_t *p_var;
...@@ -758,6 +753,8 @@ int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val ) ...@@ -758,6 +753,8 @@ int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
} }
p_var = &p_priv->p_vars[i_var]; p_var = &p_priv->p_vars[i_var];
assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
(p_var->i_type & VLC_VAR_CLASS) == expected_type );
/* Duplicate data if needed */ /* Duplicate data if needed */
p_var->ops->pf_dup( &val ); p_var->ops->pf_dup( &val );
...@@ -811,43 +808,60 @@ int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val ) ...@@ -811,43 +808,60 @@ int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
return VLC_SUCCESS; return VLC_SUCCESS;
} }
/** /**
* Get a variable's value * Set a variable's value
* *
* \param p_this The object that holds the variable * \param p_this The object that hold the variable
* \param psz_name The name of the variable * \param psz_name The name of the variable
* \param p_val Pointer to a vlc_value_t that will hold the variable's value * \param val the value to set
* after the function is finished
*/ */
int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val ) int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
{
return var_SetChecked( p_this, psz_name, 0, val );
}
int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
int expected_type, vlc_value_t *p_val )
{ {
int i_var;
variable_t *p_var;
vlc_object_internals_t *p_priv = vlc_internals( p_this ); vlc_object_internals_t *p_priv = vlc_internals( p_this );
int i_var, err = VLC_SUCCESS;
vlc_mutex_lock( &p_priv->var_lock ); vlc_mutex_lock( &p_priv->var_lock );
i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name ); i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
if( i_var >= 0 )
if( i_var < 0 )
{ {
vlc_mutex_unlock( &p_priv->var_lock ); variable_t *p_var = &p_priv->p_vars[i_var];
return VLC_ENOVAR;
}
p_var = &p_priv->p_vars[i_var]; assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
(p_var->i_type & VLC_VAR_CLASS) == expected_type );
/* Really get the variable */ /* Really get the variable */
*p_val = p_var->val; *p_val = p_var->val;
/* Duplicate value if needed */ /* Duplicate value if needed */
p_var->ops->pf_dup( p_val ); p_var->ops->pf_dup( p_val );
}
else
err = VLC_ENOVAR;
vlc_mutex_unlock( &p_priv->var_lock ); vlc_mutex_unlock( &p_priv->var_lock );
return err;
return VLC_SUCCESS;
} }
/**
* Get a variable's value
*
* \param p_this The object that holds the variable
* \param psz_name The name of the variable
* \param p_val Pointer to a vlc_value_t that will hold the variable's value
* after the function is finished
*/
int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
{
return var_GetChecked( p_this, psz_name, 0, p_val );
}
/** /**
* Register a callback in a variable * Register a callback in a variable
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment