Skip to content

Commit 9da3f90

Browse files
committed
- singleton: added equality operators & allow copy constructor.
1 parent cd10352 commit 9da3f90

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

include/creational/singleton.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@ template<typename T>
1818
class singleton {
1919

2020
public:
21-
singleton(const singleton<T>&) = delete;
21+
singleton(const singleton<T>&) = default;
2222
singleton& operator=(const singleton<T>&) = delete;
2323

24+
template <typename U>
25+
inline bool operator==(const U& rhs) const {
26+
return std::is_same<T,U>::value;
27+
}
28+
template <typename U>
29+
inline bool operator!=(const U& rhs) const {
30+
return !(*this == rhs);
31+
}
32+
2433
protected:
2534
singleton()
2635
{

test/creational/singleton.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,33 @@ class my_singleton : public dpc::singleton<my_singleton> {
1717

1818
TEST(DessignPatternSingletonTest, SingletonInstantiation)
1919
{
20-
my_singleton a;
20+
auto a = my_singleton::get();
2121
my_singleton b;
2222

23+
ASSERT_FALSE(std::addressof(a) == std::addressof(b));
24+
ASSERT_TRUE(std::addressof(*a) == std::addressof(*b));
25+
ASSERT_TRUE(std::addressof(a.get()) == std::addressof(b.get()));
2326
ASSERT_STREQ(a->instance_addr().c_str(), b->instance_addr().c_str());
24-
ASSERT_STREQ(my_singleton::get().instance_addr().c_str(),
25-
a->instance_addr().c_str());
27+
}
28+
29+
TEST(DessignPatternSingletonTest, SingletonEquality)
30+
{
31+
my_singleton a;
32+
my_singleton b;
33+
34+
ASSERT_EQ(a, b);
35+
}
36+
37+
TEST(DessignPatternSingletonTest, SingletonNotEquality)
38+
{
39+
my_singleton a;
40+
41+
class my_class {};
42+
my_class b;
43+
int c = 0;
44+
45+
ASSERT_TRUE(a != b);
46+
ASSERT_TRUE(a != c);
2647
}
2748

2849
int main(int argc, char **argv) {

0 commit comments

Comments
 (0)