Skip to content

Commit

Permalink
RequestHandler cleanups (Eyescale#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Eilemann authored and Stefan Eilemann committed Jul 25, 2014
1 parent 412a3e8 commit 64f44c3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 55 deletions.
121 changes: 68 additions & 53 deletions lunchbox/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,54 +31,20 @@ namespace lunchbox
*/
template< class T > class Request : public Future< T >
{
class Impl : public FutureImpl< T >
{
typedef typename boost::mpl::if_< boost::is_same< T, void >,
void*, T >::type value_t;
public:
Impl( RequestHandler& handler, const uint32_t req )
: request( req )
, result( 0 )
, handler_( handler )
, done_( false )
, relinquished_( false )
{}
virtual ~Impl() {}

const uint32_t request;
value_t result;

void relinquish() { relinquished_ = true; }
bool isRelinquished() const { return relinquished_; }

protected:
T wait( const uint32_t timeout ) final;
bool isReady() const final;

private:
RequestHandler& handler_;
bool done_; //!< waitRequest finished
bool relinquished_;
};
class Impl;

public:
Request( RequestHandler& handler, const uint32_t request )
: Future< T >( new Impl( handler, request ))
{}
/** Construct a new request. */
Request( RequestHandler& handler, const uint32_t request );

/**
* Destruct and wait for completion of the request, unless relinquished.
* @version 1.9.1
*/
virtual ~Request()
{
if( !static_cast< const Impl* >( this->impl_.get( ))->isRelinquished( ))
this->wait();
}
virtual ~Request();

/** @return the identifier of the request. @version 1.9.1 */
uint32_t getID() const
{ return static_cast< const Impl* >( this->impl_.get( ))->request; }
uint32_t getID() const;

/**
* Abandon the request.
Expand All @@ -87,29 +53,62 @@ template< class T > class Request : public Future< T >
* If the future has already been resolved this function has no effect.
* @version 1.9.1
*/
void relinquish()
{ static_cast< Impl* >( this->impl_.get( ))->relinquish(); }
void relinquish();
};

}

// Implementation: Here be dragons

#include <lunchbox/requestHandler.h>
namespace lunchbox
{
template< class T > inline T Request< T >::Impl::wait(
const uint32_t timeout )
template< class T > class Request< T >::Impl : public FutureImpl< T >
{
if( !done_ )
typedef typename
boost::mpl::if_< boost::is_same< T, void >, void*, T >::type value_t;

public:
Impl( RequestHandler& handler, const uint32_t req )
: request( req )
, result( 0 )
, handler_( handler )
, done_( false )
, relinquished_( false )
{}
virtual ~Impl() {}

const uint32_t request;
value_t result;

void relinquish() { relinquished_ = true; }
bool isRelinquished() const { return relinquished_; }

protected:
T wait( const uint32_t timeout ) final
{
if( relinquished_ )
LBUNREACHABLE;
if( !done_ )
{
if( relinquished_ )
LBUNREACHABLE;

if ( !handler_.waitRequest( request, result, timeout ))
throw FutureTimeout();
done_ = true;
}
return result;
}

if ( !handler_.waitRequest( request, result, timeout ))
throw FutureTimeout();
done_ = true;
bool isReady() const final
{
return done_ || ( !relinquished_ && handler_.isRequestReady( request ));
}
return result;
}

private:
RequestHandler& handler_;
bool done_; //!< waitRequest finished
bool relinquished_;
};

template<> inline void Request< void >::Impl::wait( const uint32_t timeout )
{
Expand All @@ -124,9 +123,25 @@ template<> inline void Request< void >::Impl::wait( const uint32_t timeout )
}
}

template< class T > inline bool Request< T >::Impl::isReady() const
template< class T > inline
Request< T >::Request( RequestHandler& handler, const uint32_t request )
: Future< T >( new Impl( handler, request ))
{}

template< class T > inline Request< T >::~Request()
{
if( !static_cast< const Impl* >( this->impl_.get( ))->isRelinquished( ))
this->wait();
}

template< class T > inline uint32_t Request< T >::getID() const
{
return static_cast< const Impl* >( this->impl_.get( ))->request;
}

template< class T > inline void Request< T >::relinquish()
{
return done_ || ( !relinquished_ && handler_.isRequestReady( request ));
static_cast< Impl* >( this->impl_.get( ))->relinquish();
}

}
Expand Down
2 changes: 1 addition & 1 deletion lunchbox/requestHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class RequestHandler : public boost::noncopyable
* @version 1.0
* @deprecated use the future-based registerRequest()
*/
LUNCHBOX_API uint32_t registerRequest( void* data = 0 ) LB_DEPRECATED
uint32_t registerRequest( void* data = 0 ) LB_DEPRECATED
{ return _register( data ); }

/**
Expand Down
1 change: 0 additions & 1 deletion lunchbox/uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ class URI
const std::string& getURI() const { return _uri; }

private:

std::string _uri;
URIData _uriData;
};
Expand Down

0 comments on commit 64f44c3

Please sign in to comment.