You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by jp...@apache.org on 2016/05/11 20:31:56 UTC
[trafficserver] 32/33: TS-4425: Hide Ptr internals.
This is an automated email from the ASF dual-hosted git repository.
jpeach pushed a commit to branch master
in repository https://git-dual.apache.org/repos/asf/trafficserver.git
commit 523feded25bc46486f70e11203fe3b6c3a391103
Author: James Peach <jp...@apache.org>
AuthorDate: Sat May 7 14:13:22 2016 -0700
TS-4425: Hide Ptr internals.
Make the raw pointer member in Ptr<T> private and remove the implicit
raw pointer conversion. This improves type safety and forces callers
to declare when they circumvent the refcounting.
---
lib/ts/Ptr.h | 12 +++++++++++-
lib/ts/test_Ptr.cc | 13 +++++++++++++
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/lib/ts/Ptr.h b/lib/ts/Ptr.h
index 3391608..8645fbf 100644
--- a/lib/ts/Ptr.h
+++ b/lib/ts/Ptr.h
@@ -100,6 +100,13 @@ private:
////////////////////////////////////////////////////////////////////////
template <class T> class Ptr
{
+ // https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool.
+ typedef void (Ptr::*bool_type)() const;
+ void
+ this_type_does_not_support_comparisons() const
+ {
+ }
+
public:
explicit Ptr(T *p = 0);
Ptr(const Ptr<T> &);
@@ -109,9 +116,9 @@ public:
Ptr<T> &operator=(const Ptr<T> &);
Ptr<T> &operator=(T *);
- operator T *() const { return (m_ptr); }
T *operator->() const { return (m_ptr); }
T &operator*() const { return (*m_ptr); }
+ operator bool_type() const { return m_ptr ? &Ptr::this_type_does_not_support_comparisons : 0; }
int
operator==(const T *p)
{
@@ -170,7 +177,10 @@ public:
m_ptr = ptr;
}
+private:
T *m_ptr;
+
+ friend class CoreUtils;
};
template <typename T>
diff --git a/lib/ts/test_Ptr.cc b/lib/ts/test_Ptr.cc
index 3f7f826..9029416 100644
--- a/lib/ts/test_Ptr.cc
+++ b/lib/ts/test_Ptr.cc
@@ -85,6 +85,19 @@ REGRESSION_TEST(Ptr_refcount)(RegressionTest *t, int /* atype ATS_UNUSED */, int
box.check(alive == 0, "refcounts dropped");
}
+REGRESSION_TEST(Ptr_bool)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus)
+{
+ TestBox box(t, pstatus);
+ box = REGRESSION_TEST_PASSED;
+ unsigned alive = 0;
+
+ Ptr<PtrObject> none;
+ Ptr<PtrObject> some = make_ptr(new PtrObject(&alive));
+
+ box.check(!none, "Empty Ptr<T> is false");
+ box.check(some, "Non-empty Ptr<T> is true");
+}
+
int
main(int /* argc ATS_UNUSED */, char ** /* argv ATS_UNUSED */)
{
--
To stop receiving notification emails like this one, please contact
"commits@trafficserver.apache.org" <co...@trafficserver.apache.org>.