Flyweight::get() takes emplace params and returns shared_ptr

Instead of creating an object, passing it by reference, and then copying
that object again, the Flyweight::get() function was changed to take the
parameters for the template object's constructor so that the object is
created emplace. This avoids the extra copy.

Also, using shared_ptr to make things a little more safe with memory
management.

Change-Id: Ib23010d5ee9d19b0be257eafc5297c8a158eebb0
Signed-off-by: Zane Shelley <zshelle@us.ibm.com>
diff --git a/test/flyweight_test.cpp b/test/flyweight_test.cpp
index 139d7c1..fb982e1 100644
--- a/test/flyweight_test.cpp
+++ b/test/flyweight_test.cpp
@@ -7,14 +7,8 @@
 class Foo
 {
   public:
-    Foo() = default;
     explicit Foo(int i) : iv_i(i) {}
 
-    int get() const
-    {
-        return iv_i;
-    }
-
     bool operator==(const Foo& i_r) const
     {
         return iv_i == i_r.iv_i;
@@ -29,31 +23,16 @@
     int iv_i = 0;
 };
 
-Foo& addFoo(int i)
-{
-    return Flyweight<Foo>::getSingleton().get(Foo{i});
-}
-
 TEST(FlyweightTest, TestSet1)
 {
-    // Add some unique entries in a random order and keep track of where those
-    // enties exist in memory.
-    Foo* a[5];
-    a[1] = &(addFoo(1));
-    a[2] = &(addFoo(2));
-    a[0] = &(addFoo(0));
-    a[4] = &(addFoo(4));
-    a[3] = &(addFoo(3));
+    auto& foo_factory = Flyweight<Foo>::getSingleton();
 
-    // Now add more entries and verify the 'new' entries match the same
-    // addresses as the previously added entries.
-    for (int i = 4; i >= 0; i--)
-    {
-        ASSERT_EQ(a[i], &(addFoo(i)));
-    }
+    auto f1 = foo_factory.get(1);
+    auto f2 = foo_factory.get(2);
+    auto f3 = foo_factory.get(1); // same as f1
 
-    // At this point, we have proven that duplicate entries will return
-    // references to the original unique entries. There is probably more we can
-    // do here, but this is enough to prove the Flyweight class follows the
-    // flyweight design pattern.
+    ASSERT_NE(f1, f2); // Pointing to different objects
+    ASSERT_EQ(f1, f3); // Pointing to the same object
+
+    ASSERT_EQ(2, foo_factory.size()); // Only two entries in the flyweight
 }