From ec3512409f6f166939d0f0931899a4cc983ba966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Tue, 22 Nov 2016 16:08:11 +0200 Subject: [PATCH] Widgets|libcore: Added a zero constant rule that can be used everywhere --- doomsday/sdk/libappfw/src/gridlayout.cpp | 7 ++----- .../libcore/include/de/widgets/constantrule.h | 4 ++++ .../sdk/libcore/src/widgets/constantrule.cpp | 7 +++++++ doomsday/sdk/libcore/src/widgets/rulebank.cpp | 20 ++++--------------- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/doomsday/sdk/libappfw/src/gridlayout.cpp b/doomsday/sdk/libappfw/src/gridlayout.cpp index 1c33eb4f65..a769404e3c 100644 --- a/doomsday/sdk/libappfw/src/gridlayout.cpp +++ b/doomsday/sdk/libappfw/src/gridlayout.cpp @@ -43,7 +43,6 @@ DENG2_PIMPL(GridLayout) CellAlignments cellAlignment; Rule const *colPad; Rule const *rowPad; - Rule const *zeroRule; struct Metric { Rule const *fixedLength; ///< May be @c NULL. @@ -98,7 +97,6 @@ DENG2_PIMPL(GridLayout) , fixedCellHeight(0) , colPad(0) , rowPad(0) - , zeroRule(new ConstantRule(0)) , totalWidth(new ConstantRule(0)) , totalHeight(new ConstantRule(0)) , current(0) @@ -117,7 +115,6 @@ DENG2_PIMPL(GridLayout) releaseRef(fixedCellHeight); releaseRef(colPad); releaseRef(rowPad); - releaseRef(zeroRule); releaseRef(totalWidth); releaseRef(totalHeight); releaseRef(publicWidth); @@ -757,13 +754,13 @@ Rule const &GridLayout::overrideHeight() const Rule const &GridLayout::columnPadding() const { if (d->colPad) return *d->colPad; - return *d->zeroRule; + return ConstantRule::zero(); } Rule const &GridLayout::rowPadding() const { if (d->rowPad) return *d->rowPad; - return *d->zeroRule; + return ConstantRule::zero(); } void GridLayout::setCellAlignment(Vector2i const &cell, ui::Alignment cellAlign) diff --git a/doomsday/sdk/libcore/include/de/widgets/constantrule.h b/doomsday/sdk/libcore/include/de/widgets/constantrule.h index 75818d89e7..35a90986e5 100644 --- a/doomsday/sdk/libcore/include/de/widgets/constantrule.h +++ b/doomsday/sdk/libcore/include/de/widgets/constantrule.h @@ -69,6 +69,8 @@ class DENG2_PUBLIC ConstantRule : public Rule String description() const; + static ConstantRule const &zero(); + protected: void update(); @@ -79,12 +81,14 @@ class DENG2_PUBLIC ConstantRule : public Rule template ConstantRule::Builder::operator Rule const &() const { + if (fequal(float(_number), 0)) return ConstantRule::zero(); return *refless(new ConstantRule(_number)); } template ConstantRule::Builder::operator RefArg() const { + if (fequal(float(_number), 0)) return RefArg(ConstantRule::zero()); return RefArg(new ConstantRule(_number)); } diff --git a/doomsday/sdk/libcore/src/widgets/constantrule.cpp b/doomsday/sdk/libcore/src/widgets/constantrule.cpp index fe99fda068..f6d5fdb895 100644 --- a/doomsday/sdk/libcore/src/widgets/constantrule.cpp +++ b/doomsday/sdk/libcore/src/widgets/constantrule.cpp @@ -22,6 +22,8 @@ namespace de { +static AutoRef zeroRule(new ConstantRule(0)); + ConstantRule::ConstantRule() : Rule(), _pendingValue(0) { // No valid value defined. @@ -47,6 +49,11 @@ String ConstantRule::description() const return String("Constant(%1)").arg(cachedValue()); } +ConstantRule const &ConstantRule::zero() +{ + return zeroRule; +} + void ConstantRule::update() { setValue(_pendingValue); diff --git a/doomsday/sdk/libcore/src/widgets/rulebank.cpp b/doomsday/sdk/libcore/src/widgets/rulebank.cpp index 39e0f57be2..d504e945b6 100644 --- a/doomsday/sdk/libcore/src/widgets/rulebank.cpp +++ b/doomsday/sdk/libcore/src/widgets/rulebank.cpp @@ -13,7 +13,7 @@ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser * General Public License for more details. You should have received a copy of * the GNU Lesser General Public License along with this program; if not, see: - * http://www.gnu.org/licenses + * http://www.gnu.org/licenses */ #include "de/RuleBank" @@ -22,7 +22,7 @@ namespace de { -DENG2_PIMPL(RuleBank) +DENG2_PIMPL_NOREF(RuleBank) { struct RuleSource : public ISource { @@ -46,21 +46,9 @@ DENG2_PIMPL(RuleBank) RuleData(Rule *r) : rule(holdRef(r)) {} ~RuleData() { releaseRef(rule); } }; - - ConstantRule *zero; - - Impl(Public *i) : Base(i) - { - zero = new ConstantRule(0); - } - - ~Impl() - { - releaseRef(zero); - } }; -RuleBank::RuleBank() : InfoBank("RuleBank", DisableHotStorage), d(new Impl(this)) +RuleBank::RuleBank() : InfoBank("RuleBank", DisableHotStorage), d(new Impl) {} void RuleBank::addFromInfo(File const &file) @@ -72,7 +60,7 @@ void RuleBank::addFromInfo(File const &file) Rule const &RuleBank::rule(DotPath const &path) const { - if (path.isEmpty()) return *d->zero; + if (path.isEmpty()) return ConstantRule::zero(); return *static_cast(data(path)).rule; }