On Tue, Jul 16, 2002 at 03:51:05PM +0100, Angus Leeming wrote: > On Tuesday 16 July 2002 3:58 pm, Andre Poenitz wrote: > > On Tue, Jul 16, 2002 at 03:29:12PM +0100, Angus Leeming wrote: > > > One more suggestion: naked pointers are evil. Naked pointers in an STL > > > container are doubly evil. Wrap that pointer in a boost::shared_ptr. > > > Memory is automatically delete-d as the list goes out of scope. > > > > Why are pointers used anyway? [I did not look at the source, so maybe the > > question is silly] > > You mean you'd prefer to pass around (possibly large) structs? Seems a little > excessive. Anyway, if you prefer that then this will probably also be fine > Martin. > > /// > typedef std::map<string, Counter> CounterList; > /// > CounterList counterList; > > counterList[newc] = new Counter;
Did just this (with André's further remarks). Attached. Let me make this perfectly clear: this just compiles. And looks beautiful (doesn't it?). And is believed to be politically correct. Thanks... I'm slowly getting this C++ philosophy :-) Martin :wq
Index: counters.C =================================================================== RCS file: /cvs/lyx/lyx-devel/src/counters.C,v retrieving revision 1.7 diff -u -p -r1.7 counters.C --- counters.C 2002/03/21 17:25:09 1.7 +++ counters.C 2002/07/16 20:12:35 @@ -17,6 +17,7 @@ #include "counters.h" #include "debug.h" +#include "support/lstrings.h" using std::endl; @@ -48,7 +49,6 @@ int Counter::value() const void Counter::step() { ++value_; - onstep.emit(); } @@ -57,15 +57,39 @@ void Counter::reset() value_ = 0; } +string Counter::master() const +{ + return master_; +} + +void Counter::setMaster(string const & m) +{ + master_ = m; +} + -Counters::~Counters() +Counters::Counters() { - // We need this since we store the Counter's as pointers in - // the counterList. - for (CounterList::iterator it = counterList.begin(); - it != counterList.end(); - ++it) - delete it->second; + // Ehh, should this take a textclass arg? + + // Sectioning counters: + newCounter("part"); + newCounter("chapter"); + newCounter("section", "chapter"); + newCounter("subsection", "section"); + newCounter("subsubsection", "subsection"); + newCounter("paragraph", "subsubsection"); + newCounter("subparagraph", "paragraph"); + + // Enumeration counters: + newCounter("emumi"); + newCounter("emumii", "enumi"); + newCounter("enumiii", "enumii"); + newCounter("enumiv", "enumiii"); + + // Float counters: + newCounter("figure"); + newCounter("table"); } @@ -73,36 +97,35 @@ void Counters::newCounter(string const & { // First check if newc already exist CounterList::iterator cit = counterList.find(newc); - // if alrady exist give warning and return + // if already exist give warning and return if (cit != counterList.end()) { - lyxerr << "The new counter already exist." << endl; + lyxerr << "The new counter already exists." << endl; return; } - counterList[newc] = new Counter; + counterList[newc]; + cit->second.setMaster(""); } -void Counters::newCounter(string const & newc, string const & oldc) +void Counters::newCounter(string const & newc, string const & masterc) { - // First check if newc already exist + // First check if newc already exists CounterList::iterator cit = counterList.find(newc); // if already existant give warning and return if (cit != counterList.end()) { - lyxerr << "The new counter already exist." << endl; + lyxerr << "The new counter already exists." << endl; return; } - // then check if oldc exist - CounterList::iterator it = counterList.find(oldc); + // then check if masterc exists + CounterList::iterator it = counterList.find(masterc); // if not give warning and return if (it == counterList.end()) { - lyxerr << "The old counter does not exist." << endl; + lyxerr << "The master counter does not exist." << endl; return; } - Counter * tmp = new Counter; - it->second->onstep.connect(SigC::slot(tmp, - &Counter::reset)); - counterList[newc] = tmp; + counterList[newc]; + cit->second.setMaster(masterc); } @@ -113,7 +136,7 @@ void Counters::set(string const & ctr, i lyxerr << "Counter does not exist." << endl; return; } - it->second->set(val); + it->second.set(val); } @@ -124,7 +147,7 @@ void Counters::addto(string const & ctr, lyxerr << "Counter does not exist." << endl; return; } - it->second->addto(val); + it->second.addto(val); } @@ -135,7 +158,7 @@ int Counters::value(string const & ctr) lyxerr << "Counter does not exist." << endl; return 0; } - return cit->second->value(); + return cit->second.value(); } @@ -145,6 +168,120 @@ void Counters::step(string const & ctr) if (it == counterList.end()) { lyxerr << "Counter does not exist." << endl; return; + } + it->second.step(); + if (it->second.master() != "") { + set(it->second.master(), 0); + } +} + +namespace { + +inline +char loweralphaCounter(int n) +{ + if (n < 1 || n > 26) + return '?'; + else + return 'a' + n - 1; +} + +inline +char alphaCounter(int n) +{ + if (n < 1 || n > 26) + return '?'; + else + return 'A' + n - 1; +} + +inline +char hebrewCounter(int n) +{ + static const char hebrew[22] = { + 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', + 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö', + '÷', 'ø', 'ù', 'ú' + }; + if (n < 1 || n > 22) + return '?'; + else + return hebrew[n-1]; +} + +inline +string const romanCounter(int n) +{ + static char const * roman[20] = { + "i", "ii", "iii", "iv", "v", + "vi", "vii", "viii", "ix", "x", + "xi", "xii", "xiii", "xiv", "xv", + "xvi", "xvii", "xviii", "xix", "xx" + }; + if (n < 1 || n > 20) + return "??"; + else + return roman[n-1]; +} + +} // namespace anon + +string Counters::numberlabel(string const & ctr, + string const & numbertype, + string const & langtype) +{ + ostringstream s, o; + if (numbertype == "sectioning" || numbertype == "appendix") { + if (numbertype == "appendix") { + if (langtype == "hebrew") { + o << hebrewCounter(value("chapter")); + } else { + o << alphaCounter(value("chapter")); + } + } else o << value("chapter"); + + if (ctr == "chapter") { + s << o.str(); + } else if (ctr == "section") { + s << numberlabel("chapter", numbertype, langtype) + << '.' << value("section"); + } else if (ctr == "subsection") { + s << numberlabel("section", numbertype, langtype) + << '.' << value("subsection"); + } else if (ctr == "subsubsection") { + s << numberlabel("subsection", numbertype, langtype) + << '.' << value("subsubsection"); + } else if (ctr == "paragraph") { + s << numberlabel("subsubsection", numbertype, langtype) + << '.' << value("paragraph"); + } else if (ctr == "subparagraph") { + s << numberlabel("paragraph", numbertype, langtype) + << '.' << value("subparagraph"); + } + + } else if (numbertype == "enumeration") { + string eii, eiii, eiv; + char ei; + if (langtype == "hebrew") { + ei = hebrewCounter(value("enumi")); + eii = '.' + romanCounter(value("enumii")); + eiii = '.' + alphaCounter(value("enumiii")); + eiv = '.' + value("enumiv"); + } else { + ei = loweralphaCounter(value("enumi")); + eii = romanCounter(value("enumii")) + '.'; + eiii = alphaCounter(value("enumiii")) + '.'; + eiv = value("enumiv") + '.'; + } + if (ctr == "enumi") { + s << '(' << ei << ')'; + } else if (ctr == "enumii") { + s << eii; + } else if (ctr == "enumiii") { + s << eiii; + } else if (ctr == "enumiv") { + s << eiv; + } } - it->second->step(); + return s.str(); } Index: counters.h =================================================================== RCS file: /cvs/lyx/lyx-devel/src/counters.h,v retrieving revision 1.7 diff -u -p -r1.7 counters.h --- counters.h 2002/05/29 16:20:57 1.7 +++ counters.h 2002/07/16 20:12:35 @@ -19,14 +19,10 @@ #endif #include "LString.h" - -#include <boost/signals/signal0.hpp> -#include <boost/signals/trackable.hpp> - #include <map> /// -class Counter : public boost::trackable { +class Counter { public: /// Counter(); @@ -41,10 +37,15 @@ public: /// void reset(); /// - boost::signal0<void> onstep; -private: + string master() const; /// + void setMaster(string const & m); + /// + +private: int value_; + /// + string master_; }; @@ -54,7 +55,9 @@ private: class Counters { public: /// - ~Counters(); + Counters(); + /// + //~Counters(); /// void newCounter(string const & newc); /// @@ -68,11 +71,17 @@ public: /// void step(string const & ctr); // string refstep(string const & cou); + /// + string numberlabel(string const & ctr, + string const & labeltype, + string const & langtype = "latin"); + private: /// - typedef std::map<string, Counter*> CounterList; + typedef std::map<string, Counter> CounterList; /// CounterList counterList; + }; #endif
msg41117/pgp00000.pgp
Description: PGP signature