1
0
forked from 0ad/0ad

Replace use of safe bool by explicit bool operator.

This was SVN commit r16228.
This commit is contained in:
leper 2015-01-25 03:11:24 +00:00
parent 4c1903500b
commit 123bab6793
3 changed files with 8 additions and 43 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2012 Wildfire Games. /* Copyright (C) 2015 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -57,12 +57,6 @@ class CmpPtr
private: private:
T* m; T* m;
// "Safe Bool Idiom" based on http://www.artima.com/cppsource/safebool.html
// to allow "if (cmp)" and "if (!cmp)" etc, without also allowing "int i = cmp"
// or "if (cmp1 == cmp2)" etc.
typedef void (CmpPtr::*bool_type)() const;
void this_type_does_not_support_comparisons() const {}
public: public:
CmpPtr(const CSimContext& context, entity_id_t ent) CmpPtr(const CSimContext& context, entity_id_t ent)
{ {
@ -85,24 +79,10 @@ public:
T* operator->() { return m; } T* operator->() { return m; }
operator bool_type() const explicit operator bool() const
{ {
return (m != NULL) ? &CmpPtr::this_type_does_not_support_comparisons : 0; return m != NULL;
} }
}; };
template<typename T, typename U>
bool operator!=(const CmpPtr<T>& lhs, const U& UNUSED(rhs))
{
lhs.this_type_does_not_support_comparisons();
return false;
}
template<typename T, typename U>
bool operator==(const CmpPtr<T>& lhs, const U& UNUSED(rhs))
{
lhs.this_type_does_not_support_comparisons();
return false;
}
#endif // INCLUDED_CMPPTR #endif // INCLUDED_CMPPTR

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2014 Wildfire Games. /* Copyright (C) 2015 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -51,8 +51,6 @@ private:
void inc_ref(); void inc_ref();
void dec_ref(); void dec_ref();
T* ptr; T* ptr;
typedef void (AtSmartPtr::*bool_type)() const;
void this_type_does_not_support_comparisions() const {}
public: public:
// Constructors // Constructors
AtSmartPtr() : ptr(NULL) {} AtSmartPtr() : ptr(NULL) {}
@ -68,23 +66,10 @@ public:
//operator AtSmartPtr<const T> () { return AtSmartPtr<const T>(ptr); } // (actually provided by ConstCastHelper) //operator AtSmartPtr<const T> () { return AtSmartPtr<const T>(ptr); } // (actually provided by ConstCastHelper)
// Override -> // Override ->
T* operator->() const { return ptr; } T* operator->() const { return ptr; }
// Test whether the pointer is pointing to anything using safe bool // Test whether the pointer is pointing to anything
operator bool_type() const { return (ptr!=NULL) == true ? &AtSmartPtr::this_type_does_not_support_comparisions : 0; } explicit operator bool() const { return ptr != NULL; }
}; };
template<typename T, typename U>
bool operator!=(const AtSmartPtr<T>& lhs, const U&)
{
lhs.this_type_does_not_support_comparisions();
return false;
}
template<typename T, typename U>
bool operator==(const AtSmartPtr<T>& lhs, const U&)
{
lhs.this_type_does_not_support_comparisions();
return false;
}
template<class ConstSmPtr, class T> template<class ConstSmPtr, class T>
ConstCastHelper<ConstSmPtr, T>::operator ConstSmPtr () ConstCastHelper<ConstSmPtr, T>::operator ConstSmPtr ()
{ {

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2014 Wildfire Games. /* Copyright (C) 2015 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -89,7 +89,7 @@ AtIter& AtIter::operator ++ ()
bool AtIter::defined() const bool AtIter::defined() const
{ {
return p; return (bool)p;
} }
bool AtIter::hasContent() const bool AtIter::hasContent() const