From 1cbe95140ab8834273b1ddf2bf485ea69c1de4ca Mon Sep 17 00:00:00 2001 From: Alex Wilson Date: Mon, 23 Jan 2012 16:03:24 +1300 Subject: [PATCH] Refactor SharedSolver::Validate*() methods a bit more. --- src/libs/alm/SharedSolver.cpp | 147 ++++++++++------------------------ src/libs/alm/SharedSolver.h | 4 + 2 files changed, 48 insertions(+), 103 deletions(-) diff --git a/src/libs/alm/SharedSolver.cpp b/src/libs/alm/SharedSolver.cpp index 5d5853f9b1..8cf6279ee8 100644 --- a/src/libs/alm/SharedSolver.cpp +++ b/src/libs/alm/SharedSolver.cpp @@ -13,57 +13,13 @@ using LinearProgramming::LinearSpec; -struct LayoutOperation { - LayoutOperation(BObjectList* layouts) - : - fLayouts(layouts), - fVariables(layouts->CountItems() * 2) - { - } - - virtual ~LayoutOperation() - { - } - - void Validate(SharedSolver* solver) - { - for (int32 i = fLayouts->CountItems() - 1; i >= 0; i--) { - BALMLayout* layout = fLayouts->ItemAt(i); - layout->Right()->SetRange(0, 20000); - layout->Bottom()->SetRange(0, 20000); - - fVariables.AddItem(layout->Right()); - fVariables.AddItem(layout->Bottom()); - } - - CallSolverMethod(solver->Solver(), &fVariables); - - for (int32 i = fLayouts->CountItems() - 1; i >= 0; i--) - Finalize(fLayouts->ItemAt(i), solver); - } - - virtual void CallSolverMethod(LinearSpec* spec, VariableList* vars) = 0; - virtual void Finalize(BALMLayout* layout, SharedSolver* solver) = 0; - -private: - BObjectList* fLayouts; - VariableList fVariables; -}; - - -struct SharedSolver::MinSizeValidator : LayoutOperation { - MinSizeValidator(BObjectList* layouts) - : - LayoutOperation(layouts) - { - } - - void CallSolverMethod(LinearSpec* spec, VariableList* vars) +struct SharedSolver::MinSizeValidator { + inline void CallSolverMethod(LinearSpec* spec, VariableList* vars) { spec->FindMins(vars); } - void Finalize(BALMLayout* layout, SharedSolver* solver) + inline void Finalize(BALMLayout* layout, SharedSolver* solver) { solver->SetMinSize(layout, BSize(layout->Right()->Value(), layout->Bottom()->Value())); @@ -71,19 +27,13 @@ struct SharedSolver::MinSizeValidator : LayoutOperation { }; -struct SharedSolver::MaxSizeValidator : LayoutOperation { - MaxSizeValidator(BObjectList* layouts) - : - LayoutOperation(layouts) - { - } - - void CallSolverMethod(LinearSpec* spec, VariableList* vars) +struct SharedSolver::MaxSizeValidator { + inline void CallSolverMethod(LinearSpec* spec, VariableList* vars) { spec->FindMaxs(vars); } - void Finalize(BALMLayout* layout, SharedSolver* solver) + inline void Finalize(BALMLayout* layout, SharedSolver* solver) { solver->SetMaxSize(layout, BSize(layout->Right()->Value(), layout->Bottom()->Value())); @@ -91,19 +41,13 @@ struct SharedSolver::MaxSizeValidator : LayoutOperation { }; -struct SharedSolver::PreferredSizeValidator : LayoutOperation { - PreferredSizeValidator(BObjectList* layouts) - : - LayoutOperation(layouts) - { - } - - void CallSolverMethod(LinearSpec* spec, VariableList* vars) +struct SharedSolver::PreferredSizeValidator { + inline void CallSolverMethod(LinearSpec* spec, VariableList* vars) { spec->Solve(); } - void Finalize(BALMLayout* layout, SharedSolver* solver) + inline void Finalize(BALMLayout* layout, SharedSolver* solver) { float width = layout->Right()->Value() - layout->Left()->Value(); float height = layout->Top()->Value() - layout->Bottom()->Value(); @@ -178,18 +122,7 @@ SharedSolver::LayoutLeaving(BALMLayout* layout) ResultType SharedSolver::ValidateMinSize() { - if (fMinValid) - return fMinResult; - - _ValidateConstraints(); - - MinSizeValidator validator(&fLayouts); - validator.Validate(this); - - fMinResult = fLinearSpec.Result(); - - fMinValid = true; - fLayoutValid = false; + _Validate(fMinValid, fMinResult); return fMinResult; } @@ -197,19 +130,7 @@ SharedSolver::ValidateMinSize() ResultType SharedSolver::ValidateMaxSize() { - if (fMaxValid) - return fMaxResult; - - _ValidateConstraints(); - - MaxSizeValidator validator(&fLayouts); - validator.Validate(this); - - fMaxResult = fLinearSpec.Result(); - - fMaxValid = true; - fLayoutValid = false; - + _Validate(fMaxValid, fMaxResult); return fMaxResult; } @@ -217,19 +138,7 @@ SharedSolver::ValidateMaxSize() ResultType SharedSolver::ValidatePreferredSize() { - if (fPreferredValid) - return fPreferredResult; - - _ValidateConstraints(); - - PreferredSizeValidator validator(&fLayouts); - validator.Validate(this); - - fPreferredResult = fLinearSpec.Result(); - - fPreferredValid = true; - fLayoutValid = false; - + _Validate(fPreferredValid, fPreferredResult); return fPreferredResult; } @@ -305,3 +214,35 @@ SharedSolver::_ValidateConstraints() fConstraintsValid = true; } } + + +template +void +SharedSolver::_Validate(bool& isValid, ResultType& result) +{ + if (isValid) + return; + + _ValidateConstraints(); + + VariableList variables(fLayouts.CountItems() * 2); + Validator validator; + + for (int32 i = fLayouts.CountItems() - 1; i >= 0; i--) { + BALMLayout* layout = fLayouts.ItemAt(i); + layout->Right()->SetRange(0, 20000); + layout->Bottom()->SetRange(0, 20000); + + variables.AddItem(layout->Right()); + variables.AddItem(layout->Bottom()); + } + + validator.CallSolverMethod(&fLinearSpec, &variables); + + for (int32 i = fLayouts.CountItems() - 1; i >= 0; i--) + validator.Finalize(fLayouts.ItemAt(i), this); + + result = fLinearSpec.Result(); + isValid = true; + fLayoutValid = false; +} diff --git a/src/libs/alm/SharedSolver.h b/src/libs/alm/SharedSolver.h index e5aa44342b..b9110ee0ff 100644 --- a/src/libs/alm/SharedSolver.h +++ b/src/libs/alm/SharedSolver.h @@ -63,6 +63,10 @@ private: bool _IsMaxSet(); void _ValidateConstraints(); + template + void _Validate(bool& isValid, ResultType& result); + + bool fConstraintsValid; bool fMinValid; bool fMaxValid;