From f19366d32cc29287a2730cfba90893e407754081 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 1 Oct 2021 22:09:54 +0300 Subject: [PATCH] Use constructor delegation instead of init functions when possible --- source/animation/animation.cpp | 21 +++------- source/animation/animation.h | 6 +-- source/animation/keyframe.cpp | 15 +------ source/animation/keyframe.h | 6 +-- source/builders/font.cpp | 15 +------ source/builders/font.h | 8 ++-- source/builders/grid.cpp | 60 +++++++--------------------- source/builders/grid.h | 2 +- source/builders/sequencetemplate.cpp | 15 +------ source/builders/sequencetemplate.h | 5 ++- source/core/framebuffer.cpp | 22 ++++------ source/core/framebuffer.h | 4 -- source/core/mesh.cpp | 26 +++++------- source/core/mesh.h | 3 -- source/core/texture.cpp | 18 ++------- source/core/texture.h | 6 +-- source/effects/environmentmap.cpp | 17 +------- source/effects/environmentmap.h | 3 -- source/render/object.cpp | 15 +------ source/render/object.h | 6 +-- source/render/scene.cpp | 19 ++------- source/render/scene.h | 6 +-- 22 files changed, 76 insertions(+), 222 deletions(-) diff --git a/source/animation/animation.cpp b/source/animation/animation.cpp index a9278e3d..98cfdc87 100644 --- a/source/animation/animation.cpp +++ b/source/animation/animation.cpp @@ -489,23 +489,12 @@ Matrix Animation::Iterator::get_pose_matrix(unsigned link) const } -Animation::Loader::Loader(Animation &a): - DataFile::CollectionObjectLoader(a, 0) +Animation::Loader::Loader(Animation &a, Collection *c): + DataFile::CollectionObjectLoader(a, c), + start_slope(1), + end_slope(1), + slopes_set(0) { - init(); -} - -Animation::Loader::Loader(Animation &a, Collection &c): - DataFile::CollectionObjectLoader(a, &c) -{ - init(); -} - -void Animation::Loader::init() -{ - start_slope = 1; - end_slope = 1; - slopes_set = 0; add("armature", &Animation::armature); add("control_keyframe", &Loader::control_keyframe); add("control_keyframe", &Loader::control_keyframe_inline); diff --git a/source/animation/animation.h b/source/animation/animation.h index 3d247cf5..eb5911c3 100644 --- a/source/animation/animation.h +++ b/source/animation/animation.h @@ -31,10 +31,10 @@ public: int slopes_set; public: - Loader(Animation &); - Loader(Animation &, Collection &); + Loader(Animation &a): Loader(a, 0) { } + Loader(Animation &a, Collection &c): Loader(a, &c) { } private: - void init(); + Loader(Animation &, Collection *); virtual void finish(); void check_slopes_and_control(bool, bool); diff --git a/source/animation/keyframe.cpp b/source/animation/keyframe.cpp index 6cb8ce6a..3f3dea28 100644 --- a/source/animation/keyframe.cpp +++ b/source/animation/keyframe.cpp @@ -47,19 +47,8 @@ KeyFrame::AnimatedUniform::AnimatedUniform(unsigned s, float v0, float v1, float } -KeyFrame::Loader::Loader(KeyFrame &k): - DataFile::CollectionObjectLoader(k, 0) -{ - init(); -} - -KeyFrame::Loader::Loader(KeyFrame &k, Collection &c): - DataFile::CollectionObjectLoader(k, &c) -{ - init(); -} - -void KeyFrame::Loader::init() +KeyFrame::Loader::Loader(KeyFrame &k, Collection *c): + DataFile::CollectionObjectLoader(k, c) { add("pose", &Loader::pose); add("pose", &Loader::pose_inline); diff --git a/source/animation/keyframe.h b/source/animation/keyframe.h index 7a96447e..26d18534 100644 --- a/source/animation/keyframe.h +++ b/source/animation/keyframe.h @@ -23,10 +23,10 @@ public: std::string inline_base_name; public: - Loader(KeyFrame &); - Loader(KeyFrame &, Collection &); + Loader(KeyFrame &k): Loader(k, 0) { } + Loader(KeyFrame &k, Collection &c): Loader(k, &c) { } private: - void init(); + Loader(KeyFrame &, Collection *); public: void set_inline_base_name(const std::string &); diff --git a/source/builders/font.cpp b/source/builders/font.cpp index f46475bc..1ede612a 100644 --- a/source/builders/font.cpp +++ b/source/builders/font.cpp @@ -143,19 +143,8 @@ Font::Glyph::Glyph(): { } -Font::Loader::Loader(Font &f): - DataFile::CollectionObjectLoader(f, 0) -{ - init(); -} - -Font::Loader::Loader(Font &f, Collection &c): - DataFile::CollectionObjectLoader(f, &c) -{ - init(); -} - -void Font::Loader::init() +Font::Loader::Loader(Font &f, Collection *c): + DataFile::CollectionObjectLoader(f, c) { add("native_size", &Font::native_size); add("ascent", &Font::ascent); diff --git a/source/builders/font.h b/source/builders/font.h index 2bb61f75..af65da8a 100644 --- a/source/builders/font.h +++ b/source/builders/font.h @@ -21,11 +21,11 @@ public: class Loader: public DataFile::CollectionObjectLoader { public: - Loader(Font &); - Loader(Font &, Collection &); - + Loader(Font &f): Loader(f, 0) { } + Loader(Font &f, Collection &c): Loader(f, &c) { } private: - void init(); + Loader(Font &, Collection *); + void glyph(unsigned); void kerning(unsigned, unsigned, float); void ligature(unsigned, unsigned, unsigned); diff --git a/source/builders/grid.cpp b/source/builders/grid.cpp index d812a2c2..f452c438 100644 --- a/source/builders/grid.cpp +++ b/source/builders/grid.cpp @@ -8,62 +8,32 @@ namespace Msp { namespace GL { GridBuilder::GridBuilder(float w, float h, unsigned u, unsigned v): - origin(-w/2, -h/2, 0), - side1(w, 0, 0), - side2(0, h, 0), - norm(0, 0, 1), - binorm(0, 1, 0), - u_div(u), - v_div(v) -{ - init(false); -} + GridBuilder(Vector3(-w/2, -h/2, 0), Vector3(w, 0, 0), Vector3(0, h, 0), u, v) +{ } GridBuilder::GridBuilder(const Vector3 &o, const Vector3 &s, unsigned u, unsigned v): - origin(o), - u_div(u), - v_div(v) -{ - if(abs(s.z)(p, 0) -{ - init(); -} - -SequenceTemplate::Step::Loader::Loader(Step &p, Collection &c): - DataFile::CollectionObjectLoader(p, &c) -{ - init(); -} - -void SequenceTemplate::Step::Loader::init() +SequenceTemplate::Step::Loader::Loader(Step &p, Collection *c): + DataFile::CollectionObjectLoader(p, c) { add("blend", &Loader::blend); add("blend", &Loader::blend_factors); diff --git a/source/builders/sequencetemplate.h b/source/builders/sequencetemplate.h index c49b2d43..bf1e8be1 100644 --- a/source/builders/sequencetemplate.h +++ b/source/builders/sequencetemplate.h @@ -70,9 +70,10 @@ public: std::string inline_base_name; public: - Loader(Step &); - Loader(Step &, Collection &); + Loader(Step &s): Loader(s, 0) { } + Loader(Step &s, Collection &c): Loader(s, &c) { } private: + Loader(Step &, Collection *); void init(); public: diff --git a/source/core/framebuffer.cpp b/source/core/framebuffer.cpp index 167b403a..61327723 100644 --- a/source/core/framebuffer.cpp +++ b/source/core/framebuffer.cpp @@ -27,32 +27,24 @@ Framebuffer::Framebuffer(bool s): } Framebuffer::Framebuffer(): - FramebufferBackend(false) -{ - init(); -} + FramebufferBackend(false), + width(0), + height(0), + dirty(0) +{ } Framebuffer::Framebuffer(FrameAttachment fa): - FramebufferBackend(false) + Framebuffer() { - init(); set_format(fa); } Framebuffer::Framebuffer(const FrameFormat &f): - FramebufferBackend(false) + Framebuffer() { - init(); set_format(f); } -void Framebuffer::init() -{ - width = 0; - height = 0; - dirty = 0; -} - void Framebuffer::set_format(const FrameFormat &fmt) { if(!format.empty() || !id) diff --git a/source/core/framebuffer.h b/source/core/framebuffer.h index 18e48f20..0b9af1ef 100644 --- a/source/core/framebuffer.h +++ b/source/core/framebuffer.h @@ -68,10 +68,6 @@ public: /** Creates a framebuffer and sets its format. */ Framebuffer(const FrameFormat &); -private: - void init(); - -public: /** Sets the format of the framebuffer. Once the format is set, it can't be changed. */ void set_format(const FrameFormat &); diff --git a/source/core/mesh.cpp b/source/core/mesh.cpp index 029c16e0..981a59e8 100644 --- a/source/core/mesh.cpp +++ b/source/core/mesh.cpp @@ -9,29 +9,23 @@ using namespace std; namespace Msp { namespace GL { -Mesh::Mesh(ResourceManager *rm) +Mesh::Mesh(ResourceManager *rm): + vbuf(0), + ibuf(0), + dirty(0), + disallow_rendering(false), + face_winding(NON_MANIFOLD) { - init(rm); + if(rm) + set_manager(rm); } -Mesh::Mesh(const VertexFormat &f, ResourceManager *rm) +Mesh::Mesh(const VertexFormat &f, ResourceManager *rm): + Mesh(rm) { - init(rm); storage(f); } -void Mesh::init(ResourceManager *rm) -{ - vbuf = 0; - ibuf = 0; - dirty = 0; - disallow_rendering = false; - face_winding = NON_MANIFOLD; - - if(rm) - set_manager(rm); -} - Mesh::~Mesh() { set_manager(0); diff --git a/source/core/mesh.h b/source/core/mesh.h index 010289b0..39bf55b2 100644 --- a/source/core/mesh.h +++ b/source/core/mesh.h @@ -77,9 +77,6 @@ private: public: Mesh(ResourceManager * = 0); Mesh(const VertexFormat &, ResourceManager * = 0); -private: - void init(ResourceManager *); -public: ~Mesh(); void storage(const VertexFormat &); diff --git a/source/core/texture.cpp b/source/core/texture.cpp index 4968e2e6..85f5fb58 100644 --- a/source/core/texture.cpp +++ b/source/core/texture.cpp @@ -65,22 +65,10 @@ void Texture::load_image(const string &fn, unsigned lv) } -Texture::Loader::Loader(Texture &t): - DataFile::CollectionObjectLoader(t, 0) +Texture::Loader::Loader(Texture &t, Collection *c): + CollectionObjectLoader(t, c), + levels(0) { - init(); -} - -Texture::Loader::Loader(Texture &t, Collection &c): - DataFile::CollectionObjectLoader(t, &c) -{ - init(); -} - -void Texture::Loader::init() -{ - levels = 0; - add("external_image", &Loader::external_image); add("external_image_srgb", &Loader::external_image_srgb); add("generate_mipmap", &Loader::generate_mipmap); diff --git a/source/core/texture.h b/source/core/texture.h index 6073cdcb..2f647b90 100644 --- a/source/core/texture.h +++ b/source/core/texture.h @@ -30,10 +30,10 @@ protected: unsigned levels; public: - Loader(Texture &); - Loader(Texture &, Collection &); + Loader(Texture &t): Loader(t, 0) { } + Loader(Texture &t, Collection &c): Loader(t, &c) { } private: - void init(); + Loader(Texture &, Collection *); virtual void finish(); diff --git a/source/effects/environmentmap.cpp b/source/effects/environmentmap.cpp index 2291a202..315390b1 100644 --- a/source/effects/environmentmap.cpp +++ b/source/effects/environmentmap.cpp @@ -12,16 +12,8 @@ namespace Msp { namespace GL { EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, Renderable &r, Renderable &e): - Effect(r), - environment(e), - irradiance_shprog(Resources::get_global().get("_envmap_irradiance.glsl.shader")), - specular_shprog(Resources::get_global().get("_envmap_specular.glsl.shader")), - fullscreen_mesh(Resources::get_global().get("_fullscreen_quad.mesh")), - sampler(Resources::get_global().get("_linear_clamp.samp")), - mip_sampler(Resources::get_global().get("_mip_clamp.samp")) -{ - init(s, f, 1); -} + EnvironmentMap(s, f, 1, r, e) +{ } EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, unsigned l, Renderable &r, Renderable &e): Effect(r), @@ -31,11 +23,6 @@ EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, unsigned l, Renderable fullscreen_mesh(Resources::get_global().get("_fullscreen_quad.mesh")), sampler(Resources::get_global().get("_linear_clamp.samp")), mip_sampler(Resources::get_global().get("_mip_clamp.samp")) -{ - init(s, f, l); -} - -void EnvironmentMap::init(unsigned s, PixelFormat f, unsigned l) { if(!l || (1U<<(l-1))>=s) throw invalid_argument("EnvironmentMap::EnvironmentMap"); diff --git a/source/effects/environmentmap.h b/source/effects/environmentmap.h index 98328488..bf6fbaf0 100644 --- a/source/effects/environmentmap.h +++ b/source/effects/environmentmap.h @@ -57,10 +57,7 @@ private: public: EnvironmentMap(unsigned size, PixelFormat, Renderable &rend, Renderable &env); EnvironmentMap(unsigned size, PixelFormat, unsigned, Renderable &rend, Renderable &env); -private: - void init(unsigned, PixelFormat, unsigned); -public: void set_depth_clip(float, float); /** Sets the interval in frames between environment map updates. A value of diff --git a/source/render/object.cpp b/source/render/object.cpp index 98eb05f7..34eebec7 100644 --- a/source/render/object.cpp +++ b/source/render/object.cpp @@ -188,19 +188,8 @@ void Object::resource_removed(Resource &res) } -Object::Loader::Loader(Object &o): - LodLoader(o, 0, 0) -{ - init(); -} - -Object::Loader::Loader(Object &o, Collection &c): - LodLoader(o, 0, &c) -{ - init(); -} - -void Object::Loader::init() +Object::Loader::Loader(Object &o, Collection *c): + LodLoader(o, 0, c) { add("bounding_sphere_hint", &Loader::bounding_sphere_hint); add("level_of_detail", &Loader::level_of_detail); diff --git a/source/render/object.h b/source/render/object.h index 0fbcf383..ac62c702 100644 --- a/source/render/object.h +++ b/source/render/object.h @@ -50,10 +50,10 @@ public: class Loader: public LodLoader { public: - Loader(Object &); - Loader(Object &, Collection &); + Loader(Object &o): Loader(o, 0) { } + Loader(Object &o, Collection &c): Loader(o, &c) { } private: - void init(); + Loader(Object &, Collection *); virtual void finish(); void bounding_sphere_hint(float, float, float, float); diff --git a/source/render/scene.cpp b/source/render/scene.cpp index 87d8c423..22958867 100644 --- a/source/render/scene.cpp +++ b/source/render/scene.cpp @@ -89,24 +89,11 @@ Scene::SceneRegistry &Scene::get_scene_registry() } -Scene::Loader::Loader(Scene &s, Collection &c): +Scene::Loader::Loader(Scene &s, Collection &c, ContentMap *m): DataFile::CollectionObjectLoader(s, &c), - content(0) + content(m), + inst_counter(0) { - init(); -} - -Scene::Loader::Loader(Scene &s, Collection &c, ContentMap &m): - DataFile::CollectionObjectLoader(s, &c), - content(&m) -{ - init(); -} - -void Scene::Loader::init() -{ - inst_counter = 0; - add("object", &Loader::object); add("object", &Loader::object_tagged); add("scene", &Loader::scene); diff --git a/source/render/scene.h b/source/render/scene.h index b6895344..e4f5ecdf 100644 --- a/source/render/scene.h +++ b/source/render/scene.h @@ -28,10 +28,10 @@ protected: unsigned inst_counter; public: - Loader(Scene &, Collection &); - Loader(Scene &, Collection &, ContentMap &); + Loader(Scene &s, Collection &c): Loader(s, c, 0) { } + Loader(Scene &s, Collection &c, ContentMap &m) : Loader(s, c, &m) { } private: - void init(); + Loader(Scene &, Collection &, ContentMap *); void object(const std::string &); void object_tagged(const std::string &, const std::string &); -- 2.45.2