Skip to content

Commit

Permalink
Widgets|libcore: Added a zero constant rule that can be used everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Nov 22, 2016
1 parent 171c1d0 commit ec35124
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
7 changes: 2 additions & 5 deletions doomsday/sdk/libappfw/src/gridlayout.cpp
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -117,7 +115,6 @@ DENG2_PIMPL(GridLayout)
releaseRef(fixedCellHeight);
releaseRef(colPad);
releaseRef(rowPad);
releaseRef(zeroRule);
releaseRef(totalWidth);
releaseRef(totalHeight);
releaseRef(publicWidth);
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions doomsday/sdk/libcore/include/de/widgets/constantrule.h
Expand Up @@ -69,6 +69,8 @@ class DENG2_PUBLIC ConstantRule : public Rule

String description() const;

static ConstantRule const &zero();

protected:
void update();

Expand All @@ -79,12 +81,14 @@ class DENG2_PUBLIC ConstantRule : public Rule
template <typename Type>
ConstantRule::Builder<Type>::operator Rule const &() const
{
if (fequal(float(_number), 0)) return ConstantRule::zero();
return *refless(new ConstantRule(_number));
}

template <typename Type>
ConstantRule::Builder<Type>::operator RefArg<Rule>() const
{
if (fequal(float(_number), 0)) return RefArg<Rule>(ConstantRule::zero());
return RefArg<Rule>(new ConstantRule(_number));
}

Expand Down
7 changes: 7 additions & 0 deletions doomsday/sdk/libcore/src/widgets/constantrule.cpp
Expand Up @@ -22,6 +22,8 @@

namespace de {

static AutoRef<ConstantRule> zeroRule(new ConstantRule(0));

ConstantRule::ConstantRule() : Rule(), _pendingValue(0)
{
// No valid value defined.
Expand All @@ -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);
Expand Down
20 changes: 4 additions & 16 deletions doomsday/sdk/libcore/src/widgets/rulebank.cpp
Expand Up @@ -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</small>
* http://www.gnu.org/licenses</small>
*/

#include "de/RuleBank"
Expand All @@ -22,7 +22,7 @@

namespace de {

DENG2_PIMPL(RuleBank)
DENG2_PIMPL_NOREF(RuleBank)
{
struct RuleSource : public ISource
{
Expand All @@ -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)
Expand All @@ -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<Impl::RuleData &>(data(path)).rule;
}

Expand Down

0 comments on commit ec35124

Please sign in to comment.