Lars Gullik Bjønnes wrote:

> Georg Baum <[EMAIL PROTECTED]>
> writes:
> 
> | -           transform[string(entry.lyxname)] = int(entry.lcolor);
> | +           LyXTransform[string(entry.lyxname)] = int(entry.lcolor);
> | +           LaTeXTransform[string(entry.latexname)] = int(entry.lcolor);
> 
> Can you please change the names of these variables.

Would lyxcolors and latexcolors be OK? Please make a suggestion if not,
because I don't really know what you want.

> is the "string()" cast really needed?

OK, I removed lots of casts. Would the attached be OK?
BTW, I also tried the multi_index variant because I was curious. The idea
seems nice, but too much code IMHO (or I missed something).


Georg
Index: src/LColor.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/LColor.h,v
retrieving revision 1.44
diff -u -p -r1.44 LColor.h
--- src/LColor.h	19 Jan 2005 15:03:27 -0000	1.44
+++ src/LColor.h	15 Jun 2005 14:39:58 -0000
@@ -218,6 +218,8 @@ public:
 	LColor::color getFromGUIName(std::string const & guiname) const;
 	/// \returns the LColor::color associated with the LyX name.
 	LColor::color getFromLyXName(std::string const & lyxname) const;
+	/// \returns the LColor::color associated with the LaTeX name.
+	LColor::color getFromLaTeXName(std::string const & latexname) const;
 private:
 	///
 	void addColor(LColor::color c, std::string const & lyxname) const;
Index: src/LColor.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/LColor.C,v
retrieving revision 1.55
diff -u -p -r1.55 LColor.C
--- src/LColor.C	19 Jan 2005 15:03:27 -0000	1.55
+++ src/LColor.C	15 Jun 2005 14:40:03 -0000
@@ -33,7 +33,7 @@ using std::string;
 namespace {
 
 struct ColorEntry {
-	int lcolor;
+	LColor::color lcolor;
 	char const * guiname;
 	char const * latexname;
 	char const * x11name;
@@ -61,22 +61,25 @@ public:
 	void fill(ColorEntry const & entry)
 	{
 		information in;
-		in.lyxname   = string(entry.lyxname);
-		in.latexname = string(entry.latexname);
-		in.x11name   = string(entry.x11name);
-		in.guiname   = string(entry.guiname);
+		in.lyxname   = entry.lyxname;
+		in.latexname = entry.latexname;
+		in.x11name   = entry.x11name;
+		in.guiname   = entry.guiname;
 		infotab[entry.lcolor] = in;
-		transform[string(entry.lyxname)] = int(entry.lcolor);
+		lyxcolors[entry.lyxname] = entry.lcolor;
+		latexcolors[entry.latexname] = entry.lcolor;
 	}
 
 	///
-	typedef std::map<int, information> InfoTab;
+	typedef std::map<LColor::color, information> InfoTab;
 	/// the table of color information
 	InfoTab infotab;
 
-	typedef std::map<string, int> Transform;
-	/// the transform between colour name string and integer code.
-	Transform transform;
+	typedef std::map<string, LColor::color> Transform;
+	/// the transform between LyX color name string and integer code.
+	Transform lyxcolors;
+	/// the transform between LaTeX color name string and integer code.
+	Transform latexcolors;
 
 };
 
@@ -185,7 +188,7 @@ string const LColor::getX11Name(LColor::
 		return it->second.x11name;
 
 	lyxerr << "LyX internal error: Missing color"
-		" entry in LColor.C for " << int(c) << '\n'
+	          " entry in LColor.C for " << c << '\n'
 	       << "Using black." << endl;
 	return "black";
 }
@@ -234,15 +237,14 @@ bool LColor::setColor(LColor::color col,
 bool LColor::setColor(string const & lyxname, string const &x11name)
 {
 	string const lcname = ascii_lowercase(lyxname);
-	if (pimpl_->transform.find(lcname) == pimpl_->transform.end()) {
+	if (pimpl_->lyxcolors.find(lcname) == pimpl_->lyxcolors.end()) {
 		lyxerr[Debug::GUI]
 			<< "LColor::setColor: Unknown color \""
 		       << lyxname << '"' << endl;
 		addColor(static_cast<color>(pimpl_->infotab.size()), lcname);
 	}
 
-	return setColor(static_cast<LColor::color>(pimpl_->transform[lcname]),
-			x11name);
+	return setColor(pimpl_->lyxcolors[lcname], x11name);
 }
 
 
@@ -252,7 +254,7 @@ LColor::color LColor::getFromGUIName(str
 	Pimpl::InfoTab::const_iterator end = pimpl_->infotab.end();
 	for (; it != end; ++it) {
 		if (!compare_ascii_no_case(_(it->second.guiname), guiname))
-			return static_cast<LColor::color>(it->first);
+			return it->first;
 	}
 	return LColor::inherit;
 }
@@ -268,13 +270,25 @@ void LColor::addColor(LColor::color c, s
 LColor::color LColor::getFromLyXName(string const & lyxname) const
 {
 	string const lcname = ascii_lowercase(lyxname);
-	if (pimpl_->transform.find(lcname) == pimpl_->transform.end()) {
+	if (pimpl_->lyxcolors.find(lcname) == pimpl_->lyxcolors.end()) {
 		lyxerr << "LColor::getFromLyXName: Unknown color \""
 		       << lyxname << '"' << endl;
 		return none;
 	}
 
-	return static_cast<LColor::color>(pimpl_->transform[lcname]);
+	return pimpl_->lyxcolors[lcname];
+}
+
+
+LColor::color LColor::getFromLaTeXName(string const & latexname) const
+{
+	if (pimpl_->latexcolors.find(latexname) == pimpl_->latexcolors.end()) {
+		lyxerr << "LColor::getFromLaTeXName: Unknown color \""
+		       << latexname << '"' << endl;
+		return none;
+	}
+
+	return pimpl_->latexcolors[latexname];
 }
 
 
Index: src/LColor.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/LColor.C,v
retrieving revision 1.55
diff -u -p -r1.55 LColor.C
--- src/LColor.C	19 Jan 2005 15:03:27 -0000	1.55
+++ src/LColor.C	15 Jun 2005 13:57:54 -0000
@@ -21,7 +21,9 @@
 #include "LColor.h"
 #include "support/lstrings.h"
 
-#include <map>
+#include <boost/multi_index_container.hpp>
+#include <boost/multi_index/member.hpp>
+#include <boost/multi_index/ordered_index.hpp>
 
 using lyx::support::compare_ascii_no_case;
 using lyx::support::ascii_lowercase;
@@ -33,7 +35,7 @@ using std::string;
 namespace {
 
 struct ColorEntry {
-	int lcolor;
+	LColor::color lcolor;
 	char const * guiname;
 	char const * latexname;
 	char const * x11name;
@@ -47,6 +49,11 @@ public:
 	///
 	class information {
 	public:
+		information(ColorEntry const & e) : lcolor(e.lcolor),
+			guiname(e.guiname), latexname(e.latexname),
+			x11name(e.x11name), lyxname(e.lyxname) {}
+		/// the color number
+		LColor::color lcolor;
 		/// the name as it appears in the GUI
 		string guiname;
 		/// the name used in LaTeX
@@ -57,27 +64,44 @@ public:
 		string lyxname;
 	};
 
+	// tags for accessing the corresponding indices of ColorSet
+	struct lcolor {};
+	struct latexname {};
+	struct lyxname {};
+
+	/// A color set, indexed by lcolor, lyxname and latexname
+	typedef boost::multi_index::multi_index_container<
+		information,
+		boost::multi_index::indexed_by<
+			boost::multi_index::ordered_unique<
+				boost::multi_index::tag<lcolor>,
+				BOOST_MULTI_INDEX_MEMBER(information,
+					LColor::color, lcolor)>,
+			boost::multi_index::ordered_non_unique<
+				boost::multi_index::tag<lyxname>,
+				BOOST_MULTI_INDEX_MEMBER(information,
+					string, lyxname)>,
+			boost::multi_index::ordered_non_unique<
+				boost::multi_index::tag<latexname>,
+				BOOST_MULTI_INDEX_MEMBER(information,
+					string, latexname)>
+		>
+	> ColorSet;
+	/// color set index by color number
+	typedef ColorSet::index<lcolor>::type ColorSetByColor;
+	/// color set index by latex name
+	typedef ColorSet::index<latexname>::type ColorSetByLaTeXName;
+	/// color set index by lyx name
+	typedef ColorSet::index<lyxname>::type ColorSetByLyXName;
+
+	/// the table of color information
+	ColorSet colors;
+
 	/// initialise a color entry
 	void fill(ColorEntry const & entry)
 	{
-		information in;
-		in.lyxname   = string(entry.lyxname);
-		in.latexname = string(entry.latexname);
-		in.x11name   = string(entry.x11name);
-		in.guiname   = string(entry.guiname);
-		infotab[entry.lcolor] = in;
-		transform[string(entry.lyxname)] = int(entry.lcolor);
+		colors.insert(entry);
 	}
-
-	///
-	typedef std::map<int, information> InfoTab;
-	/// the table of color information
-	InfoTab infotab;
-
-	typedef std::map<string, int> Transform;
-	/// the transform between colour name string and integer code.
-	Transform transform;
-
 };
 
 
@@ -171,21 +195,22 @@ LColor & LColor::operator=(LColor tmp)
 
 string const LColor::getGUIName(LColor::color c) const
 {
-	Pimpl::InfoTab::const_iterator it = pimpl_->infotab.find(c);
-	if (it != pimpl_->infotab.end())
-		return _(it->second.guiname);
+	Pimpl::ColorSet::const_iterator const it =
+		pimpl_->colors.get<Pimpl::lcolor>().find(c);
+	if (it != pimpl_->colors.end())
+		return _(it->guiname);
 	return "none";
 }
 
 
 string const LColor::getX11Name(LColor::color c) const
 {
-	Pimpl::InfoTab::const_iterator it = pimpl_->infotab.find(c);
-	if (it != pimpl_->infotab.end())
-		return it->second.x11name;
-
+	Pimpl::ColorSet::const_iterator const it =
+		pimpl_->colors.get<Pimpl::lcolor>().find(c);
+	if (it != pimpl_->colors.end())
+		return it->x11name;
 	lyxerr << "LyX internal error: Missing color"
-		" entry in LColor.C for " << int(c) << '\n'
+	          " entry in LColor.C for " << c << '\n'
 	       << "Using black." << endl;
 	return "black";
 }
@@ -193,40 +218,46 @@ string const LColor::getX11Name(LColor::
 
 string const LColor::getLaTeXName(LColor::color c) const
 {
-	Pimpl::InfoTab::const_iterator it = pimpl_->infotab.find(c);
-	if (it != pimpl_->infotab.end())
-		return it->second.latexname;
+	Pimpl::ColorSet::const_iterator const it =
+		pimpl_->colors.get<Pimpl::lcolor>().find(c);
+	if (it != pimpl_->colors.end())
+		return it->latexname;
 	return "black";
 }
 
 
 string const LColor::getLyXName(LColor::color c) const
 {
-	Pimpl::InfoTab::const_iterator it = pimpl_->infotab.find(c);
-	if (it != pimpl_->infotab.end())
-		return it->second.lyxname;
+	Pimpl::ColorSet::const_iterator const it =
+		pimpl_->colors.get<Pimpl::lcolor>().find(c);
+	if (it != pimpl_->colors.end())
+		return it->lyxname;
 	return "black";
 }
 
 
 bool LColor::setColor(LColor::color col, string const & x11name)
 {
-	Pimpl::InfoTab::iterator it = pimpl_->infotab.find(col);
-	if (it == pimpl_->infotab.end()) {
+	Pimpl::ColorSet::iterator const it =
+		pimpl_->colors.get<Pimpl::lcolor>().find(col);
+	if (it == pimpl_->colors.end()) {
 		lyxerr << "Color " << col << " not found in database."
-		       << std::endl;
+		       << endl;
 		return false;
 	}
 
 	// "inherit" is returned for colors not in the database
 	// (and anyway should not be redefined)
 	if (col == none || col == inherit || col == ignore) {
-		lyxerr << "Color " << getLyXName(col)
+		lyxerr << "Color " << it->lyxname
 		       << " may not be redefined" << endl;
 		return false;
 	}
 
-	it->second.x11name = x11name;
+	// it->x11name = x11name; does not work :-(
+	Pimpl::information in = *it;
+	in.x11name = x11name;
+	pimpl_->colors.replace(it, in);
 	return true;
 }
 
@@ -234,33 +265,38 @@ bool LColor::setColor(LColor::color col,
 bool LColor::setColor(string const & lyxname, string const &x11name)
 {
 	string const lcname = ascii_lowercase(lyxname);
-	if (pimpl_->transform.find(lcname) == pimpl_->transform.end()) {
+	Pimpl::ColorSetByLyXName const & colors =
+		pimpl_->colors.get<Pimpl::lyxname>();
+	Pimpl::ColorSetByLyXName::const_iterator const it =
+		colors.find(lcname);
+	if (it == colors.end()) {
 		lyxerr[Debug::GUI]
 			<< "LColor::setColor: Unknown color \""
 		       << lyxname << '"' << endl;
-		addColor(static_cast<color>(pimpl_->infotab.size()), lcname);
+		color const c = static_cast<color>(pimpl_->colors.size());
+		addColor(c, lcname);
+		return setColor(c, x11name);
 	}
 
-	return setColor(static_cast<LColor::color>(pimpl_->transform[lcname]),
-			x11name);
+	return setColor(it->lcolor, x11name);
 }
 
 
 LColor::color LColor::getFromGUIName(string const & guiname) const
 {
-	Pimpl::InfoTab::const_iterator it = pimpl_->infotab.begin();
-	Pimpl::InfoTab::const_iterator end = pimpl_->infotab.end();
+	Pimpl::ColorSet::const_iterator it = pimpl_->colors.begin();
+	Pimpl::ColorSet::const_iterator const end = pimpl_->colors.end();
 	for (; it != end; ++it) {
-		if (!compare_ascii_no_case(_(it->second.guiname), guiname))
-			return static_cast<LColor::color>(it->first);
+		if (!compare_ascii_no_case(_(it->guiname), guiname))
+			return it->lcolor;
 	}
-	return LColor::inherit;
+	return inherit;
 }
 
 
 void LColor::addColor(LColor::color c, string const & lyxname) const
 {
-	ColorEntry ce = { c, "", "", "", lyxname.c_str() };
+	ColorEntry const ce = { c, "", "", "", lyxname.c_str() };
 	pimpl_->fill(ce);
 }
 
@@ -268,13 +304,33 @@ void LColor::addColor(LColor::color c, s
 LColor::color LColor::getFromLyXName(string const & lyxname) const
 {
 	string const lcname = ascii_lowercase(lyxname);
-	if (pimpl_->transform.find(lcname) == pimpl_->transform.end()) {
+	Pimpl::ColorSetByLyXName const & colors =
+		pimpl_->colors.get<Pimpl::lyxname>();
+	Pimpl::ColorSetByLyXName::const_iterator const it =
+		colors.find(lcname);
+	if (it == colors.end()) {
 		lyxerr << "LColor::getFromLyXName: Unknown color \""
 		       << lyxname << '"' << endl;
 		return none;
 	}
 
-	return static_cast<LColor::color>(pimpl_->transform[lcname]);
+	return it->lcolor;
+}
+
+
+LColor::color LColor::getFromLaTeXName(string const & latexname) const
+{
+	Pimpl::ColorSetByLaTeXName const & colors =
+		pimpl_->colors.get<Pimpl::latexname>();
+	Pimpl::ColorSetByLaTeXName::const_iterator const it =
+		colors.find(latexname);
+	if (it == colors.end()) {
+		lyxerr << "LColor::getFromLaTeXName: Unknown color \""
+		       << latexname << '"' << endl;
+		return none;
+	}
+
+	return it->lcolor;
 }
 
 
Index: src/LColor.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/LColor.h,v
retrieving revision 1.44
diff -u -p -r1.44 LColor.h
--- src/LColor.h	19 Jan 2005 15:03:27 -0000	1.44
+++ src/LColor.h	15 Jun 2005 13:57:54 -0000
@@ -218,6 +218,8 @@ public:
 	LColor::color getFromGUIName(std::string const & guiname) const;
 	/// \returns the LColor::color associated with the LyX name.
 	LColor::color getFromLyXName(std::string const & lyxname) const;
+	/// \returns the LColor::color associated with the LaTeX name.
+	LColor::color getFromLaTeXName(std::string const & latexname) const;
 private:
 	///
 	void addColor(LColor::color c, std::string const & lyxname) const;

Reply via email to