NeverFreedPointer implementation improved and fixed for MSVC.

This commit is contained in:
John Preston 2016-04-07 20:16:02 +04:00
parent a37bbcfaca
commit a56ece4efe
3 changed files with 9 additions and 9 deletions

View File

@ -824,6 +824,13 @@ public:
} }
} }
template <typename... Args>
void makeIfNull(Args&&... args) {
if (isNull()) {
reset(new T(std::forward<Args>(args)...));
}
};
T *data() const { T *data() const {
return _p; return _p;
} }
@ -857,13 +864,6 @@ private:
}; };
template <typename T>
using NeverFreedPointerCreator = T*(*)();
template <typename T, typename... Args>
inline NeverFreedPointerCreator<T> MakeNeverFreedCreator(Args&&... args) {
return [&args...]() -> T* { return new T(std_::forward<Args>(args)...); };
}
// This pointer is used for static non-POD variables that are allocated // This pointer is used for static non-POD variables that are allocated
// on first use by constructor and are never automatically freed. // on first use by constructor and are never automatically freed.
template <typename T> template <typename T>

View File

@ -86,7 +86,7 @@ bool ClickHandler::setActive(const ClickHandlerPtr &p, ClickHandlerHost *host) {
} }
} }
if (p) { if (p) {
_active.createIfNull(MakeNeverFreedCreator<ClickHandlerPtr>()); _active.makeIfNull();
*_active = p; *_active = p;
if ((_activeHost = host)) { if ((_activeHost = host)) {
bool emitClickHandlerActiveChanged = (!_pressed || !*_pressed || *_pressed == *_active); bool emitClickHandlerActiveChanged = (!_pressed || !*_pressed || *_pressed == *_active);

View File

@ -366,7 +366,7 @@ public:
if (!_active || !*_active) { if (!_active || !*_active) {
return; return;
} }
_pressed.createIfNull(MakeNeverFreedCreator<ClickHandlerPtr>()); _pressed.makeIfNull();
*_pressed = *_active; *_pressed = *_active;
if ((_pressedHost = _activeHost)) { if ((_pressedHost = _activeHost)) {
_pressedHost->clickHandlerPressedChanged(*_pressed, true); _pressedHost->clickHandlerPressedChanged(*_pressed, true);