cont. reworking based on that idea; refactor mutation base cases

This commit is contained in:
Fischlurch 2011-06-06 03:17:42 +02:00
parent d39ae8afc6
commit 7c5f18643b
3 changed files with 127 additions and 38 deletions

View file

@ -161,12 +161,13 @@ namespace time {
}
};
template<class TI, class TAR>
struct Builder
{
static TI
buildChangedValue (TAR& target, TI const&)
buildChangedValue (TAR& target)
{
return TI(target);
}
@ -175,9 +176,18 @@ namespace time {
struct Builder<TimeSpan, TAR>
{
static TimeSpan
buildChangedValue (TAR& target, TimeSpan const& newVal)
buildChangedValue (TAR& target)
{
return TimeSpan (target, newVal.duration());
return TimeSpan (target, Duration::ZERO); /////////////TODO how to feed the "new value" duration????
}
};
template<>
struct Builder<TimeSpan, TimeSpan>
{
static TimeSpan
buildChangedValue (TimeSpan& target)
{
return target;
}
};
#ifdef LIB_TIME_TIMEQUQNT_H
@ -185,18 +195,27 @@ namespace time {
struct Builder<QuTime, TAR>
{
static QuTime
buildChangedValue (TAR& target, QuTime const&)
buildChangedValue (TAR& target)
{
return QuTime (target
,getDefaultGridFallback() //////////////////TICKET #810
);
}
};
template<>
struct Builder<QuTime, QuTime>
{
static QuTime
buildChangedValue (QuTime& target)
{
return target;
}
};
#endif
template<class TI, class TAR>
struct Adap
: Mutabor<TI>
: Mutator<TI>
, Builder<TI,TAR>
{
@ -204,12 +223,29 @@ namespace time {
imposeValueChange (TAR& target, TI const& newVal)
{
imposeChange (target,newVal);
return buildChangedValue(target,newVal);
return buildChangedValue(target);
}
static TI
imposeOffset (TAR& target, Offset const& off)
{
imposeChange (target,off);
return buildChangedValue(target);
}
static TI
imposeNudge (TAR& target, int off_by_steps)
{
imposeChange (target,off_by_steps);
return buildChangedValue(target);
}
};
template<class TI, class SRC, class TAR>
struct Policy
struct Policy;
template<class TI, class TAR>
struct Policy<TI,TI,TAR>
{
static function<TI(TI const&)>
buildChangeHandler (TAR& target)
@ -218,6 +254,26 @@ namespace time {
}
};
template<class TI, class TAR>
struct Policy<TI,Offset,TAR>
{
static function<TI(Offset const&)>
buildChangeHandler (TAR& target)
{
return bind (Adap<TI,TAR>::imposeOffset, ref(target), _1 );
}
};
template<class TI, class TAR>
struct Policy<TI,int,TAR>
{
static function<TI(int)>
buildChangeHandler (TAR& target)
{
return bind (Adap<TI,TAR>::imposeNudge, ref(target), _1 );
}
};
template<class TI>
TI
@ -423,9 +479,9 @@ namespace time {
void
Mutator<TI>::bind_to (TAR& target) const
{
setVal_ = Policy<TI,TI,TAR>::buildChangeHandler(target); //MutationPolicy<TI,TAR> ::buildChangeHandler (target);
offset_ = MutationPolicy<Offset,TAR>::buildChangeHandler (target);
nudge_ = MutationPolicy<int,TAR> ::buildChangeHandler (target);
setVal_ = Policy<TI,TI,TAR> ::buildChangeHandler (target);
offset_ = Policy<TI,Offset,TAR>::buildChangeHandler (target);
nudge_ = Policy<TI,int,TAR> ::buildChangeHandler (target);
}
template<class TI>

View file

@ -48,17 +48,6 @@ namespace time {
Mutation::~Mutation() { } // emit VTable here....
/** @internal actually force a change
* into a target time entity to mutate.
* Mutation is declared fried to TimeValue
* and thus is allowed to influence the basic
* value stored in each time entity
*/
TimeValue&
Mutation::imposeChange (TimeValue& target, TimeValue const& valueToSet)
{
return target = valueToSet;
}
@ -157,14 +146,14 @@ namespace time {
virtual void
change (Duration& target) const
{
imposeChange (target, TimeVar(target)+=adjustment_);
imposeChange (target, adjustment_);
}
virtual void
change (TimeSpan& target) const
{
imposeChange (target, TimeVar(target)+=adjustment_);
imposeChange (target, adjustment_);
}
@ -173,7 +162,7 @@ namespace time {
virtual void
change (QuTime& target) const
{
imposeChange (target, TimeVar(target)+=adjustment_);
imposeChange (target, adjustment_);
}
@ -205,7 +194,7 @@ namespace time {
/**
* concrete time value mutation:
* nudge target value by the given number of 'steps',
* relative to either the given grid.
* relative to the given grid.
*/
class NudgeMutation
: public ImposeOffsetMutation
@ -234,6 +223,8 @@ namespace time {
* @note currently the natural grid is hard wired,
* just interpreting the step parameter as
* offset in seconds.
* @see mutation#imposeChange (TimeValue, int)
* @see mutation#imposeChange (QuTime, int)
*/
class NaturalNudgeMutation
: public ClonableMutation
@ -243,31 +234,22 @@ namespace time {
virtual void
change (Duration& target) const
{
imposeChange (target, TimeVar(target)+=Time(FSecs(steps_)));
imposeChange (target, steps_);
}
virtual void
change (TimeSpan& target) const
{
imposeChange (target, TimeVar(target)+=Time(FSecs(steps_)));
imposeChange (target, steps_);
}
/** Special treatment: use the quantised time's own grid;
* retrieve the corresponding grid point, offset it by the step-parameter,
* then retrieve the corresponding time from the quantised time's
* underlying quantiser (grid).
* @note when the #steps_ parameter is zero, what happens here effectively
* is the materialisation of the quantised target time, i.e. making
* the quantisation explicit and storing the resulting value. */
/** @note special treatment: use the quantised time's own grid */
virtual void
change (QuTime& target) const
{
PQuant const& grid (target);
int64_t originalGridPoint = grid->gridPoint(target);
int64_t adjustedGridPoint = originalGridPoint + steps_;
imposeChange (target, grid->timeOf (adjustedGridPoint));
imposeChange (target, steps_);
}

View file

@ -113,6 +113,9 @@ namespace time {
protected:
static TimeValue& imposeChange (TimeValue&, TimeValue const&);
static TimeValue& imposeChange (TimeValue&, Offset const&);
static TimeValue& imposeChange (TimeValue&, int);
static TimeValue& imposeChange (QuTime&, int);
};
@ -126,5 +129,53 @@ namespace time {
#endif
/* === implementing the actual changes === */
/** @internal actually force a change into a target time entity to mutate.
* Mutation is declared fried to TimeValue and thus is allowed to influence
* the basic value stored in each time entity
*/
inline TimeValue&
Mutation::imposeChange (TimeValue& target, TimeValue const& valueToSet)
{
return target = valueToSet;
}
/** @internal variation to mutate a target time value by applying an offset */
inline TimeValue&
Mutation::imposeChange (TimeValue& target, Offset const& offset)
{
return imposeChange (target, TimeVar(target) += offset);
}
/** @internal nudge a target time value by a step wise offset.
* The standard case uses a fixed offset of 1 second per step //////////////////TICKET #810
*/
inline TimeValue&
Mutation::imposeChange (TimeValue& target, int steps)
{
return imposeChange (target, TimeVar(target) += Time(FSecs(steps)));
}
#ifdef LIB_TIME_TIMEQUQNT_H
/** @internal Special treatment for quantised target values:
* use the quantised time's own grid; retrieve the corresponding grid point,
* offset it by the step-parameter, then retrieve the corresponding time from
* the quantised time's underlying quantiser (grid) and impose that as change.
* @note when the #steps parameter is zero, what happens here effectively
* is the materialisation of the quantised target time, i.e. making
* the quantisation explicit and storing the resulting value. */
inline TimeValue&
Mutation::imposeChange (QuTime& target, int steps)
{
PQuant const& grid (target);
int64_t originalGridPoint = grid->gridPoint(target);
int64_t adjustedGridPoint = originalGridPoint + steps;
return imposeChange (target, grid->timeOf (adjustedGridPoint));
}
#endif
}} // lib::time
#endif