Skip to content

Commit

Permalink
Merge pull request #2220 from johnhaddon/contextProcessorRefactoring
Browse files Browse the repository at this point in the history
ContextProcessor refactoring
  • Loading branch information
andrewkaufman committed Aug 9, 2017
2 parents 664d2b1 + 384c804 commit 061bae6
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 72 deletions.
6 changes: 4 additions & 2 deletions include/Gaffer/Context.h
Expand Up @@ -134,8 +134,9 @@ class Context : public IECore::RefCounted
/// Removes an entry from the context if it exists
void remove( const IECore::InternedString& name );

/// Removes an entry from the context if it exists
void removeMatching( const StringAlgo::MatchPattern& pattern );
/// Removes any entries whose names match the space separated patterns
/// provided. Matching is performed using `StringAlgo::matchMultiple()`.
void removeMatching( const StringAlgo::MatchPattern &pattern );

/// When a Shared or Borrowed value is changed behind the scenes, this method
/// must be called to notify the Context of the change.
Expand Down Expand Up @@ -263,6 +264,7 @@ class Context : public IECore::RefCounted
void setTime( float timeInSeconds );

void remove( const IECore::InternedString &name );
void removeMatching( const StringAlgo::MatchPattern &pattern );

private :

Expand Down
10 changes: 4 additions & 6 deletions include/Gaffer/ContextProcessor.h
Expand Up @@ -38,6 +38,7 @@
#define GAFFER_CONTEXTPROCESSOR_H

#include "Gaffer/ComputeNode.h"
#include "Gaffer/Context.h"

namespace Gaffer
{
Expand Down Expand Up @@ -74,13 +75,10 @@ class ContextProcessor : public BaseType
virtual void hash( const ValuePlug *output, const Context *context, IECore::MurmurHash &h ) const;
virtual void compute( ValuePlug *output, const Context *context ) const;

/// Should be called by derived class affects() methods when the input
/// affects their implementation of processContext().
void appendAffectedPlugs( DependencyNode::AffectedPlugsContainer &outputs ) const;

/// Must be implemented to return true if the input is used in `processContext()`.
virtual bool affectsContext( const Plug *input ) const = 0;
/// Must be implemented to modify context in place.
/// \todo Pass `EditableScope` here in place of `Context`.
virtual void processContext( Context *context ) const = 0;
virtual void processContext( Context::EditableScope &context ) const = 0;

private :

Expand Down
39 changes: 18 additions & 21 deletions include/Gaffer/ContextProcessor.inl
Expand Up @@ -123,27 +123,26 @@ void ContextProcessor<BaseType>::affects( const Plug *input, DependencyNode::Aff
}
}
}
}

template<typename BaseType>
void ContextProcessor<BaseType>::appendAffectedPlugs( DependencyNode::AffectedPlugsContainer &outputs ) const
{
Node *n = const_cast<Node *>( static_cast<const Node *>( this ) );
for( OutputPlugIterator it( n ); !it.done(); ++it )
if( affectsContext( input ) )
{
const ValuePlug *valuePlug = IECore::runTimeCast<const ValuePlug>( it->get() );
if( 0 == valuePlug->getName().string().compare( 0, 3, "out" ) && oppositePlug( valuePlug ) )
Node *n = const_cast<Node *>( static_cast<const Node *>( this ) );
for( OutputPlugIterator it( n ); !it.done(); ++it )
{
if( valuePlug->children().size() )
const ValuePlug *valuePlug = IECore::runTimeCast<const ValuePlug>( it->get() );
if( 0 == valuePlug->getName().string().compare( 0, 3, "out" ) && oppositePlug( valuePlug ) )
{
for( ValuePlugIterator cIt( valuePlug ); !cIt.done(); ++cIt )
if( valuePlug->children().size() )
{
outputs.push_back( cIt->get() );
for( ValuePlugIterator cIt( valuePlug ); !cIt.done(); ++cIt )
{
outputs.push_back( cIt->get() );
}
}
else
{
outputs.push_back( valuePlug );
}
}
else
{
outputs.push_back( valuePlug );
}
}
}
Expand All @@ -157,9 +156,8 @@ void ContextProcessor<BaseType>::hash( const ValuePlug *output, const Context *c
{
if( enabledPlug()->getValue() )
{
ContextPtr modifiedContext = new Context( *context, Context::Borrowed );
processContext( modifiedContext.get() );
Context::Scope scopedContext( modifiedContext.get() );
Context::EditableScope scope( context );
processContext( scope );
h = input->hash();
}
else
Expand All @@ -180,9 +178,8 @@ void ContextProcessor<BaseType>::compute( ValuePlug *output, const Context *cont
{
if( enabledPlug()->getValue() )
{
ContextPtr modifiedContext = new Context( *context, Context::Borrowed );
processContext( modifiedContext.get() );
Context::Scope scopedContext( modifiedContext.get() );
Context::EditableScope scope( context );
processContext( scope );
output->setFrom( input );
}
else
Expand Down
5 changes: 2 additions & 3 deletions include/Gaffer/ContextVariables.h
Expand Up @@ -62,11 +62,10 @@ class ContextVariables : public ContextProcessor<BaseType>
AtomicCompoundDataPlug *extraVariablesPlug();
const AtomicCompoundDataPlug *extraVariablesPlug() const;

void affects( const Plug *input, DependencyNode::AffectedPlugsContainer &outputs ) const;

protected :

virtual void processContext( Context *context ) const;
virtual bool affectsContext( const Plug *input ) const;
virtual void processContext( Context::EditableScope &context ) const;

private :

Expand Down
18 changes: 5 additions & 13 deletions include/Gaffer/ContextVariables.inl
Expand Up @@ -95,36 +95,28 @@ const AtomicCompoundDataPlug *ContextVariables<BaseType>::extraVariablesPlug() c
}

template<typename BaseType>
void ContextVariables<BaseType>::affects( const Plug *input, DependencyNode::AffectedPlugsContainer &outputs ) const
bool ContextVariables<BaseType>::affectsContext( const Plug *input ) const
{
ContextProcessor<BaseType>::affects( input, outputs );

if(
variablesPlug()->isAncestorOf( input ) ||
input == extraVariablesPlug()
)
{
ContextProcessor<BaseType>::appendAffectedPlugs( outputs );
}
return variablesPlug()->isAncestorOf( input ) || input == extraVariablesPlug();
}

template<typename BaseType>
void ContextVariables<BaseType>::processContext( Context *context ) const
void ContextVariables<BaseType>::processContext( Context::EditableScope &context ) const
{
std::string name;
for( CompoundDataPlug::MemberPlugIterator it( variablesPlug() ); !it.done(); ++it )
{
IECore::DataPtr data = variablesPlug()->memberDataAndName( it->get(), name );
if( data )
{
context->set( name, data.get() );
context.set( name, data.get() );
}
}
IECore::ConstCompoundDataPtr extraVariablesData = extraVariablesPlug()->getValue();
const IECore::CompoundDataMap &extraVariables = extraVariablesData->readable();
for( IECore::CompoundDataMap::const_iterator it = extraVariables.begin(), eIt = extraVariables.end(); it != eIt; ++it )
{
context->set( it->first, it->second.get() );
context.set( it->first, it->second.get() );
}
}

Expand Down
7 changes: 3 additions & 4 deletions include/Gaffer/DeleteContextVariables.h
Expand Up @@ -57,13 +57,12 @@ class DeleteContextVariables : public ContextProcessor<BaseType>
virtual ~DeleteContextVariables();

StringPlug *variablesPlug();
const StringPlug *variablesPlug() const;

void affects( const Plug *input, DependencyNode::AffectedPlugsContainer &outputs ) const;
const StringPlug *variablesPlug() const;

protected :

virtual void processContext( Context *context ) const;
virtual bool affectsContext( const Plug *input ) const;
virtual void processContext( Context::EditableScope &context ) const;

private :

Expand Down
13 changes: 4 additions & 9 deletions include/Gaffer/DeleteContextVariables.inl
Expand Up @@ -80,20 +80,15 @@ const StringPlug *DeleteContextVariables<BaseType>::variablesPlug() const
}

template<typename BaseType>
void DeleteContextVariables<BaseType>::affects( const Plug *input, DependencyNode::AffectedPlugsContainer &outputs ) const
bool DeleteContextVariables<BaseType>::affectsContext( const Plug *input ) const
{
ContextProcessor<BaseType>::affects( input, outputs );

if( input == variablesPlug() )
{
ContextProcessor<BaseType>::appendAffectedPlugs( outputs );
}
return input == variablesPlug();
}

template<typename BaseType>
void DeleteContextVariables<BaseType>::processContext( Context *context ) const
void DeleteContextVariables<BaseType>::processContext( Context::EditableScope &context ) const
{
context->removeMatching( variablesPlug()->getValue() );
context.removeMatching( variablesPlug()->getValue() );
}

} // namespace Gaffer
Expand Down
5 changes: 2 additions & 3 deletions include/Gaffer/TimeWarp.h
Expand Up @@ -61,11 +61,10 @@ class TimeWarp : public ContextProcessor<BaseType>
FloatPlug *offsetPlug();
const FloatPlug *offsetPlug() const;

void affects( const Plug *input, DependencyNode::AffectedPlugsContainer &outputs ) const;

protected :

virtual void processContext( Context *context ) const;
virtual bool affectsContext( const Plug *input ) const;
virtual void processContext( Context::EditableScope &context ) const;

};

Expand Down
18 changes: 7 additions & 11 deletions include/Gaffer/TimeWarp.inl
Expand Up @@ -94,25 +94,21 @@ const FloatPlug *TimeWarp<BaseType>::offsetPlug() const
}

template<typename BaseType>
void TimeWarp<BaseType>::affects( const Plug *input, DependencyNode::AffectedPlugsContainer &outputs ) const
bool TimeWarp<BaseType>::affectsContext( const Plug *input ) const
{
ContextProcessor<BaseType>::affects( input, outputs );

if( input == speedPlug() || input == offsetPlug() )
{
ContextProcessor<BaseType>::appendAffectedPlugs( outputs );
}
return input == speedPlug() || input == offsetPlug();
}

template<typename BaseType>
void TimeWarp<BaseType>::processContext( Context *context ) const
void TimeWarp<BaseType>::processContext( Context::EditableScope &context ) const
{
float frame;
{
typename TimeWarpTraits<BaseType>::TimeScope timeScope( context );
frame = context->getFrame() * speedPlug()->getValue() + offsetPlug()->getValue();
const Context *c = Context::current();
typename TimeWarpTraits<BaseType>::TimeScope timeScope( c );
frame = c->getFrame() * speedPlug()->getValue() + offsetPlug()->getValue();
}
context->setFrame( frame );
context.setFrame( frame );
}

} // namespace Gaffer
5 changes: 5 additions & 0 deletions src/Gaffer/Context.cpp
Expand Up @@ -586,6 +586,11 @@ void Context::EditableScope::remove( const IECore::InternedString &name )
m_context->remove( name );
}

void Context::EditableScope::removeMatching( const StringAlgo::MatchPattern &pattern )
{
m_context->removeMatching( pattern );
}

const Context *Context::current()
{
ContextStack &stack = g_threadContexts.local();
Expand Down

0 comments on commit 061bae6

Please sign in to comment.