- Timestamp:
- Feb 23, 2013, 5:44:20 PM (12 years ago)
- Branches:
- master, release-1.10
- Children:
- e26abb6
- Parents:
- 63d3e4e
- git-author:
- David Benjamin <davidben@mit.edu> (02/16/13 15:23:25)
- git-committer:
- David Benjamin <davidben@mit.edu> (02/23/13 17:44:20)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
perl/lib/BarnOwl.pm
r3b9ca71 r523146b 426 426 427 427 sub new_variable_int { 428 unshift @_, 0, "<int>", 0, sub { "$_[0]" }, # to string 429 sub { $_[0] =~ /^-?[0-9]+$/ }, # validate 430 sub { 0 + $_[0] }; # from string 431 goto \&_new_variable; 428 my ($name, $args) = @_; 429 my $storage = defined($args->{default}) ? $args->{default} : 0; 430 BarnOwl::new_variable_full($name, { 431 %{$args}, 432 get_tostring => sub { "$storage" }, 433 set_fromstring => sub { 434 return -1 unless $_[0] =~ /^-?[0-9]+$/; 435 $storage = 0 + $_[0]; 436 return 0; 437 }, 438 validsettings => "<int>", 439 takes_on_off => 0, 440 }); 432 441 } 433 442 434 443 sub new_variable_bool { 435 unshift @_, 0, "on,off", 1, sub { $_[0] ? "on" : "off" }, # to string 436 sub { $_[0] eq "on" || $_[0] eq "off" }, # validate 437 sub { $_[0] eq "on" }; # from string 438 goto \&_new_variable; 444 my ($name, $args) = @_; 445 my $storage = defined($args->{default}) ? $args->{default} : 0; 446 BarnOwl::new_variable_full($name, { 447 %{$args}, 448 get_tostring => sub { $storage ? "on" : "off" }, 449 set_fromstring => sub { 450 return -1 unless $_[0] eq "on" || $_[0] eq "off"; 451 $storage = $_[0] eq "on"; 452 return 0; 453 }, 454 validsettings => "on,off", 455 takes_on_off => 1, 456 }); 439 457 } 440 458 441 459 sub new_variable_string { 442 unshift @_, "", "<string>", 0, sub { $_[0] }, # to string 443 sub { 1 }, # validate 444 sub { $_[0] }; # from string 445 goto \&_new_variable; 446 } 447 448 sub _new_variable { 449 my $default_default = shift; 450 my $validsettings = shift; 451 my $takes_on_off = shift; 452 my $tostring_fn = shift; 453 my $validate_fn = shift; 454 my $fromstring_fn = shift; 455 460 my ($name, $args) = @_; 461 my $storage = defined($args->{default}) ? $args->{default} : ""; 462 BarnOwl::new_variable_full($name, { 463 %{$args}, 464 get_tostring => sub { $storage }, 465 set_fromstring => sub { 466 $storage = $_[0]; 467 return 0; 468 }, 469 validsettings => "<string>", 470 takes_on_off => 0, 471 }); 472 } 473 474 =head2 new_variable_full NAME {ARGS} 475 476 Create a variable, in full generality. The keyword arguments have types below: 477 478 get_tostring : () -> string 479 set_fromstring : string -> int 480 -- optional -- 481 summary : string 482 description : string 483 validsettings : string 484 takes_on_off : int 485 486 The get/set functions are required. Note that the caller manages storage for the 487 variable. get_tostring/set_fromstring both convert AND store the value. 488 set_fromstring returns 0 on success. 489 490 If the variable takes parameters 'on' and 'off' (i.e. is boolean-looking), set 491 takes_on_off to 1. This makes :set VAR and :unset VAR work. set_fromstring will 492 be called with those arguments. 493 494 =cut 495 496 sub new_variable_full { 456 497 my $name = shift; 457 498 my $args = shift || {}; 458 499 my %args = ( 459 summary 500 summary => "", 460 501 description => "", 461 default => $default_default, 502 takes_on_off => 0, 503 validsettings => "<string>", 462 504 %{$args}); 463 505 464 # Store the value in a closure. 465 my $value = $args{default}; 466 467 my $get_tostring_fn = sub { $tostring_fn->($value) }; 506 die "get_tostring required" unless $args{get_tostring}; 507 die "set_fromstring required" unless $args{set_fromstring}; 508 509 # Strip off the bogus dummy argument. Aargh perl-Glib. 510 my $get_tostring_fn = sub { $args{get_tostring}->() }; 468 511 my $set_fromstring_fn = sub { 469 my ($dummy, $newval) = @_; 470 return -1 unless $validate_fn->($newval); 471 $value = $fromstring_fn->($newval); 472 return 0; 512 my ($dummy, $val) = @_; 513 $args{set_fromstring}->($val); 473 514 }; 474 515 475 BarnOwl::Internal::new_variable($name, $args{summary}, $args{description}, $ validsettings,476 $ takes_on_off, $get_tostring_fn, $set_fromstring_fn, undef);516 BarnOwl::Internal::new_variable($name, $args{summary}, $args{description}, $args{validsettings}, 517 $args{takes_on_off}, $get_tostring_fn, $set_fromstring_fn, undef); 477 518 } 478 519
Note: See TracChangeset
for help on using the changeset viewer.