Commit 72d9adbf authored by Davis King's avatar Davis King

Changed the member function pointer object so that:

   - It never calls new or delete
   - It can point to const member functions
   - It has an operator bool and operator! so that it can now
     be used in an if statement like a normal pointer

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402614
parent 7016a876
...@@ -16,17 +16,6 @@ namespace dlib ...@@ -16,17 +16,6 @@ namespace dlib
> >
class member_function_pointer; class member_function_pointer;
template <
typename PARAM1,
typename PARAM2,
typename PARAM3,
typename PARAM4
>
void swap (
member_function_pointer<PARAM1,PARAM2,PARAM3,PARAM4>& a,
member_function_pointer<PARAM1,PARAM2,PARAM3,PARAM4>& b
) { a.swap(b); }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template <> template <>
...@@ -53,9 +42,9 @@ namespace dlib ...@@ -53,9 +42,9 @@ namespace dlib
member_function_pointer<> my_pointer; member_function_pointer<> my_pointer;
To use a pointer to a function that takes a single int you would say: To use a pointer to a function that takes a single int you would say:
member_function_pointer<int> my_pointer; member_function_pointer<int> my_pointer;
To use a pointer to a function that takes an int and then a string To use a pointer to a function that takes an int and then a reference
you would say: to a string you would say:
member_function_pointer<int,string> my_pointer; member_function_pointer<int,string&> my_pointer;
Also note that the formal comments are only present for the first Also note that the formal comments are only present for the first
template specialization. They are all exactly the same except for the template specialization. They are all exactly the same except for the
...@@ -160,6 +149,38 @@ namespace dlib ...@@ -160,6 +149,38 @@ namespace dlib
If this exception is thrown then #is_set() == false If this exception is thrown then #is_set() == false
!*/ !*/
template <
typename T
>
void set (
const T& object,
void (T::*cb)()const
);
/*!
requires
- cb == a valid member function pointer for class T
ensures
- #is_set() == true
- calls to this->operator() will call (object.*cb)()
throws
- std::bad_alloc
If this exception is thrown then #is_set() == false
!*/
operator bool (
) const;
/*!
ensures
- returns is_set()
!*/
bool operator! (
) const;
/*!
ensures
- returns !is_set()
!*/
void operator () ( void operator () (
) const; ) const;
/*! /*!
...@@ -229,6 +250,18 @@ namespace dlib ...@@ -229,6 +250,18 @@ namespace dlib
void (T::*cb)(PARAM1) void (T::*cb)(PARAM1)
); );
template <typename T>
void set (
const T& object,
void (T::*cb)(PARAM1)const
);
operator bool (
) const;
bool operator! (
) const;
void operator () ( void operator () (
PARAM1 param1 PARAM1 param1
) const; ) const;
...@@ -284,6 +317,18 @@ namespace dlib ...@@ -284,6 +317,18 @@ namespace dlib
void (T::*cb)(PARAM1,PARAM2) void (T::*cb)(PARAM1,PARAM2)
); );
template <typename T>
void set (
const T& object,
void (T::*cb)(PARAM1,PARAM2)const
);
operator bool (
) const;
bool operator! (
) const;
void operator () ( void operator () (
PARAM1 param1, PARAM1 param1,
PARAM2 param2 PARAM2 param2
...@@ -341,6 +386,18 @@ namespace dlib ...@@ -341,6 +386,18 @@ namespace dlib
void (T::*cb)(PARAM1,PARAM2,PARAM3) void (T::*cb)(PARAM1,PARAM2,PARAM3)
); );
template <typename T>
void set (
const T& object,
void (T::*cb)(PARAM1,PARAM2,PARAM3)const
);
operator bool (
) const;
bool operator! (
) const;
void operator () ( void operator () (
PARAM1 param1, PARAM1 param1,
PARAM2 param2, PARAM2 param2,
...@@ -400,6 +457,18 @@ namespace dlib ...@@ -400,6 +457,18 @@ namespace dlib
void (T::*cb)(PARAM1,PARAM2,PARAM3,PARAM4) void (T::*cb)(PARAM1,PARAM2,PARAM3,PARAM4)
); );
template <typename T>
void set (
const T& object,
void (T::*cb)(PARAM1,PARAM2,PARAM3,PARAM4)const
);
operator bool (
) const;
bool operator! (
) const;
void operator () ( void operator () (
PARAM1 param1, PARAM1 param1,
PARAM2 param2, PARAM2 param2,
......
...@@ -19,18 +19,6 @@ namespace dlib ...@@ -19,18 +19,6 @@ namespace dlib
> >
class member_function_pointer_kernel_c; class member_function_pointer_kernel_c;
template <
typename mfpb,
typename PARAM1,
typename PARAM2,
typename PARAM3,
typename PARAM4
>
void swap (
member_function_pointer_kernel_c<mfpb,PARAM1,PARAM2,PARAM3,PARAM4>& a,
member_function_pointer_kernel_c<mfpb,PARAM1,PARAM2,PARAM3,PARAM4>& b
) { a.swap(b); }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
...@@ -60,6 +48,25 @@ namespace dlib ...@@ -60,6 +48,25 @@ namespace dlib
mfpb::set(object,cb); mfpb::set(object,cb);
} }
template <
typename T
>
void set (
const T& object,
void (T::*cb)()const
)
{
// make sure requires clause is not broken
DLIB_CASSERT(cb != 0,
"\tvoid member_function_pointer::set"
<< "\n\tthe member function pointer can't be null"
<< "\n\tthis: " << this
);
// call the real function
mfpb::set(object,cb);
}
void operator () ( void operator () (
) const ) const
{ {
...@@ -105,6 +112,25 @@ namespace dlib ...@@ -105,6 +112,25 @@ namespace dlib
mfpb::set(object,cb); mfpb::set(object,cb);
} }
template <
typename T
>
void set (
const T& object,
void (T::*cb)(PARAM1)const
)
{
// make sure requires clause is not broken
DLIB_CASSERT(cb != 0,
"\tvoid member_function_pointer::set"
<< "\n\tthe member function pointer can't be null"
<< "\n\tthis: " << this
);
// call the real function
mfpb::set(object,cb);
}
void operator () ( void operator () (
PARAM1 param1 PARAM1 param1
) const ) const
...@@ -152,6 +178,25 @@ namespace dlib ...@@ -152,6 +178,25 @@ namespace dlib
mfpb::set(object,cb); mfpb::set(object,cb);
} }
template <
typename T
>
void set (
const T& object,
void (T::*cb)(PARAM1,PARAM2)const
)
{
// make sure requires clause is not broken
DLIB_CASSERT(cb != 0,
"\tvoid member_function_pointer::set"
<< "\n\tthe member function pointer can't be null"
<< "\n\tthis: " << this
);
// call the real function
mfpb::set(object,cb);
}
void operator () ( void operator () (
PARAM1 param1, PARAM1 param1,
PARAM2 param2 PARAM2 param2
...@@ -201,6 +246,25 @@ namespace dlib ...@@ -201,6 +246,25 @@ namespace dlib
mfpb::set(object,cb); mfpb::set(object,cb);
} }
template <
typename T
>
void set (
const T& object,
void (T::*cb)(PARAM1,PARAM2,PARAM3)const
)
{
// make sure requires clause is not broken
DLIB_CASSERT(cb != 0,
"\tvoid member_function_pointer::set"
<< "\n\tthe member function pointer can't be null"
<< "\n\tthis: " << this
);
// call the real function
mfpb::set(object,cb);
}
void operator () ( void operator () (
PARAM1 param1, PARAM1 param1,
PARAM2 param2, PARAM2 param2,
...@@ -252,6 +316,25 @@ namespace dlib ...@@ -252,6 +316,25 @@ namespace dlib
mfpb::set(object,cb); mfpb::set(object,cb);
} }
template <
typename T
>
void set (
const T& object,
void (T::*cb)(PARAM1,PARAM2,PARAM3,PARAM4)const
)
{
// make sure requires clause is not broken
DLIB_CASSERT(cb != 0,
"\tvoid member_function_pointer::set"
<< "\n\tthe member function pointer can't be null"
<< "\n\tthis: " << this
);
// call the real function
mfpb::set(object,cb);
}
void operator () ( void operator () (
PARAM1 param1, PARAM1 param1,
PARAM2 param2, PARAM2 param2,
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment