|
<html><head><title>dlib C++ Library - any_function_abstract.h</title></head><body bgcolor='white'><pre> |
|
<font color='#009900'>// Copyright (C) 2011 Davis E. King (davis@dlib.net) |
|
</font><font color='#009900'>// License: Boost Software License See LICENSE.txt for the full license. |
|
</font><font color='#0000FF'>#undef</font> DLIB_AnY_FUNCTION_ABSTRACT_H_ |
|
<font color='#0000FF'>#ifdef</font> DLIB_AnY_FUNCTION_ABSTRACT_H_ |
|
|
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='any_abstract.h.html'>any_abstract.h</a>" |
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../algs.h.html'>../algs.h</a>" |
|
|
|
<font color='#0000FF'>namespace</font> dlib |
|
<b>{</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font> |
|
<font color='#0000FF'>typename</font> function_type |
|
<font color='#5555FF'>></font> |
|
<font color='#0000FF'>class</font> <b><a name='any_function'></a>any_function</b> |
|
<b>{</b> |
|
<font color='#009900'>/*! |
|
REQUIREMENTS ON function_type |
|
This type should be a function signature. Some examples are: |
|
void (int,int) // a function returning nothing and taking two ints |
|
void () // a function returning nothing and taking no arguments |
|
char (double&) // a function returning a char and taking a reference to a double |
|
|
|
The number of arguments in the function must be no greater than 10. |
|
|
|
INITIAL VALUE |
|
- is_empty() == true |
|
- for all T: contains<T>() == false |
|
|
|
WHAT THIS OBJECT REPRESENTS |
|
This object is a version of dlib::any that is restricted to containing |
|
elements which are some kind of function object with an operator() which |
|
matches the function signature defined by function_type. |
|
|
|
|
|
Here is an example: |
|
#include <iostream> |
|
#include <string> |
|
#include "dlib/any.h" |
|
using namespace std; |
|
void print_message(string str) { cout << str << endl; } |
|
|
|
int main() |
|
{ |
|
dlib::any_function<void(string)> f; |
|
f = print_message; |
|
f("hello world"); // calls print_message("hello world") |
|
} |
|
|
|
Note that any_function objects can be used to store general function |
|
objects (i.e. defined by a class with an overloaded operator()) in |
|
addition to regular global functions. |
|
!*/</font> |
|
|
|
<font color='#0000FF'>public</font>: |
|
|
|
<font color='#009900'>// This is the type of object returned by function_type functions. |
|
</font> <font color='#0000FF'>typedef</font> result_type_for_function_type result_type; |
|
<font color='#009900'>// Typedefs defining the argument types. If an argument does not exist |
|
</font> <font color='#009900'>// then it is set to void. |
|
</font> <font color='#0000FF'>typedef</font> type_of_first_argument_in_funct_type arg1_type; |
|
<font color='#0000FF'>typedef</font> type_of_second_argument_in_funct_type arg2_type; |
|
... |
|
<font color='#0000FF'>typedef</font> type_of_last_argument_in_funct_type arg10_type; |
|
<font color='#0000FF'>const</font> <font color='#0000FF'>static</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> num_args <font color='#5555FF'>=</font> total_number_of_non_void_arguments; |
|
|
|
<b><a name='any_function'></a>any_function</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font>; |
|
<font color='#009900'>/*! |
|
ensures |
|
- this object is properly initialized |
|
!*/</font> |
|
|
|
<b><a name='any_function'></a>any_function</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> any_function<font color='#5555FF'>&</font> item |
|
<font face='Lucida Console'>)</font>; |
|
<font color='#009900'>/*! |
|
ensures |
|
- copies the state of item into *this. |
|
- Note that *this and item will contain independent copies of the |
|
contents of item. That is, this function performs a deep |
|
copy and therefore does not result in *this containing |
|
any kind of reference to item. |
|
!*/</font> |
|
|
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font> <font color='#0000FF'>typename</font> T <font color='#5555FF'>></font> |
|
<b><a name='any_function'></a>any_function</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> T<font color='#5555FF'>&</font> item |
|
<font face='Lucida Console'>)</font>; |
|
<font color='#009900'>/*! |
|
ensures |
|
- #contains<T>() == true |
|
- #cast_to<T>() == item |
|
(i.e. a copy of item will be stored in *this) |
|
!*/</font> |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='clear'></a>clear</b> <font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font>; |
|
<font color='#009900'>/*! |
|
ensures |
|
- #*this will have its default value. I.e. #is_empty() == true |
|
!*/</font> |
|
|
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>></font> |
|
<font color='#0000FF'><u>bool</u></font> <b><a name='contains'></a>contains</b> <font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>; |
|
<font color='#009900'>/*! |
|
ensures |
|
- if (this object currently contains an object of type T) then |
|
- returns true |
|
- else |
|
- returns false |
|
!*/</font> |
|
|
|
<font color='#0000FF'><u>bool</u></font> <b><a name='is_empty'></a>is_empty</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>; |
|
<font color='#009900'>/*! |
|
ensures |
|
- if (this object contains any kind of object) then |
|
- returns false |
|
- else |
|
- returns true |
|
!*/</font> |
|
|
|
<font color='#0000FF'><u>bool</u></font> <b><a name='is_set'></a>is_set</b> <font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>; |
|
<font color='#009900'>/*! |
|
ensures |
|
- returns !is_empty() |
|
!*/</font> |
|
|
|
result_type <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>; |
|
<font color='#009900'>/*! |
|
requires |
|
- is_empty() == false |
|
- the signature defined by function_type takes no arguments |
|
ensures |
|
- Let F denote the function object contained within *this. Then |
|
this function performs: |
|
return F() |
|
or if result_type is void then this function performs: |
|
F() |
|
!*/</font> |
|
|
|
result_type <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> arg1_type<font color='#5555FF'>&</font> a1 |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>; |
|
<font color='#009900'>/*! |
|
requires |
|
- is_empty() == false |
|
- the signature defined by function_type takes one argument |
|
ensures |
|
- Let F denote the function object contained within *this. Then |
|
this function performs: |
|
return F(a1) |
|
or if result_type is void then this function performs: |
|
F(a1) |
|
!*/</font> |
|
|
|
result_type <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> arg1_type<font color='#5555FF'>&</font> a1, |
|
<font color='#0000FF'>const</font> arg2_type<font color='#5555FF'>&</font> a2 |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>; |
|
<font color='#009900'>/*! |
|
requires |
|
- is_empty() == false |
|
- the signature defined by function_type takes two arguments |
|
ensures |
|
- Let F denote the function object contained within *this. Then |
|
this function performs: |
|
return F(a1,a2) |
|
or if result_type is void then this function performs: |
|
F(a1,a2) |
|
!*/</font> |
|
|
|
<font color='#009900'>/* !!!!!!!!! NOTE !!!!!!!!! |
|
|
|
In addition to the above, operator() is defined for up to 10 arguments. |
|
They are not listed here because it would clutter the documentation. |
|
|
|
!!!!!!!!! NOTE !!!!!!!!! */</font> |
|
|
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>></font> |
|
T<font color='#5555FF'>&</font> <b><a name='cast_to'></a>cast_to</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font>; |
|
<font color='#009900'>/*! |
|
ensures |
|
- if (contains<T>() == true) then |
|
- returns a non-const reference to the object contained within *this |
|
- else |
|
- throws bad_any_cast |
|
!*/</font> |
|
|
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>></font> |
|
<font color='#0000FF'>const</font> T<font color='#5555FF'>&</font> <b><a name='cast_to'></a>cast_to</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>; |
|
<font color='#009900'>/*! |
|
ensures |
|
- if (contains<T>() == true) then |
|
- returns a const reference to the object contained within *this |
|
- else |
|
- throws bad_any_cast |
|
!*/</font> |
|
|
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>></font> |
|
T<font color='#5555FF'>&</font> <b><a name='get'></a>get</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font>; |
|
<font color='#009900'>/*! |
|
ensures |
|
- #is_empty() == false |
|
- #contains<T>() == true |
|
- if (contains<T>() == true) |
|
- returns a non-const reference to the object contained in *this. |
|
- else |
|
- Constructs an object of type T inside *this |
|
- Any previous object stored in this any_function object is destructed and its |
|
state is lost. |
|
- returns a non-const reference to the newly created T object. |
|
!*/</font> |
|
|
|
any_function<font color='#5555FF'>&</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>=</font> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> any_function<font color='#5555FF'>&</font> item |
|
<font face='Lucida Console'>)</font>; |
|
<font color='#009900'>/*! |
|
ensures |
|
- copies the state of item into *this. |
|
- Note that *this and item will contain independent copies of the |
|
contents of item. That is, this function performs a deep |
|
copy and therefore does not result in *this containing |
|
any kind of reference to item. |
|
!*/</font> |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='swap'></a>swap</b> <font face='Lucida Console'>(</font> |
|
any_function<font color='#5555FF'>&</font> item |
|
<font face='Lucida Console'>)</font>; |
|
<font color='#009900'>/*! |
|
ensures |
|
- swaps *this and item |
|
!*/</font> |
|
|
|
<b>}</b>; |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font> |
|
<font color='#0000FF'>typename</font> function_type |
|
<font color='#5555FF'>></font> |
|
<font color='#0000FF'>inline</font> <font color='#0000FF'><u>void</u></font> <b><a name='swap'></a>swap</b> <font face='Lucida Console'>(</font> |
|
any_function<font color='#5555FF'><</font>function_type<font color='#5555FF'>></font><font color='#5555FF'>&</font> a, |
|
any_function<font color='#5555FF'><</font>function_type<font color='#5555FF'>></font><font color='#5555FF'>&</font> b |
|
<font face='Lucida Console'>)</font> <b>{</b> a.<font color='#BB00BB'>swap</font><font face='Lucida Console'>(</font>b<font face='Lucida Console'>)</font>; <b>}</b> |
|
<font color='#009900'>/*! |
|
provides a global swap function |
|
!*/</font> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font> |
|
<font color='#0000FF'>typename</font> T, |
|
<font color='#0000FF'>typename</font> function_type |
|
<font color='#5555FF'>></font> |
|
T<font color='#5555FF'>&</font> <b><a name='any_cast'></a>any_cast</b><font face='Lucida Console'>(</font> |
|
any_function<font color='#5555FF'><</font>function_type<font color='#5555FF'>></font><font color='#5555FF'>&</font> a |
|
<font face='Lucida Console'>)</font> <b>{</b> <font color='#0000FF'>return</font> a.cast_to<font color='#5555FF'><</font>T<font color='#5555FF'>></font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <b>}</b> |
|
<font color='#009900'>/*! |
|
ensures |
|
- returns a.cast_to<T>() |
|
!*/</font> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font> |
|
<font color='#0000FF'>typename</font> T, |
|
<font color='#0000FF'>typename</font> function_type |
|
<font color='#5555FF'>></font> |
|
<font color='#0000FF'>const</font> T<font color='#5555FF'>&</font> <b><a name='any_cast'></a>any_cast</b><font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> any_function<font color='#5555FF'><</font>function_type<font color='#5555FF'>></font><font color='#5555FF'>&</font> a |
|
<font face='Lucida Console'>)</font> <b>{</b> <font color='#0000FF'>return</font> a.cast_to<font color='#5555FF'><</font>T<font color='#5555FF'>></font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <b>}</b> |
|
<font color='#009900'>/*! |
|
ensures |
|
- returns a.cast_to<T>() |
|
!*/</font> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_AnY_FUNCTION_ABSTRACT_H_ |
|
</font> |
|
|
|
</pre></body></html> |