diff options
author | David Tardon <dtardon@redhat.com> | 2016-02-24 14:28:13 +0100 |
---|---|---|
committer | David Tardon <dtardon@redhat.com> | 2016-02-24 14:48:32 +0100 |
commit | 9ed1791be476b8568ebe1878ff693c06c5d70006 (patch) | |
tree | 0e172822a229625bfb64516cf547c2637fddd8eb /src | |
parent | 458e616d7c67af5377bbdeddd710b7124268eb67 (diff) |
replace SW602Variable by boost::optional
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/SW602Font.cpp | 42 | ||||
-rw-r--r-- | src/lib/SW602Font.h | 84 | ||||
-rw-r--r-- | src/lib/SW602Paragraph.cpp | 62 | ||||
-rw-r--r-- | src/lib/SW602Paragraph.h | 34 | ||||
-rw-r--r-- | src/lib/SW602Types.h | 84 |
5 files changed, 125 insertions, 181 deletions
diff --git a/src/lib/SW602Font.cpp b/src/lib/SW602Font.cpp index 5d56239..42c86b3 100644 --- a/src/lib/SW602Font.cpp +++ b/src/lib/SW602Font.cpp @@ -62,7 +62,7 @@ std::ostream &operator<<(std::ostream &o, SW602Font::Line const &line) if (line.m_word) o << ":byword"; if (line.m_width < 1.0 || line.m_width > 1.0) o << ":w=" << line.m_width ; - if (line.m_color.isSet()) + if (line.m_color) o << ":col=" << line.m_color.get(); return o; } @@ -103,7 +103,7 @@ void SW602Font::Line::addTo(librevenge::RVNGPropertyList &propList, std::string default: break; } - if (m_color.isSet()) + if (m_color) { s.str(""); s << "style:text-" << type << "-color"; @@ -166,7 +166,7 @@ std::string SW602Font::getDebugString() const o << "id=" << id() << ","; } if (size() > 0) o << "sz=" << size() << ","; - if (m_deltaSpacing.isSet()) + if (m_deltaSpacing) { if (m_deltaSpacingUnit.get()==librevenge::RVNG_PERCENT) o << "extend/condensed=" << m_deltaSpacing.get() << "%,"; @@ -175,11 +175,11 @@ std::string SW602Font::getDebugString() const else if (m_deltaSpacing.get() < 0) o << "condensed=" << -m_deltaSpacing.get() << ","; } - if (m_widthStreching.isSet()) + if (m_widthStreching) o << "scaling[width]=" << m_widthStreching.get()*100.f << "%,"; - if (m_scriptPosition.isSet() && m_scriptPosition.get().isSet()) + if (m_scriptPosition && m_scriptPosition.get().isSet()) o << "script=" << m_scriptPosition.get().str(size()) << ","; - if (m_flags.isSet() && m_flags.get()) + if (m_flags && m_flags.get()) { o << "fl="; uint32_t flag = m_flags.get(); @@ -200,17 +200,17 @@ std::string SW602Font::getDebugString() const if (flag&reverseWritingBit) o << "reverseWriting:"; o << ","; } - if (m_overline.isSet() && m_overline->isSet()) + if (m_overline && m_overline->isSet()) o << "overline=[" << m_overline.get() << "],"; - if (m_strikeoutline.isSet() && m_strikeoutline->isSet()) + if (m_strikeoutline && m_strikeoutline->isSet()) o << "strikeOut=[" << m_strikeoutline.get() << "],"; - if (m_underline.isSet() && m_underline->isSet()) + if (m_underline && m_underline->isSet()) o << "underline=[" << m_underline.get() << "],"; if (hasColor()) o << "col=" << m_color.get()<< ","; - if (m_backgroundColor.isSet() && !m_backgroundColor.get().isWhite()) + if (m_backgroundColor && !m_backgroundColor.get().isWhite()) o << "backCol=" << m_backgroundColor.get() << ","; - if (m_language.isSet() && m_language.get().length()) + if (m_language && m_language.get().length()) o << "lang=" << m_language.get() << ","; o << m_extra; return o.str(); @@ -259,29 +259,29 @@ void SW602Font::addTo(librevenge::RVNGPropertyList &pList) const else if (attributeBits & engraveBit) pList.insert("style:font-relief", "engraved"); - if (m_scriptPosition.isSet() && m_scriptPosition->isSet()) + if (m_scriptPosition && m_scriptPosition->isSet()) { std::string pos=m_scriptPosition->str(fSize); if (pos.length()) pList.insert("style:text-position", pos.c_str()); } - if (m_overline.isSet() && m_overline->isSet()) + if (m_overline && m_overline->isSet()) m_overline->addTo(pList, "overline"); - if (m_strikeoutline.isSet() && m_strikeoutline->isSet()) + if (m_strikeoutline && m_strikeoutline->isSet()) m_strikeoutline->addTo(pList, "line-through"); - if (m_underline.isSet() && m_underline->isSet()) + if (m_underline && m_underline->isSet()) m_underline->addTo(pList, "underline"); if ((attributeBits & boxedBit) || (attributeBits & boxedRoundedBit)) { // do minimum: add a overline and a underline box Line simple(Line::Simple); - if (!m_overline.isSet() || !m_overline->isSet()) + if (!m_overline || !m_overline->isSet()) simple.addTo(pList, "overline"); - if (!m_underline.isSet() || !m_underline->isSet()) + if (!m_underline || !m_underline->isSet()) simple.addTo(pList, "underline"); } - if (m_deltaSpacing.isSet()) + if (m_deltaSpacing) { if (m_deltaSpacingUnit.get()==librevenge::RVNG_PERCENT) { @@ -295,7 +295,7 @@ void SW602Font::addTo(librevenge::RVNGPropertyList &pList) const else if (m_deltaSpacing.get() < 0 || m_deltaSpacing.get()>0) pList.insert("fo:letter-spacing", m_deltaSpacing.get(), librevenge::RVNG_POINT); } - if (m_widthStreching.isSet() && m_widthStreching.get() > 0.0 && + if (m_widthStreching && m_widthStreching.get() > 0.0 && (m_widthStreching.get()>1.0||m_widthStreching.get()<1.0)) pList.insert("style:text-scale", m_widthStreching.get(), librevenge::RVNG_PERCENT); if (attributeBits & reverseVideoBit) @@ -306,10 +306,10 @@ void SW602Font::addTo(librevenge::RVNGPropertyList &pList) const else { pList.insert("fo:color", m_color->str().c_str()); - if (m_backgroundColor.isSet() && !m_backgroundColor->isWhite()) + if (m_backgroundColor && !m_backgroundColor->isWhite()) pList.insert("fo:background-color", m_backgroundColor->str().c_str()); } - if (m_language.isSet()) + if (m_language) { size_t len=m_language->length(); std::string lang(m_language.get()); diff --git a/src/lib/SW602Font.h b/src/lib/SW602Font.h index 524d05f..9d25c36 100644 --- a/src/lib/SW602Font.h +++ b/src/lib/SW602Font.h @@ -13,6 +13,8 @@ #include <string> #include <vector> +#include <boost/optional.hpp> + #include "SW602Types.h" namespace libsw602 @@ -59,8 +61,8 @@ public: if (m_word != oth.m_word) return m_word ? -1 : 1; if (m_width < oth.m_width) return -1; if (m_width > oth.m_width) return 1; - if (m_color.isSet() != oth.m_color.isSet()) - return m_color.isSet(); + if (m_color != oth.m_color) + return bool(m_color); if (m_color.get() < oth.m_color.get()) return -1; if (m_color.get() > oth.m_color.get()) return 1; return 0; @@ -74,7 +76,7 @@ public: /** the width in point */ float m_width; /** the color ( if not set, we use the font color )*/ - SW602Variable<SW602Color> m_color; + boost::optional<SW602Color> m_color; }; /** a small struct to define the script position in SW602Font */ struct Script @@ -183,28 +185,38 @@ public: //! returns true if the font id is initialized bool isSet() const { - return m_id.isSet(); + return m_id; } //! inserts the set value in the current font void insert(SW602Font const &ft) { - m_id.insert(ft.m_id); - m_size.insert(ft.m_size); - m_deltaSpacing.insert(ft.m_deltaSpacing); - m_deltaSpacingUnit.insert(ft.m_deltaSpacingUnit); - m_widthStreching.insert(ft.m_widthStreching); - m_scriptPosition.insert(ft.m_scriptPosition); - if (ft.m_flags.isSet()) + if (ft.m_id) + m_id = ft.m_id; + if (ft.m_size) + m_size = ft.m_size; + if (ft.m_deltaSpacing) + m_deltaSpacing = ft.m_deltaSpacing; + if (ft.m_deltaSpacingUnit) + m_deltaSpacingUnit = ft.m_deltaSpacingUnit; + if (ft.m_widthStreching) + m_widthStreching = ft.m_widthStreching; + if (ft.m_scriptPosition) + m_scriptPosition = ft.m_scriptPosition; + if (ft.m_flags) { - if (m_flags.isSet()) + if (m_flags) setFlags(flags()| ft.flags()); else m_flags = ft.m_flags; } - m_overline.insert(ft.m_overline); - m_strikeoutline.insert(ft.m_strikeoutline); - m_underline.insert(ft.m_underline); - m_color.insert(ft.m_color); + if (ft.m_overline) + m_overline = ft.m_overline; + if (ft.m_strikeoutline) + m_strikeoutline = ft.m_strikeoutline; + if (ft.m_underline) + m_underline = ft.m_underline; + if (ft.m_color) + m_color = ft.m_color; m_extra += ft.m_extra; } //! sets the font id and resets size to the previous size for this font @@ -288,7 +300,7 @@ public: //! returns true if the font color is not black bool hasColor() const { - return m_color.isSet() && !m_color.get().isBlack(); + return m_color && !m_color.get().isBlack(); } //! returns the font color void getColor(SW602Color &c) const @@ -321,16 +333,16 @@ public: //! return true if the font has decorations line (overline, strikeout, underline) bool hasDecorationLines() const { - return (m_overline.isSet() && m_overline->isSet()) || - (m_strikeoutline.isSet() && m_strikeoutline->isSet()) || - (m_underline.isSet() && m_underline->isSet()); + return (m_overline && m_overline->isSet()) || + (m_strikeoutline && m_strikeoutline->isSet()) || + (m_underline && m_underline->isSet()); } //! reset the decoration void resetDecorationLines() { - if (m_overline.isSet()) m_overline=Line(Line::None); - if (m_strikeoutline.isSet()) m_strikeoutline=Line(Line::None); - if (m_underline.isSet()) m_underline=Line(Line::None); + if (m_overline) m_overline=Line(Line::None); + if (m_strikeoutline) m_strikeoutline=Line(Line::None); + if (m_underline) m_underline=Line(Line::None); } //! returns the overline Line const &getOverline() const @@ -509,19 +521,19 @@ public: } protected: - SW602Variable<int> m_id /** font identificator*/; - SW602Variable<float> m_size /** font size */; - SW602Variable<float> m_deltaSpacing /** expand(> 0), condensed(< 0) depl*/; - SW602Variable<librevenge::RVNGUnit> m_deltaSpacingUnit /** the delta spacing unit */; - SW602Variable<float> m_widthStreching /** the width streching in percent */; - SW602Variable<Script> m_scriptPosition /** the sub/super script definition */; - SW602Variable<uint32_t> m_flags /** font attributes */; - SW602Variable<Line> m_overline /** overline attributes */; - SW602Variable<Line> m_strikeoutline /** overline attributes */; - SW602Variable<Line> m_underline /** underline attributes */; - SW602Variable<SW602Color> m_color /** font color */; - SW602Variable<SW602Color> m_backgroundColor /** font background color */; - SW602Variable<std::string> m_language /** the language if set */; + boost::optional<int> m_id /** font identificator*/; + boost::optional<float> m_size /** font size */; + boost::optional<float> m_deltaSpacing /** expand(> 0), condensed(< 0) depl*/; + boost::optional<librevenge::RVNGUnit> m_deltaSpacingUnit /** the delta spacing unit */; + boost::optional<float> m_widthStreching /** the width streching in percent */; + boost::optional<Script> m_scriptPosition /** the sub/super script definition */; + boost::optional<uint32_t> m_flags /** font attributes */; + boost::optional<Line> m_overline /** overline attributes */; + boost::optional<Line> m_strikeoutline /** overline attributes */; + boost::optional<Line> m_underline /** underline attributes */; + boost::optional<SW602Color> m_color /** font color */; + boost::optional<SW602Color> m_backgroundColor /** font background color */; + boost::optional<std::string> m_language /** the language if set */; public: //! extra data std::string m_extra; diff --git a/src/lib/SW602Paragraph.cpp b/src/lib/SW602Paragraph.cpp index 2264692..86fdd83 100644 --- a/src/lib/SW602Paragraph.cpp +++ b/src/lib/SW602Paragraph.cpp @@ -129,8 +129,8 @@ SW602Paragraph::SW602Paragraph() : m_marginsUnit(librevenge::RVNG_INCH), m_spaci m_spacings[0] = 1.0; // interline normal for (int i = 0; i < 3; i++) { - m_margins[i].setSet(false); - m_spacings[i].setSet(false); + m_margins[i].reset(); + m_spacings[i].reset(); } } @@ -183,8 +183,8 @@ int SW602Paragraph::cmp(SW602Paragraph const ¶) const if (m_borders.size() > para.m_borders.size()) return 1; for (size_t i=0; i < m_borders.size(); i++) { - if (m_borders[i].isSet() != para.m_borders[i].isSet()) - return m_borders[i].isSet() ? 1 : -1; + if (bool(m_borders[i]) != bool(para.m_borders[i])) + return m_borders[i] ? 1 : -1; diff = m_borders[i]->compare(*(para.m_borders[i])); if (diff) return diff; } @@ -197,13 +197,18 @@ void SW602Paragraph::insert(SW602Paragraph const ¶) { for (int i = 0; i < 3; i++) { - m_margins[i].insert(para.m_margins[i]); - m_spacings[i].insert(para.m_spacings[i]); + if (para.m_margins[i]) + m_margins[i] = para.m_margins[i]; + if (para.m_spacings[i]) + m_spacings[i] = para.m_spacings[i]; } - m_marginsUnit.insert(para.m_marginsUnit); - m_spacingsInterlineUnit.insert(para.m_spacingsInterlineUnit); - m_spacingsInterlineType.insert(para.m_spacingsInterlineType); - if (para.m_tabs.isSet() && m_tabs.isSet()) + if (para.m_marginsUnit) + m_marginsUnit = para.m_marginsUnit; + if (para.m_spacingsInterlineUnit) + m_spacingsInterlineUnit = para.m_spacingsInterlineUnit; + if (para.m_spacingsInterlineType) + m_spacingsInterlineType = para.m_spacingsInterlineType; + if (para.m_tabs && m_tabs) { std::map<double, SW602TabStop> all; for (size_t t=0; t <m_tabs->size(); ++t) @@ -216,20 +221,29 @@ void SW602Paragraph::insert(SW602Paragraph const ¶) for (; it!=all.end(); ++it) m_tabs->push_back(it->second); } - else if (para.m_tabs.isSet()) + else if (para.m_tabs) m_tabs=para.m_tabs; - m_tabsRelativeToLeftMargin.insert(para.m_tabsRelativeToLeftMargin); - m_justify.insert(para.m_justify); - m_breakStatus.insert(para.m_breakStatus); - m_listLevelIndex.insert(para.m_listLevelIndex); - m_listId.insert(para.m_listId); - m_listStartValue.insert(m_listStartValue); - m_listLevel.insert(para.m_listLevel); - m_backgroundColor.insert(para.m_backgroundColor); + if (para.m_tabsRelativeToLeftMargin) + m_tabsRelativeToLeftMargin = para.m_tabsRelativeToLeftMargin; + if (para.m_justify) + m_justify = para.m_justify; + if (para.m_breakStatus) + m_breakStatus = para.m_breakStatus; + if (para.m_listLevelIndex) + m_listLevelIndex = para.m_listLevelIndex; + if (para.m_listId) + m_listId = para.m_listId; + if (m_listStartValue) + m_listStartValue = m_listStartValue; + if (para.m_listLevel) + m_listLevel = para.m_listLevel; + if (para.m_backgroundColor) + m_backgroundColor = para.m_backgroundColor; if (m_borders.size() < para.m_borders.size()) m_borders.resize(para.m_borders.size()); for (size_t i = 0; i < para.m_borders.size(); i++) - m_borders[i].insert(para.m_borders[i]); + if (para.m_borders[i]) + m_borders[i] = para.m_borders[i]; m_styleName=para.m_styleName; // checkme m_extra += para.m_extra; } @@ -238,7 +252,7 @@ bool SW602Paragraph::hasBorders() const { for (size_t i = 0; i < m_borders.size() && i < 4; i++) { - if (!m_borders[i].isSet()) + if (!m_borders[i]) continue; if (!m_borders[i]->isEmpty()) return true; @@ -252,7 +266,7 @@ bool SW602Paragraph::hasDifferentBorders() const if (m_borders.size() < 4) return true; for (size_t i = 1; i < m_borders.size(); i++) { - if (m_borders[i].isSet() != m_borders[0].isSet()) + if (bool(m_borders[i]) != bool(m_borders[0])) return true; if (*(m_borders[i]) != *(m_borders[0])) return true; @@ -306,7 +320,7 @@ void SW602Paragraph::addTo(librevenge::RVNGPropertyList &propList, bool inTable) { if (w && setAll) break; - if (!m_borders[w].isSet()) + if (!m_borders[w]) continue; SW602Border const &border = *(m_borders[w]); if (border.isEmpty()) @@ -467,7 +481,7 @@ std::ostream &operator<<(std::ostream &o, SW602Paragraph const &pp) for (size_t i = 0; i < pp.m_borders.size(); i++) { - if (!pp.m_borders[i].isSet()) + if (!pp.m_borders[i]) continue; SW602Border const &border = pp.m_borders[i].get(); if (border.isEmpty()) diff --git a/src/lib/SW602Paragraph.h b/src/lib/SW602Paragraph.h index 2622847..97bbf00 100644 --- a/src/lib/SW602Paragraph.h +++ b/src/lib/SW602Paragraph.h @@ -13,6 +13,8 @@ #include <iostream> #include <vector> +#include <boost/optional.hpp> + #include <librevenge/librevenge.h> #include "SW602List.h" @@ -97,7 +99,7 @@ public: { SW602Border empty; empty.m_style=SW602Border::None; - m_borders.resize(newSize, SW602Variable<SW602Border>(empty)); + m_borders.resize(newSize, boost::optional<SW602Border>(empty)); } //! set the interline void setInterline(double value, librevenge::RVNGUnit unit, LineSpacingType type=Fixed) @@ -119,43 +121,43 @@ public: * - 0: first line left margin * - 1: left margin * - 2: right margin*/ - SW602Variable<double> m_margins[3]; // 0: first line left, 1: left, 2: right + boost::optional<double> m_margins[3]; // 0: first line left, 1: left, 2: right /** the margins INCH, ... */ - SW602Variable<librevenge::RVNGUnit> m_marginsUnit; + boost::optional<librevenge::RVNGUnit> m_marginsUnit; /** the line spacing * * - 0: interline * - 1: before * - 2: after */ - SW602Variable<double> m_spacings[3]; // 0: interline, 1: before, 2: after + boost::optional<double> m_spacings[3]; // 0: interline, 1: before, 2: after /** the interline unit PERCENT or INCH, ... */ - SW602Variable<librevenge::RVNGUnit> m_spacingsInterlineUnit; + boost::optional<librevenge::RVNGUnit> m_spacingsInterlineUnit; /** the interline type: fixed, atLeast, ... */ - SW602Variable<LineSpacingType> m_spacingsInterlineType; + boost::optional<LineSpacingType> m_spacingsInterlineType; //! the tabulations - SW602Variable<std::vector<SW602TabStop> > m_tabs; + boost::optional<std::vector<SW602TabStop> > m_tabs; //! true if the tabs are relative to left margin, false if there are relative to the page margin (default) - SW602Variable<bool> m_tabsRelativeToLeftMargin; + boost::optional<bool> m_tabsRelativeToLeftMargin; /** the justification */ - SW602Variable<Justification> m_justify; + boost::optional<Justification> m_justify; /** a list of bits: 0x1 (unbreakable), 0x2 (do not break after) */ - SW602Variable<int> m_breakStatus; // BITS: 1: unbreakable, 2: dont break after + boost::optional<int> m_breakStatus; // BITS: 1: unbreakable, 2: dont break after /** the actual level index */ - SW602Variable<int> m_listLevelIndex; + boost::optional<int> m_listLevelIndex; /** the list id (if know ) */ - SW602Variable<int> m_listId; + boost::optional<int> m_listId; /** the list start value (if set ) */ - SW602Variable<int> m_listStartValue; + boost::optional<int> m_listStartValue; /** the actual level */ - SW602Variable<SW602ListLevel> m_listLevel; + boost::optional<SW602ListLevel> m_listLevel; //! the background color - SW602Variable<SW602Color> m_backgroundColor; + boost::optional<SW602Color> m_backgroundColor; //! list of border ( order SW602Border::Pos) - std::vector<SW602Variable<SW602Border> > m_borders; + std::vector<boost::optional<SW602Border> > m_borders; //! the style name std::string m_styleName; diff --git a/src/lib/SW602Types.h b/src/lib/SW602Types.h index 5d1eafb..885dbc1 100644 --- a/src/lib/SW602Types.h +++ b/src/lib/SW602Types.h @@ -364,90 +364,6 @@ typedef boost::shared_ptr<SW602SubDocument> SW602SubDocumentPtr; //! a smart pointer of SW602TextListener typedef boost::shared_ptr<SW602TextListener> SW602TextListenerPtr; -/** a generic variable template: value + flag to know if the variable is set - -\note the variable is considered set as soon a new value is set or -when its content is acceded by a function which returns a not-const -reference... You can use the function setSet to unset it. -*/ -template <class T> struct SW602Variable -{ - //! constructor - SW602Variable() : m_data(), m_set(false) {} - //! constructor with a default value - explicit SW602Variable(T const &def) : m_data(def), m_set(false) {} - //! copy constructor - SW602Variable(SW602Variable const &orig) : m_data(orig.m_data), m_set(orig.m_set) {} - //! copy operator - SW602Variable &operator=(SW602Variable const &orig) - { - if (this != &orig) - { - m_data = orig.m_data; - m_set = orig.m_set; - } - return *this; - } - //! set a value - SW602Variable &operator=(T const &val) - { - m_data = val; - m_set = true; - return *this; - } - //! update the current value if orig is set - void insert(SW602Variable const &orig) - { - if (orig.m_set) - { - m_data = orig.m_data; - m_set = orig.m_set; - } - } - //! operator* - T const *operator->() const - { - return &m_data; - } - /** operator* */ - T *operator->() - { - m_set = true; - return &m_data; - } - //! operator* - T const &operator*() const - { - return m_data; - } - //! operator* - T &operator*() - { - m_set = true; - return m_data; - } - //! return the current value - T const &get() const - { - return m_data; - } - //! return true if the variable is set - bool isSet() const - { - return m_set; - } - //! define if the variable is set - void setSet(bool newVal) - { - m_set=newVal; - } -protected: - //! the value - T m_data; - //! a flag to know if the variable is set or not - bool m_set; -}; - /* ---------- vec2/box2f ------------- */ /*! \class SW602Vec2 * \brief small class which defines a vector with 2 elements |