diff --git a/projects/kiwano/kiwano.vcxproj b/projects/kiwano/kiwano.vcxproj index 11cb1697..ee563d2b 100644 --- a/projects/kiwano/kiwano.vcxproj +++ b/projects/kiwano/kiwano.vcxproj @@ -17,7 +17,7 @@ - + @@ -52,7 +52,7 @@ - + @@ -134,7 +134,7 @@ - + diff --git a/projects/kiwano/kiwano.vcxproj.filters b/projects/kiwano/kiwano.vcxproj.filters index c0a05373..95c61c79 100644 --- a/projects/kiwano/kiwano.vcxproj.filters +++ b/projects/kiwano/kiwano.vcxproj.filters @@ -65,9 +65,6 @@ math - - core - 2d @@ -303,9 +300,6 @@ base - - base - event @@ -354,6 +348,12 @@ 2d\action + + core + + + base + @@ -539,9 +539,6 @@ base - - base - event @@ -581,6 +578,9 @@ 2d\action + + base + diff --git a/src/kiwano-audio/Sound.cpp b/src/kiwano-audio/Sound.cpp index 4f3b2636..c8da50d7 100644 --- a/src/kiwano-audio/Sound.cpp +++ b/src/kiwano-audio/Sound.cpp @@ -29,7 +29,7 @@ namespace audio { SoundPtr Sound::Create(const String& file_path) { - SoundPtr ptr = new (std::nothrow) Sound; + SoundPtr ptr = new (autogc) Sound; if (ptr) { if (!ptr->Load(file_path)) @@ -40,7 +40,7 @@ SoundPtr Sound::Create(const String& file_path) SoundPtr Sound::Create(const Resource& res) { - SoundPtr ptr = new (std::nothrow) Sound; + SoundPtr ptr = new (autogc) Sound; if (ptr) { if (!ptr->Load(res)) diff --git a/src/kiwano-audio/SoundPlayer.cpp b/src/kiwano-audio/SoundPlayer.cpp index dde27202..31efc911 100644 --- a/src/kiwano-audio/SoundPlayer.cpp +++ b/src/kiwano-audio/SoundPlayer.cpp @@ -27,7 +27,7 @@ namespace audio SoundPlayerPtr SoundPlayer::Create() { - SoundPlayerPtr ptr = new (std::nothrow) SoundPlayer; + SoundPlayerPtr ptr = new (autogc) SoundPlayer; return ptr; } @@ -47,8 +47,7 @@ size_t SoundPlayer::Load(const String& file_path) if (sound_cache_.end() != sound_cache_.find(hash)) return hash; - SoundPtr sound = new (std::nothrow) Sound; - + SoundPtr sound = new (autogc) Sound; if (sound) { if (sound->Load(file_path)) @@ -67,7 +66,7 @@ size_t SoundPlayer::Load(const Resource& res) if (sound_cache_.end() != sound_cache_.find(hash_code)) return hash_code; - SoundPtr sound = new (std::nothrow) Sound; + SoundPtr sound = new (autogc) Sound; if (sound) { diff --git a/src/kiwano-imgui/ImGuiLayer.cpp b/src/kiwano-imgui/ImGuiLayer.cpp index aecd488d..68b2d161 100644 --- a/src/kiwano-imgui/ImGuiLayer.cpp +++ b/src/kiwano-imgui/ImGuiLayer.cpp @@ -27,13 +27,13 @@ namespace imgui ImGuiLayerPtr ImGuiLayer::Create() { - ImGuiLayerPtr ptr = new (std::nothrow) ImGuiLayer; + ImGuiLayerPtr ptr = new (autogc) ImGuiLayer; return ptr; } ImGuiLayerPtr ImGuiLayer::Create(const String& name, const ImGuiPipeline& item) { - ImGuiLayerPtr ptr = new (std::nothrow) ImGuiLayer; + ImGuiLayerPtr ptr = new (autogc) ImGuiLayer; if (ptr) { ptr->AddItem(name, item); diff --git a/src/kiwano-network/HttpModule.cpp b/src/kiwano-network/HttpModule.cpp index 4fae5217..ab1d9f88 100644 --- a/src/kiwano-network/HttpModule.cpp +++ b/src/kiwano-network/HttpModule.cpp @@ -253,7 +253,7 @@ void HttpModule::NetworkThread() return; } - HttpResponsePtr response = new (std::nothrow) HttpResponse(request); + HttpResponsePtr response = new (autogc) HttpResponse(request); Perform(request, response); response_mutex_.lock(); diff --git a/src/kiwano-network/HttpRequest.cpp b/src/kiwano-network/HttpRequest.cpp index 7392aa9c..18ef0abf 100644 --- a/src/kiwano-network/HttpRequest.cpp +++ b/src/kiwano-network/HttpRequest.cpp @@ -28,7 +28,7 @@ namespace network HttpRequestPtr HttpRequest::Create(const String& url, HttpType type, const ResponseCallback& callback) { - HttpRequestPtr ptr = new (std::nothrow) HttpRequest; + HttpRequestPtr ptr = new (autogc) HttpRequest; if (ptr) { ptr->SetUrl(url); @@ -40,7 +40,7 @@ HttpRequestPtr HttpRequest::Create(const String& url, HttpType type, const Respo HttpRequestPtr HttpRequest::Create(const String& url, HttpType type, const String& data, const ResponseCallback& callback) { - HttpRequestPtr ptr = new (std::nothrow) HttpRequest; + HttpRequestPtr ptr = new (autogc) HttpRequest; if (ptr) { ptr->SetUrl(url); @@ -53,7 +53,7 @@ HttpRequestPtr HttpRequest::Create(const String& url, HttpType type, const Strin HttpRequestPtr HttpRequest::Create(const String& url, HttpType type, const Json& json, const ResponseCallback& callback) { - HttpRequestPtr ptr = new (std::nothrow) HttpRequest; + HttpRequestPtr ptr = new (autogc) HttpRequest; if (ptr) { ptr->SetUrl(url); diff --git a/src/kiwano-physics/Fixture.cpp b/src/kiwano-physics/Fixture.cpp index 2576a707..d6e14cbd 100644 --- a/src/kiwano-physics/Fixture.cpp +++ b/src/kiwano-physics/Fixture.cpp @@ -29,7 +29,7 @@ namespace physics FixturePtr Fixture::CreateCircle(const Param& param, float radius, const Point& offset) { - FixturePtr ptr = new (std::nothrow) Fixture; + FixturePtr ptr = new (autogc) Fixture; if (ptr) { auto shape = std::make_unique(); @@ -44,7 +44,7 @@ FixturePtr Fixture::CreateCircle(const Param& param, float radius, const Point& FixturePtr Fixture::CreateRect(const Param& param, const Size& size, const Point& offset, float rotation) { - FixturePtr ptr = new (std::nothrow) Fixture; + FixturePtr ptr = new (autogc) Fixture; if (ptr) { b2Vec2 b2size = global::LocalToWorld(size); @@ -61,7 +61,7 @@ FixturePtr Fixture::CreateRect(const Param& param, const Size& size, const Point FixturePtr Fixture::CreatePolygon(const Param& param, const Vector& vertexs) { - FixturePtr ptr = new (std::nothrow) Fixture; + FixturePtr ptr = new (autogc) Fixture; if (ptr) { Vector b2vertexs; @@ -82,7 +82,7 @@ FixturePtr Fixture::CreatePolygon(const Param& param, const Vector& verte FixturePtr Fixture::CreateEdge(const Param& param, const Point& p1, const Point& p2) { - FixturePtr ptr = new (std::nothrow) Fixture; + FixturePtr ptr = new (autogc) Fixture; if (ptr) { b2Vec2 start = global::LocalToWorld(p1); @@ -99,7 +99,7 @@ FixturePtr Fixture::CreateEdge(const Param& param, const Point& p1, const Point& FixturePtr Fixture::CreateChain(const Param& param, const Vector& vertices, bool loop) { - FixturePtr ptr = new (std::nothrow) Fixture; + FixturePtr ptr = new (autogc) Fixture; if (ptr) { Vector b2vertices; diff --git a/src/kiwano-physics/Joint.cpp b/src/kiwano-physics/Joint.cpp index bef942a6..220d3d29 100644 --- a/src/kiwano-physics/Joint.cpp +++ b/src/kiwano-physics/Joint.cpp @@ -109,7 +109,7 @@ void Joint::Destroy() DistanceJointPtr DistanceJoint::Create(const Param& param) { - DistanceJointPtr ptr = new (std::nothrow) DistanceJoint; + DistanceJointPtr ptr = new (autogc) DistanceJoint; if (ptr) { ptr->param_ = param; @@ -156,7 +156,7 @@ float DistanceJoint::GetLength() const FrictionJointPtr FrictionJoint::Create(const Param& param) { - FrictionJointPtr ptr = new (std::nothrow) FrictionJoint; + FrictionJointPtr ptr = new (autogc) FrictionJoint; if (ptr) { ptr->param_ = param; @@ -214,7 +214,7 @@ float FrictionJoint::GetMaxTorque() const GearJointPtr GearJoint::Create(const Param& param) { - GearJointPtr ptr = new (std::nothrow) GearJoint; + GearJointPtr ptr = new (autogc) GearJoint; if (ptr) { ptr->param_ = param; @@ -260,7 +260,7 @@ float GearJoint::GetRatio() const MotorJointPtr MotorJoint::Create(const Param& param) { - MotorJointPtr ptr = new (std::nothrow) MotorJoint; + MotorJointPtr ptr = new (autogc) MotorJoint; if (ptr) { ptr->param_ = param; @@ -319,7 +319,7 @@ float MotorJoint::GetMaxTorque() const PrismaticJointPtr PrismaticJoint::Create(const Param& param) { - PrismaticJointPtr ptr = new (std::nothrow) PrismaticJoint; + PrismaticJointPtr ptr = new (autogc) PrismaticJoint; if (ptr) { ptr->param_ = param; @@ -388,7 +388,7 @@ void PrismaticJoint::SetLimits(float lower, float upper) PulleyJointPtr PulleyJoint::Create(const Param& param) { - PulleyJointPtr ptr = new (std::nothrow) PulleyJoint; + PulleyJointPtr ptr = new (autogc) PulleyJoint; if (ptr) { ptr->param_ = param; @@ -464,7 +464,7 @@ float PulleyJoint::GetCurrentLengthB() const RevoluteJointPtr RevoluteJoint::Create(const Param& param) { - RevoluteJointPtr ptr = new (std::nothrow) RevoluteJoint; + RevoluteJointPtr ptr = new (autogc) RevoluteJoint; if (ptr) { ptr->param_ = param; @@ -544,7 +544,7 @@ float RevoluteJoint::GetMaxMotorTorque() const RopeJointPtr RopeJoint::Create(const Param& param) { - RopeJointPtr ptr = new (std::nothrow) RopeJoint; + RopeJointPtr ptr = new (autogc) RopeJoint; if (ptr) { ptr->param_ = param; @@ -592,7 +592,7 @@ float RopeJoint::GetMaxLength() const WeldJointPtr WeldJoint::Create(const Param& param) { - WeldJointPtr ptr = new (std::nothrow) WeldJoint; + WeldJointPtr ptr = new (autogc) WeldJoint; if (ptr) { ptr->param_ = param; @@ -626,7 +626,7 @@ bool WeldJoint::Init(PhysicWorld* world) WheelJointPtr WheelJoint::Create(const Param& param) { - WheelJointPtr ptr = new (std::nothrow) WheelJoint; + WheelJointPtr ptr = new (autogc) WheelJoint; if (ptr) { ptr->param_ = param; @@ -688,7 +688,7 @@ float WheelJoint::GetMaxMotorTorque() const MouseJointPtr MouseJoint::Create(const Param& param) { - MouseJointPtr ptr = new (std::nothrow) MouseJoint; + MouseJointPtr ptr = new (autogc) MouseJoint; if (ptr) { ptr->param_ = param; diff --git a/src/kiwano-physics/PhysicBody.cpp b/src/kiwano-physics/PhysicBody.cpp index 6652b5d6..770b7101 100644 --- a/src/kiwano-physics/PhysicBody.cpp +++ b/src/kiwano-physics/PhysicBody.cpp @@ -37,7 +37,7 @@ PhysicBodyPtr PhysicBody::Create(PhysicWorld* world, Type type) { KGE_ASSERT(world); - PhysicBodyPtr ptr = new (std::nothrow) PhysicBody; + PhysicBodyPtr ptr = new (autogc) PhysicBody; if (ptr) { ptr->SetType(type); diff --git a/src/kiwano-physics/PhysicWorld.cpp b/src/kiwano-physics/PhysicWorld.cpp index e97384b7..b712a343 100644 --- a/src/kiwano-physics/PhysicWorld.cpp +++ b/src/kiwano-physics/PhysicWorld.cpp @@ -213,13 +213,13 @@ public: PhysicWorldPtr PhysicWorld::Create() { - PhysicWorldPtr ptr = new (std::nothrow) PhysicWorld; + PhysicWorldPtr ptr = new (autogc) PhysicWorld; return ptr; } PhysicWorldPtr PhysicWorld::Create(const Vec2& gravity) { - PhysicWorldPtr ptr = new (std::nothrow) PhysicWorld; + PhysicWorldPtr ptr = new (autogc) PhysicWorld; if (ptr) { ptr->SetGravity(gravity); diff --git a/src/kiwano/2d/Actor.cpp b/src/kiwano/2d/Actor.cpp index c7a65db2..cc8265d4 100644 --- a/src/kiwano/2d/Actor.cpp +++ b/src/kiwano/2d/Actor.cpp @@ -41,7 +41,7 @@ void Actor::SetDefaultAnchor(float anchor_x, float anchor_y) ActorPtr Actor::Create() { - ActorPtr ptr = memory::New(); + ActorPtr ptr = new (autogc) Actor; return ptr; } @@ -264,7 +264,7 @@ bool Actor::HandleEvent(Event* evt) { hover_ = true; - MouseHoverEventPtr hover = memory::New(); + MouseHoverEventPtr hover = new (autogc) MouseHoverEvent; hover->pos = mouse_evt->pos; HandleEvent(hover.Get()); } @@ -273,7 +273,7 @@ bool Actor::HandleEvent(Event* evt) hover_ = false; pressed_ = false; - MouseOutEventPtr out = memory::New(); + MouseOutEventPtr out = new (autogc) MouseOutEvent; out->pos = mouse_evt->pos; HandleEvent(out.Get()); } @@ -290,7 +290,7 @@ bool Actor::HandleEvent(Event* evt) auto mouse_up_evt = dynamic_cast(evt); - MouseClickEventPtr click = memory::New(); + MouseClickEventPtr click = new (autogc) MouseClickEvent; click->pos = mouse_up_evt->pos; click->button = mouse_up_evt->button; HandleEvent(click.Get()); diff --git a/src/kiwano/2d/Canvas.cpp b/src/kiwano/2d/Canvas.cpp index 1cbbd781..cc848a26 100644 --- a/src/kiwano/2d/Canvas.cpp +++ b/src/kiwano/2d/Canvas.cpp @@ -26,8 +26,7 @@ namespace kiwano CanvasPtr Canvas::Create(const Size& size) { - void* mem = memory::Alloc(sizeof(Canvas)); - CanvasPtr ptr = ::new (mem) Canvas; + CanvasPtr ptr = new (autogc) Canvas; if (ptr) { try @@ -59,7 +58,7 @@ void Canvas::OnRender(RenderContext& ctx) void Canvas::ResizeAndClear(Size size) { - texture_cached_ = memory::New(); + texture_cached_ = new (autogc) Texture; render_ctx_ = RenderContext::Create(*texture_cached_, size); SetSize(render_ctx_->GetSize()); diff --git a/src/kiwano/2d/DebugActor.cpp b/src/kiwano/2d/DebugActor.cpp index 92cf5989..048c29c1 100644 --- a/src/kiwano/2d/DebugActor.cpp +++ b/src/kiwano/2d/DebugActor.cpp @@ -54,10 +54,10 @@ DebugActor::DebugActor() comma_locale_ = std::locale(std::locale(), new comma_numpunct); - background_brush_ = memory::New(); + background_brush_ = new (autogc) Brush; background_brush_->SetColor(Color::Rgba(0x000000, 0.7f)); - BrushPtr fill_brush = memory::New(); + BrushPtr fill_brush = new (autogc) Brush; fill_brush->SetColor(Color::White); debug_text_style_.font_family = "Arial"; diff --git a/src/kiwano/2d/GifSprite.cpp b/src/kiwano/2d/GifSprite.cpp index 5cd2cf19..68996b63 100644 --- a/src/kiwano/2d/GifSprite.cpp +++ b/src/kiwano/2d/GifSprite.cpp @@ -27,7 +27,7 @@ namespace kiwano GifSpritePtr GifSprite::Create(const String& file_path) { - GifSpritePtr ptr = memory::New(); + GifSpritePtr ptr = new (autogc) GifSprite; if (ptr) { if (!ptr->Load(file_path)) @@ -38,7 +38,7 @@ GifSpritePtr GifSprite::Create(const String& file_path) GifSpritePtr GifSprite::Create(const Resource& res) { - GifSpritePtr ptr = memory::New(); + GifSpritePtr ptr = new (autogc) GifSprite; if (ptr) { if (!ptr->Load(res)) @@ -49,7 +49,7 @@ GifSpritePtr GifSprite::Create(const Resource& res) GifSpritePtr GifSprite::Create(GifImagePtr gif) { - GifSpritePtr ptr = memory::New(); + GifSpritePtr ptr = new (autogc) GifSprite; if (ptr) { ptr->SetGifImage(gif); @@ -92,7 +92,7 @@ bool GifSprite::Load(GifImagePtr gif) frame_rt_.Reset(); Size frame_size = Size(float(gif_->GetWidthInPixels()), float(gif_->GetHeightInPixels())); - frame_to_render_ = memory::New(); + frame_to_render_ = new (autogc) Texture; frame_rt_ = RenderContext::Create(*frame_to_render_, frame_size); SetSize(frame_rt_->GetSize()); @@ -229,7 +229,7 @@ void GifSprite::SaveComposedFrame() if (!saved_frame_) { - saved_frame_ = memory::New(); + saved_frame_ = new (autogc) Texture; frame_rt_->CreateTexture(*saved_frame_, frame_to_render_->GetSizeInPixels()); } saved_frame_->CopyFrom(frame_to_render_); diff --git a/src/kiwano/2d/LayerActor.cpp b/src/kiwano/2d/LayerActor.cpp index 4daaddd8..f49ffe26 100644 --- a/src/kiwano/2d/LayerActor.cpp +++ b/src/kiwano/2d/LayerActor.cpp @@ -27,7 +27,7 @@ namespace kiwano LayerActorPtr LayerActor::Create() { - LayerActorPtr ptr = memory::New(); + LayerActorPtr ptr = new (autogc) LayerActor; return ptr; } diff --git a/src/kiwano/2d/ShapeActor.cpp b/src/kiwano/2d/ShapeActor.cpp index 5186451a..c837cf43 100644 --- a/src/kiwano/2d/ShapeActor.cpp +++ b/src/kiwano/2d/ShapeActor.cpp @@ -27,7 +27,7 @@ namespace kiwano ShapeActorPtr ShapeActor::Create(ShapePtr shape) { - ShapeActorPtr ptr = memory::New(); + ShapeActorPtr ptr = new (autogc) ShapeActor; if (ptr) { ptr->SetShape(shape); @@ -129,7 +129,7 @@ bool ShapeActor::CheckVisibility(RenderContext& ctx) const LineActorPtr LineActor::Create(const Point& begin, const Point& end) { - LineActorPtr ptr = memory::New(); + LineActorPtr ptr = new (autogc) LineActor; if (ptr) { ptr->SetLine(begin, end); @@ -157,7 +157,7 @@ void LineActor::SetLine(const Point& begin, const Point& end) RectActorPtr RectActor::Create(const Size& size) { - RectActorPtr ptr = memory::New(); + RectActorPtr ptr = new (autogc) RectActor; if (ptr) { ptr->SetRectSize(size); @@ -184,7 +184,7 @@ void RectActor::SetRectSize(const Size& size) RoundedRectActorPtr RoundedRectActor::Create(const Size& size, const Vec2& radius) { - RoundedRectActorPtr ptr = memory::New(); + RoundedRectActorPtr ptr = new (autogc) RoundedRectActor; if (ptr) { ptr->SetRoundedRect(size, radius); @@ -222,7 +222,7 @@ void RoundedRectActor::SetRoundedRect(const Size& size, const Vec2& radius) CircleActorPtr CircleActor::Create(float radius) { - CircleActorPtr ptr = memory::New(); + CircleActorPtr ptr = new (autogc) CircleActor; if (ptr) { ptr->SetRadius(radius); @@ -252,7 +252,7 @@ void CircleActor::SetRadius(float radius) EllipseActorPtr EllipseActor::Create(const Vec2& radius) { - EllipseActorPtr ptr = memory::New(); + EllipseActorPtr ptr = new (autogc) EllipseActor; if (ptr) { ptr->SetRadius(radius); @@ -279,7 +279,7 @@ void EllipseActor::SetRadius(const Vec2& radius) PolygonActorPtr PolygonActor::Create(const Vector& points) { - PolygonActorPtr ptr = memory::New(); + PolygonActorPtr ptr = new (autogc) PolygonActor; if (ptr) { ptr->SetVertices(points); diff --git a/src/kiwano/2d/ShapeActor.h b/src/kiwano/2d/ShapeActor.h index 6dcaf82b..ae4074f1 100644 --- a/src/kiwano/2d/ShapeActor.h +++ b/src/kiwano/2d/ShapeActor.h @@ -338,7 +338,7 @@ inline void ShapeActor::SetStrokeColor(const Color& color) { if (!stroke_brush_) { - stroke_brush_ = memory::New(); + stroke_brush_ = new (autogc) Brush; } stroke_brush_->SetColor(color); } @@ -347,7 +347,7 @@ inline void ShapeActor::SetFillColor(const Color& color) { if (!fill_brush_) { - fill_brush_ = memory::New(); + fill_brush_ = new (autogc) Brush; } fill_brush_->SetColor(color); } diff --git a/src/kiwano/2d/Sprite.cpp b/src/kiwano/2d/Sprite.cpp index 1a717463..34be7de6 100644 --- a/src/kiwano/2d/Sprite.cpp +++ b/src/kiwano/2d/Sprite.cpp @@ -26,7 +26,7 @@ namespace kiwano SpritePtr Sprite::Create(const String& file_path) { - SpritePtr ptr = memory::New(); + SpritePtr ptr = new (autogc) Sprite; if (ptr) { if (!ptr->Load(file_path)) @@ -37,7 +37,7 @@ SpritePtr Sprite::Create(const String& file_path) SpritePtr Sprite::Create(const Resource& res) { - SpritePtr ptr = memory::New(); + SpritePtr ptr = new (autogc) Sprite; if (ptr) { if (!ptr->Load(res)) @@ -48,7 +48,7 @@ SpritePtr Sprite::Create(const Resource& res) SpritePtr Sprite::Create(FramePtr frame) { - SpritePtr ptr = memory::New(); + SpritePtr ptr = new (autogc) Sprite; if (ptr) { ptr->SetFrame(frame); diff --git a/src/kiwano/2d/Stage.cpp b/src/kiwano/2d/Stage.cpp index 2c65c309..2a9ce6e3 100644 --- a/src/kiwano/2d/Stage.cpp +++ b/src/kiwano/2d/Stage.cpp @@ -27,7 +27,7 @@ namespace kiwano StagePtr Stage::Create() { - StagePtr ptr = memory::New(); + StagePtr ptr = new (autogc) Stage; return ptr; } @@ -57,13 +57,13 @@ void Stage::RenderBorder(RenderContext& ctx) if (!border_fill_brush_) { - border_fill_brush_ = memory::New(); + border_fill_brush_ = new (autogc) Brush; border_fill_brush_->SetColor(Color(Color::Red, .4f)); } if (!border_stroke_brush_) { - border_stroke_brush_ = memory::New(); + border_stroke_brush_ = new (autogc) Brush; border_stroke_brush_->SetColor(Color(Color::Red, .8f)); } diff --git a/src/kiwano/2d/TextActor.cpp b/src/kiwano/2d/TextActor.cpp index dfe0010d..0acea3ec 100644 --- a/src/kiwano/2d/TextActor.cpp +++ b/src/kiwano/2d/TextActor.cpp @@ -32,7 +32,7 @@ TextActorPtr TextActor::Create(const String& text) TextActorPtr TextActor::Create(const String& text, const TextStyle& style) { - TextActorPtr ptr = memory::New(); + TextActorPtr ptr = new (autogc) TextActor; if (ptr) { ptr->SetStyle(style); diff --git a/src/kiwano/2d/Transition.cpp b/src/kiwano/2d/Transition.cpp index acf0c722..fcd9c7a7 100644 --- a/src/kiwano/2d/Transition.cpp +++ b/src/kiwano/2d/Transition.cpp @@ -128,7 +128,7 @@ void Transition::Stop() BoxTransitionPtr BoxTransition::Create(Duration duration) { - BoxTransitionPtr ptr = memory::New(); + BoxTransitionPtr ptr = new (autogc) BoxTransition; if (ptr) { ptr->SetDuration(duration); @@ -169,7 +169,7 @@ void BoxTransition::Update(Duration dt) EmergeTransitionPtr EmergeTransition::Create(Duration duration) { - EmergeTransitionPtr ptr = memory::New(); + EmergeTransitionPtr ptr = new (autogc) EmergeTransition; if (ptr) { ptr->SetDuration(duration); @@ -201,7 +201,7 @@ void EmergeTransition::Update(Duration dt) FadeTransitionPtr FadeTransition::Create(Duration duration) { - FadeTransitionPtr ptr = memory::New(); + FadeTransitionPtr ptr = new (autogc) FadeTransition; if (ptr) { ptr->SetDuration(duration); @@ -241,7 +241,7 @@ void FadeTransition::Update(Duration dt) MoveTransitionPtr MoveTransition::Create(Duration duration, Type type) { - MoveTransitionPtr ptr = memory::New(); + MoveTransitionPtr ptr = new (autogc) MoveTransition; if (ptr) { ptr->type_ = type; @@ -330,7 +330,7 @@ void MoveTransition::Reset() RotationTransitionPtr RotationTransition::Create(Duration duration, float rotation) { - RotationTransitionPtr ptr = memory::New(); + RotationTransitionPtr ptr = new (autogc) RotationTransition; if (ptr) { ptr->rotation_ = rotation; diff --git a/src/kiwano/2d/action/Action.h b/src/kiwano/2d/action/Action.h index f9d0ac5c..2a48709a 100644 --- a/src/kiwano/2d/action/Action.h +++ b/src/kiwano/2d/action/Action.h @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/kiwano/2d/action/ActionDelay.cpp b/src/kiwano/2d/action/ActionDelay.cpp index 06f1023f..1b7750d9 100644 --- a/src/kiwano/2d/action/ActionDelay.cpp +++ b/src/kiwano/2d/action/ActionDelay.cpp @@ -30,7 +30,7 @@ ActionDelay::ActionDelay(Duration delay) ActionDelayEntityPtr ActionDelayEntity::Create(Duration delay) { - ActionDelayEntityPtr ptr = memory::New(); + ActionDelayEntityPtr ptr = new (autogc) ActionDelayEntity; if (ptr) { ptr->SetDelay(delay); diff --git a/src/kiwano/2d/action/ActionGroup.cpp b/src/kiwano/2d/action/ActionGroup.cpp index f11b7509..2128c3b2 100644 --- a/src/kiwano/2d/action/ActionGroup.cpp +++ b/src/kiwano/2d/action/ActionGroup.cpp @@ -32,7 +32,7 @@ ActionGroup::ActionGroup(const Vector& actions, bool parallel) ActionGroupEntityPtr ActionGroupEntity::Create(const Vector& actions, bool parallel) { - ActionGroupEntityPtr ptr = memory::New(); + ActionGroupEntityPtr ptr = new (autogc) ActionGroupEntity; if (ptr) { ptr->parallel_ = parallel; diff --git a/src/kiwano/2d/action/ActionTween.cpp b/src/kiwano/2d/action/ActionTween.cpp index c0c65865..4669db6c 100644 --- a/src/kiwano/2d/action/ActionTween.cpp +++ b/src/kiwano/2d/action/ActionTween.cpp @@ -145,7 +145,7 @@ ActionEntityPtr ActionTweenEntity::DoClone(ActionTweenEntityPtr to) const ActionMoveByEntityPtr ActionMoveByEntity::Create(Duration duration, const Vec2& displacement) { - ActionMoveByEntityPtr ptr = memory::New(); + ActionMoveByEntityPtr ptr = new (autogc) ActionMoveByEntity; if (ptr) { ptr->SetDuration(duration); @@ -187,7 +187,7 @@ ActionEntityPtr ActionMoveByEntity::Reverse() const ActionMoveToEntityPtr ActionMoveToEntity::Create(Duration duration, const Point& distination) { - ActionMoveToEntityPtr ptr = memory::New(); + ActionMoveToEntityPtr ptr = new (autogc) ActionMoveToEntity; if (ptr) { ptr->SetDuration(duration); @@ -215,7 +215,7 @@ void ActionMoveToEntity::Init(Actor* target) ActionJumpByEntityPtr ActionJumpByEntity::Create(Duration duration, const Vec2& displacement, float height, int count) { - ActionJumpByEntityPtr ptr = memory::New(); + ActionJumpByEntityPtr ptr = new (autogc) ActionJumpByEntity; if (ptr) { ptr->SetDuration(duration); @@ -268,7 +268,7 @@ void ActionJumpByEntity::UpdateTween(Actor* target, float percent) ActionJumpToEntityPtr ActionJumpToEntity::Create(Duration duration, const Point& distination, float height, int count) { - ActionJumpToEntityPtr ptr = memory::New(); + ActionJumpToEntityPtr ptr = new (autogc) ActionJumpToEntity; if (ptr) { ptr->SetDuration(duration); @@ -298,7 +298,7 @@ void ActionJumpToEntity::Init(Actor* target) ActionScaleByEntityPtr ActionScaleByEntity::Create(Duration duration, float scale_x, float scale_y) { - ActionScaleByEntityPtr ptr = memory::New(); + ActionScaleByEntityPtr ptr = new (autogc) ActionScaleByEntity; if (ptr) { ptr->SetDuration(duration); @@ -342,7 +342,7 @@ ActionEntityPtr ActionScaleByEntity::Reverse() const ActionScaleToEntityPtr ActionScaleToEntity::Create(Duration duration, float scale_x, float scale_y) { - ActionScaleToEntityPtr ptr = memory::New(); + ActionScaleToEntityPtr ptr = new (autogc) ActionScaleToEntity; if (ptr) { ptr->SetDuration(duration); @@ -376,7 +376,7 @@ void ActionScaleToEntity::Init(Actor* target) ActionFadeToEntityPtr ActionFadeToEntity::Create(Duration duration, float opacity) { - ActionFadeToEntityPtr ptr = memory::New(); + ActionFadeToEntityPtr ptr = new (autogc) ActionFadeToEntity; if (ptr) { ptr->SetDuration(duration); @@ -417,7 +417,7 @@ ActionEntityPtr ActionFadeToEntity::Clone() const ActionRotateByEntityPtr ActionRotateByEntity::Create(Duration duration, float rotation) { - ActionRotateByEntityPtr ptr = memory::New(); + ActionRotateByEntityPtr ptr = new (autogc) ActionRotateByEntity; if (ptr) { ptr->SetDuration(duration); @@ -461,7 +461,7 @@ ActionEntityPtr ActionRotateByEntity::Reverse() const ActionRotateToEntityPtr ActionRotateToEntity::Create(Duration duration, float rotation) { - ActionRotateToEntityPtr ptr = memory::New(); + ActionRotateToEntityPtr ptr = new (autogc) ActionRotateToEntity; if (ptr) { ptr->SetDuration(duration); @@ -492,7 +492,7 @@ void ActionRotateToEntity::Init(Actor* target) ActionCustomEntityPtr ActionCustomEntity::Create(Duration duration, ActionCustom::TweenFunc tween_func) { - ActionCustomEntityPtr ptr = memory::New(); + ActionCustomEntityPtr ptr = new (autogc) ActionCustomEntity; if (ptr) { ptr->SetDuration(duration); diff --git a/src/kiwano/2d/action/ActionWalk.cpp b/src/kiwano/2d/action/ActionWalk.cpp index 8a1eb12c..db197249 100644 --- a/src/kiwano/2d/action/ActionWalk.cpp +++ b/src/kiwano/2d/action/ActionWalk.cpp @@ -31,7 +31,7 @@ ActionWalk::ActionWalk(Duration duration, ShapePtr path, bool rotating, float st ActionWalkEntityPtr ActionWalkEntity::Create(Duration duration, ShapePtr path, bool rotating, float start, float end) { - ActionWalkEntityPtr ptr = memory::New(); + ActionWalkEntityPtr ptr = new (autogc) ActionWalkEntity; if (ptr) { ptr->SetDuration(duration); diff --git a/src/kiwano/2d/action/Animation.cpp b/src/kiwano/2d/action/Animation.cpp index 3e494f48..4828db46 100644 --- a/src/kiwano/2d/action/Animation.cpp +++ b/src/kiwano/2d/action/Animation.cpp @@ -32,7 +32,7 @@ Animation::Animation(Duration dur, FrameSequencePtr frame_seq) AnimationEntityPtr AnimationEntity::Create(Duration dur, FrameSequencePtr frame_seq) { - AnimationEntityPtr ptr = memory::New(); + AnimationEntityPtr ptr = new (autogc) AnimationEntity; if (ptr) { ptr->SetDuration(dur); diff --git a/src/kiwano/base/Director.cpp b/src/kiwano/base/Director.cpp index d4d09a92..263d1564 100644 --- a/src/kiwano/base/Director.cpp +++ b/src/kiwano/base/Director.cpp @@ -99,7 +99,7 @@ void Director::ShowDebugInfo(bool show) if (show) { if (!debug_actor_) - debug_actor_ = memory::New(); + debug_actor_ = new (autogc) DebugActor; } else { diff --git a/src/kiwano/base/ObjectBase.cpp b/src/kiwano/base/ObjectBase.cpp index 339350ea..99c05f9e 100644 --- a/src/kiwano/base/ObjectBase.cpp +++ b/src/kiwano/base/ObjectBase.cpp @@ -19,7 +19,6 @@ // THE SOFTWARE. #include -#include #include #include #include @@ -28,9 +27,11 @@ namespace kiwano { namespace { + bool tracing_leaks = false; Vector tracing_objects; uint32_t last_object_id = 0; + } // namespace ObjectBase::ObjectBase() @@ -57,11 +58,6 @@ ObjectBase::~ObjectBase() #endif } -void ObjectBase::AutoRelease() -{ - ObjectPool::GetInstance().AddObject(this); -} - const Any& ObjectBase::GetUserData() const { return user_data_; diff --git a/src/kiwano/base/ObjectBase.h b/src/kiwano/base/ObjectBase.h index 927e2549..7f7859bd 100644 --- a/src/kiwano/base/ObjectBase.h +++ b/src/kiwano/base/ObjectBase.h @@ -22,8 +22,8 @@ #include #include #include -#include -#include +#include +#include namespace kiwano { @@ -34,7 +34,7 @@ KGE_DECLARE_SMART_PTR(ObjectBase); * @brief 基础对象 */ class KGE_API ObjectBase - : public RefCounter + : public RefObject , public Serializable { public: @@ -44,10 +44,6 @@ public: virtual ~ObjectBase(); - /// \~chinese - /// @brief 自动释放 - void AutoRelease(); - /// \~chinese /// @brief 设置对象名 void SetName(const String& name); diff --git a/src/kiwano/base/ObjectPool.cpp b/src/kiwano/base/ObjectPool.cpp index 66ba259c..189a43de 100644 --- a/src/kiwano/base/ObjectPool.cpp +++ b/src/kiwano/base/ObjectPool.cpp @@ -23,56 +23,35 @@ namespace kiwano { -List ObjectPool::pools_; - -ObjectPool& ObjectPool::GetInstance() -{ - static ObjectPool instance; - return *pools_.back(); -} - ObjectPool::ObjectPool() { - pools_.push_back(this); } ObjectPool::~ObjectPool() { Clear(); - - auto iter = std::find(pools_.begin(), pools_.end(), this); - if (iter != pools_.end()) - pools_.erase(iter); } -void ObjectPool::AddObject(ObjectBase* obj) +void ObjectPool::AddObject(RefObject* obj) { if (obj) { + std::lock_guard lock(mutex_); if (!Contains(obj)) { - obj->Retain(); - - std::lock_guard lock(mutex_); objects_.push_back(obj); } } } -bool ObjectPool::Contains(ObjectBase* obj) const +bool ObjectPool::Contains(RefObject* obj) const { - std::lock_guard lock(const_cast(mutex_)); - - for (auto iter = pools_.rbegin(); iter != pools_.rend(); iter++) - for (const auto o : (*iter)->objects_) - if (obj == o) - return true; - return false; + return std::find(objects_.begin(), objects_.end(), obj) != objects_.end(); } void ObjectPool::Clear() { - Vector copied; + Vector copied; { std::lock_guard lock(mutex_); diff --git a/src/kiwano/base/ObjectPool.h b/src/kiwano/base/ObjectPool.h index 72ac3d5b..af17d68f 100644 --- a/src/kiwano/base/ObjectPool.h +++ b/src/kiwano/base/ObjectPool.h @@ -19,21 +19,22 @@ // THE SOFTWARE. #pragma once +#include #include #include namespace kiwano { + /** * \~chinese * @brief 对象池 */ -class KGE_API ObjectPool - : public Noncopyable +class KGE_API ObjectPool : public Singleton { -public: - static ObjectPool& GetInstance(); + friend Singleton; +public: ObjectPool(); virtual ~ObjectPool(); @@ -41,16 +42,16 @@ public: /** * \~chinese * @brief 添加对象到内存池 - * @param[in] obj 基础对象 + * @param[in] obj 引用计数对象 */ - void AddObject(ObjectBase* obj); + void AddObject(RefObject* obj); /** * \~chinese * @brief 判断对象是否在对象池中 - * @param[in] obj 基础对象 + * @param[in] obj 引用计数对象 */ - bool Contains(ObjectBase* obj) const; + bool Contains(RefObject* obj) const; /** * \~chinese @@ -65,8 +66,7 @@ private: private: std::mutex mutex_; - Vector objects_; - - static List pools_; + Vector objects_; }; + } // namespace kiwano diff --git a/src/kiwano/base/RefCounter.cpp b/src/kiwano/base/RefObject.cpp similarity index 61% rename from src/kiwano/base/RefCounter.cpp rename to src/kiwano/base/RefObject.cpp index 27e44e5e..04464071 100644 --- a/src/kiwano/base/RefCounter.cpp +++ b/src/kiwano/base/RefObject.cpp @@ -18,29 +18,63 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#include +#include +#include namespace kiwano { -RefCounter::RefCounter() - : ref_count_(0) + +autogc_t const autogc; + +RefObject::RefObject() + : ref_count_(1) { } -RefCounter::~RefCounter() {} +RefObject::~RefObject() {} -void RefCounter::Retain() +void RefObject::Retain() { ++ref_count_; } -void RefCounter::Release() +void RefObject::Release() { --ref_count_; if (ref_count_ == 0) { - memory::Delete(this); + delete this; } } +void RefObject::AutoRelease() +{ + ObjectPool::GetInstance().AddObject(this); +} + +void* RefObject::operator new(std::size_t size) +{ + return memory::Alloc(size); +} + +void* RefObject::operator new(std::size_t size, autogc_t const&) +{ + void* ptr = memory::Alloc(size); + if (ptr) + { + ObjectPool::GetInstance().AddObject((ObjectBase*)ptr); + } + return ptr; +} + +void RefObject::operator delete(void* ptr) +{ + memory::Free(ptr); +} + +void RefObject::operator delete(void* ptr, autogc_t const&) +{ + memory::Free(ptr); +} + } // namespace kiwano diff --git a/src/kiwano/base/RefCounter.h b/src/kiwano/base/RefObject.h similarity index 74% rename from src/kiwano/base/RefCounter.h rename to src/kiwano/base/RefObject.h index 9a030876..ede57076 100644 --- a/src/kiwano/base/RefCounter.h +++ b/src/kiwano/base/RefObject.h @@ -24,17 +24,21 @@ namespace kiwano { + +struct autogc_t +{ + autogc_t() = default; +}; + +extern autogc_t const autogc; + /** * \~chinese * @brief 引用计数器 */ -class KGE_API RefCounter : protected Noncopyable +class KGE_API RefObject : protected Noncopyable { public: - RefCounter(); - - virtual ~RefCounter(); - /// \~chinese /// @brief 增加引用计数 void Retain(); @@ -43,17 +47,34 @@ public: /// @brief 减少引用计数 void Release(); + /// \~chinese + /// @brief 自动释放 + void AutoRelease(); + /// \~chinese /// @brief 获取引用计数 uint32_t GetRefCount() const; + void* operator new(std::size_t size); + + void* operator new(std::size_t size, autogc_t const&); + + void operator delete(void* ptr); + + void operator delete(void* ptr, autogc_t const&); + +protected: + RefObject(); + + virtual ~RefObject(); + private: std::atomic ref_count_; }; -inline uint32_t RefCounter::GetRefCount() const +inline uint32_t RefObject::GetRefCount() const { - return ref_count_; + return ref_count_.load(); } } // namespace kiwano diff --git a/src/kiwano/base/component/Button.cpp b/src/kiwano/base/component/Button.cpp index c8fbc49b..64388a98 100644 --- a/src/kiwano/base/component/Button.cpp +++ b/src/kiwano/base/component/Button.cpp @@ -33,7 +33,7 @@ ButtonPtr Button::Create(const Callback& click) ButtonPtr Button::Create(const Callback& click, const Callback& pressed, const Callback& mouse_over, const Callback& mouse_out) { - ButtonPtr ptr = memory::New