ICU-10086 change non-member swap functions, use in-class definition of friend function; prevent self assignment of UnicodeString stack buffer (harmless but causes warnings)

X-SVN-Rev: 37498
This commit is contained in:
Markus Scherer 2015-06-04 00:16:09 +00:00
parent df332d3722
commit 884da1b2a9
4 changed files with 51 additions and 64 deletions

View file

@ -207,6 +207,14 @@ public:
LocalPointerBase<T>::ptr=other.ptr;
other.ptr=temp;
}
/**
* Non-member LocalMemory swap function.
* @param p1 will get p2's pointer
* @param p2 will get p1's pointer
*/
friend inline void swap(LocalMemory<T> &p1, LocalMemory<T> &p2) U_NOEXCEPT {
p1.swap(p2);
}
/**
* Deletes the array it owns,
* and adopts (takes ownership of) the one passed in.
@ -246,16 +254,6 @@ public:
T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
};
/**
* Non-member LocalMemory swap function.
* @param p1 will get p2's pointer
* @param p2 will get p1's pointer
*/
template<typename T>
inline void swap(LocalMemory<T> &p1, LocalMemory<T> &p2) U_NOEXCEPT {
p1.swap(p2);
}
template<typename T>
inline T *LocalMemory<T>::allocateInsteadAndReset(int32_t newCapacity) {
if(newCapacity>0) {

View file

@ -266,6 +266,15 @@ public:
LocalPointerBase<T>::ptr=other.ptr;
other.ptr=temp;
}
/**
* Non-member LocalPointer swap function.
* @param p1 will get p2's pointer
* @param p2 will get p1's pointer
* @draft ICU 56
*/
friend inline void swap(LocalPointer<T> &p1, LocalPointer<T> &p2) U_NOEXCEPT {
p1.swap(p2);
}
#endif /* U_HIDE_DRAFT_API */
/**
* Deletes the object it owns,
@ -307,19 +316,6 @@ public:
#endif /* U_HIDE_DRAFT_API */
};
#ifndef U_HIDE_DRAFT_API
/**
* Non-member LocalPointer swap function.
* @param p1 will get p2's pointer
* @param p2 will get p1's pointer
* @draft ICU 56
*/
template<typename T>
inline void swap(LocalPointer<T> &p1, LocalPointer<T> &p2) U_NOEXCEPT {
p1.swap(p2);
}
#endif /* U_HIDE_DRAFT_API */
/**
* "Smart pointer" class, deletes objects via the C++ array delete[] operator.
* For most methods see the LocalPointerBase base class.
@ -422,6 +418,15 @@ public:
LocalPointerBase<T>::ptr=other.ptr;
other.ptr=temp;
}
/**
* Non-member LocalArray swap function.
* @param p1 will get p2's pointer
* @param p2 will get p1's pointer
* @draft ICU 56
*/
friend inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) U_NOEXCEPT {
p1.swap(p2);
}
#endif /* U_HIDE_DRAFT_API */
/**
* Deletes the array it owns,
@ -471,19 +476,6 @@ public:
T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
};
#ifndef U_HIDE_DRAFT_API
/**
* Non-member LocalArray swap function.
* @param p1 will get p2's pointer
* @param p2 will get p1's pointer
* @draft ICU 56
*/
template<typename T>
inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) U_NOEXCEPT {
p1.swap(p2);
}
#endif /* U_HIDE_DRAFT_API */
/**
* \def U_DEFINE_LOCAL_OPEN_POINTER
* "Smart pointer" definition macro, deletes objects via the closeFunction.
@ -531,15 +523,14 @@ inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) U_NOEXCEPT {
LocalPointerBase<Type>::ptr=other.ptr; \
other.ptr=temp; \
} \
friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
p1.swap(p2); \
} \
void adoptInstead(Type *p) { \
closeFunction(ptr); \
ptr=p; \
} \
}; \
inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
p1.swap(p2); \
} \
class LocalPointerClassName
}
#else
#define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
class LocalPointerClassName : public LocalPointerBase<Type> { \
@ -557,19 +548,15 @@ inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) U_NOEXCEPT {
LocalPointerBase<Type>::ptr=other.ptr; \
other.ptr=temp; \
} \
friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
p1.swap(p2); \
} \
void adoptInstead(Type *p) { \
closeFunction(ptr); \
ptr=p; \
} \
}; \
inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
p1.swap(p2); \
} \
class LocalPointerClassName
}
#endif
// The trailing class forward declaration at the end of U_DEFINE_LOCAL_OPEN_POINTER
// prevents a warning or error from -pedantic compilation
// due to an extra ';' after the non-member swap function definition.
U_NAMESPACE_END

View file

@ -1909,6 +1909,17 @@ public:
* @draft ICU 56
*/
void swap(UnicodeString &other) U_NOEXCEPT;
/**
* Non-member UnicodeString swap function.
* @param s1 will get s2's contents and state
* @param s2 will get s1's contents and state
* @draft ICU 56
*/
friend U_COMMON_API inline void U_EXPORT2
swap(UnicodeString &s1, UnicodeString &s2) U_NOEXCEPT {
s1.swap(s2);
}
#endif /* U_HIDE_DRAFT_API */
/**
@ -3668,19 +3679,6 @@ private:
U_COMMON_API UnicodeString U_EXPORT2
operator+ (const UnicodeString &s1, const UnicodeString &s2);
#ifndef U_HIDE_DRAFT_API
/**
* Non-member UnicodeString swap function.
* @param s1 will get s2's contents and state
* @param s2 will get s1's contents and state
* @draft ICU 56
*/
U_COMMON_API inline void U_EXPORT2
swap(UnicodeString &s1, UnicodeString &s2) U_NOEXCEPT {
s1.swap(s2);
}
#endif /* U_HIDE_DRAFT_API */
//========================================
// Inline members
//========================================

View file

@ -556,8 +556,12 @@ void UnicodeString::copyFieldsFrom(UnicodeString &src, UBool setSrcToBogus) U_NO
int16_t lengthAndFlags = fUnion.fFields.fLengthAndFlags = src.fUnion.fFields.fLengthAndFlags;
if(lengthAndFlags & kUsingStackBuffer) {
// Short string using the stack buffer, copy the contents.
uprv_memcpy(fUnion.fStackFields.fBuffer, src.fUnion.fStackFields.fBuffer,
getShortLength() * U_SIZEOF_UCHAR);
// Check for self assignment to prevent "overlap in memcpy" warnings,
// although it should be harmless to copy a buffer to itself exactly.
if(this != &src) {
uprv_memcpy(fUnion.fStackFields.fBuffer, src.fUnion.fStackFields.fBuffer,
getShortLength() * U_SIZEOF_UCHAR);
}
} else {
// In all other cases, copy all fields.
fUnion.fFields.fArray = src.fUnion.fFields.fArray;