Changeset 69f74c2
- Timestamp:
- Feb 19, 2013, 8:29:34 PM (12 years ago)
- Branches:
- master, release-1.10
- 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)
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
owl.h
r5001a3d r69f74c2 239 239 char *summary; /* summary of usage */ 240 240 char *description; /* detailed description */ 241 GValue val; /* current value */242 241 GClosure *set_fromstring_fn; 243 242 /* sets the variable to a value … … 251 250 252 251 /* These are only valid for OWL_VARIABLE_{INT,BOOL,STRING} */ 252 GValue val; /* current value, if default get_fn/set_fn */ 253 253 254 GCallback get_fn; 254 255 /* returns a reference to the current value. -
perl/lib/BarnOwl.pm
r374089a r69f74c2 426 426 427 427 sub new_variable_int { 428 unshift @_, \&BarnOwl::Internal::new_variable_int, 0; 428 unshift @_, 0, "<int>", sub { "$_[0]" }, # to string 429 sub { $_[0] =~ /^-?[0-9]+$/ }, # validate 430 sub { 0 + $_[0] }; # from string 429 431 goto \&_new_variable; 430 432 } 431 433 432 434 sub new_variable_bool { 433 unshift @_, \&BarnOwl::Internal::new_variable_bool, 0; 435 unshift @_, 0, "on,off", sub { $_[0] ? "on" : "off" }, # to string 436 sub { $_[0] eq "on" || $_[0] eq "off" }, # validate 437 sub { $_[0] eq "on" }; # from string 434 438 goto \&_new_variable; 435 439 } 436 440 437 441 sub new_variable_string { 438 unshift @_, \&BarnOwl::Internal::new_variable_string, ""; 442 unshift @_, "", "<string>", sub { $_[0] }, # to string 443 sub { 1 }, # validate 444 sub { $_[0] }; # from string 439 445 goto \&_new_variable; 440 446 } 441 447 442 448 sub _new_variable { 443 my $func = shift;444 449 my $default_default = shift; 450 my $validsettings = shift; 451 my $tostring_fn = shift; 452 my $validate_fn = shift; 453 my $fromstring_fn = shift; 454 445 455 my $name = shift; 446 456 my $args = shift || {}; … … 450 460 default => $default_default, 451 461 %{$args}); 452 $func->($name, $args{default}, $args{summary}, $args{description}); 462 463 # Store the value in a closure. 464 my $value = $args{default}; 465 466 my $get_tostring_fn = sub { $tostring_fn->($value) }; 467 my $set_fromstring_fn = sub { 468 my ($dummy, $newval) = @_; 469 return -1 unless $validate_fn->($newval); 470 $value = $fromstring_fn->($newval); 471 return 0; 472 }; 473 474 BarnOwl::Internal::new_variable($name, $args{summary}, $args{description}, $validsettings, 475 $get_tostring_fn, $set_fromstring_fn, undef); 453 476 } 454 477 -
perlglue.xs
r353719a r69f74c2 405 405 MODULE = BarnOwl PACKAGE = BarnOwl::Internal 406 406 407 /* TODO: Revise this after churn is done. 408 void 409 new_variable_full(name, summary, desc, type, data, default_val, get_fn, get_default_fn, tostring_fn, validate_fn, set_fn, fromstring_fn) 407 void 408 new_variable(name, summary, description, validsettings, get_tostring_fn, set_fromstring_fn, data) 410 409 const char *name 411 410 const char *summary 412 const char *desc 413 int type 411 const char *description 412 const char *validsettings 413 SV *get_tostring_fn 414 SV *set_fromstring_fn 414 415 SV *data 415 SV *default_val416 SV *get_fn417 SV *get_default_fn418 SV *tostring_fn419 SV *validate_fn420 SV *set_fn421 SV *fromstring_fn422 416 CODE: 423 417 { 424 owl_variable *variable = NULL; 425 int count = 0; 426 int res = -1; 427 GClosure *delete_fn = NULL; 428 if(!SV_IS_CODEREF(get_fn)) { 429 croak("Get function must be a coderef!"); 430 } 431 if(!SV_IS_CODEREF(tostring_fn)) { 418 /* data is somewhat redundant given we can create closures, but oh 419 * well. Might be convenient sometimes. */ 420 if(!SV_IS_CODEREF(get_tostring_fn)) { 432 421 croak("To-string function must be a coderef!"); 433 422 } 434 if(!SV_IS_CODEREF(validate_fn)) { 435 croak("Validation function must be a coderef!"); 436 } 437 if(!SV_IS_CODEREF(set_fn)) { 438 croak("Set function must be a coderef!"); 439 } 440 if(!SV_IS_CODEREF(fromstring_fn)) { 423 if(!SV_IS_CODEREF(set_fromstring_fn)) { 441 424 croak("From-string function must be a coderef!"); 442 425 } 443 if(!SV_IS_CODEREF(get_default_fn)) { 444 croak("Get-default function must be a coderef!"); 445 } 446 variable = owl_variable_newvar(name, summary, desc); 447 variable->type = type; 448 variable->get_fn = perl_closure_new(get_fn, data, false); 449 variable->get_tostring_fn = perl_closure_new(tostring_fn, data, false); 450 variable->validate_fn = perl_closure_new(validate_fn, data, false); 451 variable->set_fn = perl_closure_new(set_fn, data, false); 452 variable->set_fromstring_fn = perl_closure_new(set_fn, data, false); 453 variable->get_default_fn = perl_closure_new(get_default_fn, 454 data, false); 455 delete_fn = g_cclosure_new(G_CALLBACK(owl_perl_delete_perl_variable), 456 data, NULL); 457 g_closure_set_marshal(delete_fn,g_cclosure_marshal_VOID__VOID); 458 g_closure_ref(delete_fn); 459 g_closure_sink(delete_fn); 460 variable->delete_fn = delete_fn; 461 462 SvREFCNT_inc(data); 463 464 PUSHMARK(SP); 465 XPUSHs(sv_2mortal(newSViv(PTR2IV(variable)))); 466 XPUSHs(default_val); 467 XPUSHs(data); 468 PUTBACK; 469 count = call_sv(set_fn, G_SCALAR | G_EVAL); 470 SPAGAIN; 471 472 res = POPi; 473 owl_dict_insert_element(owl_global_get_vardict(&g), 474 variable->name, variable, NULL); 475 PUTBACK; 426 427 owl_variable_dict_newvar_other(owl_global_get_vardict(&g), 428 name, summary, description, validsettings, 429 perl_closure_new(get_tostring_fn, data, false), 430 perl_closure_new(set_fromstring_fn, data, false)); 476 431 } 477 */478 479 void480 new_variable_string(name, ival, summ, desc)481 const char * name482 const char * ival483 const char * summ484 const char * desc485 CODE:486 owl_variable_dict_newvar_string(owl_global_get_vardict(&g),487 name,488 summ,489 desc,490 ival);491 492 void493 new_variable_int(name, ival, summ, desc)494 const char * name495 int ival496 const char * summ497 const char * desc498 CODE:499 owl_variable_dict_newvar_int(owl_global_get_vardict(&g),500 name,501 summ,502 desc,503 ival);504 505 void506 new_variable_bool(name, ival, summ, desc)507 const char * name508 int ival509 const char * summ510 const char * desc511 CODE:512 owl_variable_dict_newvar_bool(owl_global_get_vardict(&g),513 name,514 summ,515 desc,516 ival);517 432 518 433 void -
variable.c
r897fc1c r69f74c2 680 680 } 681 681 682 CALLER_OWN owl_variable *owl_variable_newvar(const char *name, const char *summary, const char *description)682 void 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) 683 683 { 684 684 owl_variable *var = g_new0(owl_variable, 1); … … 686 686 var->summary = g_strdup(summary); 687 687 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); 780 701 } 781 702
Note: See TracChangeset
for help on using the changeset viewer.