Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

cmdparam.h

Go to the documentation of this file.
00001 
00006 #ifndef ___CMDPARAM_H___
00007 #define ___CMDPARAM_H___
00008 
00009 #include <stdio.h>
00010 #include <string>
00011 using std::string;
00012 
00013 #include "ipc.h"
00014 #include "stringparser.h"
00015 #include "parameter.h"
00016 #include "parametermap.h"
00017 #include "expressionparser.h"
00018 
00019 
00020 /***********************************************************************************/
00021 /***********************************************************************************/
00022 /* Legacy code support                                                             */
00023 /* This should all be removed eventually                                           */
00024 /* Do not use these definitions in new code.                                       */
00025    
00026 typedef ParameterDefinition<>        ParamType;
00027 typedef ParameterMap<ParamType>      BlackboardType;
00028 typedef ExpressionParser<>           Parse;
00029 typedef ParamStringParser<>          CmdParamStringParser;
00030 
00031 /* The variable 'blackboard' was at one point a global variable, but
00032    it is now a dereferenced pointer to the global_blackboard so that
00033    the default blackboard may be set dynamically */
00034 extern BlackboardType  global_blackboard;
00035 extern BlackboardType* blackboard_ptr;
00036 #define blackboard (*blackboard_ptr)
00037  
00038 #define params_add_lower_bound_int(name,val)        blackboard.lookup(name).add_lower_bound(val)
00039 #define params_add_upper_bound_int(name,val)        blackboard.lookup(name).add_upper_bound(val)
00040 #define params_check_all()                          blackboard.check_all()
00041 #define params_define_default_expr(name,val)        blackboard.lookup(name).add_default_expr(val)
00042 #define params_define_doc(name,val)                 blackboard.lookup(name).add_doc(val)
00043 #define params_define_param(name,type,constant,ptr) blackboard.define_param(ParamType(name,type,constant,ptr))
00044 #define params_print_constants(fp)                  blackboard.print_constants(fp)
00045 #define params_print_constants_doc(fp)              blackboard.print_constants_doc(fp)
00046 #define params_print_parameters(fp)                 blackboard.print_parameters(fp)
00047 #define params_print_parameters_doc(fp)             blackboard.print_parameters_doc(fp)
00048 #define params_update_all_default_values()          blackboard.update_all_default_values(CmdParamStringParser(blackboard))
00049 #define cmdparam_set(name,expr)                     blackboard.lookup(name).set(CmdParamStringParser(blackboard),expr)
00050 
00051 
00052 #define cmdparam_changes_verbose ParameterDefinition<>::changes_verbose
00053  
00054 /* Shortcuts to the parser.  For new code use a StringParser and/or StringArgs instead. */
00055 #define PARSE_I(x) Parse(blackboard,x).cmdi()
00056 #define PARSE_F(x) Parse(blackboard,x).cmdf()
00057 #define PARSE_C(x) Parse(blackboard,x).cmds()
00058 
00059 
00060 #define SETFNOBJ_DEFINE2(cargs,paramname,fnname) \
00061 blackboard.lookup(#paramname).add_setfn(new setfnobj2_ ## fnname(cargs))
00062   
00064 #define SETFN_DEFINE2(paramname,fnname)                     \
00065 SETFNOBJ_DEFINE2(,paramname,fnname)
00066 
00067 
00068 /***********************************************************************************/
00069 /***********************************************************************************/
00070 
00071 
00072 /* Current recommended way to define a parameter.  Bounds can go as C
00073    code in options.  The parameter is allocated internally, and does
00074    not refer to an external value.  */ 
00075 #define DECLARE_A(name_str,is_constant,initial_value) \
00076 BlackboardType::ParamType(allocating_Polymorph(initial_value),name_str,is_constant)
00077 
00078 #define PARAM_A(bboard,name_str,initial_value,options,doc) \
00079 bboard.define_param(DECLARE_A(name_str,false,initial_value).add_doc(doc)options)
00080 
00081 
00082 
00083 /*    Typical ways to declare parameters for external values
00084 
00085   To use these macros, first define a type "BlackboardType" compatible
00086   with ParameterMap<> and a variable "blackboard" of this type.
00087   The blackboard need not be the global one above; a local variable
00088   with the same name will also work.
00089 */
00090 
00092 #define DECLARE_PARAM(type,name_str,is_constant,location) \
00093 BlackboardType::ParamType(make_Polymorph(location),name_str,is_constant)
00094 //BlackboardType::ParamType(name_str,type,is_constant,(void*)location)
00095 
00096 #define PARAM(type,name,location) \
00097 DECLARE_PARAM(type,#name,false,location)
00098 
00100 #define PARAM_NN(type,name,location,doc) \
00101 blackboard.define_param(PARAM(type,name,location).add_doc(doc))
00102 
00104 #define PARAM_LL(type,name,location,lower,doc) \
00105 blackboard.define_param(PARAM(type,name,location).add_doc(doc).add_lower_bound(lower))
00106 
00108 #define PARAM_II(type,name,location,lower,upper,doc) \
00109 blackboard.define_param(PARAM(type,name,location).add_doc(doc).add_lower_bound(lower).add_upper_bound(upper))
00110 
00111 
00112 /* Deprecated versions: Same as above but assumes C variable name same as param */
00113 #define PARAM_N(type,name,doc) \
00114 blackboard.define_param(PARAM(type,name,&name).add_doc(doc))
00115 #define PARAM_L(type,name,lower,doc) \
00116 blackboard.define_param(PARAM(type,name,&name).add_doc(doc).add_lower_bound(lower))
00117 #define PARAM_I(type,name,lower,upper,doc) \
00118 blackboard.define_param(PARAM(type,name,&name).add_doc(doc).add_lower_bound(lower).add_upper_bound(upper))
00119 
00120 
00121 /*
00122   Typical ways to declare symbolic constants, allocating local storage
00123   for them.  Unless is_param is true, the value of the constant is not
00124   saved when params are saved to a file.
00125 */
00126      
00128 #define CONST_I(name,value,is_param,doc)                                                 \
00129 { static int paramstorage=value;       \
00130   blackboard.define_param(DECLARE_PARAM(PARAM_INT,#name,!is_param,&paramstorage).add_doc(doc).add_lower_bound(value).add_upper_bound(value)); }
00131 
00132 
00134 #define CONST_B(name,value,is_param,doc)   \
00135 { static Tristate paramstorage=value;       \
00136   blackboard.define_param(DECLARE_PARAM(PARAM_3BOOL,#name,!is_param,&paramstorage).add_doc(doc).add_lower_bound(value).add_upper_bound(value)); }
00137 
00138 
00140 #define CONST_F(name,value,is_param,doc) \
00141 { static double paramstorage=value;       \
00142   blackboard.define_param(DECLARE_PARAM(PARAM_DOUBLE,#name,!is_param,&paramstorage).add_doc(doc).add_lower_bound(value).add_upper_bound(value)); }
00143 
00144 
00146 #define CONST_S(name,is_param,doc) \
00147 { static string paramstorage=#name; \
00148   blackboard.define_param(DECLARE_PARAM(PARAM_USTRING,#name,!is_param,&paramstorage).add_doc(doc)); }
00149 
00150 
00151 
00152 
00154 #define CMD_MAX_ARGUMENTS     512
00155 
00157 #define CMD_MAX_LINE_LENGTH  16384
00158 
00159 
00160 /*
00161   Command return codes 
00162 
00163   If successful, the command may return any non-negative integer
00164 */
00165 typedef int cmdstat; 
00166 #define CMD_NO_ERROR              0
00167 #define CMD_FATAL_ERROR      -20000
00168 #define CMD_MISC_ERROR       -20001
00169 #define CMD_FILE_ERROR       -20002
00170 #define CMD_PARAMETER_ERROR  -20003
00171 #define CMD_MEMORY_ERROR     -20004
00172 #define CMD_PREREQ_ERROR     -20005
00173 #define CMD_EMPTY            -20006  
00174 #define CMD_PARSE_ERROR      -20007  
00175 #define CMD_NEVER_CALLED     -20008  
00176 #define CMD_SIGINT_ERROR     -20009  
00177 #define CMD_SHELL_ERROR      -20010  
00179 /* Standard interface to commands */
00180 #define CMD_ARGS        int argc, const char *argv[]
00181 #define CMD_ARGS_DECL   int argc=0, const char *argv[]=0
00182 #define CMD_UNUSED_ARGS int, const char *[]
00183 #define CMD_CALL_ARGS    argc,argv
00184 typedef cmdstat (*commandfunptr)( CMD_ARGS );
00185 
00186 
00192 class CmdWrapper {
00193 public:
00194   CmdWrapper() { }
00195   virtual ~CmdWrapper() { }
00197   virtual cmdstat operator() ( CMD_ARGS_DECL )=0;
00198 };
00199 
00200 
00202 #define CMD_HEADER(name) \
00203 cmdstat cmd_ ## name ( CMD_ARGS_DECL )
00204 
00205 
00207 #define CMD_DECLARE(name)                                 \
00208 cmdstat cmd_ ## name ( CMD_ARGS );                         \
00209 struct cmdobj_ ## name : public CmdWrapper {                \
00210   cmdstat operator() ( CMD_ARGS_DECL ) {                     \
00211     return cmd_ ## name(CMD_CALL_ARGS);                       \
00212   }                                                            \
00213 }
00214 
00215 
00217 #define CMDOBJ_DOC(name,prereq,cargs,numargs,usage,doc)                \
00218 cmddefs_define_command(#name,new cmdobj_ ## name(cargs),numargs,False); \
00219 cmddefs_define_command_doc(#name,usage,doc);                             \
00220 cmddefs_define_command_prereq(#name,prereq)
00221 
00222 
00224 #define CMD_DOC(name,prereq,numargs,usage,doc) \
00225 CMDOBJ_DOC(name,prereq,,numargs,usage,doc)
00226 
00227 
00232 #define CMDOBJ_DEFINE_CATCHUP(cargs,numargs,name,usage,doc)          \
00233 cmddefs_define_command(#name,new cmdobj_ ## name(cargs),numargs,True); \
00234 cmddefs_define_command_doc(#name,usage,doc)
00235 
00236 
00238 #define CMD_DEFINE_CATCHUP(numargs,name,usage,doc) \
00239 CMDOBJ_DEFINE_CATCHUP(,numargs,name,usage,doc)
00240 
00241 
00243 #define CMDOBJ_DECLARE(Owner,name)                        \
00244 cmdstat cmd_ ## name ( CMD_ARGS_DECL );                    \
00245 class cmdobj_ ## name : public CmdWrapper {                 \
00246   Owner* owner;                                              \
00247 public:                                                       \
00248   cmdobj_ ## name (Owner* owner_i) : owner(owner_i) { };       \
00249   cmdstat operator() ( CMD_ARGS_DECL ) {                        \
00250     return owner->cmd_ ## name (CMD_CALL_ARGS);                  \
00251   }                                                               \
00252 };                                                                 \
00253 friend class cmdobj_ ## name
00254 
00255 
00256 
00258 typedef int (*CmdDefs_LineGenerator)( void );
00259 extern char cmddefs_line_buffer[CMD_MAX_LINE_LENGTH+1+sizeof(int)];
00260      
00262 typedef int HooklistNum; 
00263 
00264 /* Global variables */
00265 extern int command_num_called;
00266 
00267 
00268 
00269 /* Commands */
00270 CMD_HEADER(set);
00271 CMD_HEADER(hook);
00272 
00273 
00274 
00275 /* Misc public functions */
00276 char*  cmdparams_completion_generator(char *text, int state);
00277 void   cmdparam_init_hook(void);
00278 int    cmdparams_print_doc(FILE *fp, const char* name);
00279 int    cmdparam_save_current_state(const char * filename);
00280 char*  cmdparam_dupstr (const char *str);
00281 bool   cmdparams_set_single( BlackboardType& params, const string& arg,
00282                              bool& seterror, bool mark=true,
00283                              bool verbose=ParamType::changes_verbose,
00284                              bool copysetfn=true);
00285 cmdstat cmdparams_set(       BlackboardType& params, StringArgs& arglist,
00286                              bool mark=true, bool warn=false,
00287                              bool verbose=ParamType::changes_verbose,
00288                              bool copysetfn=true);
00289 
00290 char*  params_completion_generator (char *text, int state);
00291 
00292 char*  cmddefs_completion_generator (char *text, int state);
00293 void   cmddefs_declare_prereq_status(   const char * name, cmdstat status );
00294 void   cmddefs_define_command( const char* name, CmdWrapper* function, int minargs, int exec_for_catchups );
00295 void   cmddefs_define_command_doc( const char* name, const char *usage, const char *doc );
00296 void   cmddefs_define_command_prereq( const char* name, const char *prereq );
00297 int    cmddefs_exec_batch(CmdDefs_LineGenerator fn, const char * description);
00298 int    cmddefs_exec_file(const char *filename);
00299 cmdstat cmddefs_exec_by_name(const char* name, CMD_ARGS);
00300 cmdstat cmddefs_exec_str(const char* cmdtxt);
00301 inline cmdstat cmddefs_exec_str(const string& cmdtxt) {  return cmddefs_exec_str(cmdtxt.c_str());  }
00302 int    cmddefs_num_total(void);
00303 void   cmddefs_print_all(FILE *fp);
00304 void   cmddefs_print_all_doc(FILE *file);
00305 void   cmddefs_print_doc(FILE *fp, const char* name);
00306 void   cmddefs_print_doc_for_index(FILE *fp, int idx);
00307 
00308 void   hooklists_empty_list(HooklistNum hooklistnum);
00309 void   hooklists_reset_list(HooklistNum hooklist);
00310 void   hooklists_run_list(HooklistNum hooklist, int counter, int catchuponly);
00311 int    hooklists_define_list( const char * listname, const char *countername );
00312 
00313 
00314 /*
00315   Temporary glue between older argc,argv style of command invocation
00316   and cleaner vector<string> style. Use like:
00317   <pre>
00318     cmdstat cmd_commandname( CMD_ARGS )
00319     {
00320       CMD_ARG_LIST;
00321       const string name = arglist.next(string(""));
00322       // Body of command      
00323     }
00324   </pre>
00325 */
00326 #define CMD_ARG_LIST \
00327   StringParser::arglist argl(&argv[0],&argv[argc]);\
00328   const CmdParamStringParser p(blackboard);\
00329   StringArgs arglist(p, argl.begin(), argl.end())
00330 
00331 
00332 /* Call if the argument list should be entirely empty */
00333 #define CMD_ARG_ASSERT_UNUSED \
00334   {  (void)argv; if (argc) ipc_notify(IPC_ONE,IPC_WARNING,"All arguments are ignored");  }
00335 
00336 
00337 
00341 #define CMD_ARG_PARAMS(name) \
00342   CMD_ARG_LIST;\
00343   BlackboardType argparams(&blackboard.lookup_map("::cmd::" #name),"arguments");\
00344   cmdparams_set( argparams,arglist,true,false,false,false)
00345 
00346 
00348 #define CMD_ARG_ASSERT_EMPTY {\
00349   if (!arglist.empty()) {\
00350     const string rep = arglist.stringrep();\
00351     ipc_notify(IPC_ONE,IPC_WARNING,"Unused arguments: %s",rep.c_str());}}
00352 
00353 
00354 #endif /* ___CMDPARAM_H___ */

Generated on Mon Jan 20 02:35:44 2003 for RF-LISSOM by doxygen1.3-rc2