Lesson 35 - Get Compute Auth Token Working
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
// Copyright (C) 2007 Davis E. King (davis@dlib.net)
|
||||
// License: Boost Software License See LICENSE.txt for the full license.
|
||||
#ifndef DLIB_TIMEOUT_KERNEl_1_
|
||||
#define DLIB_TIMEOUT_KERNEl_1_
|
||||
|
||||
#include "../threads.h"
|
||||
#include "../algs.h"
|
||||
#include "../misc_api.h"
|
||||
#include "timeout_abstract.h"
|
||||
#include "../uintn.h"
|
||||
#include "../timer.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// this is to disable the "'this' : used in base member initializer list"
|
||||
// warning you get from some of the GUI objects since all the objects
|
||||
// require that their parent class be passed into their constructor.
|
||||
// In this case though it is totally safe so it is ok to disable this warning.
|
||||
#pragma warning(disable : 4355)
|
||||
#endif // _MSC_VER
|
||||
|
||||
namespace dlib
|
||||
{
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
class timeout
|
||||
{
|
||||
/*!
|
||||
INITIAL VALUE
|
||||
- b == a pointer to some kind of bind object
|
||||
|
||||
CONVENTION
|
||||
- b == a pointer to some kind of bind object
|
||||
!*/
|
||||
|
||||
class bind
|
||||
{
|
||||
public:
|
||||
virtual void go() = 0;
|
||||
virtual ~bind() {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class functor : public bind
|
||||
{
|
||||
public:
|
||||
functor(const T& f) : function(f) {}
|
||||
T function;
|
||||
void go() { function(); }
|
||||
};
|
||||
|
||||
template <typename T, typename R>
|
||||
class zero : public bind
|
||||
{
|
||||
public:
|
||||
T* object;
|
||||
R (T::*callback_function)();
|
||||
void go() { (object->*callback_function)(); }
|
||||
|
||||
};
|
||||
|
||||
template <typename T, typename R, typename U>
|
||||
class one : public bind
|
||||
{
|
||||
public:
|
||||
T* object;
|
||||
R (T::*callback_function)(U);
|
||||
U val;
|
||||
void go() { (object->*callback_function)(val); }
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
// This typedef is here for backwards compatibility with previous versions of dlib.
|
||||
typedef timeout kernel_1a;
|
||||
|
||||
template <
|
||||
typename T
|
||||
>
|
||||
timeout (
|
||||
T callback_function,
|
||||
unsigned long ms_to_timeout
|
||||
) :
|
||||
t(*this,&timeout::trigger_timeout)
|
||||
{
|
||||
b = new functor<T>(callback_function);
|
||||
t.set_delay_time(ms_to_timeout);
|
||||
t.start();
|
||||
}
|
||||
|
||||
template <
|
||||
typename T
|
||||
>
|
||||
timeout (
|
||||
T& object,
|
||||
void (T::*callback_function)(),
|
||||
unsigned long ms_to_timeout
|
||||
):
|
||||
t(*this,&timeout::trigger_timeout)
|
||||
{
|
||||
zero<T,void>* B = new zero<T,void>;
|
||||
b = B;
|
||||
B->object = &object;
|
||||
B->callback_function = callback_function;
|
||||
t.set_delay_time(ms_to_timeout);
|
||||
t.start();
|
||||
}
|
||||
|
||||
template <
|
||||
typename T,
|
||||
typename U
|
||||
>
|
||||
timeout (
|
||||
T& object,
|
||||
void (T::*callback_function)(U callback_function_argument),
|
||||
unsigned long ms_to_timeout,
|
||||
U callback_function_argument
|
||||
):
|
||||
t(*this,&timeout::trigger_timeout)
|
||||
{
|
||||
one<T,void,U>* B = new one<T,void,U>;
|
||||
b = B;
|
||||
B->object = &object;
|
||||
B->callback_function = callback_function;
|
||||
B->val = callback_function_argument;
|
||||
t.set_delay_time(ms_to_timeout);
|
||||
t.start();
|
||||
}
|
||||
|
||||
template <
|
||||
typename T
|
||||
>
|
||||
timeout (
|
||||
T& object,
|
||||
int (T::*callback_function)(),
|
||||
unsigned long ms_to_timeout
|
||||
):
|
||||
t(*this,&timeout::trigger_timeout)
|
||||
{
|
||||
zero<T,int>* B = new zero<T,int>;
|
||||
b = B;
|
||||
B->object = &object;
|
||||
B->callback_function = callback_function;
|
||||
t.set_delay_time(ms_to_timeout);
|
||||
t.start();
|
||||
}
|
||||
|
||||
template <
|
||||
typename T,
|
||||
typename U
|
||||
>
|
||||
timeout (
|
||||
T& object,
|
||||
int (T::*callback_function)(U callback_function_argument),
|
||||
unsigned long ms_to_timeout,
|
||||
U callback_function_argument
|
||||
):
|
||||
t(*this,&timeout::trigger_timeout)
|
||||
{
|
||||
one<T,int,U>* B = new one<T,int,U>;
|
||||
b = B;
|
||||
B->object = &object;
|
||||
B->callback_function = callback_function;
|
||||
B->val = callback_function_argument;
|
||||
t.set_delay_time(ms_to_timeout);
|
||||
t.start();
|
||||
}
|
||||
|
||||
virtual ~timeout (
|
||||
)
|
||||
{
|
||||
t.stop_and_wait();
|
||||
delete b;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void trigger_timeout ()
|
||||
{
|
||||
b->go();
|
||||
t.stop();
|
||||
}
|
||||
|
||||
dlib::timer<timeout> t;
|
||||
bind* b;
|
||||
|
||||
// restricted functions
|
||||
timeout(const timeout&); // copy constructor
|
||||
timeout& operator=(const timeout&); // assignment operator
|
||||
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
|
||||
#endif // DLIB_TIMEOUT_KERNEl_1_
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
// Copyright (C) 2007 Davis E. King (davis@dlib.net)
|
||||
// License: Boost Software License See LICENSE.txt for the full license.
|
||||
#undef DLIB_TIMEOUT_KERNEl_ABSTRACT_
|
||||
#ifdef DLIB_TIMEOUT_KERNEl_ABSTRACT_
|
||||
|
||||
#include "../threads.h"
|
||||
|
||||
namespace dlib
|
||||
{
|
||||
|
||||
class timeout
|
||||
{
|
||||
/*!
|
||||
WHAT THIS OBJECT REPRESENTS
|
||||
This object provides a simple way to implement a timeout. An example will
|
||||
make its use clear. Suppose we want to read from a socket but we want to
|
||||
terminate the connection if the read takes longer than 10 seconds. This
|
||||
could be accomplished as follows:
|
||||
|
||||
connection* con = a connection from somewhere;
|
||||
{
|
||||
// setup a timer that will call con->shutdown() in 10 seconds
|
||||
timeout t(*con,&connection::shutdown,10000);
|
||||
// Now call read on the connection. If this call to read() takes more
|
||||
// than 10 seconds then the t timeout will trigger and shutdown the
|
||||
// connection. If read completes in less than 10 seconds then the t
|
||||
// object will be destructed on the next line due to the } and then the
|
||||
// timeout won't trigger.
|
||||
con->read(buf,100);
|
||||
}
|
||||
|
||||
|
||||
Alternatively, if you have a compiler capable of using C++11 lambda
|
||||
functions, you can use a syntax like this:
|
||||
{
|
||||
timeout t([con](){ con->shutdown(); }, 10000);
|
||||
con->read(buf,100);
|
||||
}
|
||||
|
||||
More generally, you can use this with things other than sockets. For
|
||||
example, the following statement will print "Hello world!" after 1000ms:
|
||||
timeout t([](){ cout << "Hello world!" << endl; }, 1000);
|
||||
|
||||
|
||||
|
||||
THREAD SAFETY
|
||||
All methods of this class are thread safe.
|
||||
!*/
|
||||
|
||||
public:
|
||||
|
||||
template <
|
||||
typename T
|
||||
>
|
||||
timeout (
|
||||
T callback_function,
|
||||
unsigned long ms_to_timeout
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- callback_function does not throw
|
||||
ensures
|
||||
- does not block.
|
||||
- #*this is properly initialized
|
||||
- if (this object isn't destructed in ms_to_timeout milliseconds) then
|
||||
- callback_function() will be called in ms_to_timeout milliseconds.
|
||||
throws
|
||||
- std::bad_alloc
|
||||
- dlib::thread_error
|
||||
!*/
|
||||
|
||||
template <
|
||||
typename T
|
||||
>
|
||||
timeout (
|
||||
T& object,
|
||||
void (T::*callback_function)(),
|
||||
unsigned long ms_to_timeout
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- callback_function does not throw
|
||||
ensures
|
||||
- does not block.
|
||||
- #*this is properly initialized
|
||||
- if (this object isn't destructed in ms_to_timeout milliseconds) then
|
||||
- (object.*callback_function)() will be called in ms_to_timeout
|
||||
milliseconds.
|
||||
throws
|
||||
- std::bad_alloc
|
||||
- dlib::thread_error
|
||||
!*/
|
||||
|
||||
template <
|
||||
typename T,
|
||||
typename U
|
||||
>
|
||||
timeout (
|
||||
T& object,
|
||||
void (T::*callback_function)(U callback_function_argument),
|
||||
unsigned long ms_to_timeout,
|
||||
U callback_function_argument
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- callback_function does not throw
|
||||
ensures
|
||||
- does not block.
|
||||
- #*this is properly initialized
|
||||
- if (this object isn't destructed in ms_to_timeout milliseconds) then
|
||||
- (object.*callback_function)(callback_function_argument) will be
|
||||
called in ms_to_timeout milliseconds.
|
||||
throws
|
||||
- std::bad_alloc
|
||||
- dlib::thread_error
|
||||
!*/
|
||||
|
||||
template <
|
||||
typename T
|
||||
>
|
||||
timeout (
|
||||
T& object,
|
||||
int (T::*callback_function)(),
|
||||
unsigned long ms_to_timeout
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- callback_function does not throw
|
||||
ensures
|
||||
- does not block.
|
||||
- #*this is properly initialized
|
||||
- if (this object isn't destructed in ms_to_timeout milliseconds) then
|
||||
- (object.*callback_function)() will be called in ms_to_timeout
|
||||
milliseconds.
|
||||
throws
|
||||
- std::bad_alloc
|
||||
- dlib::thread_error
|
||||
!*/
|
||||
|
||||
template <
|
||||
typename T,
|
||||
typename U
|
||||
>
|
||||
timeout (
|
||||
T& object,
|
||||
int (T::*callback_function)(U callback_function_argument),
|
||||
unsigned long ms_to_timeout,
|
||||
U callback_function_argument
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- callback_function does not throw
|
||||
ensures
|
||||
- does not block.
|
||||
- #*this is properly initialized
|
||||
- if (this object isn't destructed in ms_to_timeout milliseconds) then
|
||||
- (object.*callback_function)(callback_function_argument) will be
|
||||
called in ms_to_timeout milliseconds.
|
||||
throws
|
||||
- std::bad_alloc
|
||||
- dlib::thread_error
|
||||
!*/
|
||||
|
||||
virtual ~timeout (
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- is not called from inside the callback_function given to the
|
||||
constructor.
|
||||
ensures
|
||||
- any resources associated with *this have been released
|
||||
- if (the callback_function hasn't been called yet) then
|
||||
- the callback_function specified in the constructor will not be called
|
||||
!*/
|
||||
|
||||
private:
|
||||
|
||||
// restricted functions
|
||||
timeout(const timeout&); // copy constructor
|
||||
timeout& operator=(const timeout&); // assignment operator
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // DLIB_TIMEOUT_KERNEl_ABSTRACT_
|
||||
|
||||
|
||||
Reference in New Issue
Block a user