Skip to content

Conversation

@codemercenary
Copy link
Contributor

Allow users to retrieve std::shared_ptr<void> directly from an AnySharedPointer.

Allow users to retrieve `std::shared_ptr<void>` directly from an `AnySharedPointer`.
}

const std::shared_ptr<void>& as_void(void) const { return m_ptr; }

Copy link
Contributor

@ajohnston33 ajohnston33 Apr 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a means of having an un-typed container for a pointer without losing type safety. I can understand wanting this to be able to write simpler template logic elsewhere. What confuses me here is throwing away your ability to guard type safe access by returning a shared_ptr<void>. Shouldn't callers need to provide the type of the underlying pointer so it can be checked before being allowed access to the shared pointer?

Copy link
Contributor Author

@codemercenary codemercenary Apr 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite correct. Actually, the main motivation to using AnySharedPointer is not necessarily that the code becomes any simpler, but that I can avoid using templated functions altogether and move to runtime versions of the same. These runtime versions are compiled once and don't increase build times downstream.

This particular routine is mainly intended to make unit tests a little easier to write in that sometimes I don't actually care about the underlying type, I just want whatever the pointed-to object is. There are implicit dangers of doing this--in particular:

std::shared_ptr<Derived> p;
AnySharedPointer x = std::static_pointer_cast<Base>(p);
ASSERT_EQ(p.get(), x.as_void());  // Might fail!

But we tolerate this because it's not really used by anyone except autowiring, and that's the only intended customer for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also when I use this type I get burned almost every time and have to rely pretty heavily on unit tests to ensure correctness. It's definitely not the friendliest thing in the world.

Copy link
Contributor

@ajohnston33 ajohnston33 Apr 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking something like this?

template<class T>
std::shared_ptr<T> as_shared(void) const {
  "Check that auto_id of T == m_ti"
  return static_pointer_cast<T>(m_ptr);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I have something like that now, it's called as

@ajohnston33 ajohnston33 merged commit ab025c6 into develop Apr 15, 2016
@codemercenary codemercenary deleted the feature-voidasp branch April 15, 2016 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants