"docs/vscode:/vscode.git/clone" did not exist on "dbb16bedd54ffc2d7d2a2eac8d8064711f0c7286"
Commit 72d9adbf authored by Davis King's avatar Davis King
Browse files

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
>
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 <>
......@@ -53,9 +42,9 @@ namespace dlib
member_function_pointer<> my_pointer;
To use a pointer to a function that takes a single int you would say:
member_function_pointer<int> my_pointer;
To use a pointer to a function that takes an int and then a string
you would say:
member_function_pointer<int,string> my_pointer;
To use a pointer to a function that takes an int and then a reference
to a string you would say:
member_function_pointer<int,string&> my_pointer;
Also note that the formal comments are only present for the first
template specialization. They are all exactly the same except for the
......@@ -160,6 +149,38 @@ namespace dlib
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 () (
) const;
/*!
......@@ -229,6 +250,18 @@ namespace dlib
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 () (
PARAM1 param1
) const;
......@@ -284,6 +317,18 @@ namespace dlib
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 () (
PARAM1 param1,
PARAM2 param2
......@@ -341,6 +386,18 @@ namespace dlib
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 () (
PARAM1 param1,
PARAM2 param2,
......@@ -400,6 +457,18 @@ namespace dlib
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 () (
PARAM1 param1,
PARAM2 param2,
......
......@@ -19,18 +19,6 @@ namespace dlib
>
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 <
......@@ -60,6 +48,25 @@ namespace dlib
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 () (
) const
{
......@@ -105,6 +112,25 @@ namespace dlib
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 () (
PARAM1 param1
) const
......@@ -152,6 +178,25 @@ namespace dlib
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 () (
PARAM1 param1,
PARAM2 param2
......@@ -201,6 +246,25 @@ namespace dlib
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 () (
PARAM1 param1,
PARAM2 param2,
......@@ -252,6 +316,25 @@ namespace dlib
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 () (
PARAM1 param1,
PARAM2 param2,
......
......@@ -19,6 +19,34 @@ namespace
logger dlog("test.member_function_pointer");
class mfp_test_helper_other
{
public:
mfp_test_helper_other (
): i(-1) {}
mutable int i;
void go0 (
) { i = 0; }
void go1 (
int v1
) { i = 1*v1; }
void go2 (
int v1,int v2
) { i = 2*v1*v2; }
void go3 (
int v1,int v2,int v3
) { i = 3*v1*v2*v3; }
void go4 (
int v1,int v2,int v3,int v4
) { i = 4*v1*v2*v3*v4; }
};
class mfp_test_helper
{
public:
......@@ -26,27 +54,54 @@ namespace
): i(-1) {}
int i;
mutable int i;
void go0 (
) { i = 0; }
void go1 (
int
) { i = 1; }
int v1
) { i = 1*v1; }
void go2 (
int,int
) { i = 2; }
int v1,int v2
) { i = 2*v1*v2; }
void go3 (
int,int,int
) { i = 3; }
int v1,int v2,int v3
) { i = 3*v1*v2*v3; }
void go4 (
int,int,int,int
) { i = 4; }
int v1,int v2,int v3,int v4
) { i = 4*v1*v2*v3*v4; }
};
class mfp_test_helper_const
{
public:
mfp_test_helper_const (
): i(-1) {}
mutable int i;
void go0 (
) const { i = 0; }
void go1 (
int v1
) const { i = 1*v1; }
void go2 (
int v1,int v2
) const { i = 2*v1*v2; }
void go3 (
int v1,int v2,int v3
) const { i = 3*v1*v2*v3; }
void go4 (
int v1,int v2,int v3,int v4
) const { i = 4*v1*v2*v3*v4; }
};
template <
template <typename P1 = void, typename P2 = void, typename P3 = void, typename P4 = void> class mfp
template <typename P1 = void, typename P2 = void, typename P3 = void, typename P4 = void> class mfp,
typename test_helper
>
void member_function_pointer_kernel_test (
)
......@@ -59,7 +114,7 @@ namespace
{
mfp_test_helper helper;
test_helper helper;
mfp<> a0, b0;
mfp<int> a1, b1;
......@@ -89,33 +144,64 @@ namespace
DLIB_CASSERT(a0c.is_set() == false,"");
DLIB_CASSERT(b0c.is_set() == false,"");
DLIB_CASSERT(!a0 ,"");
DLIB_CASSERT(!b0 ,"");
DLIB_CASSERT(!a0c,"");
DLIB_CASSERT(!b0c,"");
DLIB_CASSERT(a1.is_set() == false,"");
DLIB_CASSERT(b1.is_set() == false,"");
DLIB_CASSERT(a1c.is_set() == false,"");
DLIB_CASSERT(b1c.is_set() == false,"");
DLIB_CASSERT(!a1 ,"");
DLIB_CASSERT(!b1 ,"");
DLIB_CASSERT(!a1c,"");
DLIB_CASSERT(!b1c,"");
DLIB_CASSERT(a2.is_set() == false,"");
DLIB_CASSERT(b2.is_set() == false,"");
DLIB_CASSERT(a2c.is_set() == false,"");
DLIB_CASSERT(b2c.is_set() == false,"");
DLIB_CASSERT(!a2,"");
DLIB_CASSERT(!b2,"");
DLIB_CASSERT(!a2c,"");
DLIB_CASSERT(!b2c,"");
DLIB_CASSERT(a3.is_set() == false,"");
DLIB_CASSERT(b3.is_set() == false,"");
DLIB_CASSERT(a3c.is_set() == false,"");
DLIB_CASSERT(b3c.is_set() == false,"");
DLIB_CASSERT(!a3,"");
DLIB_CASSERT(!b3,"");
DLIB_CASSERT(!a3c,"");
DLIB_CASSERT(!b3c,"");
DLIB_CASSERT(a4.is_set() == false,"");
DLIB_CASSERT(b4.is_set() == false,"");
DLIB_CASSERT(a4c.is_set() == false,"");
DLIB_CASSERT(b4c.is_set() == false,"");
a0.set(helper,&mfp_test_helper::go0);
a0c.set(helper,&mfp_test_helper::go0);
DLIB_CASSERT(!a4,"");
DLIB_CASSERT(!b4,"");
DLIB_CASSERT(!a4c,"");
DLIB_CASSERT(!b4c,"");
a0.set(helper,&test_helper::go0);
a0c.set(helper,&test_helper::go0);
DLIB_CASSERT(a0.is_set() == true,"");
DLIB_CASSERT(a0c.is_set() == true,"");
DLIB_CASSERT(b0.is_set() == false,"");
DLIB_CASSERT(b0c.is_set() == false,"");
DLIB_CASSERT(a0,"");
DLIB_CASSERT(a0c,"");
DLIB_CASSERT(!b0,"");
DLIB_CASSERT(!b0c,"");
a0 = a0;
DLIB_CASSERT(a0 == a0, "");
DLIB_CASSERT(!(a0 != a0),"");
......@@ -124,6 +210,11 @@ namespace
DLIB_CASSERT(b0.is_set() == false,"");
DLIB_CASSERT(b0c.is_set() == false,"");
DLIB_CASSERT(a0,"");
DLIB_CASSERT(a0c,"");
DLIB_CASSERT(!b0,"");
DLIB_CASSERT(!b0c,"");
swap(a0,b0);
swap(a0c,b0c);
DLIB_CASSERT(a0.is_set() == false,"");
......@@ -131,6 +222,11 @@ namespace
DLIB_CASSERT(b0.is_set() == true,"");
DLIB_CASSERT(b0c.is_set() == true,"");
DLIB_CASSERT(!a0,"");
DLIB_CASSERT(!a0c,"");
DLIB_CASSERT(b0,"");
DLIB_CASSERT(b0c,"");
a0 = b0;
DLIB_CASSERT(a0 == a0, "");
DLIB_CASSERT(a0 == b0, "");
......@@ -140,6 +236,11 @@ namespace
DLIB_CASSERT(b0.is_set() == true,"");
DLIB_CASSERT(b0c.is_set() == true,"");
DLIB_CASSERT(a0 ,"");
DLIB_CASSERT(!a0c,"");
DLIB_CASSERT(b0,"");
DLIB_CASSERT(b0c,"");
a0.clear();
a0c.clear();
......@@ -151,8 +252,8 @@ namespace
DLIB_CASSERT(b0c.is_set() == false,"");
a1.set(helper,&mfp_test_helper::go1);
a1c.set(helper,&mfp_test_helper::go1);
a1.set(helper,&test_helper::go1);
a1c.set(helper,&test_helper::go1);
DLIB_CASSERT(a1.is_set() == true,"");
DLIB_CASSERT(a1c.is_set() == true,"");
DLIB_CASSERT(b1.is_set() == false,"");
......@@ -164,6 +265,11 @@ namespace
DLIB_CASSERT(b1.is_set() == true,"");
DLIB_CASSERT(b1c.is_set() == true,"");
DLIB_CASSERT(!a1,"");
DLIB_CASSERT(!a1c,"");
DLIB_CASSERT(b1,"");
DLIB_CASSERT(b1c,"");
a1 = b1;
DLIB_CASSERT(a1 == a1, "");
......@@ -185,8 +291,8 @@ namespace
DLIB_CASSERT(b1c.is_set() == false,"");
a2.set(helper,&mfp_test_helper::go2);
a2c.set(helper,&mfp_test_helper::go2);
a2.set(helper,&test_helper::go2);
a2c.set(helper,&test_helper::go2);
DLIB_CASSERT(a2.is_set() == true,"");
DLIB_CASSERT(a2c.is_set() == true,"");
DLIB_CASSERT(b2.is_set() == false,"");
......@@ -198,6 +304,27 @@ namespace
DLIB_CASSERT(b2.is_set() == true,"");
DLIB_CASSERT(b2c.is_set() == true,"");
DLIB_CASSERT(!a2,"");
DLIB_CASSERT(!a2c,"");
DLIB_CASSERT(b2,"");
DLIB_CASSERT(b2c,"");
if (b2)
{
}
else
{
DLIB_CASSERT(false,"");
}
if (a2c)
{
DLIB_CASSERT(false,"");
}
else
{
DLIB_CASSERT(true,"");
}
a2 = b2;
DLIB_CASSERT(a2 == a2, "");
DLIB_CASSERT(a2 == b2, "");
......@@ -217,8 +344,8 @@ namespace
DLIB_CASSERT(b2c.is_set() == false,"");
a3.set(helper,&mfp_test_helper::go3);
a3c.set(helper,&mfp_test_helper::go3);
a3.set(helper,&test_helper::go3);
a3c.set(helper,&test_helper::go3);
DLIB_CASSERT(a3.is_set() == true,"");
DLIB_CASSERT(a3c.is_set() == true,"");
DLIB_CASSERT(b3.is_set() == false,"");
......@@ -250,8 +377,8 @@ namespace
DLIB_CASSERT(b3c.is_set() == false,"");
a4.set(helper,&mfp_test_helper::go4);
a4c.set(helper,&mfp_test_helper::go4);
a4.set(helper,&test_helper::go4);
a4c.set(helper,&test_helper::go4);
DLIB_CASSERT(a4.is_set() == true,"");
DLIB_CASSERT(a4c.is_set() == true,"");
DLIB_CASSERT(b4.is_set() == false,"");
......@@ -286,8 +413,8 @@ namespace
DLIB_CASSERT(b4c.is_set() == false,"");
a0.set(helper,&mfp_test_helper::go0);
a0c.set(helper,&mfp_test_helper::go0);
a0.set(helper,&test_helper::go0);
a0c.set(helper,&test_helper::go0);
b0 = a0;
b0c = a0c;
helper.i = -1;
......@@ -304,76 +431,99 @@ namespace
DLIB_CASSERT(helper.i == 0,"");
a1.set(helper,&mfp_test_helper::go1);
a1c.set(helper,&mfp_test_helper::go1);
a1.set(helper,&test_helper::go1);
a1c.set(helper,&test_helper::go1);
b1 = a1;
b1c = a1c;
helper.i = -1;
a1(0);
a1(1);
DLIB_CASSERT(helper.i == 1,"");
helper.i = -1;
b1(0);
DLIB_CASSERT(helper.i == 1,"");
b1(10);
DLIB_CASSERT(helper.i == 1*10,"");
helper.i = -1;
a1c(0);
DLIB_CASSERT(helper.i == 1,"");
a1c(20);
DLIB_CASSERT(helper.i == 1*20,"");
helper.i = -1;
b1c(0);
DLIB_CASSERT(helper.i == 1,"");
b1c(30);
DLIB_CASSERT(helper.i == 1*30,"");
a2.set(helper,&mfp_test_helper::go2);
a2c.set(helper,&mfp_test_helper::go2);
a2.set(helper,&test_helper::go2);
a2c.set(helper,&test_helper::go2);
b2 = a2;
b2c = a2c;
helper.i = -1;
a2(0,0);
DLIB_CASSERT(helper.i == 2,"");
a2(1,2);
DLIB_CASSERT(helper.i == 2*1*2,"");
helper.i = -1;
b2(0,0);
DLIB_CASSERT(helper.i == 2,"");
b2(3,4);
DLIB_CASSERT(helper.i == 2*3*4,"");
helper.i = -1;
a2c(0,0);
DLIB_CASSERT(helper.i == 2,"");
a2c(5,6);
DLIB_CASSERT(helper.i == 2*5*6,"");
helper.i = -1;
b2c(0,0);
DLIB_CASSERT(helper.i == 2,"");
b2c(7,8);
DLIB_CASSERT(helper.i == 2*7*8,"");
a3.set(helper,&mfp_test_helper::go3);
a3c.set(helper,&mfp_test_helper::go3);
a3.set(helper,&test_helper::go3);
a3c.set(helper,&test_helper::go3);
b3 = a3;
b3c = a3c;
helper.i = -1;
a3(0,0,0);
DLIB_CASSERT(helper.i == 3,"");
a3(1,2,3);
DLIB_CASSERT(helper.i == 3*1*2*3,"");
helper.i = -1;
b3(0,0,0);
DLIB_CASSERT(helper.i == 3,"");
b3(4,5,6);
DLIB_CASSERT(helper.i == 3*4*5*6,"");
helper.i = -1;
a3c(0,0,0);
DLIB_CASSERT(helper.i == 3,"");
a3c(7,8,9);
DLIB_CASSERT(helper.i == 3*7*8*9,"");
helper.i = -1;
b3c(0,0,0);
DLIB_CASSERT(helper.i == 3,"");
b3c(1,2,3);
DLIB_CASSERT(helper.i == 3*1*2*3,"");
a4.set(helper,&mfp_test_helper::go4);
a4c.set(helper,&mfp_test_helper::go4);
a4.set(helper,&test_helper::go4);
a4c.set(helper,&test_helper::go4);
DLIB_CASSERT(a4 == a4c,"");
b4 = a4;
b4c = a4c;
helper.i = -1;
a4(0,0,0,0);
DLIB_CASSERT(helper.i == 4,"");
a4(1,2,3,4);
DLIB_CASSERT(helper.i == 4*1*2*3*4,"");
helper.i = -1;
b4(0,0,0,0);
DLIB_CASSERT(helper.i == 4,"");
b4(5,6,7,8);
DLIB_CASSERT(helper.i == 4*5*6*7*8,"");
helper.i = -1;
a4c(0,0,0,0);
DLIB_CASSERT(helper.i == 4,"");
a4c(9,1,2,3);
DLIB_CASSERT(helper.i == 4*9*1*2*3,"");
helper.i = -1;
b4c(0,0,0,0);
DLIB_CASSERT(helper.i == 4,"");
b4c(4,5,6,7);
DLIB_CASSERT(helper.i == 4*4*5*6*7,"");
DLIB_CASSERT(a4 == b4,"");
DLIB_CASSERT(a4,"");
DLIB_CASSERT(a4 == b4,"");
a4.clear();
DLIB_CASSERT(a4 != b4,"");
DLIB_CASSERT(!a4,"");
DLIB_CASSERT(a4 == 0,"");
DLIB_CASSERT(a4 == a4,"");
a4 = a4;
DLIB_CASSERT(a4 != b4,"");
DLIB_CASSERT(!a4,"");
DLIB_CASSERT(a4 == a4,"");
mfp_test_helper_other other;
a4.set(other,&mfp_test_helper_other::go4);
DLIB_CASSERT(a4 != b4,"");
DLIB_CASSERT(a4,"");
DLIB_CASSERT(a4 == a4,"");
a4.set(helper,&test_helper::go4);
DLIB_CASSERT(a4 == b4,"");
DLIB_CASSERT(a4,"");
DLIB_CASSERT(a4 == a4,"");
......@@ -393,7 +543,8 @@ namespace
void perform_test (
)
{
member_function_pointer_kernel_test<member_function_pointer_kernel_1>();
member_function_pointer_kernel_test<member_function_pointer_kernel_1,mfp_test_helper>();
member_function_pointer_kernel_test<member_function_pointer_kernel_1,mfp_test_helper_const>();
}
} a;
......
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