Changeset 69f74c2 for variable.c


Ignore:
Timestamp:
Feb 19, 2013, 8:29:34 PM (9 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master
Children:
3b9ca71
Parents:
e5210c9
git-author:
David Benjamin <davidben@mit.edu> (06/09/12 23:05:48)
git-committer:
David Benjamin <davidben@mit.edu> (02/19/13 20:29:34)
Message:
And now... the moment you've all been waiting for...

Expose OWL_VARIABLE_OTHER variables to perl. Rewrite all perl variable
entry points to use that one. From now on, C only thinks of these
variables as strings.

(This has one regression to be fixed in a later commit: you can't
set/unset with no arguments since we don't know how to set from
boolean.)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • variable.c

    r897fc1c r69f74c2  
    680680}
    681681
    682 CALLER_OWN owl_variable *owl_variable_newvar(const char *name, const char *summary, const char *description)
     682void owl_variable_dict_newvar_other(owl_vardict *vd, const char *name, const char *summary, const char *description, const char *validsettings, GClosure *get_tostring_fn, GClosure *set_fromstring_fn)
    683683{
    684684  owl_variable *var = g_new0(owl_variable, 1);
     
    686686  var->summary = g_strdup(summary);
    687687  var->description = g_strdup(description);
    688   return var;
    689 }
    690 
    691 void owl_variable_update(owl_variable *var, const char *summary, const char *desc) {
    692   g_free(var->summary);
    693   var->summary = g_strdup(summary);
    694   g_free(var->description);
    695   var->description = g_strdup(desc);
    696 }
    697 
    698 #define OWL_VARIABLE_SETUP_DEFAULT_FUNCS(variable, type, gtype) do { \
    699   variable->set_fn = G_CALLBACK(owl_variable_##type##_set_default); \
    700   variable->get_fn = G_CALLBACK(owl_variable_##type##_get_default); \
    701   variable->validate_fn = G_CALLBACK(owl_variable_##type##_validate_default); \
    702   variable->set_fromstring_fn = owl_variable_make_closure(variable, \
    703     G_CALLBACK(owl_variable_##type##_set_fromstring_default), \
    704     g_cclosure_user_marshal_INT__STRING); \
    705   variable->get_tostring_fn = owl_variable_make_closure(variable, \
    706     G_CALLBACK(owl_variable_##type##_get_tostring_default), \
    707     g_cclosure_user_marshal_STRING__VOID); \
    708   } while(0)
    709 
    710 void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *summ, const char *desc, const char *initval)
    711 {
    712   owl_variable *old = owl_variable_get_var(vd, name);
    713   if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING) {
    714     owl_variable_update(old, summ, desc);
    715     /* g_value_copy(default_gval, &(old->gval_default)); */
    716     /* TODO: Resolve this after churn; this function is only called from perl
    717      * anyway. */
    718   } else {
    719     owl_variable * var = owl_variable_newvar(name, summ, desc);
    720     var->type = OWL_VARIABLE_STRING;
    721     var->validsettings = g_strdup("<string>");
    722     g_value_init(&var->val, G_TYPE_STRING);
    723     OWL_VARIABLE_SETUP_DEFAULT_FUNCS(var, string, STRING);
    724 
    725     owl_variable_set_string(var, initval);
    726 
    727     /* record the initial value as a string */
    728     var->default_str = owl_variable_get_tostring(var);
    729 
    730     owl_variable_dict_add_variable(vd, var);
    731   }
    732 }
    733 
    734 void owl_variable_dict_newvar_int(owl_vardict *vd, const char *name, const char *summ, const char *desc, int initval)
    735 {
    736   owl_variable *old = owl_variable_get_var(vd, name);
    737   if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT) {
    738     owl_variable_update(old, summ, desc);
    739     /* g_value_copy(default_gval, &(old->gval_default)); */
    740     /* TODO: Resolve this after churn; this function is only called from perl
    741      * anyway. */
    742   } else {
    743     owl_variable * var = owl_variable_newvar(name, summ, desc);
    744     var->type = OWL_VARIABLE_INT;
    745     var->validsettings = g_strdup("<int>");
    746     g_value_init(&var->val, G_TYPE_INT);
    747     OWL_VARIABLE_SETUP_DEFAULT_FUNCS(var, int, INT);
    748 
    749     owl_variable_set_int(var, initval);
    750 
    751     /* record the initial value as a string */
    752     var->default_str = owl_variable_get_tostring(var);
    753 
    754     owl_variable_dict_add_variable(vd, var);
    755   }
    756 }
    757 
    758 void owl_variable_dict_newvar_bool(owl_vardict *vd, const char *name, const char *summ, const char *desc, gboolean initval)
    759 {
    760   owl_variable *old = owl_variable_get_var(vd, name);
    761   if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL) {
    762     owl_variable_update(old, summ, desc);
    763     /* g_value_copy(default_gval, &(old->gval_default)); */
    764     /* TODO: Resolve this after churn; this function is only called from perl
    765      * anyway. */
    766   } else {
    767     owl_variable * var = owl_variable_newvar(name, summ, desc);
    768     var->type = OWL_VARIABLE_BOOL;
    769     var->validsettings = g_strdup("on,off");
    770     g_value_init(&var->val, G_TYPE_BOOLEAN);
    771     OWL_VARIABLE_SETUP_DEFAULT_FUNCS(var, bool, BOOLEAN);
    772 
    773     owl_variable_set_bool(var, initval);
    774 
    775     /* record the initial value as a string */
    776     var->default_str = owl_variable_get_tostring(var);
    777 
    778     owl_variable_dict_add_variable(vd, var);
    779   }
     688  var->validsettings = g_strdup(validsettings);
     689
     690  var->get_tostring_fn = g_closure_ref(get_tostring_fn);
     691  g_closure_sink(get_tostring_fn);
     692
     693  var->set_fromstring_fn = g_closure_ref(set_fromstring_fn);
     694  g_closure_sink(set_fromstring_fn);
     695
     696  var->default_str = owl_variable_get_tostring(var);
     697
     698  /* Note: this'll overwrite any existing variable of that name, even a C one,
     699     but it's consistent with previous behavior and commands. */
     700  owl_variable_dict_add_variable(vd, var);
    780701}
    781702
Note: See TracChangeset for help on using the changeset viewer.