Commit c36d88aa authored by Davis King's avatar Davis King

Added the is_same_object() function.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403885
parent 42175e8f
......@@ -444,6 +444,41 @@ namespace dlib
const static bool value = sizeof(test(from_helper())) == sizeof(yes_type);
};
// ----------------------------------------------------------------------------------------
/*!A is_same_object
This is a templated function which checks if both of its arguments are actually
references to the same object. It returns true if they are and false otherwise.
!*/
// handle the case where T and U are unrelated types.
template < typename T, typename U >
typename disable_if_c<is_convertible<T*, U*>::value || is_convertible<U*,T*>::value, bool>::type
is_same_object (
const T& a,
const U& b
)
{
return ((void*)&a == (void*)&b);
}
// handle the case where T and U are related types because their pointers can be
// implicitly converted into one or the other. E.g. a derived class and its base class.
// Or where both T and U are just the same type. This way we make sure that if there is a
// valid way to convert between these two pointer types then we will take that route rather
// than the void* approach used otherwise.
template < typename T, typename U >
typename enable_if_c<is_convertible<T*, U*>::value || is_convertible<U*,T*>::value, bool>::type
is_same_object (
const T& a,
const U& b
)
{
return (&a == &b);
}
// ----------------------------------------------------------------------------------------
/*!A is_unsigned_type
......
......@@ -42,6 +42,7 @@ set (tests
hash_set.cpp
hash_table.cpp
image.cpp
is_same_object.cpp
kcentroid.cpp
kernel_matrix.cpp
linear_manifold_regularizer.cpp
......
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include "tester.h"
#include <dlib/svm.h>
#include <vector>
#include <sstream>
namespace
{
using namespace test;
using namespace dlib;
using namespace std;
dlib::logger dlog("test.is_same_object");
class is_same_object_tester : public tester
{
/*!
WHAT THIS OBJECT REPRESENTS
This object represents a unit test. When it is constructed
it adds itself into the testing framework.
!*/
public:
is_same_object_tester (
) :
tester (
"test_is_same_object", // the command line argument name for this test
"Run tests on the is_same_object function.", // the command line argument description
0 // the number of command line arguments for this test
)
{
}
struct base {};
struct derived : public base {};
template <bool truth>
void go(const base& a, const base& b)
{
DLIB_TEST( is_same_object(a,b) == truth) ;
DLIB_TEST( is_same_object(b,a) == truth) ;
}
template <bool truth>
void go2(const base& a, const derived& b)
{
DLIB_TEST( is_same_object(a,b) == truth) ;
DLIB_TEST( is_same_object(b,a) == truth) ;
}
void perform_test (
)
{
print_spinner();
int a, b;
double d;
DLIB_TEST( is_same_object(a,a) == true) ;
DLIB_TEST( is_same_object(a,b) == false) ;
DLIB_TEST( is_same_object(d,b) == false) ;
DLIB_TEST( is_same_object(d,d) == true) ;
base sb;
derived sd, sd2;
DLIB_TEST( is_same_object(sb,sd) == false) ;
DLIB_TEST( is_same_object(sd,sb) == false) ;
go<true>(sd, sd);
go<false>(sd, sd2);
go<true>(sb, sb);
go<false>(sd, sb);
go2<true>(sd, sd);
go2<false>(sd2, sd);
go2<false>(sd, sd2);
go2<false>(sb, sd);
}
};
// Create an instance of this object. Doing this causes this test
// to be automatically inserted into the testing framework whenever this cpp file
// is linked into the project. Note that since we are inside an unnamed-namespace
// we won't get any linker errors about the symbol a being defined multiple times.
is_same_object_tester a;
}
......@@ -52,6 +52,7 @@ SRC += hash_map.cpp
SRC += hash_set.cpp
SRC += hash_table.cpp
SRC += image.cpp
SRC += is_same_object.cpp
SRC += kcentroid.cpp
SRC += kernel_matrix.cpp
SRC += linear_manifold_regularizer.cpp
......
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