正在加载图片...
$18.4 APPLICATIONS OF ONCE ROUTINES 651 Once procedures The function close should only be called once.We recommend using a global variable in your application to check that close is not called more than once. (From the manual for a commercial C library.) The "once"mechanism is interesting not just for functions but for procedures as well A once procedure is appropriate when some facility used on a system-wide basis must be initialized,but it is not known in advance which system component will be the first to use the facility.It is like having a rule that whoever comes in first in the morning should turn on the heating A simple example is a graphics library providing a number of display routines, where the first display routine called in any system execution must set up the terminal.The library author could of course require every client to perform a setup call before the first display call.This is a nuisance for clients and does not really solve the problem anyway: to deal properly with errors,any routine should be able to detect that it has been called without proper setup;but if it is smart enough to detect this case,the routine might just as well do the setup and avoid bothering the client! Once procedures provide a better solution: check setup is --Perform terminal setup if not done yet. once terminal setup --Actual setup action end Thenevery display routine in the library should begin with a call to check setup.The first call will do the setup;subsequent ones will do nothing.Note that check setup does not have to be exported;client authors do not need to know about it. This is an important technique to improve the usability of any library or other software package.Any time you can remove a usage rule-such as "Always call procedure xy=before the first operation"-and instead take care of the needed operations automatically and silently,you have made the software better. Arguments Like other routines,once routines-procedures and functions-can have arguments But because of the definition of the mechanism,these arguments are only useful in the call that gets executed first. In the earlier analogy,imagine a thermostat dial which anyone coming into the building may turn to any marking,but such that only the first person to do so will set the temperature:subsequent attempts have no effect.§18.4 APPLICATIONS OF ONCE ROUTINES 651 Once procedures The function close should only be called once. We recommend using a global variable in your application to check that close is not called more than once. (From the manual for a commercial C library.) The “once” mechanism is interesting not just for functions but for procedures as well. A once procedure is appropriate when some facility used on a system-wide basis must be initialized, but it is not known in advance which system component will be the first to use the facility. It is like having a rule that whoever comes in first in the morning should turn on the heating. A simple example is a graphics library providing a number of display routines, where the first display routine called in any system execution must set up the terminal. The library author could of course require every client to perform a setup call before the first display call. This is a nuisance for clients and does not really solve the problem anyway: to deal properly with errors, any routine should be able to detect that it has been called without proper setup; but if it is smart enough to detect this case, the routine might just as well do the setup and avoid bothering the client! Once procedures provide a better solution: check_setup is -- Perform terminal setup if not done yet. once terminal_setup -- Actual setup action end Then every display routine in the library should begin with a call to check_setup. The first call will do the setup; subsequent ones will do nothing. Note that check_setup does not have to be exported; client authors do not need to know about it. This is an important technique to improve the usability of any library or other software package. Any time you can remove a usage rule — such as “Always call procedure xyz before the first operation” — and instead take care of the needed operations automatically and silently, you have made the software better. Arguments Like other routines, once routines — procedures and functions — can have arguments. But because of the definition of the mechanism, these arguments are only useful in the call that gets executed first. In the earlier analogy, imagine a thermostat dial which anyone coming into the building may turn to any marking, but such that only the first person to do so will set the temperature: subsequent attempts have no effect
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有