From e0431021b556869bdef78463fb5a56b2617a523c Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Sun, 29 Jul 2012 00:34:40 -0300 Subject: [PATCH] Huge engine change, replace all std::shared_ptrs Create a new shared pointer type stdext::shared_object_ptr and stdext::shared_obj using boost::intrusive_ptr Advantages: * half memory usage * faster and lightweight Disadvantages: * using weak_ptr is not supported anymore * compiling seems slower --- modules/game_battle/battle.lua | 8 +- modules/game_hotkeys/hotkeys_manager.lua | 2 +- modules/game_interface/gameinterface.lua | 12 +-- modules/game_interface/widgets/uigamemap.lua | 2 +- src/framework/core/binarytree.h | 2 +- src/framework/core/declarations.h | 12 +-- src/framework/core/filestream.h | 2 +- src/framework/core/module.h | 2 +- src/framework/core/modulemanager.cpp | 2 +- src/framework/graphics/animatedtexture.cpp | 2 +- src/framework/graphics/animatedtexture.h | 2 +- src/framework/graphics/bitmapfont.h | 3 +- src/framework/graphics/declarations.h | 33 +++--- src/framework/graphics/fontmanager.cpp | 1 + src/framework/graphics/framebuffer.h | 2 +- src/framework/graphics/image.h | 2 +- src/framework/graphics/particle.h | 3 +- src/framework/graphics/particleaffector.h | 3 +- src/framework/graphics/particleemitter.cpp | 9 +- src/framework/graphics/particleemitter.h | 10 +- src/framework/graphics/particlesystem.cpp | 5 +- src/framework/graphics/particlesystem.h | 2 +- src/framework/graphics/shader.h | 2 +- src/framework/graphics/texture.h | 2 +- src/framework/graphics/texturemanager.cpp | 7 +- src/framework/graphics/texturemanager.h | 2 +- src/framework/luaengine/declarations.h | 2 +- src/framework/luaengine/luabinder.h | 12 +-- src/framework/luaengine/luaobject.cpp | 2 +- src/framework/luaengine/luaobject.h | 6 +- src/framework/luaengine/luavaluecasts.h | 11 +- src/framework/luafunctions.cpp | 14 ++- src/framework/net/connection.cpp | 26 ++--- src/framework/net/connection.h | 3 +- src/framework/net/declarations.h | 11 +- src/framework/net/protocol.h | 2 +- src/framework/otml/declarations.h | 5 +- src/framework/otml/otmldocument.cpp | 2 +- src/framework/otml/otmlnode.cpp | 13 +-- src/framework/otml/otmlnode.h | 9 +- src/framework/otml/otmlparser.cpp | 3 +- src/framework/otml/otmlparser.h | 1 + src/framework/sound/declarations.h | 12 +-- src/framework/sound/soundbuffer.h | 2 +- src/framework/sound/soundfile.h | 2 +- src/framework/sound/soundsource.h | 3 +- src/framework/stdext/stdext.h | 1 + src/framework/ui/declarations.h | 20 ++-- src/framework/ui/uianchorlayout.h | 2 +- src/framework/ui/uiboxlayout.h | 2 +- src/framework/ui/uigridlayout.h | 4 +- src/framework/ui/uihorizontallayout.h | 2 +- src/framework/ui/uilayout.cpp | 7 +- src/framework/ui/uilayout.h | 15 ++- src/framework/ui/uimanager.cpp | 2 +- src/framework/ui/uimanager.h | 1 + src/framework/ui/uiverticallayout.h | 2 +- src/framework/ui/uiwidget.cpp | 106 ++++++++++--------- src/framework/ui/uiwidget.h | 8 +- src/framework/ui/uiwidgetbasestyle.cpp | 14 ++- src/main.cpp | 5 - src/otclient/animatedtext.h | 2 +- src/otclient/creature.cpp | 14 +-- src/otclient/creature.h | 5 +- src/otclient/declarations.h | 50 ++++----- src/otclient/effect.h | 2 +- src/otclient/game.cpp | 8 +- src/otclient/game.h | 4 + src/otclient/item.h | 2 +- src/otclient/localplayer.h | 2 +- src/otclient/luafunctions.cpp | 12 --- src/otclient/map.cpp | 21 ++-- src/otclient/map.h | 1 + src/otclient/mapview.h | 3 +- src/otclient/missile.h | 2 +- src/otclient/player.h | 2 +- src/otclient/statictext.h | 2 +- src/otclient/thing.cpp | 6 +- src/otclient/thing.h | 12 --- src/otclient/tile.cpp | 32 +++--- src/otclient/tile.h | 2 +- 81 files changed, 314 insertions(+), 336 deletions(-) diff --git a/modules/game_battle/battle.lua b/modules/game_battle/battle.lua index 55c1eaf0..aca33c28 100644 --- a/modules/game_battle/battle.lua +++ b/modules/game_battle/battle.lua @@ -113,13 +113,13 @@ function doCreatureFitFilters(creature) local hideSkulls = hideSkullsButton:isChecked() local hideParty = hidePartyButton:isChecked() - if hidePlayers and creature:asPlayer() then + if hidePlayers and creature:isPlayer() then return false - elseif hideNPCs and creature:asNpc() then + elseif hideNPCs and creature:isNpc() then return false - elseif hideMonsters and creature:asMonster() then + elseif hideMonsters and creature:isMonster() then return false - elseif hideSkulls and creature:asPlayer() and creature:getSkull() == SkullNone then + elseif hideSkulls and creature:isPlayer() and creature:getSkull() == SkullNone then return false elseif hideParty and creature:getShield() > ShieldWhiteBlue then return false diff --git a/modules/game_hotkeys/hotkeys_manager.lua b/modules/game_hotkeys/hotkeys_manager.lua index b6dbaa0d..a351b646 100644 --- a/modules/game_hotkeys/hotkeys_manager.lua +++ b/modules/game_hotkeys/hotkeys_manager.lua @@ -150,7 +150,7 @@ function onChooseItemMouseRelease(self, mousePosition, mouseButton) if tile then local thing = tile:getTopMoveThing() if thing then - item = thing:asItem() + item = thing:isItem() end end elseif clickedWidget:getClassName() == 'UIItem' and not clickedWidget:isVirtual() then diff --git a/modules/game_interface/gameinterface.lua b/modules/game_interface/gameinterface.lua index 54893e80..7a243ada 100644 --- a/modules/game_interface/gameinterface.lua +++ b/modules/game_interface/gameinterface.lua @@ -267,7 +267,7 @@ function createThingMenu(menuPosition, lookThing, useThing, creatureThing) end - if lookThing and not lookThing:asCreature() and not lookThing:isNotMoveable() and lookThing:isPickupable() then + if lookThing and not lookThing:isCreature() and not lookThing:isNotMoveable() and lookThing:isPickupable() then menu:addSeparator() menu:addOption(tr('Trade with ...'), function() startTradeWith(lookThing) end) end @@ -282,7 +282,7 @@ function createThingMenu(menuPosition, lookThing, useThing, creatureThing) if creatureThing then menu:addSeparator() - if creatureThing:asLocalPlayer() then + if creatureThing:isLocalPlayer() then menu:addOption(tr('Set Outfit'), function() g_game.requestOutfit() end) if creatureThing:isPartyMember() --[[and not fighting]] then @@ -310,7 +310,7 @@ function createThingMenu(menuPosition, lookThing, useThing, creatureThing) menu:addOption(tr('Stop Follow'), function() g_game.cancelFollow() end) end - if creatureThing:asPlayer() then + if creatureThing:isPlayer() then menu:addSeparator() local creatureName = creatureThing:getName() menu:addOption(tr('Message to %s', creatureName), function() g_game.openPrivateChannel(creatureName) end) @@ -322,7 +322,7 @@ function createThingMenu(menuPosition, lookThing, useThing, creatureThing) menu:addOption(tr('Add to VIP list'), function() g_game.addVip(creatureName) end) end - local localPlayerShield = localPlayer:asCreature():getShield() + local localPlayerShield = localPlayer:isCreature():getShield() local creatureShield = creatureThing:getShield() if localPlayerShield == ShieldNone or localPlayerShield == ShieldWhiteBlue then @@ -394,8 +394,8 @@ function processMouseAction(menuPosition, mouseButton, autoWalkPos, lookThing, u else if multiUseThing and keyboardModifiers == KeyboardNoModifier and mouseButton == MouseRightButton and not g_mouse.isPressed(MouseLeftButton) then local player = g_game.getLocalPlayer() - if multiUseThing:asCreature() and multiUseThing:asCreature() ~= player then - g_game.attack(multiUseThing:asCreature()) + if multiUseThing:isCreature() and multiUseThing:isCreature() ~= player then + g_game.attack(multiUseThing:isCreature()) return true elseif multiUseThing:isContainer() then if multiUseThing:getParentContainer() then diff --git a/modules/game_interface/widgets/uigamemap.lua b/modules/game_interface/widgets/uigamemap.lua index 21c07dad..d0bbf7fb 100644 --- a/modules/game_interface/widgets/uigamemap.lua +++ b/modules/game_interface/widgets/uigamemap.lua @@ -38,7 +38,7 @@ function UIGameMap:onDrop(widget, mousePos) local thingPos = thing:getPosition() if thingPos.x == toPos.x and thingPos.y == toPos.y and thingPos.z == toPos.z then return false end - if thing:asItem() and thing:getCount() > 1 then + if thing:isItem() and thing:getCount() > 1 then modules.game_interface.moveStackableItem(thing, toPos) else g_game.move(thing, toPos, 1) diff --git a/src/framework/core/binarytree.h b/src/framework/core/binarytree.h index a0966bb6..7adabf16 100644 --- a/src/framework/core/binarytree.h +++ b/src/framework/core/binarytree.h @@ -33,7 +33,7 @@ enum { BINARYTREE_NODE_END = 0xFF }; -class BinaryTree +class BinaryTree : public stdext::shared_object { public: BinaryTree(const FileStreamPtr& fin); diff --git a/src/framework/core/declarations.h b/src/framework/core/declarations.h index d13467c3..e6c90614 100644 --- a/src/framework/core/declarations.h +++ b/src/framework/core/declarations.h @@ -33,13 +33,11 @@ class ScheduledEvent; class FileStream; class BinaryTree; -typedef std::shared_ptr ModulePtr; -typedef std::shared_ptr EventPtr; -typedef std::shared_ptr ScheduledEventPtr; -typedef std::shared_ptr FileStreamPtr; -typedef std::shared_ptr BinaryTreePtr; - -typedef std::weak_ptr BinaryTreeWeakPtr; +typedef stdext::shared_object_ptr ModulePtr; +typedef stdext::shared_object_ptr EventPtr; +typedef stdext::shared_object_ptr ScheduledEventPtr; +typedef stdext::shared_object_ptr FileStreamPtr; +typedef stdext::shared_object_ptr BinaryTreePtr; typedef std::vector BinaryTreeVec; diff --git a/src/framework/core/filestream.h b/src/framework/core/filestream.h index 367d9f41..2d237adb 100644 --- a/src/framework/core/filestream.h +++ b/src/framework/core/filestream.h @@ -61,7 +61,7 @@ public: void addString(const std::string& v); BinaryTreePtr makeTree(); - FileStreamPtr asFileStream() { return std::static_pointer_cast(shared_from_this()); } + FileStreamPtr asFileStream() { return self_cast(); } private: void checkWrite(); diff --git a/src/framework/core/module.h b/src/framework/core/module.h index 7ac37c31..24b00a34 100644 --- a/src/framework/core/module.h +++ b/src/framework/core/module.h @@ -56,7 +56,7 @@ public: int getAutoLoadPriority() { return m_autoLoadPriority; } // @dontbind - ModulePtr asModule() { return std::static_pointer_cast(shared_from_this()); } + ModulePtr asModule() { return self_cast(); } protected: void discover(const OTMLNodePtr& moduleNode); diff --git a/src/framework/core/modulemanager.cpp b/src/framework/core/modulemanager.cpp index 786a75e0..5a45b820 100644 --- a/src/framework/core/modulemanager.cpp +++ b/src/framework/core/modulemanager.cpp @@ -46,7 +46,7 @@ void ModuleManager::discoverModules() if(boost::ends_with(moduleFile, ".otmod")) { ModulePtr module = discoverModule("/" + moduleDir + "/" + moduleFile); if(module && module->isAutoLoad()) - m_autoLoadModules.insert(make_pair(module->getAutoLoadPriority(), module)); + m_autoLoadModules.insert(std::make_pair(module->getAutoLoadPriority(), module)); } } } diff --git a/src/framework/graphics/animatedtexture.cpp b/src/framework/graphics/animatedtexture.cpp index 5cc6c8d3..63c5f459 100644 --- a/src/framework/graphics/animatedtexture.cpp +++ b/src/framework/graphics/animatedtexture.cpp @@ -70,7 +70,7 @@ void AnimatedTexture::processAnimation() AnimatedTexturePtr self = asAnimatedTexture(); // continue to animate only if something still referencing this texture - if(self.use_count() > 1) + if(self->ref_count() > 1) g_dispatcher.scheduleEvent(std::bind(&AnimatedTexture::processAnimation, self), m_framesDelay[m_currentFrame]); } */ \ No newline at end of file diff --git a/src/framework/graphics/animatedtexture.h b/src/framework/graphics/animatedtexture.h index 23b7acf0..aef89173 100644 --- a/src/framework/graphics/animatedtexture.h +++ b/src/framework/graphics/animatedtexture.h @@ -34,7 +34,7 @@ public: void enableBilinearFilter(); void processAnimation(); - AnimatedTexturePtr asAnimatedTexture() { return std::static_pointer_cast(shared_from_this()); } + AnimatedTexturePtr asAnimatedTexture() { return self_cast(); } private: std::vector m_framesTextureId; diff --git a/src/framework/graphics/bitmapfont.h b/src/framework/graphics/bitmapfont.h index 90ffc403..d30be619 100644 --- a/src/framework/graphics/bitmapfont.h +++ b/src/framework/graphics/bitmapfont.h @@ -24,11 +24,12 @@ #define BITMAPFONT_H #include "declarations.h" +#include "texture.h" #include #include -class BitmapFont +class BitmapFont : public stdext::shared_object { public: BitmapFont(const std::string& name) : m_name(name) { } diff --git a/src/framework/graphics/declarations.h b/src/framework/graphics/declarations.h index 6eb9361e..1533faf4 100644 --- a/src/framework/graphics/declarations.h +++ b/src/framework/graphics/declarations.h @@ -44,24 +44,21 @@ class ParticleSystem; class ParticleEffect; class ParticleEffectType; -typedef std::weak_ptr TextureWeakPtr; -typedef std::weak_ptr ParticleSystemWeakPtr; - -typedef std::shared_ptr ImagePtr; -typedef std::shared_ptr TexturePtr; -typedef std::shared_ptr AnimatedTexturePtr; -typedef std::shared_ptr BitmapFontPtr; -typedef std::shared_ptr CachedTextPtr; -typedef std::shared_ptr FrameBufferPtr; -typedef std::shared_ptr ShaderPtr; -typedef std::shared_ptr ShaderProgramPtr; -typedef std::shared_ptr PainterShaderProgramPtr; -typedef std::shared_ptr ParticlePtr; -typedef std::shared_ptr ParticleEmitterPtr; -typedef std::shared_ptr ParticleAffectorPtr; -typedef std::shared_ptr ParticleSystemPtr; -typedef std::shared_ptr ParticleEffectPtr; -typedef std::shared_ptr ParticleEffectTypePtr; +typedef stdext::shared_object_ptr ImagePtr; +typedef stdext::shared_object_ptr TexturePtr; +typedef stdext::shared_object_ptr AnimatedTexturePtr; +typedef stdext::shared_object_ptr BitmapFontPtr; +typedef stdext::shared_object_ptr CachedTextPtr; +typedef stdext::shared_object_ptr FrameBufferPtr; +typedef stdext::shared_object_ptr ShaderPtr; +typedef stdext::shared_object_ptr ShaderProgramPtr; +typedef stdext::shared_object_ptr PainterShaderProgramPtr; +typedef stdext::shared_object_ptr ParticlePtr; +typedef stdext::shared_object_ptr ParticleEmitterPtr; +typedef stdext::shared_object_ptr ParticleAffectorPtr; +typedef stdext::shared_object_ptr ParticleSystemPtr; +typedef stdext::shared_object_ptr ParticleEffectPtr; +typedef stdext::shared_object_ptr ParticleEffectTypePtr; typedef std::vector ShaderList; #endif diff --git a/src/framework/graphics/fontmanager.cpp b/src/framework/graphics/fontmanager.cpp index 786f069f..15367330 100644 --- a/src/framework/graphics/fontmanager.cpp +++ b/src/framework/graphics/fontmanager.cpp @@ -21,6 +21,7 @@ */ #include "fontmanager.h" +#include "texture.h" #include #include diff --git a/src/framework/graphics/framebuffer.h b/src/framework/graphics/framebuffer.h index ce1c567d..8b6e69b2 100644 --- a/src/framework/graphics/framebuffer.h +++ b/src/framework/graphics/framebuffer.h @@ -26,7 +26,7 @@ #include "declarations.h" #include "texture.h" -class FrameBuffer +class FrameBuffer : public stdext::shared_object { protected: FrameBuffer(); diff --git a/src/framework/graphics/image.h b/src/framework/graphics/image.h index 6d00139b..1762785d 100644 --- a/src/framework/graphics/image.h +++ b/src/framework/graphics/image.h @@ -26,7 +26,7 @@ #include "declarations.h" #include -class Image +class Image : public stdext::shared_object { public: Image(const Size& size, int bpp = 4, uint8 *pixels = nullptr); diff --git a/src/framework/graphics/particle.h b/src/framework/graphics/particle.h index c4f1e829..8a86f743 100644 --- a/src/framework/graphics/particle.h +++ b/src/framework/graphics/particle.h @@ -27,7 +27,8 @@ #include "painter.h" #include -class Particle { +class Particle : public stdext::shared_object +{ public: Particle(const Point& pos, const Size& startSize, const Size& finalSize, const PointF& velocity, const PointF& acceleration, float duration, float ignorePhysicsAfter, const std::vector& colors, const std::vector& colorsStops, Painter::CompositionMode compositionMode = Painter::CompositionMode_Normal, TexturePtr texture = nullptr); diff --git a/src/framework/graphics/particleaffector.h b/src/framework/graphics/particleaffector.h index 810b08bc..e2692dc8 100644 --- a/src/framework/graphics/particleaffector.h +++ b/src/framework/graphics/particleaffector.h @@ -26,7 +26,8 @@ #include "declarations.h" #include -class ParticleAffector { +class ParticleAffector : public stdext::shared_object +{ public: ParticleAffector(); diff --git a/src/framework/graphics/particleemitter.cpp b/src/framework/graphics/particleemitter.cpp index 8b33e37f..1d503986 100644 --- a/src/framework/graphics/particleemitter.cpp +++ b/src/framework/graphics/particleemitter.cpp @@ -26,10 +26,8 @@ #include #include -ParticleEmitter::ParticleEmitter(const ParticleSystemPtr& parent) +ParticleEmitter::ParticleEmitter() { - m_parent = parent; - m_position = Point(0, 0); m_duration = -1; m_delay = 0; @@ -179,7 +177,7 @@ bool ParticleEmitter::load(const OTMLNodePtr& node) return true; } -void ParticleEmitter::update(float elapsedTime) +void ParticleEmitter::update(float elapsedTime, const ParticleSystemPtr& system) { // check if finished if(m_duration >= 0 && m_elapsedTime >= m_duration + m_delay) { @@ -214,8 +212,7 @@ void ParticleEmitter::update(float elapsedTime) float pAccelerationAngle = stdext::random_range(m_pMinAccelerationAngle, m_pMaxAccelerationAngle); PointF pAcceleration(pAccelerationAbs * cos(pAccelerationAngle), pAccelerationAbs * sin(pAccelerationAngle)); - ParticleSystemPtr particleSystem = m_parent.lock(); - particleSystem->addParticle(ParticlePtr(new Particle(pPosition, m_pStartSize, m_pFinalSize, pVelocity, pAcceleration, pDuration, m_pIgnorePhysicsAfter, m_pColors, m_pColorsStops, m_pCompositionMode, m_pTexture))); + system->addParticle(ParticlePtr(new Particle(pPosition, m_pStartSize, m_pFinalSize, pVelocity, pAcceleration, pDuration, m_pIgnorePhysicsAfter, m_pColors, m_pColorsStops, m_pCompositionMode, m_pTexture))); } } diff --git a/src/framework/graphics/particleemitter.h b/src/framework/graphics/particleemitter.h index 2e618524..c40999b4 100644 --- a/src/framework/graphics/particleemitter.h +++ b/src/framework/graphics/particleemitter.h @@ -29,20 +29,18 @@ #include #include -class ParticleEmitter { +class ParticleEmitter : public stdext::shared_object +{ public: - - ParticleEmitter(const ParticleSystemPtr& parent); + ParticleEmitter(); bool load(const OTMLNodePtr& node); - void update(float elapsedTime); + void update(float elapsedTime, const ParticleSystemPtr& system); bool hasFinished() { return m_finished; } private: - ParticleSystemWeakPtr m_parent; - // self related Point m_position; float m_duration, m_delay; diff --git a/src/framework/graphics/particlesystem.cpp b/src/framework/graphics/particlesystem.cpp index f8a21123..664ea844 100644 --- a/src/framework/graphics/particlesystem.cpp +++ b/src/framework/graphics/particlesystem.cpp @@ -34,7 +34,7 @@ bool ParticleSystem::load(const OTMLNodePtr& node) { for(const OTMLNodePtr& childNode : node->children()) { if(childNode->tag() == "Emitter") { - ParticleEmitterPtr emitter = ParticleEmitterPtr(new ParticleEmitter(shared_from_this())); + ParticleEmitterPtr emitter = ParticleEmitterPtr(new ParticleEmitter()); if(!emitter->load(childNode)) return false; m_emitters.push_back(emitter); @@ -84,6 +84,7 @@ void ParticleSystem::update() return; m_lastUpdateTime = g_clock.seconds() - std::fmod(elapsedTime, delay); + auto self = self_cast(); for(int i = 0; i < elapsedTime / delay; ++i) { // update emitters @@ -93,7 +94,7 @@ void ParticleSystem::update() it = m_emitters.erase(it); continue; } - emitter->update(delay); + emitter->update(delay, self); ++it; } diff --git a/src/framework/graphics/particlesystem.h b/src/framework/graphics/particlesystem.h index 3a065db8..a432b6ef 100644 --- a/src/framework/graphics/particlesystem.h +++ b/src/framework/graphics/particlesystem.h @@ -27,7 +27,7 @@ #include "particleemitter.h" #include "particleaffector.h" -class ParticleSystem : public std::enable_shared_from_this { +class ParticleSystem : public stdext::shared_object { public: ParticleSystem(); diff --git a/src/framework/graphics/shader.h b/src/framework/graphics/shader.h index 414cd428..d3f5d703 100644 --- a/src/framework/graphics/shader.h +++ b/src/framework/graphics/shader.h @@ -25,7 +25,7 @@ #include "declarations.h" -class Shader +class Shader : public stdext::shared_object { public: enum ShaderType { diff --git a/src/framework/graphics/texture.h b/src/framework/graphics/texture.h index e7dcca7e..f67d8538 100644 --- a/src/framework/graphics/texture.h +++ b/src/framework/graphics/texture.h @@ -25,7 +25,7 @@ #include "declarations.h" -class Texture : public std::enable_shared_from_this +class Texture : public stdext::shared_object { public: Texture(); diff --git a/src/framework/graphics/texturemanager.cpp b/src/framework/graphics/texturemanager.cpp index 98796958..6f35be54 100644 --- a/src/framework/graphics/texturemanager.cpp +++ b/src/framework/graphics/texturemanager.cpp @@ -41,7 +41,7 @@ void TextureManager::terminate() // check for leaks int refs = 0; for(const auto& it : m_textures) - if(it.second.use_count() > 1) + if(it.second->ref_count() > 1) refs++; if(refs > 0) g_logger.debug(stdext::format("%d textures references left", refs)); @@ -65,10 +65,7 @@ TexturePtr TextureManager::getTexture(const std::string& fileName) // check if the texture is already loaded auto it = m_textures.find(filePath); if(it != m_textures.end()) { - if(it->second.expired()) - m_textures.erase(it); - else - texture = it->second.lock(); + texture = it->second; } // texture not found, load it diff --git a/src/framework/graphics/texturemanager.h b/src/framework/graphics/texturemanager.h index e83d164c..79ee2f95 100644 --- a/src/framework/graphics/texturemanager.h +++ b/src/framework/graphics/texturemanager.h @@ -38,7 +38,7 @@ public: private: TexturePtr loadPNG(std::stringstream& file); - std::unordered_map m_textures; + std::unordered_map m_textures; TexturePtr m_emptyTexture; }; diff --git a/src/framework/luaengine/declarations.h b/src/framework/luaengine/declarations.h index 92ec4090..4b3e322b 100644 --- a/src/framework/luaengine/declarations.h +++ b/src/framework/luaengine/declarations.h @@ -30,6 +30,6 @@ class LuaObject; typedef std::function LuaCppFunction; typedef std::unique_ptr LuaCppFunctionPtr; -typedef std::shared_ptr LuaObjectPtr; +typedef stdext::shared_object_ptr LuaObjectPtr; #endif diff --git a/src/framework/luaengine/luabinder.h b/src/framework/luaengine/luabinder.h index 6155e34b..c230743b 100644 --- a/src/framework/luaengine/luabinder.h +++ b/src/framework/luaengine/luabinder.h @@ -152,18 +152,18 @@ namespace luabinder /// Create member function lambdas template - std::function&, const Args&...)> make_mem_func(Ret (C::* f)(Args...)) { + std::function&, const Args&...)> make_mem_func(Ret (C::* f)(Args...)) { auto mf = std::mem_fn(f); - return [=](const std::shared_ptr& obj, const Args&... args) mutable -> Ret { + return [=](const stdext::shared_object_ptr& obj, const Args&... args) mutable -> Ret { if(!obj) throw LuaException("failed to call a member function because the passed object is nil"); return mf(obj.get(), args...); }; } template - std::function&, const Args&...)> make_mem_func(void (C::* f)(Args...)) { + std::function&, const Args&...)> make_mem_func(void (C::* f)(Args...)) { auto mf = std::mem_fn(f); - return [=](const std::shared_ptr& obj, const Args&... args) mutable -> void { + return [=](const stdext::shared_object_ptr& obj, const Args&... args) mutable -> void { if(!obj) throw LuaException("failed to call a member function because the passed object is nil"); mf(obj.get(), args...); @@ -186,7 +186,7 @@ namespace luabinder /// Bind member functions template LuaCppFunction bind_mem_fun(Ret (FC::* f)(Args...)) { - typedef typename std::tuple, typename remove_const_ref::type...> Tuple; + typedef typename std::tuple, typename remove_const_ref::type...> Tuple; auto lambda = make_mem_func(f); return bind_fun_specializer::type, decltype(lambda), @@ -209,7 +209,7 @@ namespace luabinder LuaCppFunction bind_mem_fun(int (C::*f)(LuaInterface*)) { auto mf = std::mem_fn(f); return [=](LuaInterface* lua) -> int { - auto obj = lua->castValue>(1); + auto obj = lua->castValue>(1); lua->remove(1); return mf(obj, lua); }; diff --git a/src/framework/luaengine/luaobject.cpp b/src/framework/luaengine/luaobject.cpp index c43190a9..27944b3f 100644 --- a/src/framework/luaengine/luaobject.cpp +++ b/src/framework/luaengine/luaobject.cpp @@ -108,5 +108,5 @@ void LuaObject::luaGetFieldsTable() int LuaObject::getUseCount() { - return shared_from_this().use_count() - 1; + return ref_count(); } diff --git a/src/framework/luaengine/luaobject.h b/src/framework/luaengine/luaobject.h index e26ee774..242044b5 100644 --- a/src/framework/luaengine/luaobject.h +++ b/src/framework/luaengine/luaobject.h @@ -27,8 +27,7 @@ /// LuaObject, all script-able classes have it as base // @bindclass -#pragma pack(push,1) // disable memory alignment -class LuaObject : public std::enable_shared_from_this +class LuaObject : public stdext::shared_object { public: LuaObject(); @@ -81,7 +80,7 @@ public: return stdext::demangle_name(typeid(*this).name()); } - LuaObjectPtr asLuaObject() { return shared_from_this(); } + LuaObjectPtr asLuaObject() { return self_cast(); } void operator=(const LuaObject& other) { } @@ -89,7 +88,6 @@ private: int m_fieldsTableRef; int m_metatableRef; }; -#pragma pack(pop) #include "luainterface.h" diff --git a/src/framework/luaengine/luavaluecasts.h b/src/framework/luaengine/luavaluecasts.h index 15377100..f0b51ce2 100644 --- a/src/framework/luaengine/luavaluecasts.h +++ b/src/framework/luaengine/luavaluecasts.h @@ -111,7 +111,7 @@ bool luavalue_cast(int index, LuaObjectPtr& obj); template typename std::enable_if::value, bool>::type -luavalue_cast(int index, std::shared_ptr& ptr); +luavalue_cast(int index, stdext::shared_object_ptr& ptr); // std::function template @@ -156,7 +156,7 @@ int push_internal_luavalue(const std::tuple& tuple); #include "luaexception.h" #include "luainterface.h" - +#include "luaobject.h" template int push_internal_luavalue(T v) { @@ -181,11 +181,14 @@ push_luavalue(const T& obj) { template typename std::enable_if::value, bool>::type -luavalue_cast(int index, std::shared_ptr& ptr) { +luavalue_cast(int index, stdext::shared_object_ptr& ptr) { LuaObjectPtr obj; if(!luavalue_cast(index, obj)) return false; - ptr = std::dynamic_pointer_cast(obj); + if(obj) + ptr = obj->dynamic_self_cast(); + else + ptr = nullptr; return true; } diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index 6597f0f5..e29c6218 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -340,7 +340,6 @@ void Application::registerLuaFunctions() g_lua.bindClassMemberFunction("recursiveGetChildrenByPos", &UIWidget::recursiveGetChildrenByPos); g_lua.bindClassMemberFunction("recursiveGetChildrenByMarginPos", &UIWidget::recursiveGetChildrenByMarginPos); g_lua.bindClassMemberFunction("backwardsGetWidgetById", &UIWidget::backwardsGetWidgetById); - g_lua.bindClassMemberFunction("asUIWidget", &UIWidget::asUIWidget); g_lua.bindClassMemberFunction("resize", &UIWidget::resize); g_lua.bindClassMemberFunction("move", &UIWidget::move); g_lua.bindClassMemberFunction("hide", &UIWidget::hide); @@ -541,12 +540,11 @@ void Application::registerLuaFunctions() g_lua.bindClassMemberFunction("getParentWidget", &UILayout::getParentWidget); g_lua.bindClassMemberFunction("isUpdateDisabled", &UILayout::isUpdateDisabled); g_lua.bindClassMemberFunction("isUpdating", &UILayout::isUpdating); - g_lua.bindClassMemberFunction("asUILayout", &UILayout::asUILayout); - g_lua.bindClassMemberFunction("asUIAnchorLayout", &UILayout::asUIAnchorLayout); - g_lua.bindClassMemberFunction("asUIBoxLayout", &UILayout::asUIBoxLayout); - g_lua.bindClassMemberFunction("asUIHorizontalLayout", &UILayout::asUIHorizontalLayout); - g_lua.bindClassMemberFunction("asUIVerticalLayout", &UILayout::asUIVerticalLayout); - g_lua.bindClassMemberFunction("asUIGridLayout", &UILayout::asUIGridLayout); + g_lua.bindClassMemberFunction("isUIAnchorLayout", &UILayout::isUIAnchorLayout); + g_lua.bindClassMemberFunction("isUIBoxLayout", &UILayout::isUIBoxLayout); + g_lua.bindClassMemberFunction("isUIHorizontalLayout", &UILayout::isUIHorizontalLayout); + g_lua.bindClassMemberFunction("isUIVerticalLayout", &UILayout::isUIVerticalLayout); + g_lua.bindClassMemberFunction("isUIGridLayout", &UILayout::isUIGridLayout); // UIBoxLayout g_lua.registerClass(); @@ -573,7 +571,7 @@ void Application::registerLuaFunctions() g_lua.bindClassMemberFunction("setFlow", &UIGridLayout::setFlow); g_lua.bindClassMemberFunction("setNumColumns", &UIGridLayout::setNumColumns); g_lua.bindClassMemberFunction("setNumLines", &UIGridLayout::setNumLines); - g_lua.bindClassMemberFunction("asUIGridLayout", &UIGridLayout::asUIGridLayout); + g_lua.bindClassMemberFunction("isUIGridLayout", &UIGridLayout::isUIGridLayout); // UIAnchorLayout g_lua.registerClass(); diff --git a/src/framework/net/connection.cpp b/src/framework/net/connection.cpp index 0bd08a5b..2266bd7d 100644 --- a/src/framework/net/connection.cpp +++ b/src/framework/net/connection.cpp @@ -65,23 +65,23 @@ void Connection::connect(const std::string& host, uint16 port, const std::functi asio::ip::tcp::resolver::query query(host, stdext::unsafe_cast(port)); - auto weakSelf = ConnectionWeakPtr(shared_from_this()); + auto self = asConnection(); m_resolver.async_resolve(query, [=](const boost::system::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator) { - if(!weakSelf.lock()) + if(self->is_unique_ref()) return; m_readTimer.cancel(); if(!error) { - m_socket.async_connect(*endpointIterator, std::bind(&Connection::onConnect, shared_from_this(), std::placeholders::_1)); + m_socket.async_connect(*endpointIterator, std::bind(&Connection::onConnect, asConnection(), std::placeholders::_1)); m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT)); - m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1)); + m_readTimer.async_wait(std::bind(&Connection::onTimeout, asConnection(), std::placeholders::_1)); } else handleError(error); }); m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT)); - m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1)); + m_readTimer.async_wait(std::bind(&Connection::onTimeout, asConnection(), std::placeholders::_1)); } void Connection::close() @@ -124,20 +124,20 @@ void Connection::write(uint8* buffer, uint16 size) m_sendBufferSize += size; if(!m_sendEvent || m_sendEvent->isExecuted() || m_sendEvent->isCanceled()) { - auto weakSelf = ConnectionWeakPtr(shared_from_this()); + auto self = asConnection(); // wait 1 ms to do the real send m_sendEvent = g_dispatcher.scheduleEvent([=] { - if(!weakSelf.lock()) + if(self->is_unique_ref()) return; //m_writeTimer.cancel(); asio::async_write(m_socket, asio::buffer(m_sendBuffer, m_sendBufferSize), - std::bind(&Connection::onWrite, shared_from_this(), std::placeholders::_1, std::placeholders::_2)); + std::bind(&Connection::onWrite, asConnection(), std::placeholders::_1, std::placeholders::_2)); m_writeTimer.expires_from_now(boost::posix_time::seconds(WRITE_TIMEOUT)); - m_writeTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1)); + m_writeTimer.async_wait(std::bind(&Connection::onTimeout, asConnection(), std::placeholders::_1)); m_sendBufferSize = 0; }, SEND_INTERVAL); @@ -155,10 +155,10 @@ void Connection::read(uint16 bytes, const RecvCallback& callback) asio::async_read(m_socket, asio::buffer(m_recvBuffer, bytes), - std::bind(&Connection::onRecv, shared_from_this(), std::placeholders::_1, bytes)); + std::bind(&Connection::onRecv, asConnection(), std::placeholders::_1, bytes)); m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT)); - m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1)); + m_readTimer.async_wait(std::bind(&Connection::onTimeout, asConnection(), std::placeholders::_1)); } void Connection::read_some(const RecvCallback& callback) @@ -171,10 +171,10 @@ void Connection::read_some(const RecvCallback& callback) m_recvCallback = callback; m_socket.async_read_some(asio::buffer(m_recvBuffer, RECV_BUFFER_SIZE), - std::bind(&Connection::onRecv, shared_from_this(), std::placeholders::_1, std::placeholders::_2)); + std::bind(&Connection::onRecv, asConnection(), std::placeholders::_1, std::placeholders::_2)); m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT)); - m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1)); + m_readTimer.async_wait(std::bind(&Connection::onTimeout, asConnection(), std::placeholders::_1)); } void Connection::onConnect(const boost::system::error_code& error) diff --git a/src/framework/net/connection.h b/src/framework/net/connection.h index 06093cfb..b91af883 100644 --- a/src/framework/net/connection.h +++ b/src/framework/net/connection.h @@ -28,7 +28,7 @@ #include #include -class Connection : public std::enable_shared_from_this, boost::noncopyable +class Connection : public stdext::shared_object { typedef std::function ErrorCallback; typedef std::function RecvCallback; @@ -61,6 +61,7 @@ public: bool isConnecting() { return m_connecting; } bool isConnected() { return m_connected; } + ConnectionPtr asConnection() { return self_cast(); } protected: void onConnect(const boost::system::error_code& error); void onWrite(const boost::system::error_code& error, size_t); diff --git a/src/framework/net/declarations.h b/src/framework/net/declarations.h index 6ab486b9..e1c189ff 100644 --- a/src/framework/net/declarations.h +++ b/src/framework/net/declarations.h @@ -34,11 +34,10 @@ class Connection; class Protocol; class Server; -typedef std::shared_ptr InputMessagePtr; -typedef std::shared_ptr OutputMessagePtr; -typedef std::shared_ptr ConnectionPtr; -typedef std::weak_ptr ConnectionWeakPtr; -typedef std::shared_ptr ProtocolPtr; -typedef std::shared_ptr ServerPtr; +typedef stdext::shared_object_ptr InputMessagePtr; +typedef stdext::shared_object_ptr OutputMessagePtr; +typedef stdext::shared_object_ptr ConnectionPtr; +typedef stdext::shared_object_ptr ProtocolPtr; +typedef stdext::shared_object_ptr ServerPtr; #endif diff --git a/src/framework/net/protocol.h b/src/framework/net/protocol.h index 90c04d81..1622acd6 100644 --- a/src/framework/net/protocol.h +++ b/src/framework/net/protocol.h @@ -51,7 +51,7 @@ public: virtual void send(const OutputMessagePtr& outputMessage); void recv(); - ProtocolPtr asProtocol() { return std::static_pointer_cast(shared_from_this()); } + ProtocolPtr asProtocol() { return self_cast(); } protected: virtual void onConnect(); diff --git a/src/framework/otml/declarations.h b/src/framework/otml/declarations.h index 740cef4a..35de3b56 100644 --- a/src/framework/otml/declarations.h +++ b/src/framework/otml/declarations.h @@ -30,9 +30,8 @@ class OTMLDocument; class OTMLParser; class OTMLEmitter; -typedef std::shared_ptr OTMLNodePtr; -typedef std::shared_ptr OTMLDocumentPtr; -typedef std::weak_ptr OTMLNodeWeakPtr; +typedef stdext::shared_object_ptr OTMLNodePtr; +typedef stdext::shared_object_ptr OTMLDocumentPtr; typedef std::vector OTMLNodeList; #endif diff --git a/src/framework/otml/otmldocument.cpp b/src/framework/otml/otmldocument.cpp index 2235b949..e7302a8c 100644 --- a/src/framework/otml/otmldocument.cpp +++ b/src/framework/otml/otmldocument.cpp @@ -52,7 +52,7 @@ OTMLDocumentPtr OTMLDocument::parse(std::istream& in, const std::string& source) std::string OTMLDocument::emit() { - return OTMLEmitter::emitNode(shared_from_this()) + "\n"; + return OTMLEmitter::emitNode(asOTMLNode()) + "\n"; } bool OTMLDocument::save(const std::string& fileName) diff --git a/src/framework/otml/otmlnode.cpp b/src/framework/otml/otmlnode.cpp index dbd1be8e..d6afd9dc 100644 --- a/src/framework/otml/otmlnode.cpp +++ b/src/framework/otml/otmlnode.cpp @@ -77,14 +77,14 @@ OTMLNodePtr OTMLNode::at(const std::string& childTag) } } if(!res) - throw OTMLException(shared_from_this(), stdext::format("child node with tag '%s' not found", childTag)); + throw OTMLException(asOTMLNode(), stdext::format("child node with tag '%s' not found", childTag)); return res; } OTMLNodePtr OTMLNode::atIndex(int childIndex) { if(childIndex >= size() || childIndex < 0) - throw OTMLException(shared_from_this(), stdext::mkstr("child node with index '%d' not found", childIndex)); + throw OTMLException(asOTMLNode(), stdext::mkstr("child node with index '%d' not found", childIndex)); return m_children[childIndex]; } @@ -109,7 +109,6 @@ void OTMLNode::addChild(const OTMLNodePtr& newChild) while(it != m_children.end()) { OTMLNodePtr node = (*it); if(node != newChild && node->tag() == newChild->tag()) { - node->setParent(nullptr); it = m_children.erase(it); } else ++it; @@ -120,7 +119,6 @@ void OTMLNode::addChild(const OTMLNodePtr& newChild) } m_children.push_back(newChild); - newChild->setParent(shared_from_this()); } bool OTMLNode::removeChild(const OTMLNodePtr& oldChild) @@ -128,7 +126,6 @@ bool OTMLNode::removeChild(const OTMLNodePtr& oldChild) auto it = std::find(m_children.begin(), m_children.end(), oldChild); if(it != m_children.end()) { m_children.erase(it); - oldChild->setParent(nullptr); return true; } return false; @@ -138,8 +135,6 @@ bool OTMLNode::replaceChild(const OTMLNodePtr& oldChild, const OTMLNodePtr& newC { auto it = std::find(m_children.begin(), m_children.end(), oldChild); if(it != m_children.end()) { - oldChild->setParent(nullptr); - newChild->setParent(shared_from_this()); it = m_children.erase(it); m_children.insert(it, newChild); return true; @@ -169,8 +164,6 @@ void OTMLNode::merge(const OTMLNodePtr& node) void OTMLNode::clear() { - for(const OTMLNodePtr& child : m_children) - child->setParent(nullptr); m_children.clear(); } @@ -198,6 +191,6 @@ OTMLNodePtr OTMLNode::clone() std::string OTMLNode::emit() { - return OTMLEmitter::emitNode(shared_from_this(), 0); + return OTMLEmitter::emitNode(asOTMLNode(), 0); } diff --git a/src/framework/otml/otmlnode.h b/src/framework/otml/otmlnode.h index 82fc088d..6e9fac2a 100644 --- a/src/framework/otml/otmlnode.h +++ b/src/framework/otml/otmlnode.h @@ -25,7 +25,7 @@ #include "declarations.h" -class OTMLNode : public std::enable_shared_from_this +class OTMLNode : public stdext::shared_object { public: virtual ~OTMLNode() { } @@ -35,7 +35,6 @@ public: std::string tag() { return m_tag; } int size() { return m_children.size(); } - OTMLNodePtr parent() { return m_parent.lock(); } std::string source() { return m_source; } std::string rawValue() { return m_value; } @@ -52,7 +51,6 @@ public: void setValue(const std::string& value) { m_value = value; } void setNull(bool null) { m_null = null; } void setUnique(bool unique) { m_unique = unique; } - void setParent(const OTMLNodePtr& parent) { m_parent = parent; } void setSource(const std::string& source) { m_source = source; } OTMLNodePtr get(const std::string& childTag); @@ -91,11 +89,12 @@ public: virtual std::string emit(); + OTMLNodePtr asOTMLNode() { return self_cast(); } + protected: OTMLNode() : m_unique(false), m_null(false) { } OTMLNodeList m_children; - OTMLNodeWeakPtr m_parent; std::string m_tag; std::string m_value; std::string m_source; @@ -123,7 +122,7 @@ template T OTMLNode::value() { T ret; if(!stdext::cast(m_value, ret)) - throw OTMLException(shared_from_this(), stdext::format("failed to cast node value '%s' to type '%s'", m_value, stdext::demangle_type())); + throw OTMLException(asOTMLNode(), stdext::format("failed to cast node value '%s' to type '%s'", m_value, stdext::demangle_type())); return ret; } diff --git a/src/framework/otml/otmlparser.cpp b/src/framework/otml/otmlparser.cpp index d205e60d..cfc2c3e9 100644 --- a/src/framework/otml/otmlparser.cpp +++ b/src/framework/otml/otmlparser.cpp @@ -96,7 +96,7 @@ void OTMLParser::parseLine(std::string line) // a depth below, change parent to previous parent } else if(depth < currentDepth) { for(int i=0;iparent(); + currentParent = parentMap[currentParent]; // if it isn't the current depth, it's a syntax error } else if(depth != currentDepth) throw OTMLException(doc, "invalid indentation depth, are you indenting correctly?", currentLine); @@ -198,5 +198,6 @@ void OTMLParser::parseNode(const std::string& data) } currentParent->addChild(node); + parentMap[node] = currentParent; previousNode = node; } diff --git a/src/framework/otml/otmlparser.h b/src/framework/otml/otmlparser.h index 39871c72..1d37b201 100644 --- a/src/framework/otml/otmlparser.h +++ b/src/framework/otml/otmlparser.h @@ -48,6 +48,7 @@ private: int currentLine; OTMLDocumentPtr doc; OTMLNodePtr currentParent; + std::unordered_map parentMap; OTMLNodePtr previousNode; std::istream& in; }; diff --git a/src/framework/sound/declarations.h b/src/framework/sound/declarations.h index aa094a8e..251d7790 100644 --- a/src/framework/sound/declarations.h +++ b/src/framework/sound/declarations.h @@ -38,11 +38,11 @@ class StreamSoundSource; class CombinedSoundSource; class OggSoundFile; -typedef std::shared_ptr SoundSourcePtr; -typedef std::shared_ptr SoundFilePtr; -typedef std::shared_ptr SoundBufferPtr; -typedef std::shared_ptr StreamSoundSourcePtr; -typedef std::shared_ptr CombinedSoundSourcePtr; -typedef std::shared_ptr OggSoundFilePtr; +typedef stdext::shared_object_ptr SoundSourcePtr; +typedef stdext::shared_object_ptr SoundFilePtr; +typedef stdext::shared_object_ptr SoundBufferPtr; +typedef stdext::shared_object_ptr StreamSoundSourcePtr; +typedef stdext::shared_object_ptr CombinedSoundSourcePtr; +typedef stdext::shared_object_ptr OggSoundFilePtr; #endif diff --git a/src/framework/sound/soundbuffer.h b/src/framework/sound/soundbuffer.h index 21a1020a..1593630e 100644 --- a/src/framework/sound/soundbuffer.h +++ b/src/framework/sound/soundbuffer.h @@ -27,7 +27,7 @@ #include -class SoundBuffer +class SoundBuffer : public stdext::shared_object { public: SoundBuffer(); diff --git a/src/framework/sound/soundfile.h b/src/framework/sound/soundfile.h index d5002675..e665a0fd 100644 --- a/src/framework/sound/soundfile.h +++ b/src/framework/sound/soundfile.h @@ -26,7 +26,7 @@ #include "declarations.h" #include -class SoundFile +class SoundFile : public stdext::shared_object { public: SoundFile(const FileStreamPtr& fileStream); diff --git a/src/framework/sound/soundsource.h b/src/framework/sound/soundsource.h index 81df87b4..3169cedc 100644 --- a/src/framework/sound/soundsource.h +++ b/src/framework/sound/soundsource.h @@ -24,8 +24,9 @@ #define SOUNDSOURCE_H #include "declarations.h" +#include "soundbuffer.h" -class SoundSource +class SoundSource : public stdext::shared_object { protected: SoundSource(uint sourceId) : m_sourceId(sourceId) { } diff --git a/src/framework/stdext/stdext.h b/src/framework/stdext/stdext.h index 7800d36a..e94a1f1c 100644 --- a/src/framework/stdext/stdext.h +++ b/src/framework/stdext/stdext.h @@ -32,5 +32,6 @@ #include "string.h" #include "dumper.h" #include "time.h" +#include "shared_object.h" #endif diff --git a/src/framework/ui/declarations.h b/src/framework/ui/declarations.h index 31ccb1f4..5fc04ef5 100644 --- a/src/framework/ui/declarations.h +++ b/src/framework/ui/declarations.h @@ -36,17 +36,15 @@ class UIGridLayout; class UIAnchorLayout; class UIParticles; -typedef std::shared_ptr UIWidgetPtr; -typedef std::weak_ptr UIWidgetWeakPtr; - -typedef std::shared_ptr UIParticlesPtr; -typedef std::shared_ptr UITextEditPtr; -typedef std::shared_ptr UILayoutPtr; -typedef std::shared_ptr UIBoxLayoutPtr; -typedef std::shared_ptr UIHorizontalLayoutPtr; -typedef std::shared_ptr UIVerticalLayoutPtr; -typedef std::shared_ptr UIGridLayoutPtr; -typedef std::shared_ptr UIAnchorLayoutPtr; +typedef stdext::shared_object_ptr UIWidgetPtr; +typedef stdext::shared_object_ptr UIParticlesPtr; +typedef stdext::shared_object_ptr UITextEditPtr; +typedef stdext::shared_object_ptr UILayoutPtr; +typedef stdext::shared_object_ptr UIBoxLayoutPtr; +typedef stdext::shared_object_ptr UIHorizontalLayoutPtr; +typedef stdext::shared_object_ptr UIVerticalLayoutPtr; +typedef stdext::shared_object_ptr UIGridLayoutPtr; +typedef stdext::shared_object_ptr UIAnchorLayoutPtr; typedef std::deque UIWidgetList; diff --git a/src/framework/ui/uianchorlayout.h b/src/framework/ui/uianchorlayout.h index 522273c0..5049cd1c 100644 --- a/src/framework/ui/uianchorlayout.h +++ b/src/framework/ui/uianchorlayout.h @@ -74,7 +74,7 @@ public: void addWidget(const UIWidgetPtr& widget); void removeWidget(const UIWidgetPtr& widget); - UIAnchorLayoutPtr asUIAnchorLayout() { return std::static_pointer_cast(shared_from_this()); } + bool isUIAnchorLayout() { return true; } protected: bool internalUpdate(); diff --git a/src/framework/ui/uiboxlayout.h b/src/framework/ui/uiboxlayout.h index f0bd6f3d..b2d29fb2 100644 --- a/src/framework/ui/uiboxlayout.h +++ b/src/framework/ui/uiboxlayout.h @@ -38,7 +38,7 @@ public: void setSpacing(int spacing) { m_spacing = spacing; update(); } void setFitChildren(bool fitParent) { m_fitChildren = fitParent; update(); } - UIBoxLayoutPtr asUIBoxLayout() { return std::static_pointer_cast(shared_from_this()); } + bool isUIBoxLayout() { return true; } protected: Boolean m_fitChildren; diff --git a/src/framework/ui/uigridlayout.h b/src/framework/ui/uigridlayout.h index 307df314..08dbe3be 100644 --- a/src/framework/ui/uigridlayout.h +++ b/src/framework/ui/uigridlayout.h @@ -23,7 +23,7 @@ #ifndef UIGRIDLAYOUT_H #define UIGRIDLAYOUT_H -#include +#include "uilayout.h" // @bindclass class UIGridLayout : public UILayout @@ -45,7 +45,7 @@ public: void setFitChildren(bool enable) { m_fitChildren = enable; update(); } void setFlow(bool enable) { m_flow = enable; update(); } - virtual UIGridLayoutPtr asUIGridLayout() { return nullptr; } + virtual bool isUIGridLayout() { return true; } protected: bool internalUpdate(); diff --git a/src/framework/ui/uihorizontallayout.h b/src/framework/ui/uihorizontallayout.h index 90fb2548..16f08f70 100644 --- a/src/framework/ui/uihorizontallayout.h +++ b/src/framework/ui/uihorizontallayout.h @@ -34,7 +34,7 @@ public: void setAlignRight(bool aliginRight) { m_alignRight = aliginRight; update(); } - UIHorizontalLayoutPtr asUIHorizontalLayout() { return std::static_pointer_cast(shared_from_this()); } + bool isUIHorizontalLayout() { return true; } protected: bool internalUpdate(); diff --git a/src/framework/ui/uilayout.cpp b/src/framework/ui/uilayout.cpp index 43d4d66d..3efdcd92 100644 --- a/src/framework/ui/uilayout.cpp +++ b/src/framework/ui/uilayout.cpp @@ -28,8 +28,7 @@ void UILayout::update() { //logTraceCounter(); - UIWidgetPtr parentWidget = getParentWidget(); - if(!parentWidget || parentWidget->isDestroyed()) + if(!m_parentWidget) return; /* @@ -52,7 +51,7 @@ void UILayout::update() m_updating = true; internalUpdate(); - parentWidget->onLayoutUpdate(); + m_parentWidget->onLayoutUpdate(); m_updating = false; } @@ -64,7 +63,7 @@ void UILayout::updateLater() if(!getParentWidget()) return; - auto self = asUILayout(); + auto self = self_cast(); g_dispatcher.addEvent([self] { self->m_updateScheduled = false; self->update(); diff --git a/src/framework/ui/uilayout.h b/src/framework/ui/uilayout.h index 3925a5cd..b4cb4967 100644 --- a/src/framework/ui/uilayout.h +++ b/src/framework/ui/uilayout.h @@ -43,17 +43,16 @@ public: void enableUpdates() { m_updateDisabled = std::max(m_updateDisabled-1,0); } void setParent(UIWidgetPtr parentWidget) { m_parentWidget = parentWidget; } - UIWidgetPtr getParentWidget() { return m_parentWidget.lock(); } + UIWidgetPtr getParentWidget() { return m_parentWidget; } bool isUpdateDisabled() { return m_updateDisabled > 0; } bool isUpdating() { return m_updating; } - UILayoutPtr asUILayout() { return std::static_pointer_cast(shared_from_this()); } - virtual UIAnchorLayoutPtr asUIAnchorLayout() { return nullptr; } - virtual UIBoxLayoutPtr asUIBoxLayout() { return nullptr; } - virtual UIHorizontalLayoutPtr asUIHorizontalLayout() { return nullptr; } - virtual UIVerticalLayoutPtr asUIVerticalLayout() { return nullptr; } - virtual UIGridLayoutPtr asUIGridLayout() { return nullptr; } + virtual bool isUIAnchorLayout() { return false; } + virtual bool isUIBoxLayout() { return false; } + virtual bool isUIHorizontalLayout() { return false; } + virtual bool isUIVerticalLayout() { return false; } + virtual bool isUIGridLayout() { return false; } protected: virtual bool internalUpdate() = 0; @@ -61,7 +60,7 @@ protected: int m_updateDisabled; Boolean m_updating; Boolean m_updateScheduled; - UIWidgetWeakPtr m_parentWidget; + UIWidgetPtr m_parentWidget; }; #endif diff --git a/src/framework/ui/uimanager.cpp b/src/framework/ui/uimanager.cpp index 67c05137..4adedb4b 100644 --- a/src/framework/ui/uimanager.cpp +++ b/src/framework/ui/uimanager.cpp @@ -285,7 +285,7 @@ void UIManager::onWidgetDestroy(const UIWidgetPtr& widget) g_dispatcher.scheduleEvent([backupList] { g_lua.collectGarbage(); for(const UIWidgetPtr& widget : backupList) { - if(widget->getUseCount() != 1) + if(widget->ref_count() != 1) g_logger.warning(stdext::format("widget '%s' destroyed but still have %d reference(s) left", widget->getId(), widget->getUseCount()-1)); } }, 1); diff --git a/src/framework/ui/uimanager.h b/src/framework/ui/uimanager.h index e96a1407..27280fdd 100644 --- a/src/framework/ui/uimanager.h +++ b/src/framework/ui/uimanager.h @@ -24,6 +24,7 @@ #define UIMANAGER_H #include "declarations.h" +#include "uiwidget.h" #include #include diff --git a/src/framework/ui/uiverticallayout.h b/src/framework/ui/uiverticallayout.h index bc0fd945..6e6dea85 100644 --- a/src/framework/ui/uiverticallayout.h +++ b/src/framework/ui/uiverticallayout.h @@ -35,7 +35,7 @@ public: void setAlignBottom(bool aliginBottom) { m_alignBottom = aliginBottom; update(); } - UIVerticalLayoutPtr asUIVerticalLayout() { return std::static_pointer_cast(shared_from_this()); } + bool isUIVerticalLayout() { return true; } protected: bool internalUpdate(); diff --git a/src/framework/ui/uiwidget.cpp b/src/framework/ui/uiwidget.cpp index a6420a04..fd17d264 100644 --- a/src/framework/ui/uiwidget.cpp +++ b/src/framework/ui/uiwidget.cpp @@ -141,11 +141,11 @@ void UIWidget::addChild(const UIWidgetPtr& child) UIWidgetPtr oldLastChild = getLastChild(); m_children.push_back(child); - child->setParent(asUIWidget()); + child->setParent(self_cast()); // create default layout if(!m_layout) - m_layout = UIAnchorLayoutPtr(new UIAnchorLayout(asUIWidget())); + m_layout = UIAnchorLayoutPtr(new UIAnchorLayout(self_cast())); // add to layout and updates it m_layout->addWidget(child); @@ -184,11 +184,11 @@ void UIWidget::insertChild(int index, const UIWidgetPtr& child) // retrieve child by index auto it = m_children.begin() + index; m_children.insert(it, child); - child->setParent(asUIWidget()); + child->setParent(self_cast()); // create default layout if needed if(!m_layout) - m_layout = UIAnchorLayoutPtr(new UIAnchorLayout(asUIWidget())); + m_layout = UIAnchorLayoutPtr(new UIAnchorLayout(self_cast())); // add to layout and updates it m_layout->addWidget(child); @@ -218,7 +218,7 @@ void UIWidget::removeChild(UIWidgetPtr child) m_children.erase(it); // reset child parent - assert(child->getParent() == asUIWidget()); + assert(child->getParent() == self_cast()); child->setParent(nullptr); m_layout->removeWidget(child); @@ -504,7 +504,7 @@ void UIWidget::applyStyle(const OTMLNodePtr& styleNode) callLuaField("onStyleApply", styleNode->tag(), styleNode); if(m_firstOnStyle) { - auto self = asUIWidget(); + auto self = self_cast(); g_dispatcher.addEvent([self] { self->callLuaField("onSetup"); }); @@ -525,7 +525,7 @@ void UIWidget::addAnchor(Fw::AnchorEdge anchoredEdge, const std::string& hookedW return; if(UIAnchorLayoutPtr anchorLayout = getAnchoredLayout()) - anchorLayout->addAnchor(asUIWidget(), anchoredEdge, hookedWidgetId, hookedEdge); + anchorLayout->addAnchor(self_cast(), anchoredEdge, hookedWidgetId, hookedEdge); else g_logger.error(stdext::format("cannot add anchors to widget '%s': the parent doesn't use anchors layout", m_id)); } @@ -541,8 +541,8 @@ void UIWidget::centerIn(const std::string& hookedWidgetId) return; if(UIAnchorLayoutPtr anchorLayout = getAnchoredLayout()) { - anchorLayout->addAnchor(asUIWidget(), Fw::AnchorHorizontalCenter, hookedWidgetId, Fw::AnchorHorizontalCenter); - anchorLayout->addAnchor(asUIWidget(), Fw::AnchorVerticalCenter, hookedWidgetId, Fw::AnchorVerticalCenter); + anchorLayout->addAnchor(self_cast(), Fw::AnchorHorizontalCenter, hookedWidgetId, Fw::AnchorHorizontalCenter); + anchorLayout->addAnchor(self_cast(), Fw::AnchorVerticalCenter, hookedWidgetId, Fw::AnchorVerticalCenter); } else g_logger.error(stdext::format("cannot add anchors to widget '%s': the parent doesn't use anchors layout", m_id)); } @@ -553,10 +553,10 @@ void UIWidget::fill(const std::string& hookedWidgetId) return; if(UIAnchorLayoutPtr anchorLayout = getAnchoredLayout()) { - anchorLayout->addAnchor(asUIWidget(), Fw::AnchorLeft, hookedWidgetId, Fw::AnchorLeft); - anchorLayout->addAnchor(asUIWidget(), Fw::AnchorRight, hookedWidgetId, Fw::AnchorRight); - anchorLayout->addAnchor(asUIWidget(), Fw::AnchorTop, hookedWidgetId, Fw::AnchorTop); - anchorLayout->addAnchor(asUIWidget(), Fw::AnchorBottom, hookedWidgetId, Fw::AnchorBottom); + anchorLayout->addAnchor(self_cast(), Fw::AnchorLeft, hookedWidgetId, Fw::AnchorLeft); + anchorLayout->addAnchor(self_cast(), Fw::AnchorRight, hookedWidgetId, Fw::AnchorRight); + anchorLayout->addAnchor(self_cast(), Fw::AnchorTop, hookedWidgetId, Fw::AnchorTop); + anchorLayout->addAnchor(self_cast(), Fw::AnchorBottom, hookedWidgetId, Fw::AnchorBottom); } else g_logger.error(stdext::format("cannot add anchors to widget '%s': the parent doesn't use anchors layout", m_id)); } @@ -567,7 +567,7 @@ void UIWidget::breakAnchors() return; if(UIAnchorLayoutPtr anchorLayout = getAnchoredLayout()) - anchorLayout->removeAnchors(asUIWidget()); + anchorLayout->removeAnchors(self_cast()); } void UIWidget::updateParentLayout() @@ -601,7 +601,7 @@ void UIWidget::lock() return; if(UIWidgetPtr parent = getParent()) - parent->lockChild(asUIWidget()); + parent->lockChild(self_cast()); } void UIWidget::unlock() @@ -610,7 +610,7 @@ void UIWidget::unlock() return; if(UIWidgetPtr parent = getParent()) - parent->unlockChild(asUIWidget()); + parent->unlockChild(self_cast()); } void UIWidget::focus() @@ -622,7 +622,7 @@ void UIWidget::focus() return; if(UIWidgetPtr parent = getParent()) - parent->focusChild(asUIWidget(), Fw::ActiveFocusReason); + parent->focusChild(self_cast(), Fw::ActiveFocusReason); } void UIWidget::recursiveFocus(Fw::FocusReason reason) @@ -632,7 +632,7 @@ void UIWidget::recursiveFocus(Fw::FocusReason reason) if(UIWidgetPtr parent = getParent()) { if(m_focusable) - parent->focusChild(asUIWidget(), reason); + parent->focusChild(self_cast(), reason); parent->recursiveFocus(reason); } } @@ -644,7 +644,7 @@ void UIWidget::lower() UIWidgetPtr parent = getParent(); if(parent) - parent->lowerChild(asUIWidget()); + parent->lowerChild(self_cast()); } void UIWidget::raise() @@ -654,7 +654,7 @@ void UIWidget::raise() UIWidgetPtr parent = getParent(); if(parent) - parent->raiseChild(asUIWidget()); + parent->raiseChild(self_cast()); } void UIWidget::grabMouse() @@ -662,12 +662,12 @@ void UIWidget::grabMouse() if(m_destroyed) return; - g_ui.setMouseReceiver(asUIWidget()); + g_ui.setMouseReceiver(self_cast()); } void UIWidget::ungrabMouse() { - if(g_ui.getMouseReceiver() == asUIWidget()) + if(g_ui.getMouseReceiver() == self_cast()) g_ui.resetMouseReceiver(); } @@ -676,12 +676,12 @@ void UIWidget::grabKeyboard() if(m_destroyed) return; - g_ui.setKeyboardReceiver(asUIWidget()); + g_ui.setKeyboardReceiver(self_cast()); } void UIWidget::ungrabKeyboard() { - if(g_ui.getKeyboardReceiver() == asUIWidget()) + if(g_ui.getKeyboardReceiver() == self_cast()) g_ui.resetKeyboardReceiver(); } @@ -705,9 +705,12 @@ void UIWidget::internalDestroy() m_destroyed = true; m_visible = false; m_enabled = false; - m_parent.reset(); m_focusedChild = nullptr; - m_layout = nullptr; + if(m_layout) { + m_layout->setParent(nullptr); + m_layout = nullptr; + } + m_parent = nullptr; m_lockedChildren.clear(); for(const UIWidgetPtr& child : m_children) @@ -718,7 +721,7 @@ void UIWidget::internalDestroy() releaseLuaFieldsTable(); - g_ui.onWidgetDestroy(asUIWidget()); + g_ui.onWidgetDestroy(self_cast()); } void UIWidget::destroy() @@ -727,7 +730,7 @@ void UIWidget::destroy() g_logger.warning(stdext::format("attempt to destroy widget '%s' two times", m_id)); // hold itself reference - UIWidgetPtr self = asUIWidget(); + UIWidgetPtr self = self_cast(); m_destroyed = true; // remove itself from parent @@ -772,7 +775,7 @@ void UIWidget::setParent(const UIWidgetPtr& parent) if(oldParent == parent) return; - UIWidgetPtr self = asUIWidget(); + UIWidgetPtr self = self_cast(); if(oldParent && oldParent->hasChild(self)) oldParent->removeChild(self); @@ -794,7 +797,7 @@ void UIWidget::setLayout(const UILayoutPtr& layout) if(m_layout) m_layout->disableUpdates(); - layout->setParent(asUIWidget()); + layout->setParent(self_cast()); layout->disableUpdates(); for(const UIWidgetPtr& child : m_children) { @@ -833,7 +836,7 @@ bool UIWidget::setRect(const Rect& rect) // avoid massive update events if(!m_updateEventScheduled) { - UIWidgetPtr self = asUIWidget(); + UIWidgetPtr self = self_cast(); g_dispatcher.addEvent([self, oldRect]() { self->m_updateEventScheduled = false; if(oldRect != self->getRect()) @@ -893,9 +896,9 @@ void UIWidget::setVisible(bool visible) // visibility can change the current hovered widget if(visible) - g_ui.onWidgetAppear(asUIWidget()); + g_ui.onWidgetAppear(self_cast()); else - g_ui.onWidgetDisappear(asUIWidget()); + g_ui.onWidgetDisappear(self_cast()); callLuaField("onVisibilityChange", visible); } @@ -960,14 +963,14 @@ bool UIWidget::isVisible() else if(UIWidgetPtr parent = getParent()) return parent->isVisible(); else - return asUIWidget() == g_ui.getRootWidget(); + return self_cast() == g_ui.getRootWidget(); } bool UIWidget::isAnchored() { if(UIWidgetPtr parent = getParent()) if(UIAnchorLayoutPtr anchorLayout = parent->getAnchoredLayout()) - return anchorLayout->hasAnchors(asUIWidget()); + return anchorLayout->hasAnchors(self_cast()); return false; } @@ -1041,7 +1044,10 @@ UIAnchorLayoutPtr UIWidget::getAnchoredLayout() if(!parent) return nullptr; - return parent->getLayout()->asUIAnchorLayout(); + UILayoutPtr layout = parent->getLayout(); + if(layout->isUIAnchorLayout()) + return layout->self_cast(); + return nullptr; } UIWidgetPtr UIWidget::getRootParent() @@ -1049,7 +1055,7 @@ UIWidgetPtr UIWidget::getRootParent() if(UIWidgetPtr parent = getParent()) return parent->getRootParent(); else - return asUIWidget(); + return self_cast(); } UIWidgetPtr UIWidget::getChildAfter(const UIWidgetPtr& relativeChild) @@ -1225,7 +1231,7 @@ void UIWidget::updateState(Fw::WidgetState state) switch(state) { case Fw::ActiveState: { - UIWidgetPtr widget = asUIWidget(); + UIWidgetPtr widget = self_cast(); UIWidgetPtr parent; do { parent = widget->getParent(); @@ -1240,24 +1246,24 @@ void UIWidget::updateState(Fw::WidgetState state) break; } case Fw::FocusState: { - newStatus = (getParent() && getParent()->getFocusedChild() == asUIWidget()); + newStatus = (getParent() && getParent()->getFocusedChild() == self_cast()); break; } case Fw::HoverState: { - newStatus = (g_ui.getHoveredWidget() == asUIWidget() && isEnabled()); + newStatus = (g_ui.getHoveredWidget() == self_cast() && isEnabled()); break; } case Fw::PressedState: { - newStatus = (g_ui.getPressedWidget() == asUIWidget()); + newStatus = (g_ui.getPressedWidget() == self_cast()); break; } case Fw::DraggingState: { - newStatus = (g_ui.getDraggingWidget() == asUIWidget()); + newStatus = (g_ui.getDraggingWidget() == self_cast()); break; } case Fw::DisabledState: { bool enabled = true; - UIWidgetPtr widget = asUIWidget(); + UIWidgetPtr widget = self_cast(); do { if(!widget->isExplicitlyEnabled()) { enabled = false; @@ -1269,19 +1275,19 @@ void UIWidget::updateState(Fw::WidgetState state) break; } case Fw::FirstState: { - newStatus = (getParent() && getParent()->getFirstChild() == asUIWidget()); + newStatus = (getParent() && getParent()->getFirstChild() == self_cast()); break; } case Fw::MiddleState: { - newStatus = (getParent() && getParent()->getFirstChild() != asUIWidget() && getParent()->getLastChild() != asUIWidget()); + newStatus = (getParent() && getParent()->getFirstChild() != self_cast() && getParent()->getLastChild() != self_cast()); break; } case Fw::LastState: { - newStatus = (getParent() && getParent()->getLastChild() == asUIWidget()); + newStatus = (getParent() && getParent()->getLastChild() == self_cast()); break; } case Fw::AlternateState: { - newStatus = (getParent() && (getParent()->getChildIndex(asUIWidget()) % 2) == 1); + newStatus = (getParent() && (getParent()->getChildIndex(self_cast()) % 2) == 1); break; } default: @@ -1331,7 +1337,7 @@ void UIWidget::updateStyle() return; if(m_loadingStyle && !m_updateStyleScheduled) { - UIWidgetPtr self = asUIWidget(); + UIWidgetPtr self = self_cast(); g_dispatcher.addEvent([self] { self->m_updateStyleScheduled = false; self->updateStyle(); @@ -1636,7 +1642,7 @@ bool UIWidget::propagateOnMouseEvent(const Point& mousePos, UIWidgetList& widget } } - widgetList.push_back(asUIWidget()); + widgetList.push_back(self_cast()); if(!isPhantom()) ret = true; @@ -1651,6 +1657,6 @@ bool UIWidget::propagateOnMouseMove(const Point& mousePos, const Point& mouseMov child->propagateOnMouseMove(mousePos, mouseMoved, widgetList); } - widgetList.push_back(asUIWidget()); + widgetList.push_back(self_cast()); return true; } diff --git a/src/framework/ui/uiwidget.h b/src/framework/ui/uiwidget.h index de4e5f93..07ebe8c2 100644 --- a/src/framework/ui/uiwidget.h +++ b/src/framework/ui/uiwidget.h @@ -24,6 +24,8 @@ #define UIWIDGET_H #include "declarations.h" +#include "uilayout.h" + #include #include #include @@ -68,7 +70,7 @@ protected: Boolean m_destroyed; Boolean m_clipping; UILayoutPtr m_layout; - UIWidgetWeakPtr m_parent; + UIWidgetPtr m_parent; UIWidgetList m_children; UIWidgetList m_lockedChildren; UIWidgetPtr m_focusedChild; @@ -152,8 +154,6 @@ public: UIWidgetList recursiveGetChildrenByMarginPos(const Point& childPos); UIWidgetPtr backwardsGetWidgetById(const std::string& id); - UIWidgetPtr asUIWidget() { return std::static_pointer_cast(shared_from_this()); } - private: Boolean m_updateEventScheduled; Boolean m_loadingStyle; @@ -248,7 +248,7 @@ public: bool containsPoint(const Point& point) { return m_rect.contains(point); } std::string getId() { return m_id; } - UIWidgetPtr getParent() { return m_parent.lock(); } + UIWidgetPtr getParent() { return m_parent; } UIWidgetPtr getFocusedChild() { return m_focusedChild; } UIWidgetList getChildren() { return m_children; } UIWidgetPtr getFirstChild() { return getChildByIndex(1); } diff --git a/src/framework/ui/uiwidgetbasestyle.cpp b/src/framework/ui/uiwidgetbasestyle.cpp index cf9ea1ac..d6b87ad8 100644 --- a/src/framework/ui/uiwidgetbasestyle.cpp +++ b/src/framework/ui/uiwidgetbasestyle.cpp @@ -243,13 +243,13 @@ void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode) if(!layoutType.empty()) { UILayoutPtr layout; if(layoutType == "horizontalBox") - layout = UIHorizontalLayoutPtr(new UIHorizontalLayout(asUIWidget())); + layout = UIHorizontalLayoutPtr(new UIHorizontalLayout(self_cast())); else if(layoutType == "verticalBox") - layout = UIVerticalLayoutPtr(new UIVerticalLayout(asUIWidget())); + layout = UIVerticalLayoutPtr(new UIVerticalLayout(self_cast())); else if(layoutType == "grid") - layout = UIGridLayoutPtr(new UIGridLayout(asUIWidget())); + layout = UIGridLayoutPtr(new UIGridLayout(self_cast())); else if(layoutType == "anchor") - layout = UIAnchorLayoutPtr(new UIAnchorLayout(asUIWidget())); + layout = UIAnchorLayoutPtr(new UIAnchorLayout(self_cast())); else throw OTMLException(node, "cannot determine layout type"); setLayout(layout); @@ -268,7 +268,11 @@ void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode) continue; } - UIAnchorLayoutPtr anchorLayout = parent->getLayout()->asUIAnchorLayout(); + UILayoutPtr layout = parent->getLayout(); + UIAnchorLayoutPtr anchorLayout; + if(layout->isUIAnchorLayout()) + anchorLayout = layout->self_cast(); + if(!anchorLayout) throw OTMLException(node, "cannot create anchor, the parent widget doesn't use anchor layout!"); diff --git a/src/main.cpp b/src/main.cpp index 5a074e10..c3162f42 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,11 +25,6 @@ #include #include -#include -#include -#include -#include - int main(int argc, const char* argv[]) { std::vector args(argv, argv + argc); diff --git a/src/otclient/animatedtext.h b/src/otclient/animatedtext.h index d2dc4ada..7c34c17f 100644 --- a/src/otclient/animatedtext.h +++ b/src/otclient/animatedtext.h @@ -40,7 +40,7 @@ public: void setColor(int color); void setText(const std::string& text); - AnimatedTextPtr asAnimatedText() { return std::static_pointer_cast(shared_from_this()); } + AnimatedTextPtr asAnimatedText() { return self_cast(); } bool isAnimatedText() { return true; } private: diff --git a/src/otclient/creature.cpp b/src/otclient/creature.cpp index ef510032..2bc4b9be 100644 --- a/src/otclient/creature.cpp +++ b/src/otclient/creature.cpp @@ -353,9 +353,9 @@ void Creature::updateWalkingTile() if(newWalkingTile != m_walkingTile) { if(m_walkingTile) - m_walkingTile->removeWalkingCreature(asCreature()); + m_walkingTile->removeWalkingCreature(self_cast()); if(newWalkingTile) - newWalkingTile->addWalkingCreature(asCreature()); + newWalkingTile->addWalkingCreature(self_cast()); m_walkingTile = newWalkingTile; } } @@ -371,7 +371,7 @@ void Creature::nextWalkUpdate() // schedules next update if(m_walking) { - auto self = asCreature(); + auto self = self_cast(); m_walkUpdateEvent = g_dispatcher.scheduleEvent([self] { self->m_walkUpdateEvent = nullptr; self->nextWalkUpdate(); @@ -409,7 +409,7 @@ void Creature::terminateWalk() } if(m_walkingTile) { - m_walkingTile->removeWalkingCreature(asCreature()); + m_walkingTile->removeWalkingCreature(self_cast()); m_walkingTile = nullptr; } @@ -496,7 +496,7 @@ void Creature::setShieldTexture(const std::string& filename, bool blink) m_showShieldTexture = true; if(blink && !m_shieldBlink) { - auto self = asCreature(); + auto self = self_cast(); g_dispatcher.scheduleEvent([self]() { self->updateShield(); }, SHIELD_BLINK_TICKS); @@ -516,7 +516,7 @@ void Creature::addTimedSquare(uint8 color) m_timedSquareColor = Color::from8bit(color); // schedule removal - auto self = asCreature(); + auto self = self_cast(); g_dispatcher.scheduleEvent([self]() { self->removeTimedSquare(); }, VOLATILE_SQUARE_DURATION); @@ -527,7 +527,7 @@ void Creature::updateShield() m_showShieldTexture = !m_showShieldTexture; if(m_shield != Otc::ShieldNone && m_shieldBlink) { - auto self = asCreature(); + auto self = self_cast(); g_dispatcher.scheduleEvent([self]() { self->updateShield(); }, SHIELD_BLINK_TICKS); diff --git a/src/otclient/creature.h b/src/otclient/creature.h index 587616fa..6180de04 100644 --- a/src/otclient/creature.h +++ b/src/otclient/creature.h @@ -25,6 +25,8 @@ #include "thing.h" #include "outfit.h" +#include "tile.h" +#include #include #include #include @@ -93,7 +95,6 @@ public: bool isWalking() { return m_walking; } bool isRemoved() { return m_removed; } - CreaturePtr asCreature() { return std::static_pointer_cast(shared_from_this()); } bool isCreature() { return true; } const ThingTypePtr& getThingType(); @@ -150,7 +151,6 @@ protected: class Npc : public Creature { public: - NpcPtr asNpc() { return std::static_pointer_cast(shared_from_this()); } bool isNpc() { return true; } }; @@ -158,7 +158,6 @@ public: class Monster : public Creature { public: - MonsterPtr asMonster() { return std::static_pointer_cast(shared_from_this()); } bool isMonster() { return true; } }; diff --git a/src/otclient/declarations.h b/src/otclient/declarations.h index d5dbf769..fa06e51f 100644 --- a/src/otclient/declarations.h +++ b/src/otclient/declarations.h @@ -50,25 +50,25 @@ class House; class Town; class CreatureType; -typedef std::shared_ptr MapViewPtr; -typedef std::shared_ptr TilePtr; -typedef std::shared_ptr ThingPtr; -typedef std::shared_ptr ItemPtr; -typedef std::shared_ptr ContainerPtr; -typedef std::shared_ptr CreaturePtr; -typedef std::shared_ptr MonsterPtr; -typedef std::shared_ptr NpcPtr; -typedef std::shared_ptr PlayerPtr; -typedef std::shared_ptr LocalPlayerPtr; -typedef std::shared_ptr EffectPtr; -typedef std::shared_ptr MissilePtr; -typedef std::shared_ptr AnimatedTextPtr; -typedef std::shared_ptr StaticTextPtr; -typedef std::shared_ptr ThingTypePtr; -typedef std::shared_ptr ItemTypePtr; -typedef std::shared_ptr HousePtr; -typedef std::shared_ptr TownPtr; -typedef std::shared_ptr CreatureTypePtr; +typedef stdext::shared_object_ptr MapViewPtr; +typedef stdext::shared_object_ptr TilePtr; +typedef stdext::shared_object_ptr ThingPtr; +typedef stdext::shared_object_ptr ItemPtr; +typedef stdext::shared_object_ptr ContainerPtr; +typedef stdext::shared_object_ptr CreaturePtr; +typedef stdext::shared_object_ptr MonsterPtr; +typedef stdext::shared_object_ptr NpcPtr; +typedef stdext::shared_object_ptr PlayerPtr; +typedef stdext::shared_object_ptr LocalPlayerPtr; +typedef stdext::shared_object_ptr EffectPtr; +typedef stdext::shared_object_ptr MissilePtr; +typedef stdext::shared_object_ptr AnimatedTextPtr; +typedef stdext::shared_object_ptr StaticTextPtr; +typedef stdext::shared_object_ptr ThingTypePtr; +typedef stdext::shared_object_ptr ItemTypePtr; +typedef stdext::shared_object_ptr HousePtr; +typedef stdext::shared_object_ptr TownPtr; +typedef stdext::shared_object_ptr CreatureTypePtr; typedef std::vector ThingList; typedef std::vector ThingTypeList; @@ -81,8 +81,8 @@ typedef std::unordered_map TileMap; class ProtocolLogin; class ProtocolGame; -typedef std::shared_ptr ProtocolGamePtr; -typedef std::shared_ptr ProtocolLoginPtr; +typedef stdext::shared_object_ptr ProtocolGamePtr; +typedef stdext::shared_object_ptr ProtocolLoginPtr; // ui class UIItem; @@ -90,9 +90,9 @@ class UICreature; class UIMap; class UIProgressRect; -typedef std::shared_ptr UIItemPtr; -typedef std::shared_ptr UICreaturePtr; -typedef std::shared_ptr UIMapPtr; -typedef std::shared_ptr UIProgressRectPtr; +typedef stdext::shared_object_ptr UIItemPtr; +typedef stdext::shared_object_ptr UICreaturePtr; +typedef stdext::shared_object_ptr UIMapPtr; +typedef stdext::shared_object_ptr UIProgressRectPtr; #endif diff --git a/src/otclient/effect.h b/src/otclient/effect.h index 5b9d2888..694070db 100644 --- a/src/otclient/effect.h +++ b/src/otclient/effect.h @@ -41,7 +41,7 @@ public: uint32 getId() { return m_id; } - EffectPtr asEffect() { return std::static_pointer_cast(shared_from_this()); } + EffectPtr asEffect() { return self_cast(); } bool isEffect() { return true; } const ThingTypePtr& getThingType(); diff --git a/src/otclient/game.cpp b/src/otclient/game.cpp index 5f499744..9e54d9a8 100644 --- a/src/otclient/game.cpp +++ b/src/otclient/game.cpp @@ -659,8 +659,8 @@ void Game::useWith(const ItemPtr& item, const ThingPtr& toThing) m_localPlayer->lockWalk(); - if(CreaturePtr creature = toThing->asCreature()) - m_protocolGame->sendUseOnCreature(pos, item->getId(), item->getStackpos(), creature->getId()); + if(toThing->isCreature()) + m_protocolGame->sendUseOnCreature(pos, item->getId(), item->getStackpos(), toThing->getId()); else m_protocolGame->sendUseItemWith(pos, item->getId(), item->getStackpos(), toThing->getPosition(), toThing->getId(), toThing->getStackpos()); } @@ -674,8 +674,8 @@ void Game::useInventoryItemWith(int itemId, const ThingPtr& toThing) Position pos = Position(0xFFFF, 0, 0); // means that is a item in inventory - if(CreaturePtr creature = toThing->asCreature()) - m_protocolGame->sendUseOnCreature(pos, itemId, 0, creature->getId()); + if(toThing->isCreature()) + m_protocolGame->sendUseOnCreature(pos, itemId, 0, toThing->getId()); else m_protocolGame->sendUseItemWith(pos, itemId, 0, toThing->getPosition(), toThing->getId(), toThing->getStackpos()); } diff --git a/src/otclient/game.h b/src/otclient/game.h index 44a9f585..0bd4e3e1 100644 --- a/src/otclient/game.h +++ b/src/otclient/game.h @@ -25,6 +25,10 @@ #include "declarations.h" #include "item.h" +#include "creature.h" +#include "container.h" +#include "protocolgame.h" +#include "localplayer.h" #include "outfit.h" #include diff --git a/src/otclient/item.h b/src/otclient/item.h index 5b252792..a01f571e 100644 --- a/src/otclient/item.h +++ b/src/otclient/item.h @@ -115,7 +115,7 @@ public: bool isTeleport() { return m_attribs.has(ATTR_TELE_DEST); } bool isMoveable(); - ItemPtr asItem() { return std::static_pointer_cast(shared_from_this()); } + ItemPtr asItem() { return self_cast(); } bool isItem() { return true; } const ThingTypePtr& getThingType(); diff --git a/src/otclient/localplayer.h b/src/otclient/localplayer.h index 8fa3b207..7d38ab80 100644 --- a/src/otclient/localplayer.h +++ b/src/otclient/localplayer.h @@ -78,7 +78,7 @@ public: bool isAutoWalking() { return m_autoWalking; } bool isPremium() { return m_premium; } - LocalPlayerPtr asLocalPlayer() { return std::static_pointer_cast(shared_from_this()); } + LocalPlayerPtr asLocalPlayer() { return self_cast(); } bool isLocalPlayer() { return true; } protected: diff --git a/src/otclient/luafunctions.cpp b/src/otclient/luafunctions.cpp index 03f897e1..725cab91 100644 --- a/src/otclient/luafunctions.cpp +++ b/src/otclient/luafunctions.cpp @@ -248,15 +248,6 @@ void OTClient::registerLuaFunctions() g_lua.bindClassMemberFunction("getPosition", &Thing::getPosition); g_lua.bindClassMemberFunction("getStackPriority", &Thing::getStackPriority); g_lua.bindClassMemberFunction("getAnimationPhases", &Thing::getAnimationPhases); - g_lua.bindClassMemberFunction("asThing", &Thing::asThing); - g_lua.bindClassMemberFunction("asItem", &Thing::asItem); - g_lua.bindClassMemberFunction("asCreature", &Thing::asCreature); - g_lua.bindClassMemberFunction("asEffect", &Thing::asEffect); - g_lua.bindClassMemberFunction("asMissile", &Thing::asMissile); - g_lua.bindClassMemberFunction("asPlayer", &Thing::asPlayer); - g_lua.bindClassMemberFunction("asLocalPlayer", &Thing::asLocalPlayer); - g_lua.bindClassMemberFunction("asAnimatedText", &Thing::asAnimatedText); - g_lua.bindClassMemberFunction("asStaticText", &Thing::asStaticText); g_lua.bindClassMemberFunction("isItem", &Thing::isItem); g_lua.bindClassMemberFunction("isCreature", &Thing::isCreature); g_lua.bindClassMemberFunction("isEffect", &Thing::isEffect); @@ -331,8 +322,6 @@ void OTClient::registerLuaFunctions() g_lua.bindClassMemberFunction("showStaticSquare", &Creature::showStaticSquare); g_lua.bindClassMemberFunction("hideStaticSquare", &Creature::hideStaticSquare); g_lua.bindClassMemberFunction("isWalking", &Creature::isWalking); - g_lua.bindClassMemberFunction("asMonster", &Creature::asMonster); - g_lua.bindClassMemberFunction("asNpc", &Creature::asNpc); g_lua.registerClass(); g_lua.bindClassMemberFunction("getServerId", &ItemType::getServerId); @@ -398,7 +387,6 @@ void OTClient::registerLuaFunctions() g_lua.bindClassMemberFunction("isPremium", &LocalPlayer::isPremium); g_lua.bindClassMemberFunction("isKnown", &LocalPlayer::isKnown); g_lua.bindClassMemberFunction("isPreWalking", &LocalPlayer::isPreWalking); - g_lua.bindClassMemberFunction("asLocalPlayer", &LocalPlayer::asLocalPlayer); g_lua.registerClass(); g_lua.bindClassMemberFunction("clean", &Tile::clean); diff --git a/src/otclient/map.cpp b/src/otclient/map.cpp index 5124b5ce..25676ec8 100644 --- a/src/otclient/map.cpp +++ b/src/otclient/map.cpp @@ -543,7 +543,9 @@ void Map::saveOtcm(const std::string& fileName) const auto& list = tile->getThings(); auto first = std::find_if(list.begin(), list.end(), [](const ThingPtr& thing) { return thing->isItem(); }); for(auto it = first, end = list.end(); it != end; ++it) { - if(ItemPtr item = (*it)->asItem()) { + const ThingPtr& thing = *it; + if(thing->isItem()) { + ItemPtr item = thing->self_cast(); fin->addU16(item->getId()); fin->addU8(item->getCountOrSubType()); } @@ -611,11 +613,11 @@ void Map::addThing(const ThingPtr& thing, const Position& pos, int stackPos) if(thing->isItem() || thing->isCreature() || thing->isEffect()) { tile->addThing(thing, stackPos); } else if(thing->isMissile()) { - m_floorMissiles[pos.z].push_back(thing->asMissile()); + m_floorMissiles[pos.z].push_back(thing->self_cast()); } else if(thing->isAnimatedText()) { - m_animatedTexts.push_back(thing->asAnimatedText()); + m_animatedTexts.push_back(thing->self_cast()); } else if(thing->isStaticText()) { - StaticTextPtr staticText = thing->asStaticText(); + StaticTextPtr staticText = thing->self_cast(); bool mustAdd = true; for(auto it = m_staticTexts.begin(), end = m_staticTexts.end(); it != end; ++it) { StaticTextPtr cStaticText = *it; @@ -639,7 +641,7 @@ void Map::addThing(const ThingPtr& thing, const Position& pos, int stackPos) thing->setPosition(pos); if(thing->isCreature()) { - CreaturePtr creature = thing->asCreature(); + CreaturePtr creature = thing->self_cast(); if(oldPos != pos) { if(oldPos.isInRange(pos,1,1)) g_game.processCreatureMove(creature, oldPos, pos); @@ -665,20 +667,23 @@ bool Map::removeThing(const ThingPtr& thing) notificateTileUpdateToMapViews(thing->getPosition()); - if(MissilePtr missile = thing->asMissile()) { + if(thing->isMissile()) { + MissilePtr missile = thing->self_cast(); int z = missile->getPosition().z; auto it = std::find(m_floorMissiles[z].begin(), m_floorMissiles[z].end(), missile); if(it != m_floorMissiles[z].end()) { m_floorMissiles[z].erase(it); return true; } - } else if(AnimatedTextPtr animatedText = thing->asAnimatedText()) { + } else if(thing->isAnimatedText()) { + AnimatedTextPtr animatedText = thing->self_cast(); auto it = std::find(m_animatedTexts.begin(), m_animatedTexts.end(), animatedText); if(it != m_animatedTexts.end()) { m_animatedTexts.erase(it); return true; } - } else if(StaticTextPtr staticText = thing->asStaticText()) { + } else if(thing->isStaticText()) { + StaticTextPtr staticText = thing->self_cast(); auto it = std::find(m_staticTexts.begin(), m_staticTexts.end(), staticText); if(it != m_staticTexts.end()) { m_staticTexts.erase(it); diff --git a/src/otclient/map.h b/src/otclient/map.h index 5cde3ba6..ee33868e 100644 --- a/src/otclient/map.h +++ b/src/otclient/map.h @@ -28,6 +28,7 @@ #include "towns.h" #include "creatures.h" #include "animatedtext.h" +#include "statictext.h" #include #include diff --git a/src/otclient/mapview.h b/src/otclient/mapview.h index 0e70b755..22f48b8e 100644 --- a/src/otclient/mapview.h +++ b/src/otclient/mapview.h @@ -24,6 +24,7 @@ #define MAPVIEW_H #include "declarations.h" +#include #include #include #include @@ -113,7 +114,7 @@ public: // get tile TilePtr getTile(const Point& mousePos, const Rect& mapRect); - MapViewPtr asMapView() { return std::static_pointer_cast(shared_from_this()); } + MapViewPtr asMapView() { return self_cast(); } private: int calcFirstVisibleFloor(); diff --git a/src/otclient/missile.h b/src/otclient/missile.h index d9125f37..de16a60c 100644 --- a/src/otclient/missile.h +++ b/src/otclient/missile.h @@ -42,7 +42,7 @@ public: uint32 getId() { return m_id; } - MissilePtr asMissile() { return std::static_pointer_cast(shared_from_this()); } + MissilePtr asMissile() { return self_cast(); } bool isMissile() { return true; } const ThingTypePtr& getThingType(); diff --git a/src/otclient/player.h b/src/otclient/player.h index 36b9c318..c2f58952 100644 --- a/src/otclient/player.h +++ b/src/otclient/player.h @@ -32,7 +32,7 @@ public: Player() { } virtual ~Player() { } - PlayerPtr asPlayer() { return std::static_pointer_cast(shared_from_this()); } + PlayerPtr asPlayer() { return self_cast(); } bool isPlayer() { return true; } }; diff --git a/src/otclient/statictext.h b/src/otclient/statictext.h index 4680112a..f6b1803b 100644 --- a/src/otclient/statictext.h +++ b/src/otclient/statictext.h @@ -43,7 +43,7 @@ public: bool addMessage(const std::string& name, Otc::MessageMode mode, const std::string& text); - StaticTextPtr asStaticText() { return std::static_pointer_cast(shared_from_this()); } + StaticTextPtr asStaticText() { return self_cast(); } bool isStaticText() { return true; } private: diff --git a/src/otclient/thing.cpp b/src/otclient/thing.cpp index 1081789c..0bb66220 100644 --- a/src/otclient/thing.cpp +++ b/src/otclient/thing.cpp @@ -43,7 +43,7 @@ int Thing::getStackPriority() return 2; else if(isOnTop()) return 3; - else if(asCreature()) + else if(isCreature()) return 4; else // common items return 5; @@ -65,10 +65,10 @@ ContainerPtr Thing::getParentContainer() int Thing::getStackpos() { - if(m_position.x == 65535 && asItem()) // is inside a container + if(m_position.x == 65535 && isItem()) // is inside a container return m_position.z; else if(const TilePtr& tile = getTile()) - return tile->getThingStackpos(asThing()); + return tile->getThingStackpos(self_cast()); else { g_logger.traceError("got a thing with invalid stackpos"); return -1; diff --git a/src/otclient/thing.h b/src/otclient/thing.h index 3cefef80..a7f7c8dc 100644 --- a/src/otclient/thing.h +++ b/src/otclient/thing.h @@ -49,18 +49,6 @@ public: ContainerPtr getParentContainer(); int getStackpos(); - ThingPtr asThing() { return std::static_pointer_cast(shared_from_this()); } - virtual ItemPtr asItem() { return nullptr; } - virtual EffectPtr asEffect() { return nullptr; } - virtual MissilePtr asMissile() { return nullptr; } - virtual CreaturePtr asCreature() { return nullptr; } - virtual NpcPtr asNpc() { return nullptr; } - virtual MonsterPtr asMonster() { return nullptr; } - virtual PlayerPtr asPlayer() { return nullptr; } - virtual LocalPlayerPtr asLocalPlayer() { return nullptr; } - virtual AnimatedTextPtr asAnimatedText() { return nullptr; } - virtual StaticTextPtr asStaticText() { return nullptr; } - virtual bool isItem() { return false; } virtual bool isEffect() { return false; } virtual bool isMissile() { return false; } diff --git a/src/otclient/tile.cpp b/src/otclient/tile.cpp index 1a33e1bd..b1155f93 100644 --- a/src/otclient/tile.cpp +++ b/src/otclient/tile.cpp @@ -113,7 +113,7 @@ void Tile::draw(const Point& dest, float scaleFactor, int drawFlags) const ThingPtr& thing = *it; if(!thing->isCreature()) continue; - CreaturePtr creature = thing->asCreature(); + CreaturePtr creature = thing->self_cast(); if(creature && (!creature->isWalking() || !animate)) creature->draw(dest - m_drawElevation*scaleFactor, scaleFactor, animate); } @@ -158,7 +158,7 @@ void Tile::addThing(const ThingPtr& thing, int stackPos) return; if(thing->isEffect()) { - m_effects.push_back(thing->asEffect()); + m_effects.push_back(thing->self_cast()); return; } @@ -204,7 +204,8 @@ bool Tile::removeThing(ThingPtr thing) bool removed = false; - if(EffectPtr effect = thing->asEffect()) { + if(thing->isEffect()) { + EffectPtr effect = thing->self_cast(); auto it = std::find(m_effects.begin(), m_effects.end(), effect); if(it != m_effects.end()) { m_effects.erase(it); @@ -264,8 +265,10 @@ std::vector Tile::getItems() { std::vector items; for(const ThingPtr& thing : m_things) { - if(ItemPtr item = thing->asItem()) - items.push_back(item); + if(!thing->isItem()) + continue; + ItemPtr item = thing->self_cast(); + items.push_back(item); } return items; } @@ -274,8 +277,8 @@ std::vector Tile::getCreatures() { std::vector creatures; for(const ThingPtr& thing : m_things) { - if(CreaturePtr creature = thing->asCreature()) - creatures.push_back(creature); + if(thing->isCreature()) + creatures.push_back(thing->self_cast()); } return creatures; } @@ -285,8 +288,8 @@ ItemPtr Tile::getGround() ThingPtr firstObject = getThing(0); if(!firstObject) return nullptr; - if(firstObject->isGround()) - return firstObject->asItem(); + if(firstObject->isGround() && firstObject->isItem()) + return firstObject->self_cast(); return nullptr; } @@ -344,10 +347,10 @@ CreaturePtr Tile::getTopCreature() CreaturePtr creature; for(uint i = 0; i < m_things.size(); ++i) { ThingPtr thing = m_things[i]; - if(thing->asLocalPlayer()) // return local player if there is no other creature - creature = thing->asCreature(); + if(thing->isLocalPlayer()) // return local player if there is no other creature + creature = thing->self_cast(); else if(thing->isCreature() && !thing->isLocalPlayer()) - return thing->asCreature(); + return thing->self_cast(); } if(!creature && !m_walkingCreatures.empty()) creature = m_walkingCreatures.back(); @@ -404,7 +407,8 @@ bool Tile::isWalkable() if(thing->isNotWalkable()) return false; - if(CreaturePtr creature = thing->asCreature()) { + if(thing->isCreature()) { + CreaturePtr creature = thing->self_cast(); if(!creature->isPassable()) return false; } @@ -491,7 +495,7 @@ bool Tile::mustHookSouth() bool Tile::hasCreature() { for(const ThingPtr& thing : m_things) - if(thing->asCreature()) + if(thing->isCreature()) return true; return false; } diff --git a/src/otclient/tile.h b/src/otclient/tile.h index 47fba1c3..305e9c37 100644 --- a/src/otclient/tile.h +++ b/src/otclient/tile.h @@ -106,7 +106,7 @@ public: void setHouseId(uint32 hid) { if(m_flags & TILESTATE_HOUSE) m_houseId = hid; } uint32 getHouseId() { return m_houseId; } - TilePtr asTile() { return std::static_pointer_cast(shared_from_this()); } + TilePtr asTile() { return self_cast(); } private: std::vector m_walkingCreatures;