From 23e42e320f2957a97d1a23c88e60c7cddb79fa01 Mon Sep 17 00:00:00 2001 From: nyamatongwe Date: Thu, 2 May 2013 14:51:06 +1000 Subject: Replacing raw pointers and allocations with std::vector and std::map. --- src/Editor.cxx | 4 +- src/Style.cxx | 27 ++++++-- src/Style.h | 5 +- src/ViewStyle.cxx | 200 +++++++++++++++++++++--------------------------------- src/ViewStyle.h | 31 +++++---- 5 files changed, 123 insertions(+), 144 deletions(-) (limited to 'src') diff --git a/src/Editor.cxx b/src/Editor.cxx index 9b98775c9..e209ede62 100644 --- a/src/Editor.cxx +++ b/src/Editor.cxx @@ -3854,7 +3854,7 @@ long Editor::FormatRange(bool draw, Sci_RangeToFormat *pfr) { vsPrint.braceBadLightIndicatorSet = false; // Set colours for printing according to users settings - for (size_t sty = 0; sty < vsPrint.stylesSize; sty++) { + for (size_t sty = 0; sty < vsPrint.styles.size(); sty++) { if (printColourMode == SC_PRINT_INVERTLIGHT) { vsPrint.styles[sty].fore = InvertedLight(vsPrint.styles[sty].fore); vsPrint.styles[sty].back = InvertedLight(vsPrint.styles[sty].back); @@ -8263,7 +8263,7 @@ sptr_t Editor::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) { break; case SCI_TEXTWIDTH: - PLATFORM_ASSERT(wParam < vs.stylesSize); + PLATFORM_ASSERT(wParam < vs.styles.size()); PLATFORM_ASSERT(lParam); return TextWidth(wParam, CharPtrFromSPtr(lParam)); diff --git a/src/Style.cxx b/src/Style.cxx index 375738c67..8b5b42dbf 100644 --- a/src/Style.cxx +++ b/src/Style.cxx @@ -21,7 +21,7 @@ FontAlias::FontAlias() { FontAlias::~FontAlias() { SetID(0); - // ~Font will not release the actual font resource sine it is now 0 + // ~Font will not release the actual font resource since it is now 0 } void FontAlias::MakeAlias(Font &fontOrigin) { @@ -32,12 +32,29 @@ void FontAlias::ClearFont() { SetID(0); } -bool FontSpecification::EqualTo(const FontSpecification &other) const { - return weight == other.weight && +bool FontSpecification::operator==(const FontSpecification &other) const { + return fontName == other.fontName && + weight == other.weight && italic == other.italic && size == other.size && characterSet == other.characterSet && - fontName == other.fontName; + extraFontFlag == other.extraFontFlag; +} + +bool FontSpecification::operator<(const FontSpecification &other) const { + if (fontName != other.fontName) + return fontName < other.fontName; + if (weight != other.weight) + return weight < other.weight; + if (italic != other.italic) + return italic == false; + if (size != other.size) + return size < other.size; + if (characterSet != other.characterSet) + return characterSet < other.characterSet; + if (extraFontFlag != other.extraFontFlag) + return extraFontFlag < other.extraFontFlag; + return false; } FontMeasurements::FontMeasurements() { @@ -68,6 +85,7 @@ Style::Style(const Style &source) : FontSpecification(), FontMeasurements() { weight = source.weight; italic = source.italic; size = source.size; + fontName = source.fontName; eolFilled = source.eolFilled; underline = source.underline; caseForce = source.caseForce; @@ -91,6 +109,7 @@ Style &Style::operator=(const Style &source) { weight = source.weight; italic = source.italic; size = source.size; + fontName = source.fontName; eolFilled = source.eolFilled; underline = source.underline; caseForce = source.caseForce; diff --git a/src/Style.h b/src/Style.h index 85663acf2..4bd79de14 100644 --- a/src/Style.h +++ b/src/Style.h @@ -27,7 +27,8 @@ struct FontSpecification { characterSet(0), extraFontFlag(0) { } - bool EqualTo(const FontSpecification &other) const; + bool operator==(const FontSpecification &other) const; + bool operator<(const FontSpecification &other) const; }; // Just like Font but only has a copy of the FontID so should not delete it @@ -77,7 +78,7 @@ public: const char *fontName_, int characterSet_, int weight_, bool italic_, bool eolFilled_, bool underline_, ecaseForced caseForce_, - bool visible_, bool changeable_, bool hotspot_); + bool visible_, bool changeable_, bool hotspot_); void ClearTo(const Style &source); void Copy(Font &font_, const FontMeasurements &fm_); bool IsProtected() const { return !(changeable && visible);} diff --git a/src/ViewStyle.cxx b/src/ViewStyle.cxx index 9181d9b24..9991aa02f 100644 --- a/src/ViewStyle.cxx +++ b/src/ViewStyle.cxx @@ -33,100 +33,52 @@ MarginStyle::MarginStyle() : // A list of the fontnames - avoids wasting space in each style FontNames::FontNames() { - size = 8; - names = new char *[size]; - max = 0; } FontNames::~FontNames() { Clear(); - delete []names; - names = 0; } void FontNames::Clear() { - for (int i=0; i= size) { - // Grow array - int sizeNew = size * 2; - char **namesNew = new char *[sizeNew]; - for (int j=0; j::const_iterator it=names.begin(); it != names.end(); ++it) { + if (strcmp(*it, name) == 0) { + return *it; } - delete []names; - names = namesNew; - size = sizeNew; } - names[max] = new char[strlen(name) + 1]; - strcpy(names[max], name); - max++; - return names[max-1]; + char *nameSave = new char[strlen(name) + 1]; + strcpy(nameSave, name); + names.push_back(nameSave); + return nameSave; } -FontRealised::FontRealised(const FontSpecification &fs) { - frNext = NULL; - (FontSpecification &)(*this) = fs; +FontRealised::FontRealised() { } FontRealised::~FontRealised() { font.Release(); - delete frNext; - frNext = 0; } -void FontRealised::Realise(Surface &surface, int zoomLevel, int technology) { - PLATFORM_ASSERT(fontName); - sizeZoomed = size + zoomLevel * SC_FONT_SIZE_MULTIPLIER; +void FontRealised::Realise(Surface &surface, int zoomLevel, int technology, const FontSpecification &fs) { + PLATFORM_ASSERT(fs.fontName); + sizeZoomed = fs.size + zoomLevel * SC_FONT_SIZE_MULTIPLIER; if (sizeZoomed <= 2 * SC_FONT_SIZE_MULTIPLIER) // Hangs if sizeZoomed <= 1 sizeZoomed = 2 * SC_FONT_SIZE_MULTIPLIER; float deviceHeight = surface.DeviceHeightFont(sizeZoomed); - FontParameters fp(fontName, deviceHeight / SC_FONT_SIZE_MULTIPLIER, weight, italic, extraFontFlag, technology, characterSet); + FontParameters fp(fs.fontName, deviceHeight / SC_FONT_SIZE_MULTIPLIER, fs.weight, fs.italic, fs.extraFontFlag, technology, fs.characterSet); font.Create(fp); ascent = surface.Ascent(font); descent = surface.Descent(font); aveCharWidth = surface.AverageCharWidth(font); spaceWidth = surface.WidthChar(font, ' '); - if (frNext) { - frNext->Realise(surface, zoomLevel, technology); - } -} - -FontRealised *FontRealised::Find(const FontSpecification &fs) { - if (!fs.fontName) - return this; - FontRealised *fr = this; - while (fr) { - if (fr->EqualTo(fs)) - return fr; - fr = fr->frNext; - } - return 0; -} - -void FontRealised::FindMaxAscentDescent(unsigned int &maxAscent, unsigned int &maxDescent) { - FontRealised *fr = this; - while (fr) { - if (maxAscent < fr->ascent) - maxAscent = fr->ascent; - if (maxDescent < fr->descent) - maxDescent = fr->descent; - fr = fr->frNext; - } } ViewStyle::ViewStyle() { @@ -134,9 +86,8 @@ ViewStyle::ViewStyle() { } ViewStyle::ViewStyle(const ViewStyle &source) { - frFirst = NULL; - Init(source.stylesSize); - for (unsigned int sty=0; stysecond; + } + fonts.clear(); } void ViewStyle::Init(size_t stylesSize_) { - frFirst = NULL; - stylesSize = 0; - styles = NULL; AllocStyles(stylesSize_); nextExtendedStyle = 256; fontNames.Clear(); @@ -334,52 +283,42 @@ void ViewStyle::Init(size_t stylesSize_) { braceBadLightIndicator = 0; } -void ViewStyle::CreateFont(const FontSpecification &fs) { - if (fs.fontName) { - for (FontRealised *cur=frFirst; cur; cur=cur->frNext) { - if (cur->EqualTo(fs)) - return; - if (!cur->frNext) { - cur->frNext = new FontRealised(fs); - return; - } - } - frFirst = new FontRealised(fs); +void ViewStyle::Refresh(Surface &surface) { + for (FontMap::iterator it = fonts.begin(); it != fonts.end(); ++it) { + delete it->second; } -} + fonts.clear(); -void ViewStyle::Refresh(Surface &surface) { - delete frFirst; - frFirst = NULL; selbar = Platform::Chrome(); selbarlight = Platform::ChromeHighlight(); - for (unsigned int i=0; iRealise(surface, zoomLevel, technology); + for (FontMap::iterator it = fonts.begin(); it != fonts.end(); ++it) { + it->second->Realise(surface, zoomLevel, technology, it->first); + } - for (unsigned int k=0; kFind(styles[k]); + for (unsigned int k=0; kfont, *fr); } maxAscent = 1; maxDescent = 1; - frFirst->FindMaxAscentDescent(maxAscent, maxDescent); + FindMaxAscentDescent(maxAscent, maxDescent); maxAscent += extraAscent; maxDescent += extraDescent; lineHeight = maxAscent + maxDescent; someStylesProtected = false; someStylesForceCase = false; - for (unsigned int l=0; l STYLE_DEFAULT) { - for (; i= stylesSize) { - size_t sizeNew = stylesSize * 2; - while (sizeNew <= index) - sizeNew *= 2; - AllocStyles(sizeNew); + if (index >= styles.size()) { + AllocStyles(index+1); } } @@ -449,7 +366,7 @@ void ViewStyle::ResetDefaultStyle() { void ViewStyle::ClearStyles() { // Reset all styles to be like the default style - for (unsigned int i=0; i STYLE_DEFAULT) { + for (; isecond; + FontMap::iterator it = fonts.find(fs); + if (it != fonts.end()) { + // Should always reach here since map was just set for all styles + return it->second; + } + return 0; +} + +void ViewStyle::FindMaxAscentDescent(unsigned int &maxAscent, unsigned int &maxDescent) { + for (FontMap::const_iterator it = fonts.begin(); it != fonts.end(); ++it) { + if (maxAscent < it->second->ascent) + maxAscent = it->second->ascent; + if (maxDescent < it->second->descent) + maxDescent = it->second->descent; + } +} diff --git a/src/ViewStyle.h b/src/ViewStyle.h index bd26613cb..5593c0b47 100644 --- a/src/ViewStyle.h +++ b/src/ViewStyle.h @@ -28,9 +28,7 @@ public: */ class FontNames { private: - char **names; - int size; - int max; + std::vector names; // Private so FontNames objects can not be copied FontNames(const FontNames &); @@ -41,32 +39,30 @@ public: const char *Save(const char *name); }; -class FontRealised : public FontSpecification, public FontMeasurements { +class FontRealised : public FontMeasurements { // Private so FontRealised objects can not be copied FontRealised(const FontRealised &); FontRealised &operator=(const FontRealised &); public: Font font; - FontRealised *frNext; - FontRealised(const FontSpecification &fs); + FontRealised(); virtual ~FontRealised(); - void Realise(Surface &surface, int zoomLevel, int technology); - FontRealised *Find(const FontSpecification &fs); - void FindMaxAscentDescent(unsigned int &maxAscent, unsigned int &maxDescent); + void Realise(Surface &surface, int zoomLevel, int technology, const FontSpecification &fs); }; enum IndentView {ivNone, ivReal, ivLookForward, ivLookBoth}; enum WhiteSpaceVisibility {wsInvisible=0, wsVisibleAlways=1, wsVisibleAfterIndent=2}; +typedef std::map FontMap; + /** */ class ViewStyle { -public: FontNames fontNames; - FontRealised *frFirst; - size_t stylesSize; - Style *styles; + FontMap fonts; +public: + std::vector