[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

static local variables

From: Bryce Gardner
Subject: static local variables
Date: Fri, 21 Mar 1997 14:19:10 +0500

I have an m-file solution to static variables.  It doesn't provide as good
of static variable implimentation as if it was built-in to octave, but it
only puts one extra variable into the global name space no matter how many
static variables exist in a program.  This may be useful.

Also I think it could be an initial test case for the design of
a built-in static variable implementation.

An m-file implementation could be made to be as good as a built-in version
if in a user-defined function, you could:
        1) get the name of the function that called the current function
        2) get the name of the output arguments in calling function
        3) attach a function (or set of functions) to always be called
           upon exit from a user-defined function.

Well, that was my two cents worth on static variables.

Bryce Gardner

%The file static.m follows:

function val=static(func_name,var_name,curr_val,if_initial)
%usage:  val=static(calling_function_name, variable_name,
%                   current_value, "init");
%or:     val=static(calling_function_name, variable_name, current_value);
%This function impliments a limited form of static variables in octave
%In order to put a static variable, x, with an initial value of: 0, in
%the function dog:   
%      1)  put the following line near the begining of function, dog:
%                x = static("dog","x",0,"init");
%      2)  at the end of the function dog put:
%                static("dog","x",x);
%The advantage over this method over global variables is that it only
%puts one variable name in the global name space no matter how many
%static variables are used and that you can use short non-globally
%unique names in the local function.
%The disadvantage of this method over global variables is that in this
%method there are two copies of the variables.  (This is probably only
%important if you have large matrices that you want to make static.)

global STATIC_variable_list;

if (!exist("STATIC_variable_list"))
   STATIC_variable_list.null = 0;

full_static_var_name = ["STATIC_variable_list.",func_name,"_",var_name];
static_var_name = [func_name,"_",var_name];

if (nargin == 3 || 
    (nargin==4 && !struct_contains(STATIC_variable_list,static_var_name)))
   eval([full_static_var_name," = curr_val;"]);

val = eval([full_static_var_name,";"]);


%The following function is an example of how to use the static function:
%The file exstatic.m follows:

function z=exstatic(y)
%usage:  z=exstatic(y);
%This function is a simple function to test static variables
% this is where you would put a: static x = 0;  
% statement if static variables were supported
x = static("exstatic","x",0,"init");
% main stuff goes here
% end of main stuff
% this update would happen automatically 
% if static variables were supported

reply via email to

[Prev in Thread] Current Thread [Next in Thread]