Refactor SharedSolver::Validate*() methods a bit more.

This commit is contained in:
Alex Wilson 2012-01-23 16:03:24 +13:00
parent 3ade12dd15
commit 1cbe95140a
2 changed files with 48 additions and 103 deletions

View File

@ -13,57 +13,13 @@
using LinearProgramming::LinearSpec; using LinearProgramming::LinearSpec;
struct LayoutOperation { struct SharedSolver::MinSizeValidator {
LayoutOperation(BObjectList<BALMLayout>* layouts) inline void CallSolverMethod(LinearSpec* spec, VariableList* vars)
:
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<BALMLayout>* fLayouts;
VariableList fVariables;
};
struct SharedSolver::MinSizeValidator : LayoutOperation {
MinSizeValidator(BObjectList<BALMLayout>* layouts)
:
LayoutOperation(layouts)
{
}
void CallSolverMethod(LinearSpec* spec, VariableList* vars)
{ {
spec->FindMins(vars); spec->FindMins(vars);
} }
void Finalize(BALMLayout* layout, SharedSolver* solver) inline void Finalize(BALMLayout* layout, SharedSolver* solver)
{ {
solver->SetMinSize(layout, BSize(layout->Right()->Value(), solver->SetMinSize(layout, BSize(layout->Right()->Value(),
layout->Bottom()->Value())); layout->Bottom()->Value()));
@ -71,19 +27,13 @@ struct SharedSolver::MinSizeValidator : LayoutOperation {
}; };
struct SharedSolver::MaxSizeValidator : LayoutOperation { struct SharedSolver::MaxSizeValidator {
MaxSizeValidator(BObjectList<BALMLayout>* layouts) inline void CallSolverMethod(LinearSpec* spec, VariableList* vars)
:
LayoutOperation(layouts)
{
}
void CallSolverMethod(LinearSpec* spec, VariableList* vars)
{ {
spec->FindMaxs(vars); spec->FindMaxs(vars);
} }
void Finalize(BALMLayout* layout, SharedSolver* solver) inline void Finalize(BALMLayout* layout, SharedSolver* solver)
{ {
solver->SetMaxSize(layout, BSize(layout->Right()->Value(), solver->SetMaxSize(layout, BSize(layout->Right()->Value(),
layout->Bottom()->Value())); layout->Bottom()->Value()));
@ -91,19 +41,13 @@ struct SharedSolver::MaxSizeValidator : LayoutOperation {
}; };
struct SharedSolver::PreferredSizeValidator : LayoutOperation { struct SharedSolver::PreferredSizeValidator {
PreferredSizeValidator(BObjectList<BALMLayout>* layouts) inline void CallSolverMethod(LinearSpec* spec, VariableList* vars)
:
LayoutOperation(layouts)
{
}
void CallSolverMethod(LinearSpec* spec, VariableList* vars)
{ {
spec->Solve(); spec->Solve();
} }
void Finalize(BALMLayout* layout, SharedSolver* solver) inline void Finalize(BALMLayout* layout, SharedSolver* solver)
{ {
float width = layout->Right()->Value() - layout->Left()->Value(); float width = layout->Right()->Value() - layout->Left()->Value();
float height = layout->Top()->Value() - layout->Bottom()->Value(); float height = layout->Top()->Value() - layout->Bottom()->Value();
@ -178,18 +122,7 @@ SharedSolver::LayoutLeaving(BALMLayout* layout)
ResultType ResultType
SharedSolver::ValidateMinSize() SharedSolver::ValidateMinSize()
{ {
if (fMinValid) _Validate<MinSizeValidator>(fMinValid, fMinResult);
return fMinResult;
_ValidateConstraints();
MinSizeValidator validator(&fLayouts);
validator.Validate(this);
fMinResult = fLinearSpec.Result();
fMinValid = true;
fLayoutValid = false;
return fMinResult; return fMinResult;
} }
@ -197,19 +130,7 @@ SharedSolver::ValidateMinSize()
ResultType ResultType
SharedSolver::ValidateMaxSize() SharedSolver::ValidateMaxSize()
{ {
if (fMaxValid) _Validate<MaxSizeValidator>(fMaxValid, fMaxResult);
return fMaxResult;
_ValidateConstraints();
MaxSizeValidator validator(&fLayouts);
validator.Validate(this);
fMaxResult = fLinearSpec.Result();
fMaxValid = true;
fLayoutValid = false;
return fMaxResult; return fMaxResult;
} }
@ -217,19 +138,7 @@ SharedSolver::ValidateMaxSize()
ResultType ResultType
SharedSolver::ValidatePreferredSize() SharedSolver::ValidatePreferredSize()
{ {
if (fPreferredValid) _Validate<PreferredSizeValidator>(fPreferredValid, fPreferredResult);
return fPreferredResult;
_ValidateConstraints();
PreferredSizeValidator validator(&fLayouts);
validator.Validate(this);
fPreferredResult = fLinearSpec.Result();
fPreferredValid = true;
fLayoutValid = false;
return fPreferredResult; return fPreferredResult;
} }
@ -305,3 +214,35 @@ SharedSolver::_ValidateConstraints()
fConstraintsValid = true; fConstraintsValid = true;
} }
} }
template <class Validator>
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;
}

View File

@ -63,6 +63,10 @@ private:
bool _IsMaxSet(); bool _IsMaxSet();
void _ValidateConstraints(); void _ValidateConstraints();
template <class Validator>
void _Validate(bool& isValid, ResultType& result);
bool fConstraintsValid; bool fConstraintsValid;
bool fMinValid; bool fMinValid;
bool fMaxValid; bool fMaxValid;