diff --git a/src/mathed/ChangeLog b/src/mathed/ChangeLog index 2561a99a01..da620d7221 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -1,5 +1,45 @@ 2001-03-01 Lars Gullik Bjønnes + * math_macrotemplate.h: make noncopyable, remove last arg to + constructor, remove flags_ add edit_ + (flags): remove method + + * math_macrotemplate.C (setArgument): delete method + (update): make idx const + (Metrics): use edit_, remove flags_ + (draw): use edit_, remove flags_ + (setEditMode): set edit_, remove flags_ + (MathMacroTemplate): initialize edit_, remove flags_ + + * math_macro.h: remove x,y from MacroArgumentBase, make tmplate_ a + shared_ptr, let constructor take a shared_ptr + + * math_macro.C (MathMacro): take shared_ptr as arg, remove double + initialization. + (draw): remove unneded update, remove removed update of x,y + (GetXY): call tmplates GetMacroXY directly + (SetFocus): call Metrics() instead of update() + (Write): remove support for math macro environments, simplifications. + + * math_macrotable.h: make class noncopyable, change getMacro to + createMacro, change vector to map store a + shared_ptr in it., remove unneeded typedef + + * math_macrotable.C: add pragma + (createMacro): change name from getMacro, use shared_ptr + (getTemplate): Use map lookup to get macro, return shared_ptr + (addTemplate): assert that m containse a valid pointer, use map[] + insert. + (builtinMacros): by using shared_ptr fix the mem leak, remove + commented out unsupported macros + + * math_cursor.C: changes because of the above + * math_parser.C: changes because of the abobe + + * formulamacro.h: change tmacro_ to be a shared_ptr, remove + default arg on constructor. + * formulamacro.C: changes because of the above + * math_parinset.h: add pragma (SetAlign): move inline out of class def (GetColumns): ditto diff --git a/src/mathed/formulamacro.C b/src/mathed/formulamacro.C index 344457c48f..5965b2b2f1 100644 --- a/src/mathed/formulamacro.C +++ b/src/mathed/formulamacro.C @@ -42,7 +42,6 @@ using std::istream; InsetFormulaMacro::InsetFormulaMacro() : InsetFormula(true) { - tmacro_ = 0; opened_ = false; } @@ -51,8 +50,8 @@ InsetFormulaMacro::InsetFormulaMacro(string nm, int na) : InsetFormula(true), name_(nm) { tmacro_ = MathMacroTable::mathMTable.getTemplate(name_); - if (!tmacro_) { - tmacro_ = new MathMacroTemplate(name_, na); + if (!tmacro_.get()) { + tmacro_.reset(new MathMacroTemplate(name_, na)); MathMacroTable::mathMTable.addTemplate(tmacro_); } opened_ = false; @@ -61,13 +60,16 @@ InsetFormulaMacro::InsetFormulaMacro(string nm, int na) InsetFormulaMacro::~InsetFormulaMacro() { + // We do not want the InsetFormula destructor to + // delete this. That is taken care of elsewhere (Lgb) par = 0; } Inset * InsetFormulaMacro::Clone(Buffer const &) const { - return new InsetFormulaMacro(name_); + // This should really use a proper copy constructor + return new InsetFormulaMacro(name_, 0); } @@ -105,7 +107,7 @@ void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex) MathedArray ar; mathed_parse(ar, 0, reinterpret_cast(&tmacro_)); - // since tmacro_ == 0 when mathed_parse is called we need to sett + // Since tmacro_ == 0 when mathed_parse is called we need to set // its contents explicitly afterwards (Lgb) tmacro_->setData(ar); @@ -114,7 +116,7 @@ void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex) MathMacroTable::mathMTable.addTemplate(tmacro_); name_ = tmacro_->GetName(); - par = tmacro_; + par = tmacro_.get(); // reading of end_inset in the inset!!! while (lex.IsOK()) { diff --git a/src/mathed/formulamacro.h b/src/mathed/formulamacro.h index 8eab7cc3d2..3ab9c44acb 100644 --- a/src/mathed/formulamacro.h +++ b/src/mathed/formulamacro.h @@ -17,12 +17,14 @@ #ifndef INSET_FORMULA_MACRO_H #define INSET_FORMULA_MACRO_H +#include + +#include "formula.h" + #ifdef __GNUG__ #pragma interface #endif -#include "formula.h" - class MathMacroTemplate; @@ -33,7 +35,7 @@ public: InsetFormulaMacro(); /// explicit - InsetFormulaMacro(string name, int na = 0); + InsetFormulaMacro(string name, int na); /// ~InsetFormulaMacro(); /// @@ -74,7 +76,7 @@ private: /// string name_; /// - MathMacroTemplate * tmacro_; + boost::shared_ptr tmacro_; }; diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index 09c8e90823..76769f4f46 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -40,6 +40,7 @@ #include "math_decorationinset.h" #include "math_dotsinset.h" #include "math_accentinset.h" +#include "math_macrotemplate.h" #include "mathed/support.h" static MathedArray selarray; @@ -751,7 +752,7 @@ void MathedCursor::Interpret(string const & s) l = in_word_set(s); if (!l) { - p = MathMacroTable::mathMTable.getMacro(s); + p = MathMacroTable::mathMTable.createMacro(s); if (!p) { lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl; if (s == "root") { @@ -815,7 +816,7 @@ void MathedCursor::Interpret(string const & s) break; case LM_TK_MACRO: - p = MathMacroTable::mathMTable.getMacro(s); + p = MathMacroTable::mathMTable.createMacro(s); tcode = static_cast(p)->getTCode(); lyxerr[Debug::MATHED] << "Macro " << s << ' ' << tcode << endl; break; @@ -872,7 +873,7 @@ void MathedCursor::MacroModeClose() latexkeys const * l = in_word_set(imacro->GetName()); if (!imacro->GetName().empty() && (!l || (l && IsMacro(l->token, l->id))) && - !MathMacroTable::mathMTable.getMacro(imacro->GetName())) { + !MathMacroTable::mathMTable.createMacro(imacro->GetName())) { if (!l) { //imacro->SetName(macrobf); // This guarantees that the string will be removed by destructor @@ -886,7 +887,7 @@ void MathedCursor::MacroModeClose() static_cast(cursor->GetInset())->getAccentCode()); } cursor->Delete(); - if (l || MathMacroTable::mathMTable.getMacro(imacro->GetName())) { + if (l || MathMacroTable::mathMTable.createMacro(imacro->GetName())) { Interpret(imacro->GetName()); } imacro->SetName(""); diff --git a/src/mathed/math_macro.C b/src/mathed/math_macro.C index 8e5d2b1c0f..ca40c21d96 100644 --- a/src/mathed/math_macro.C +++ b/src/mathed/math_macro.C @@ -44,15 +44,13 @@ ostream & operator<<(ostream & o, MathedTextCodes mtc) } -MathMacro::MathMacro(MathMacroTemplate * t) - : MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate_(t) +MathMacro::MathMacro(boost::shared_ptr const & t) + : MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), + tmplate_(t) { nargs_ = tmplate_->getNoArgs(); tcode_ = tmplate_->getTCode(); args_.resize(nargs_); - for (int i = 0; i < nargs_; ++i) { - args_[i].row = 0; - } idx_ = 0; SetName(tmplate_->GetName()); } @@ -103,12 +101,8 @@ void MathMacro::draw(Painter & pain, int x, int y) xo(x); yo(y); Metrics(); - tmplate_->update(this); tmplate_->SetStyle(size()); tmplate_->draw(pain, x, y); - for (int i = 0; i < nargs_; ++i) { - tmplate_->GetMacroXY(i, args_[i].x, args_[i].y); - } } @@ -154,8 +148,13 @@ int MathMacro::GetColumns() const void MathMacro::GetXY(int & x, int & y) const { - x = args_[idx_].x; - y = args_[idx_].y; +#if 0 + x = args_[idx_].x_; + y = args_[idx_].y_; +#else + const_cast(this)->Metrics(); + tmplate_->GetMacroXY(idx_, x, y); +#endif } @@ -169,7 +168,7 @@ bool MathMacro::Permit(short f) const void MathMacro::SetFocus(int x, int y) { - tmplate_->update(this); + Metrics(); tmplate_->SetMacroFocus(idx_, x, y); } @@ -180,8 +179,6 @@ void MathMacro::setData(MathedArray const & a) } - - MathedRowSt * MathMacro::getRowSt() const { return args_[idx_].row; @@ -196,43 +193,18 @@ MathedTextCodes MathMacro::getTCode() const void MathMacro::Write(ostream & os, bool fragile) { - if (tmplate_->flags() & MMF_Exp) { - lyxerr[Debug::MATHED] << "Expand " << tmplate_->flags() - << ' ' << MMF_Exp << endl; - tmplate_->update(this); - tmplate_->Write(os, fragile); - } else { - if (tmplate_->flags() & MMF_Env) { - os << "\\begin{" - << name - << "} "; - } else { - os << '\\' << name; - } -// if (options) { -// file += '['; -// file += options; -// file += ']'; -// } - - if (!(tmplate_->flags() & MMF_Env) && nargs_ > 0) - os << '{'; - + os << '\\' << name; + + if (nargs_ > 0) { + os << '{'; + for (int i = 0; i < nargs_; ++i) { array = args_[i].array; MathParInset::Write(os, fragile); if (i < nargs_ - 1) os << "}{"; - } - if (tmplate_->flags() & MMF_Env) { - os << "\\end{" - << name - << '}'; - } else { - if (nargs_ > 0) - os << '}'; - else - os << ' '; } - } + os << '}'; + } else + os << ' '; } diff --git a/src/mathed/math_macro.h b/src/mathed/math_macro.h index 964e57ba17..0e8429eebc 100644 --- a/src/mathed/math_macro.h +++ b/src/mathed/math_macro.h @@ -22,6 +22,8 @@ #endif #include +#include + #include "math_parinset.h" class MathMacroTemplate; @@ -35,7 +37,7 @@ class MathMacro : public MathParInset { public: /// A macro can only be builded from an existing template explicit - MathMacro(MathMacroTemplate *); + MathMacro(boost::shared_ptr const &); /// or from another macro. explicit MathMacro(MathMacro const &); @@ -73,20 +75,16 @@ public: bool Permit(short) const; private: /// - MathMacroTemplate * tmplate_; + boost::shared_ptr tmplate_; /// struct MacroArgumentBase { - /// Position of the macro - int x; - /// - int y; /// MathedRowSt * row; /// MathedArray array; /// MacroArgumentBase() - : x(0), y(0), row(0) + : row(0) {} }; std::vector args_; diff --git a/src/mathed/math_macrotable.C b/src/mathed/math_macrotable.C index 24be222fe3..9312faa008 100644 --- a/src/mathed/math_macrotable.C +++ b/src/mathed/math_macrotable.C @@ -1,5 +1,9 @@ #include +#ifdef __GNUG__ +#pragma implementation +#endif + #include "math_macrotable.h" #include "math_macro.h" #include "math_macrotemplate.h" @@ -10,6 +14,7 @@ #include "math_fracinset.h" #include "math_parinset.h" #include "debug.h" +#include "support/LAssert.h" using std::endl; @@ -18,68 +23,61 @@ MathMacroTable MathMacroTable::mathMTable; bool MathMacroTable::built = false; -MathMacro * MathMacroTable::getMacro(string const & name) const +MathMacro * MathMacroTable::createMacro(string const & name) const { - MathMacroTemplate * mt = getTemplate(name); - return (mt) ? new MathMacro(mt): 0; + boost::shared_ptr mt = getTemplate(name); + return (mt.get()) ? new MathMacro(mt) : 0; } -// The search is currently linear but will be binary or hash, later. -MathMacroTemplate * MathMacroTable::getTemplate(string const & name) const +boost::shared_ptr const +MathMacroTable::getTemplate(string const & name) const { - for (size_type i = 0; i < macro_table.size(); ++i) { - if (name == macro_table[i]->GetName()) - return macro_table[i]; - } - - return 0; -} - -void MathMacroTable::addTemplate(MathMacroTemplate * m) -{ - macro_table.push_back(m); + table_type::const_iterator cit = macro_table.find(name); + if (cit != macro_table.end()) return (*cit).second; + return boost::shared_ptr(); } -// All this stuff aparently leaks because it's created here and is not -// deleted never, but it have to live all the LyX sesion. OK, would not -// so hard to do it in the MacroTable destructor, but this doesn't harm -// seriously, so don't bother me with purify results here. ;-) +void +MathMacroTable::addTemplate(boost::shared_ptr const & m) +{ + Assert(m.get()); + macro_table[m->GetName()] = m; +} + void MathMacroTable::builtinMacros() { - MathParInset * inset;// *arg; - built = true; lyxerr[Debug::MATHED] << "Building macros" << endl; // This macro doesn't have arguments - MathMacroTemplate * m = new MathMacroTemplate("notin"); // this leaks - addTemplate(m); { + boost::shared_ptr m(new MathMacroTemplate("notin", 0)); + addTemplate(m); MathedArray array; MathedIter iter(&array); iter.insertInset(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not), - LM_TC_INSET); // this leaks + LM_TC_INSET); m->setData(array); } // These two are only while we are still with LyX 2.x - m = new MathMacroTemplate("emptyset"); // this leaks - addTemplate(m); { + boost::shared_ptr m(new MathMacroTemplate("emptyset", 0)); + addTemplate(m); MathedArray array; MathedIter iter(&array); iter.insertInset(new MathAccentInset('O', LM_TC_RM, LM_not), - LM_TC_INSET); // this leaks + LM_TC_INSET); m->setData(array); } - m = new MathMacroTemplate("perp"); // this leaks - addTemplate(m); { + boost::shared_ptr m(new MathMacroTemplate("perp", 0)); + addTemplate(m); MathedArray array; MathedIter iter(&array); iter.insert(LM_bot, LM_TC_BOP); @@ -87,13 +85,13 @@ void MathMacroTable::builtinMacros() } // binom has two arguments - m = new MathMacroTemplate("binom", 2); - addTemplate(m); { + boost::shared_ptr m(new MathMacroTemplate("binom", 2)); + addTemplate(m); MathedArray array; m->setData(array); MathedIter iter(&array); - inset = new MathDelimInset('(', ')'); + MathParInset * inset = new MathDelimInset('(', ')'); iter.insertInset(inset, LM_TC_ACTIVE_INSET); array = MathedArray(); MathedIter iter2(&array); @@ -108,34 +106,4 @@ void MathMacroTable::builtinMacros() iter4.insertInset(m->getMacroPar(1), LM_TC_INSET); frac->SetData(array, array2); } - -/* - // Cases has 1 argument - m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks - addTemplate(m); - array = new MathedArray; // this leaks - iter.SetData(array); - arg = new MathMatrixInset(2, 1); // this leaks - - m->setArgument(arg); - arg->SetAlign('c', "ll"); - iter.Insert(arg, LM_TC_ACTIVE_INSET); - inset = new MathDelimInset('{', '.'); // this leaks - inset->SetData(array); - array = new MathedArray; // this leaks - iter.SetData(array); - iter.Insert(inset, LM_TC_ACTIVE_INSET); - m->SetData(array); - - - // the environment substack has 1 argument - m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks - addTemplate(m); - arg = new MathMatrixInset(1, 1); // this leaks - m->setArgument(arg); - arg->SetType(LM_OT_MACRO); - array = new MathedArray; // this leaks - iter.SetData(array); - iter.Insert(arg, LM_TC_ACTIVE_INSET); - m->SetData(array);*/ } diff --git a/src/mathed/math_macrotable.h b/src/mathed/math_macrotable.h index 1814e3a1aa..cbf848659a 100644 --- a/src/mathed/math_macrotable.h +++ b/src/mathed/math_macrotable.h @@ -2,33 +2,39 @@ #ifndef MATHMACROTABLE #define MATHMACROTABLE -#include +#include #include "LString.h" +#include +#include + +#ifdef __GNUG__ +#pragma interface +#endif + class MathMacroTemplate; class MathMacro; /// -class MathMacroTable { +class MathMacroTable : noncopyable { public: - /// - void addTemplate(MathMacroTemplate *); - /// - MathMacro * getMacro(string const &) const; - /// - MathMacroTemplate * getTemplate(string const &) const; - /// - void builtinMacros(); - /// - static MathMacroTable mathMTable; - /// - static bool built; + /// + void addTemplate(boost::shared_ptr const &); + /// + MathMacro * createMacro(string const &) const; + /// + boost::shared_ptr const + getTemplate(string const &) const; + /// + void builtinMacros(); + /// + static MathMacroTable mathMTable; + /// + static bool built; private: - /// - typedef std::vector table_type; - /// - typedef table_type::size_type size_type; - /// - table_type macro_table; + /// + typedef std::map > table_type; + /// + table_type macro_table; }; #endif diff --git a/src/mathed/math_macrotemplate.C b/src/mathed/math_macrotemplate.C index f2f2f7265f..5f03561647 100644 --- a/src/mathed/math_macrotemplate.C +++ b/src/mathed/math_macrotemplate.C @@ -13,9 +13,10 @@ using std::ostream; -MathMacroTemplate::MathMacroTemplate(string const & nm, int na, int flg): +MathMacroTemplate::MathMacroTemplate(string const & nm, int na): MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO), - flags_(flg), nargs_(na) + edit_(false), + nargs_(na) { if (nargs_ > 0) { tcode_ = LM_TC_ACTIVE_INSET; @@ -52,12 +53,12 @@ int MathMacroTemplate::getNoArgs() const void MathMacroTemplate::setEditMode(bool ed) { if (ed) { - flags_ |= MMF_Edit; + edit_ = true; for (int i = 0; i < nargs_; ++i) { args_[i].setExpand(false); } } else { - flags_ &= ~MMF_Edit; + edit_ = false; for (int i = 0; i < nargs_; ++i) { args_[i].setExpand(true); } @@ -70,7 +71,7 @@ void MathMacroTemplate::draw(Painter & pain, int x, int y) int x2; int y2; bool expnd = (nargs_ > 0) ? args_[0].getExpand() : false; - if (flags_ & MMF_Edit) { + if (edit_) { for (int i = 0; i < nargs_; ++i) { args_[i].setExpand(false); } @@ -95,9 +96,9 @@ void MathMacroTemplate::draw(Painter & pain, int x, int y) void MathMacroTemplate::Metrics() { - bool expnd = (nargs_ > 0) ? args_[0].getExpand() : false; + bool const expnd = (nargs_ > 0) ? args_[0].getExpand() : false; - if (flags_ & MMF_Edit) { + if (edit_) { for (int i = 0; i < nargs_; ++i) { args_[i].setExpand(false); } @@ -117,7 +118,7 @@ void MathMacroTemplate::Metrics() void MathMacroTemplate::update(MathMacro * macro) { Assert(macro); - int idx = macro->getArgumentIdx(); + int const idx = macro->getArgumentIdx(); for (int i = 0; i < nargs_; ++i) { macro->setArgumentIdx(i); args_[i].setData(macro->GetData()); @@ -145,12 +146,6 @@ void MathMacroTemplate::WriteDef(ostream & os, bool fragile) } -void MathMacroTemplate::setArgument(MathedArray * a, int i) -{ - args_[i].setData(*a); -} - - void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const { args_[i].GetXY(x, y); diff --git a/src/mathed/math_macrotemplate.h b/src/mathed/math_macrotemplate.h index b351a4ff6e..64a9cf7ce4 100644 --- a/src/mathed/math_macrotemplate.h +++ b/src/mathed/math_macrotemplate.h @@ -4,6 +4,8 @@ #include +#include + #include "math_parinset.h" #include "math_macroarg.h" @@ -16,11 +18,11 @@ class MathMacro; /** This class contains the macro definition \author Alejandro Aguilar Sierra */ -class MathMacroTemplate : public MathParInset { +class MathMacroTemplate : public MathParInset, public noncopyable { public: /// A template constructor needs all the data explicit - MathMacroTemplate(string const &, int na = 0, int f = 0); + MathMacroTemplate(string const &, int na); /// void draw(Painter &, int, int); /// @@ -28,11 +30,9 @@ public: /// void WriteDef(std::ostream &, bool fragile); /// useful for special insets - void setTCode(MathedTextCodes t); + void setTCode(MathedTextCodes t); /// MathedTextCodes getTCode() const; - /// - void setArgument(MathedArray *, int i= 0); /// Number of arguments int getNoArgs() const; /// @@ -46,11 +46,9 @@ public: /// Replace the appropriate arguments with a specific macro's data void update(MathMacro * m); - /// - short flags() const; private: - /// - short flags_; + /// Are we in edit mode or not? + bool edit_; /// MathedTextCodes tcode_; /// @@ -58,10 +56,4 @@ private: /// int nargs_; }; - - -inline -short MathMacroTemplate::flags() const { - return flags_; -} #endif diff --git a/src/mathed/math_parser.C b/src/mathed/math_parser.C index 4d1bcd32f5..b2d4bb911b 100644 --- a/src/mathed/math_parser.C +++ b/src/mathed/math_parser.C @@ -783,7 +783,7 @@ void mathed_parse(MathedArray & array, unsigned flags = 0, { MathMacro * p = - MathMacroTable::mathMTable.getMacro(yylval.s); + MathMacroTable::mathMTable.createMacro(yylval.s); if (p) { if (accent) data.insertInset(doAccent(p), p->getTCode()); @@ -881,7 +881,7 @@ void mathed_parse(MathedArray & array, unsigned flags = 0, } else { // lyxerr << "MATHCRO[" << yytext << "]"; MathMacro * p = - MathMacroTable::mathMTable.getMacro(yytext.data()); + MathMacroTable::mathMTable.createMacro(yytext.data()); if (p) { data.insertInset(p, p->getTCode()); p->setArgumentIdx(0); @@ -898,7 +898,7 @@ void mathed_parse(MathedArray & array, unsigned flags = 0, case LM_TK_MACRO: { MathedInset * p = - MathMacroTable::mathMTable.getMacro(yylval.l->name); + MathMacroTable::mathMTable.createMacro(yylval.l->name); if (p) { if (accent) {