Abdelrazak Younes wrote:
[EMAIL PROTECTED] wrote:
Author: rgheck
Date: Thu Oct 25 06:13:56 2007
New Revision: 21194

URL: http://www.lyx.org/trac/changeset/21194
Log:
Move the findInfo() and defaultCommand() routines out of InsetCommand and into its subclasses, so that the subclasses know what parameters they want, etc. Also, introduce an "isCompatibleCommand()" routine, so the subclasses can tell us which commands they are prepared to accept.


Modified: lyx-devel/trunk/src/insets/InsetBibitem.cpp
URL: http://www.lyx.org/trac/file/lyx-devel/trunk/src/insets/InsetBibitem.cpp?rev=21194 ==============================================================================
--- lyx-devel/trunk/src/insets/InsetBibitem.cpp (original)
+++ lyx-devel/trunk/src/insets/InsetBibitem.cpp Thu Oct 25 06:13:56 2007
@@ -44,6 +44,15 @@
 {
     if (getParam("key").empty())
         setParam("key", key_prefix + convert<docstring>(++key_counter));
+}
+
+
+CommandInfo const * InsetBibitem::findInfo(std::string const & /* cmdName */)
+{
+    static const char * const paramnames[] = {"label", "key", ""};
+    static const bool isoptional[] = {true, false};
+    static const CommandInfo info = {2, paramnames, isoptional};
+    return &info;
 }

Hum, I think I'd prefer a static map initialisation:

map<InsetCode, CommandInfo> command_info;

command_info[BIBITEM_CODE] = CommandInfo(2, {"label", "key", ""}, {true, false}; command_info[BIBTEX_CODE] = CommandInfo(3, {"options", "btprint", "bibfiles", ""}, {true, true, false};

Hum... no, even that is too complicated, why not using ICPInfo directly instead of this intermediate structure? Instead of a list, I'd choose a vector:

typedef std::vector<ICPInfo> PList;
typeded map<InsetCode, PList> command_info;

And then in the factory or in the InsetBibitem ctor:

command_info[BIBITEM_CODE] = PList({{"label", true}, {"key", false}});

Something like the attached, unfinished, patch....

Abdel.
Index: factory.cpp
===================================================================
--- factory.cpp (revision 21196)
+++ factory.cpp (working copy)
@@ -80,7 +80,21 @@
 
 using support::compare_ascii_no_case;
 
+namespace {
 
+typedef std::map<InsetCode, CommandInfo> CommandInfoMap;
+
+CommandInfoMap command_info;
+
+void initCommandInfoMap()
+{
+       CommandInfoParam const bibitem[] = {{"label", true}, {"key", false}};
+       command_info[BIBITEM_CODE] = CommandInfo(BIBITEM_CODE, "", bibitem, 2);
+}
+
+}
+
+
 Inset * createInset(BufferView * bv, FuncRequest const & cmd)
 {
        BufferParams const & params = bv->buffer().params();
@@ -147,7 +161,7 @@
                        return new InsetOptArg(params);
 
                case LFUN_BIBITEM_INSERT:
-                       return new 
InsetBibitem(InsetCommandParams(BIBITEM_CODE));
+                       return new 
InsetBibitem(InsetCommandParams(command_info[BIBITEM_CODE]));
 
                case LFUN_FLOAT_INSERT: {
                        // check if the float type exists
@@ -182,7 +196,7 @@
                        return new InsetIndex(params);
 
                case LFUN_NOMENCL_INSERT: {
-                       InsetCommandParams icp(NOMENCL_CODE);
+                       InsetCommandParams icp(command_info[NOMENCL_CODE]);
                        icp["symbol"] = cmd.argument().empty() ?
                                
bv->cursor().innerText()->getStringToIndex(bv->cursor()) :
                                cmd.argument();
@@ -211,13 +225,16 @@
                }
 
                case LFUN_INDEX_PRINT:
-                       return new 
InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE));
+                       return new InsetPrintIndex(
+                               
InsetCommandParams(command_info[INDEX_PRINT_CODE]));
 
                case LFUN_NOMENCL_PRINT:
-                       return new 
InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
+                       return new InsetPrintNomencl(
+                               
InsetCommandParams(command_info[NOMENCL_PRINT_CODE]));
 
                case LFUN_TOC_INSERT:
-                       return new InsetTOC(InsetCommandParams(TOC_CODE));
+                       return new InsetTOC(
+                               InsetCommandParams(command_info[TOC_CODE]));
 
                case LFUN_ENVIRONMENT_INSERT:
                        return new InsetEnvironment(params, cmd.argument());
@@ -238,19 +255,19 @@
                                return 0;
                        
                        case BIBITEM_CODE: {
-                               InsetCommandParams icp(code);
+                               InsetCommandParams icp(command_info[code]);
                                InsetCommandMailer::string2params(name, 
to_utf8(cmd.argument()), icp);
                                return new InsetBibitem(icp);
                        }
                        
                        case BIBTEX_CODE: {
-                               InsetCommandParams icp(code);
+                               InsetCommandParams icp(command_info[code]);
                                InsetCommandMailer::string2params(name, 
to_utf8(cmd.argument()), icp);
                                return new InsetBibtex(icp);
                        }
                        
                        case CITE_CODE: {
-                               InsetCommandParams icp(code);
+                               InsetCommandParams icp(command_info[code]);
                                InsetCommandMailer::string2params(name, 
to_utf8(cmd.argument()), icp);
                                return new InsetCitation(icp);
                        }
@@ -286,13 +303,13 @@
                        }
                        
                        case HYPERLINK_CODE: {
-                               InsetCommandParams icp(code);
+                               InsetCommandParams icp(command_info[code]);
                                InsetCommandMailer::string2params(name, 
to_utf8(cmd.argument()), icp);
                                return new InsetHyperlink(icp);
                        }
                        
                        case INCLUDE_CODE: {
-                               InsetCommandParams icp(code);
+                               InsetCommandParams icp(command_info[code]);
                                InsetCommandMailer::string2params(name, 
to_utf8(cmd.argument()), icp);
                                return new InsetInclude(icp);
                        }
@@ -301,25 +318,25 @@
                                return new InsetIndex(params);
                        
                        case NOMENCL_CODE: {
-                               InsetCommandParams icp(code);
+                               InsetCommandParams icp(command_info[code]);
                                InsetCommandMailer::string2params(name, 
lyx::to_utf8(cmd.argument()), icp);
                                return new InsetNomencl(icp);
                        }
                        
                        case LABEL_CODE: {
-                               InsetCommandParams icp(code);
+                               InsetCommandParams icp(command_info[code]);
                                InsetCommandMailer::string2params(name, 
to_utf8(cmd.argument()), icp);
                                return new InsetLabel(icp);
                        }
                        
                        case REF_CODE: {
-                               InsetCommandParams icp(code);
+                               InsetCommandParams icp(command_info[code]);
                                InsetCommandMailer::string2params(name, 
to_utf8(cmd.argument()), icp);
                                return new InsetRef(icp, bv->buffer());
                        }
                        
                        case TOC_CODE: {
-                               InsetCommandParams icp(code);
+                               InsetCommandParams icp(command_info[code]);
                                InsetCommandMailer::string2params(name, 
to_utf8(cmd.argument()), icp);
                                return new InsetTOC(icp);
                        }
@@ -418,7 +435,7 @@
                //we do not know in advance that we're dealing with a command 
inset.
                //Worst case, we could put it in each case below. Better, we 
could
                //pass the lexer to the constructor and let the params be built 
there.
-               InsetCommandParams inscmd(code);
+               InsetCommandParams inscmd(command_info[code]);
                inscmd.read(lex);
 
                switch (code) {
Index: insets/InsetCommandParams.cpp
===================================================================
--- insets/InsetCommandParams.cpp       (revision 21196)
+++ insets/InsetCommandParams.cpp       (working copy)
@@ -48,9 +48,7 @@
 using support::ExceptionMessage;
 using support::WarningException;
 
-ICPInfo::ICPInfo(std::string const & s, bool b)
-       : paramName(s), optional(b)
-{}
+{ {"label", true}, {"key", false} };
 
 
 void ICPList::addParam(std::string const & s, bool b) {
Index: insets/InsetCommandParams.h
===================================================================
--- insets/InsetCommandParams.h (revision 21196)
+++ insets/InsetCommandParams.h (working copy)
@@ -29,58 +29,40 @@
 
 // No parameter may be named "preview", because that is a required
 // flag for all commands.
-struct CommandInfo {
-       /// Number of parameters
-       size_t n;
-       /// Parameter names. paramnames[n] must be "".
-       char const * const * paramnames;
-       /// Tells whether a parameter is optional
-       bool const * optional;
-};
-
-///
-struct ICPInfo {
-       ///
-       ICPInfo(std::string const & s, bool b);
+struct CommandInfoParam {
        /// Parameter name.
-       std::string paramName;
+       std::string name;
        /// Whether it is optional.
        bool optional;
 };
+
 ///
-typedef std::list<ICPInfo> PList;
-///
-class ICPList {
-       public:
-               ///
-               PList::const_iterator begin() { return plist_.begin(); }
-               ///
-               PList::const_iterator end()   { return plist_.end(); }
-               ///
-               void clear() { plist_.clear(); }
-               ///
-               void addParam(std::string const & s, bool b = false);
-               ///
-               bool hasParam(std::string const & s);
-       private:
-               ///
-               PList plist_;
-       
+struct CommandInfo {
+       CommandInfo(InsetCode c, std::string n, CommandInfoParam const p[], 
size_t nparams)
+               : code(c), name(n)
+       {
+               for (size_t i = 0; i != nparams; ++i)
+                       params.push_back(p[i]);
+       }
+
+       /// what kind of inset we're the parameters for.
+       InsetCode code;
+       /// The name of this command as it appears in .lyx and .tex files.
+       std::string name;
+       ///
+       std::vector<CommandInfoParam> params;
 };
 
 
 class InsetCommandParams {
 public:
        /// Construct parameters for inset of type \p code.
-       explicit InsetCommandParams(InsetCode code);
-       /// Construct parameters for inset of type \p code with
-       /// command name \p cmdName.
-       explicit InsetCommandParams(InsetCode code,
-                       std::string const & cmdName);
+       explicit InsetCommandParams(CommandInfo const &);
+
        ///
-       std::string insetType() const { return insetName(insetCode_); }
+       std::string insetType() const { return insetName(info_->code); }
        ///
-       InsetCode code() const { return insetCode_; }
+       InsetCode code() const { return info_->code; }
        ///
        void read(Lexer &);
        /// Parse the command
@@ -89,7 +71,8 @@
        /// Build the complete LaTeX command
        docstring const getCommand() const;
        /// Return the command name
-       std::string const & getCmdName() const { return cmdName_; }
+       std::string const & getCmdName() const { return info_->name; }
+
        /// Set the name to \p n. This must be a known name. All parameters
        /// are cleared except those that exist also in the new command.
        /// What matters here is the parameter name, not position.
@@ -125,10 +108,6 @@
        std::string getDefaultCmd(InsetCode);
        /// Description of all command properties
        CommandInfo const * info_;
-       /// what kind of inset we're the parameters for
-       InsetCode insetCode_;
-       /// The name of this command as it appears in .lyx and .tex files
-       std::string cmdName_;
        ///
        typedef std::vector<docstring> ParamVector;
        /// The parameters (both optional and required ones). The order is

Reply via email to