diff options
author | nyamatongwe <unknown> | 2013-05-02 14:51:06 +1000 |
---|---|---|
committer | nyamatongwe <unknown> | 2013-05-02 14:51:06 +1000 |
commit | 23e42e320f2957a97d1a23c88e60c7cddb79fa01 (patch) | |
tree | 3613acbf69ed21b7cfc2e92a31ad17f6a71e7300 | |
parent | 1f8862974636d649b93b514fc0ff8e5526e304b7 (diff) | |
download | scintilla-mirror-23e42e320f2957a97d1a23c88e60c7cddb79fa01.tar.gz |
Replacing raw pointers and allocations with std::vector and std::map.
-rw-r--r-- | src/Editor.cxx | 4 | ||||
-rw-r--r-- | src/Style.cxx | 27 | ||||
-rw-r--r-- | src/Style.h | 5 | ||||
-rw-r--r-- | src/ViewStyle.cxx | 200 | ||||
-rw-r--r-- | src/ViewStyle.h | 31 |
5 files changed, 123 insertions, 144 deletions
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<max; i++) { - delete []names[i]; - } - max = 0; + names.clear(); } const char *FontNames::Save(const char *name) { if (!name) return 0; - for (int i=0; i<max; i++) { - if (strcmp(names[i], name) == 0) { - return names[i]; - } - } - if (max >= size) { - // Grow array - int sizeNew = size * 2; - char **namesNew = new char *[sizeNew]; - for (int j=0; j<max; j++) { - namesNew[j] = names[j]; + + for (std::vector<char *>::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; sty<source.stylesSize; sty++) { + Init(source.styles.size()); + for (unsigned int sty=0; sty<source.styles.size(); sty++) { styles[sty] = source.styles[sty]; // Can't just copy fontname as its lifetime is relative to its owning ViewStyle styles[sty].fontName = fontNames.Save(source.styles[sty].fontName); @@ -218,16 +169,14 @@ ViewStyle::ViewStyle(const ViewStyle &source) { } ViewStyle::~ViewStyle() { - delete []styles; - styles = NULL; - delete frFirst; - frFirst = NULL; + styles.clear(); + for (FontMap::iterator it = fonts.begin(); it != fonts.end(); ++it) { + delete it->second; + } + 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; i<stylesSize; i++) { + for (unsigned int i=0; i<styles.size(); i++) { styles[i].extraFontFlag = extraFontFlag; } CreateFont(styles[STYLE_DEFAULT]); - for (unsigned int j=0; j<stylesSize; j++) { + for (unsigned int j=0; j<styles.size(); j++) { CreateFont(styles[j]); } - assert(frFirst); - frFirst->Realise(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; k<stylesSize; k++) { - FontRealised *fr = frFirst->Find(styles[k]); + for (unsigned int k=0; k<styles.size(); k++) { + FontRealised *fr = Find(styles[k]); styles[k].Copy(fr->font, *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<stylesSize; l++) { + for (unsigned int l=0; l<styles.size(); l++) { if (styles[l].IsProtected()) { someStylesProtected = true; } @@ -401,25 +340,6 @@ void ViewStyle::Refresh(Surface &surface) { textStart = marginInside ? fixedColumnWidth : leftMarginWidth; } -void ViewStyle::AllocStyles(size_t sizeNew) { - Style *stylesNew = new Style[sizeNew]; - size_t i=0; - for (; i<stylesSize; i++) { - stylesNew[i] = styles[i]; - stylesNew[i].fontName = styles[i].fontName; - } - if (stylesSize > STYLE_DEFAULT) { - for (; i<sizeNew; i++) { - if (i != STYLE_DEFAULT) { - stylesNew[i].ClearTo(styles[STYLE_DEFAULT]); - } - } - } - delete []styles; - styles = stylesNew; - stylesSize = sizeNew; -} - void ViewStyle::ReleaseAllExtendedStyles() { nextExtendedStyle = 256; } @@ -431,11 +351,8 @@ int ViewStyle::AllocateExtendedStyles(int numberStyles) { } void ViewStyle::EnsureStyle(size_t index) { - if (index >= 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<stylesSize; i++) { + for (unsigned int i=0; i<styles.size(); i++) { if (i != STYLE_DEFAULT) { styles[i].ClearTo(styles[STYLE_DEFAULT]); } @@ -470,7 +387,7 @@ bool ViewStyle::ProtectionActive() const { } bool ViewStyle::ValidStyle(size_t styleIndex) const { - return styleIndex < stylesSize; + return styleIndex < styles.size(); } void ViewStyle::CalcLargestMarkerHeight() { @@ -488,3 +405,44 @@ void ViewStyle::CalcLargestMarkerHeight() { } } } + +void ViewStyle::AllocStyles(size_t sizeNew) { + size_t i=styles.size(); + styles.resize(sizeNew); + if (styles.size() > STYLE_DEFAULT) { + for (; i<sizeNew; i++) { + if (i != STYLE_DEFAULT) { + styles[i].ClearTo(styles[STYLE_DEFAULT]); + } + } + } +} + +void ViewStyle::CreateFont(const FontSpecification &fs) { + if (fs.fontName) { + FontMap::iterator it = fonts.find(fs); + if (it == fonts.end()) { + fonts[fs] = new FontRealised(); + } + } +} + +FontRealised *ViewStyle::Find(const FontSpecification &fs) { + if (!fs.fontName) // Invalid specification so return arbitrary object + return fonts.begin()->second; + 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<char *> 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<FontSpecification, FontRealised *> FontMap; + /** */ class ViewStyle { -public: FontNames fontNames; - FontRealised *frFirst; - size_t stylesSize; - Style *styles; + FontMap fonts; +public: + std::vector<Style> styles; size_t nextExtendedStyle; LineMarker markers[MARKER_MAX + 1]; int largestMarkerHeight; @@ -143,9 +139,7 @@ public: ViewStyle(const ViewStyle &source); ~ViewStyle(); void Init(size_t stylesSize_=64); - void CreateFont(const FontSpecification &fs); void Refresh(Surface &surface); - void AllocStyles(size_t sizeNew); void ReleaseAllExtendedStyles(); int AllocateExtendedStyles(int numberStyles); void EnsureStyle(size_t index); @@ -155,6 +149,13 @@ public: bool ProtectionActive() const; bool ValidStyle(size_t styleIndex) const; void CalcLargestMarkerHeight(); +private: + void AllocStyles(size_t sizeNew); + void CreateFont(const FontSpecification &fs); + FontRealised *Find(const FontSpecification &fs); + void FindMaxAscentDescent(unsigned int &maxAscent, unsigned int &maxDescent); + // Private so can only be copied through copy constructor which ensures font names initialised correctly + ViewStyle &operator=(const ViewStyle &); }; #ifdef SCI_NAMESPACE |