Native Client

pp::CompletionCallbackFactory< T, RefCount > Class Template Reference

CompletionCallbackFactory may be used to create CompletionCallback objects that are bound to member functions. More...

List of all members.

Classes

class  BackPointer
class  CallbackData
class  Dispatcher0
class  Dispatcher1
class  Dispatcher2

Public Functions

 CompletionCallbackFactory (T *object=NULL)
 ~CompletionCallbackFactory ()
void CancelAll ()
void Initialize (T *object)
T * GetObject ()
template
CompletionCallback NewCallback (Method method)
template
CompletionCallback NewRequiredCallback (Method method)
template
CompletionCallback NewOptionalCallback (Method method)
template
CompletionCallback NewCallback (Method method, const A &a)
template
CompletionCallback NewRequiredCallback (Method method, const A &a)
template
CompletionCallback NewOptionalCallback (Method method, const A &a)
template
CompletionCallback NewCallback (Method method, const A &a, const B &b)
template
CompletionCallback NewRequiredCallback (Method method, const A &a, const B &b)
template
CompletionCallback NewOptionalCallback (Method method, const A &a, const B &b)

Detailed Description

template
class pp::CompletionCallbackFactory< T, RefCount >

CompletionCallbackFactory may be used to create CompletionCallback objects that are bound to member functions.

If a factory is destroyed, then any pending callbacks will be cancelled preventing any bound member functions from being called. The CancelAll() method allows pending callbacks to be cancelled without destroying the factory.

Note: CompletionCallbackFactory isn't thread safe, but you can make it more thread-friendly by passing a thread-safe refcounting class as the second template element. However, it only guarantees safety for creating a callback from another thread, the callback itself needs to execute on the same thread as the thread that creates/destroys the factory. With this restriction, it is safe to create the CompletionCallbackFactory on the main thread, create callbacks from any thread and pass them to CallOnMainThread().

Example:

   class MyHandler {
    public:
     MyHandler() : factory_(this), offset_(0) {
     }

    void ProcessFile(const FileRef& file) {
       CompletionCallback cc = factory_.NewRequiredCallback(
           &MyHandler::DidOpen);
       int32_t rv = fio_.Open(file, PP_FileOpenFlag_Read, cc);
       CHECK(rv == PP_OK_COMPLETIONPENDING);
     }

    private:
     CompletionCallback NewCallback() {
       return factory_.NewCallback(&MyHandler::DidCompleteIO);
     }

     void DidOpen(int32_t result) {
       if (result == PP_OK) {
         // The file is open, and we can begin reading.
         offset_ = 0;
         ReadMore();
       } else {
         // Failed to open the file with error given by 'result'.
       }
     }

     void DidRead(int32_t result) {
       if (result > 0) {
         // buf_ now contains 'result' number of bytes from the file.
         ProcessBytes(buf_, result);
         offset_ += result;
         ReadMore();
       } else {
         // Done reading (possibly with an error given by 'result').
       }
     }

     void ReadMore() {
       CompletionCallback cc =
           factory_.NewOptionalCallback(&MyHandler::DidRead);
       int32_t rv = fio_.Read(offset_, buf_, sizeof(buf_),
                              cc.pp_completion_callback());
       if (rv != PP_OK_COMPLETIONPENDING)
         cc.Run(rv);
     }

     void ProcessBytes(const char* bytes, int32_t length) {
       // Do work ...
     }

     pp::CompletionCallbackFactory factory_;
     pp::FileIO fio_;
     char buf_[4096];
     int64_t offset_;
   };

Constructor and Destructor Details

template
pp::CompletionCallbackFactory< T, RefCount >::CompletionCallbackFactory ( T *  object = NULL) [inline, explicit]

This constructor creates a CompletionCallbackFactory bound to an object.

If the constructor is called without an argument, the default value of NULL is used. The user then must call Initialize() to initialize the object.

param[in] object Optional parameter. An object whose member functions are to be bound to CompletionCallbacks created by this CompletionCallbackFactory. The default value of this parameter is NULL.

template
pp::CompletionCallbackFactory< T, RefCount >::~CompletionCallbackFactory ( ) [inline]

Destructor.


Function Details

template
void pp::CompletionCallbackFactory< T, RefCount >::CancelAll ( ) [inline]

CancelAll() cancels all CompletionCallbacks allocated from this factory.

template
T* pp::CompletionCallbackFactory< T, RefCount >::GetObject ( ) [inline]

GetObject() returns the object that was passed at initialization to Intialize().

Returns:
the object passed to the constructor or Intialize().
template
void pp::CompletionCallbackFactory< T, RefCount >::Initialize ( T *  object) [inline]

Initialize() binds the CallbackFactory to a particular object.

Use this when the object is not available at CallbackFactory creation, and the NULL default is passed to the constructor. The object may only be initialized once, either by the constructor, or by a call to Initialize().

Parameters:
[in]objectThe object whose member functions are to be bound to the CompletionCallback created by this CompletionCallbackFactory.
template
template
CompletionCallback pp::CompletionCallbackFactory< T, RefCount >::NewCallback ( Method  method) [inline]

NewCallback allocates a new, single-use CompletionCallback.

The CompletionCallback must be run in order for the memory allocated by the methods to be freed. NewCallback() is equivalent to NewRequiredCallback() below.

Parameters:
[in]methodThe method to be invoked upon completion of the operation.
Returns:
A CompletionCallback.
template
template
CompletionCallback pp::CompletionCallbackFactory< T, RefCount >::NewCallback ( Method  method,
const A &  a,
const B &  b 
) [inline]

NewCallback() allocates a new, single-use CompletionCallback.

The CompletionCallback must be run in order for the memory allocated by the methods to be freed. NewCallback() is equivalent to NewRequiredCallback() below.

Parameters:
methodThe method taking the callback. Method should be of type: void (T::*)(int32_t result, const A& a, const B& b)
[in]aPassed to method when the completion callback runs.
[in]bPassed to method when the completion callback runs.
Returns:
A CompletionCallback.
template
template
CompletionCallback pp::CompletionCallbackFactory< T, RefCount >::NewCallback ( Method  method,
const A &  a 
) [inline]

NewCallback() allocates a new, single-use CompletionCallback.

The CompletionCallback must be run in order for the memory allocated by the methods to be freed. NewCallback() is equivalent to NewRequiredCallback() below.

Parameters:
[in]methodThe method to be invoked upon completion of the operation. Method should be of type: void (T::*)(int32_t result, const A& a)
[in]aPassed to method when the completion callback runs.
Returns:
A CompletionCallback.
template
template
CompletionCallback pp::CompletionCallbackFactory< T, RefCount >::NewOptionalCallback ( Method  method,
const A &  a,
const B &  b 
) [inline]

NewOptionalCallback() allocates a new, single-use CompletionCallback that might not run if the method taking it can complete synchronously.

Thus, if after passing the CompletionCallback to a Pepper method, the method does not return PP_OK_COMPLETIONPENDING, then you should manually call the CompletionCallback's Run method, or memory will be leaked.

Parameters:
[in]methodThe method taking the callback. Method should be of type: void (T::*)(int32_t result, const A& a, const B& b)
[in]aPassed to method when the completion callback runs.
[in]bPassed to method when the completion callback runs.
Returns:
A CompletionCallback.
template
template
CompletionCallback pp::CompletionCallbackFactory< T, RefCount >::NewOptionalCallback ( Method  method,
const A &  a 
) [inline]

NewOptionalCallback() allocates a new, single-use CompletionCallback that might not run if the method taking it can complete synchronously.

Thus, if after passing the CompletionCallback to a Pepper method, the method does not return PP_OK_COMPLETIONPENDING, then you should manually call the CompletionCallback's Run method, or memory will be leaked.

Parameters:
[in]methodThe method to be invoked upon completion of the operation. Method should be of type: void (T::*)(int32_t result, const A& a)
[in]aPassed to method when the completion callback runs.
Returns:
A CompletionCallback.
template
template
CompletionCallback pp::CompletionCallbackFactory< T, RefCount >::NewOptionalCallback ( Method  method) [inline]

NewOptionalCallback() allocates a new, single-use CompletionCallback that might not run if the method taking it can complete synchronously.

Thus, if after passing the CompletionCallback to a Pepper method, the method does not return PP_OK_COMPLETIONPENDING, then you should manually call the CompletionCallback's Run method, or memory will be leaked.

Parameters:
[in]methodThe method to be invoked upon completion of the operation.
Returns:
A CompletionCallback.
template
template
CompletionCallback pp::CompletionCallbackFactory< T, RefCount >::NewRequiredCallback ( Method  method,
const A &  a,
const B &  b 
) [inline]

NewRequiredCallback() allocates a new, single-use CompletionCallback that will always run.

The CompletionCallback must be run in order for the memory allocated by the methods to be freed.

Parameters:
methodThe method taking the callback. Method should be of type: void (T::*)(int32_t result, const A& a, const B& b)
[in]aPassed to method when the completion callback runs.
[in]bPassed to method when the completion callback runs.
Returns:
A CompletionCallback.
template
template
CompletionCallback pp::CompletionCallbackFactory< T, RefCount >::NewRequiredCallback ( Method  method) [inline]

NewRequiredCallback() allocates a new, single-use CompletionCallback that will always run.

The CompletionCallback must be run in order for the memory allocated by the methods to be freed.

Parameters:
[in]methodThe method to be invoked upon completion of the operation.
Returns:
A CompletionCallback.
template
template
CompletionCallback pp::CompletionCallbackFactory< T, RefCount >::NewRequiredCallback ( Method  method,
const A &  a 
) [inline]

NewRequiredCallback() allocates a new, single-use CompletionCallback that will always run.

The CompletionCallback must be run in order for the memory allocated by the methods to be freed.

Parameters:
[in]methodThe method to be invoked upon completion of the operation. Method should be of type: void (T::*)(int32_t result, const A& a)
[in]aPassed to method when the completion callback runs.
Returns:
A CompletionCallback.

The documentation for this class was generated from the following file:

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.