From: Mikko Rasa Date: Sun, 20 Aug 2023 09:10:32 +0000 (+0300) Subject: Simplify constructors with C++11 X-Git-Url: http://git.tdb.fi/?p=libs%2Fgltk.git;a=commitdiff_plain;h=9f38197854e699a6093a906ab43f4238f3cd2388 Simplify constructors with C++11 Use default member initializers, defaulted constructors and constructor delegation. --- diff --git a/source/arrangement.cpp b/source/arrangement.cpp index 838a488..2adb965 100644 --- a/source/arrangement.cpp +++ b/source/arrangement.cpp @@ -79,10 +79,6 @@ Layout::ConstraintType Arrangement::get_align_constraint(Side s) } -Arrangement::Edge::Edge(): - aligned(false) -{ } - void Arrangement::Edge::clear() { widgets.clear(); diff --git a/source/arrangement.h b/source/arrangement.h index 31001c4..164a091 100644 --- a/source/arrangement.h +++ b/source/arrangement.h @@ -32,9 +32,7 @@ protected: struct Edge { std::list widgets; - bool aligned; - - Edge(); + bool aligned = false; bool empty() { return widgets.empty(); } void clear(); @@ -43,7 +41,7 @@ protected: }; Layout &layout; - Arrangement *parent; + Arrangement *parent = 0; Edge edges[4]; Arrangement(Layout &); diff --git a/source/button.cpp b/source/button.cpp index abdc1f6..0dd20c7 100644 --- a/source/button.cpp +++ b/source/button.cpp @@ -8,10 +8,7 @@ using namespace std; namespace Msp { namespace GLtk { -Button::Button(const std::string &t): - text(), - icon(0), - pressed(false) +Button::Button(const std::string &t) { input_type = INPUT_NAVIGATION; set_text(t); diff --git a/source/button.h b/source/button.h index 2af9cf9..09084f1 100644 --- a/source/button.h +++ b/source/button.h @@ -29,8 +29,8 @@ public: private: Text text; - const GL::Texture2D *icon; - bool pressed; + const GL::Texture2D *icon = 0; + bool pressed = false; public: Button(const std::string & = std::string()); diff --git a/source/container.cpp b/source/container.cpp index d5fa6d5..ef837a2 100644 --- a/source/container.cpp +++ b/source/container.cpp @@ -12,17 +12,6 @@ hierarchy_error::hierarchy_error(const string &w): { } -Container::Container(): - click_focus(0), - click_button(0), - pointer_focus(0), - pointer_grabbed(false), - input_focus(0), - saved_input_focus(0), - touch_focus(0), - children_rebuild_needed(false) -{ } - Container::~Container() { while(!children.empty()) diff --git a/source/container.h b/source/container.h index 4c38675..61502e4 100644 --- a/source/container.h +++ b/source/container.h @@ -24,7 +24,7 @@ protected: struct Child: public sigc::trackable { Container &container; - Widget *widget; + Widget *widget = 0; Time::TimeDelta time_since_animate; Child(Container &, Widget *); @@ -39,16 +39,16 @@ protected: }; std::list children; - Widget *click_focus; - unsigned click_button; - Widget *pointer_focus; - bool pointer_grabbed; - Widget *input_focus; - Widget *saved_input_focus; - Widget *touch_focus; - bool children_rebuild_needed; - - Container(); + Widget *click_focus = 0; + unsigned click_button = 0; + Widget *pointer_focus = 0; + bool pointer_grabbed = false; + Widget *input_focus = 0; + Widget *saved_input_focus = 0; + Widget *touch_focus = 0; + bool children_rebuild_needed = false; + + Container() = default; public: virtual ~Container(); diff --git a/source/dialog.cpp b/source/dialog.cpp index a264fbe..5f63dcf 100644 --- a/source/dialog.cpp +++ b/source/dialog.cpp @@ -6,10 +6,6 @@ using namespace std; namespace Msp { namespace GLtk { -Dialog::Dialog(): - stale(false) -{ } - void Dialog::add_button(Button &button, int code) { add(button); diff --git a/source/dialog.h b/source/dialog.h index 3eb3f98..e712227 100644 --- a/source/dialog.h +++ b/source/dialog.h @@ -27,11 +27,9 @@ public: sigc::signal signal_response; private: - bool stale; + bool stale = false; public: - Dialog(); - virtual const char *get_class() const { return "dialog"; } /** Adds an action button to the dialog. Pressing the button will invoke diff --git a/source/draghandle.cpp b/source/draghandle.cpp index 067a515..c9e7c58 100644 --- a/source/draghandle.cpp +++ b/source/draghandle.cpp @@ -4,12 +4,6 @@ namespace Msp { namespace GLtk { -DragHandle::DragHandle(): - dragging(false), - drag_x(0), - drag_y(0) -{ } - void DragHandle::button_press(int x, int y, unsigned btn) { if(btn==1 && parent) diff --git a/source/draghandle.h b/source/draghandle.h index 0aae732..d3cd6f2 100644 --- a/source/draghandle.h +++ b/source/draghandle.h @@ -14,13 +14,11 @@ into a movable window. class MSPGLTK_API DragHandle: public Widget { private: - bool dragging; - int drag_x; - int drag_y; + bool dragging = false; + int drag_x = 0; + int drag_y = 0; public: - DragHandle(); - virtual const char *get_class() const { return "draghandle"; } virtual void button_press(int, int, unsigned); diff --git a/source/dropdown.cpp b/source/dropdown.cpp index 1084612..68c1bee 100644 --- a/source/dropdown.cpp +++ b/source/dropdown.cpp @@ -27,8 +27,6 @@ void Dropdown::init() { input_type = INPUT_NAVIGATION; - dropped = false; - add(list); list.set_visible(false); list.set_view_all(); diff --git a/source/dropdown.h b/source/dropdown.h index 2de7a87..70a9faa 100644 --- a/source/dropdown.h +++ b/source/dropdown.h @@ -27,7 +27,7 @@ public: private: List list; - bool dropped; + bool dropped = false; Text text; public: diff --git a/source/entry.cpp b/source/entry.cpp index 7ee7a32..19bbaf9 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -13,20 +13,7 @@ using namespace std; namespace Msp { namespace GLtk { -Entry::Entry(const string &t): - text(), - multiline(false), - edit_width(10), - edit_height(1), - edit_pos(0), - first_row(0), - visible_rows(1), - text_part(0), - slider(0), - got_key_press(false), - cursor_blink(true), - selection_active(false), - selection_pos(0) +Entry::Entry(const string &t) { input_type = INPUT_TEXT; set_text(t); diff --git a/source/entry.h b/source/entry.h index 60beef7..871024d 100644 --- a/source/entry.h +++ b/source/entry.h @@ -39,18 +39,18 @@ public: private: Text text; - bool multiline; - unsigned edit_width; - unsigned edit_height; - unsigned edit_pos; - unsigned first_row; - unsigned visible_rows; - const Part *text_part; - VSlider *slider; - bool got_key_press; - bool cursor_blink; - bool selection_active; - unsigned selection_pos; + bool multiline = false; + unsigned edit_width = 10; + unsigned edit_height = 1; + unsigned edit_pos = 0; + unsigned first_row = 0; + unsigned visible_rows = 1; + const Part *text_part = 0; + VSlider *slider = 0; + bool got_key_press = false; + bool cursor_blink = true; + bool selection_active = false; + unsigned selection_pos = 0; public: Entry(const std::string & = std::string()); diff --git a/source/geometry.cpp b/source/geometry.cpp index 0cb78ff..ba47c05 100644 --- a/source/geometry.cpp +++ b/source/geometry.cpp @@ -14,13 +14,6 @@ bool Geometry::is_inside_relative(int x_, int y_) const } -Sides::Sides(): - top(0), - right(0), - bottom(0), - left(0) -{ } - Sides::Sides(unsigned s): top(s), right(s), diff --git a/source/geometry.h b/source/geometry.h index 8c8c989..fac3bbc 100644 --- a/source/geometry.h +++ b/source/geometry.h @@ -12,10 +12,12 @@ Specifies the position and size of a widget or graphic. */ struct MSPGLTK_API Geometry { - int x, y; - unsigned w, h; + int x = 0; + int y = 0; + unsigned w = 1; + unsigned h = 1; - Geometry(): x(0), y(0), w(1), h(1) { } + Geometry() = default; Geometry(int x_, int y_, unsigned w_, unsigned h_): x(x_), y(y_), w(w_), h(h_) { } bool is_inside(int, int) const; bool is_inside_relative(int, int) const; @@ -36,12 +38,12 @@ struct MSPGLTK_API Sides void vertical(unsigned); }; - unsigned top; - unsigned right; - unsigned bottom; - unsigned left; + unsigned top = 0; + unsigned right = 0; + unsigned bottom = 0; + unsigned left = 0; - Sides(); + Sides() = default; Sides(unsigned); Sides(unsigned, unsigned); Sides(unsigned, unsigned, unsigned); @@ -54,10 +56,11 @@ Performs alignment of nested geometries, such as widgets and their parts. */ struct MSPGLTK_API Alignment { - float x, y; - float w, h; + float x = 0.0f; + float y = 0.0f; + float w = 1.0f; + float h = 1.0f; - Alignment(): x(0), y(0), w(1), h(1) { } void apply(Geometry &, const Geometry &) const; void apply(Geometry &, const Geometry &, const Sides &) const; }; diff --git a/source/graphic.cpp b/source/graphic.cpp index 75e1088..095e194 100644 --- a/source/graphic.cpp +++ b/source/graphic.cpp @@ -6,11 +6,6 @@ using namespace std; namespace Msp { namespace GLtk { -Graphic::Graphic(): - texture(0), - repeat(false) -{ } - void Graphic::build(unsigned wd, unsigned ht, GL::PrimitiveBuilder &bld) const { vector x, y; diff --git a/source/graphic.h b/source/graphic.h index 4fed368..b114d7e 100644 --- a/source/graphic.h +++ b/source/graphic.h @@ -33,12 +33,11 @@ public: private: Sides border; Sides shadow; - const GL::Texture2D *texture; + const GL::Texture2D *texture = 0; Geometry slice; - bool repeat; + bool repeat = false; public: - Graphic(); const Sides &get_border() const { return border; } const Sides &get_shadow() const { return shadow; } const GL::Texture2D *get_texture() const { return texture; } diff --git a/source/grid.cpp b/source/grid.cpp index 3b44ac8..c8631cf 100644 --- a/source/grid.cpp +++ b/source/grid.cpp @@ -5,9 +5,7 @@ namespace GLtk { Grid::Grid(Layout &l, unsigned c): Arrangement(l), - columns(c), - first_row(true), - column(0) + columns(c) { } void Grid::skip() diff --git a/source/grid.h b/source/grid.h index 29d7b6d..9324f95 100644 --- a/source/grid.h +++ b/source/grid.h @@ -30,8 +30,8 @@ private: std::vector columns; Edge row_top; Edge row_bottom; - bool first_row; - unsigned column; + bool first_row = true; + unsigned column = 0; public: Grid(Layout &, unsigned); diff --git a/source/image.cpp b/source/image.cpp index bc91856..8dbf6cf 100644 --- a/source/image.cpp +++ b/source/image.cpp @@ -11,8 +11,7 @@ namespace Msp { namespace GLtk { Image::Image(const GL::Texture2D *i): - image(i), - keep_aspect(true) + image(i) { } diff --git a/source/image.h b/source/image.h index 7735756..026aaca 100644 --- a/source/image.h +++ b/source/image.h @@ -21,9 +21,9 @@ public: }; private: - const GL::Texture2D *image; + const GL::Texture2D *image = 0; std::string icon_name; - bool keep_aspect; + bool keep_aspect = true; public: Image(const GL::Texture2D * = 0); diff --git a/source/inputmethod.h b/source/inputmethod.h index 1e0f613..28019cd 100644 --- a/source/inputmethod.h +++ b/source/inputmethod.h @@ -43,7 +43,7 @@ protected: InputMethod(Root &); public: - virtual ~InputMethod() { } + virtual ~InputMethod() = default; }; } // namespace GLtk diff --git a/source/layout.cpp b/source/layout.cpp index 200801f..b6248e6 100644 --- a/source/layout.cpp +++ b/source/layout.cpp @@ -37,11 +37,11 @@ private: Column(); }; - unsigned n_columns; - unsigned n_rows; + unsigned n_columns = 1; + unsigned n_rows = 1; std::vector columns; - bool solved; - bool infeasible; + bool solved = false; + bool infeasible = false; public: LinearProgram(unsigned); @@ -85,17 +85,6 @@ Layout::Pointers Layout::pointers[2] = } }; -Layout::Layout(): - container(0), - n_active_slots(0), - margin(8), - row_spacing(5), - col_spacing(4) -{ - n_slack_vars[0] = 0; - n_slack_vars[1] = 0; -} - Layout::~Layout() { for(Slot *s: slots) @@ -481,23 +470,13 @@ void Layout::solve_constraints(int dir, SolveMode mode) Layout::Constraint::Constraint(ConstraintType t, Slot &s): type(t), - target(s), - spacing(-1) -{ } - - -Layout::Packing::Packing(): - gravity(-1), - expand(false) + target(s) { } Layout::Slot::Slot(Layout &l, Widget &w): layout(l), - index(0), - widget(w), - ghost(false), - floating(false) + widget(w) { vert_pack.gravity = 1; widget.signal_autosize_changed.connect(sigc::mem_fun(this, &Slot::autosize_changed)); @@ -647,10 +626,7 @@ void operator>>(const LexicalConverter &conv, Layout::ConstraintType &ctype) Layout::LinearProgram::LinearProgram(unsigned s): n_columns(s), - n_rows(1), - columns(n_columns), - solved(false), - infeasible(false) + columns(n_columns) { } Layout::LinearProgram::Row Layout::LinearProgram::add_row() diff --git a/source/layout.h b/source/layout.h index 7986686..8b1c527 100644 --- a/source/layout.h +++ b/source/layout.h @@ -133,31 +133,29 @@ private: { ConstraintType type; Slot ⌖ - int spacing; + int spacing = -1; Constraint(ConstraintType, Slot &); }; struct Packing { - int gravity; - bool expand; - - Packing(); + int gravity = -1; + bool expand = false; }; struct Slot: public sigc::trackable { Layout &layout; - int index; + int index = 0; Widget &widget; Geometry autosize_geom; Geometry geom; std::list constraints; Packing horiz_pack; Packing vert_pack; - bool ghost; - bool floating; + bool ghost = false; + bool floating = false; Slot(Layout &, Widget &); @@ -174,20 +172,19 @@ private: class LinearProgram; struct Pointers; - Container *container; + Container *container = 0; std::list slots; - unsigned n_active_slots; - unsigned n_slack_vars[2]; - Sides margin; - unsigned row_spacing; - unsigned col_spacing; + unsigned n_active_slots = 0; + unsigned n_slack_vars[2] = { 0, 0 }; + Sides margin{ 8 }; + unsigned row_spacing = 5; + unsigned col_spacing = 4; Geometry autosize_geom; std::list arrangement_stack; static Pointers pointers[2]; public: - Layout(); ~Layout(); void set_container(Container &); diff --git a/source/lineararrangement.cpp b/source/lineararrangement.cpp index 5294252..3ac686e 100644 --- a/source/lineararrangement.cpp +++ b/source/lineararrangement.cpp @@ -5,15 +5,7 @@ namespace GLtk { LinearArrangement::LinearArrangement(Layout &l, Side p): Arrangement(l), - primary(p), - opposite(static_cast((primary+2)%4)), - first(true), - split_here(false), - gravity(opposite), - internal_aligned(false), - uniform(false), - uniform_ref(0), - next_spacing(-1) + primary(p) { } void LinearArrangement::set_uniform(bool u) diff --git a/source/lineararrangement.h b/source/lineararrangement.h index 71ffcfe..917b595 100644 --- a/source/lineararrangement.h +++ b/source/lineararrangement.h @@ -23,16 +23,16 @@ public: }; protected: - Side primary; - Side opposite; + Side primary = RIGHT; + Side opposite = static_cast(primary^2); Edge next; - bool first; - bool split_here; - Side gravity; - bool internal_aligned; - bool uniform; - Widget *uniform_ref; - int next_spacing; + bool first = true; + bool split_here = false; + Side gravity = opposite; + bool internal_aligned = false; + bool uniform = false; + Widget *uniform_ref = 0; + int next_spacing = -1; LinearArrangement(Layout &, Side); diff --git a/source/list.cpp b/source/list.cpp index 5dd6d3e..897ea39 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -21,39 +21,17 @@ incompatible_data::incompatible_data(const type_info &ti): List::List(): - data(new BasicListData), - own_data(true) + List(*new BasicListData) { - init(); + own_data = true; } List::List(ListData &d): data(&d), - own_data(false) -{ - init(); -} - -void List::init() + observer(new DataObserver(*this)) { input_type = INPUT_NAVIGATION; - item_factory = 0; - view_mode = LIST; - sel_index = -1; - focus_index = -1; - first_row = 0; - max_scroll = 0; - view_rows = 5; - view_columns = 5; - items_part = 0; - ignore_slider_change = false; - dragging = false; - drag_start_x = 0; - drag_start_y = 0; - - observer = new DataObserver(*this); - add(slider); slider.set_step(1); slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed)); diff --git a/source/list.h b/source/list.h index 68a96bf..9bc30c6 100644 --- a/source/list.h +++ b/source/list.h @@ -81,7 +81,7 @@ public: class SimpleItem: public Item { protected: - SimpleItem() { } + SimpleItem() = default; virtual void on_style_change(); }; @@ -89,7 +89,7 @@ public: class MultiColumnItem: public Item { protected: - MultiColumnItem() { } + MultiColumnItem() = default; virtual void check_widths(std::vector &) const; virtual void set_widths(const std::vector &); @@ -110,9 +110,9 @@ private: class ItemFactory { protected: - ItemFactory() { } + ItemFactory() = default; public: - virtual ~ItemFactory() { } + virtual ~ItemFactory() = default; virtual void set_data(const ListData &) = 0; virtual Item *create_item(unsigned) const = 0; @@ -157,22 +157,22 @@ public: sigc::signal signal_selection_cleared; private: - ListData *data; - bool own_data; - DataObserver *observer; - ItemFactory *item_factory; - ViewMode view_mode; - int sel_index; - int focus_index; - unsigned first_row; - unsigned max_scroll; - unsigned view_rows; - unsigned view_columns; - const Part *items_part; - bool ignore_slider_change; - bool dragging; - int drag_start_x; - int drag_start_y; + ListData *data = 0; + bool own_data = false; + DataObserver *observer = 0; + ItemFactory *item_factory = 0; + ViewMode view_mode = LIST; + int sel_index = -1; + int focus_index = -1; + unsigned first_row = 0; + unsigned max_scroll = 0; + unsigned view_rows = 5; + unsigned view_columns = 5; + const Part *items_part = 0; + bool ignore_slider_change = false; + bool dragging = false; + int drag_start_x = 0; + int drag_start_y = 0; VSlider slider; std::vector items; @@ -181,9 +181,6 @@ private: public: List(); List(ListData &); -private: - void init(); -public: virtual ~List(); virtual const char *get_class() const { return "list"; } diff --git a/source/listdata.h b/source/listdata.h index 9f7d8ba..00d2b9b 100644 --- a/source/listdata.h +++ b/source/listdata.h @@ -20,9 +20,9 @@ public: sigc::signal signal_refresh_item; protected: - ListData() { } + ListData() = default; public: - virtual ~ListData() { } + virtual ~ListData() = default; virtual unsigned size() const = 0; virtual std::string get_string(unsigned) const = 0; @@ -40,7 +40,7 @@ class ListDataStore: public ListData protected: std::vector items; - ListDataStore() { } + ListDataStore() = default; public: void append(const T &v) { insert(items.size(), v); } @@ -111,7 +111,7 @@ public: typedef std::string Func(const T &); private: - Func *func; + Func *func = 0; public: FunctionListData(Func f): func(f) { } diff --git a/source/panel.cpp b/source/panel.cpp index fa16ec7..871d7a2 100644 --- a/source/panel.cpp +++ b/source/panel.cpp @@ -27,8 +27,7 @@ namespace GLtk { TypeRegistry Panel::widget_registry; bool Panel::widget_registry_init_done = false; -Panel::Panel(): - layout(0) +Panel::Panel() { input_type = INPUT_NAVIGATION; } diff --git a/source/panel.h b/source/panel.h index 22f03a2..4b013a8 100644 --- a/source/panel.h +++ b/source/panel.h @@ -67,7 +67,7 @@ private: protected: std::vector nav_order; - Layout *layout; + Layout *layout = 0; static TypeRegistry widget_registry; static bool widget_registry_init_done; diff --git a/source/part.cpp b/source/part.cpp index 22aa378..21dacc6 100644 --- a/source/part.cpp +++ b/source/part.cpp @@ -11,9 +11,7 @@ namespace GLtk { Part::Part(const string &n): name(n) -{ - fill(graphic, graphic+N_STATES_, static_cast(0)); -} +{ } const Graphic *Part::get_graphic(State state) const { diff --git a/source/part.h b/source/part.h index a8776c2..35b8f7d 100644 --- a/source/part.h +++ b/source/part.h @@ -36,7 +36,7 @@ public: private: std::string name; - const Graphic *graphic[N_STATES_]; + const Graphic *graphic[N_STATES_] = { }; Geometry geom; Sides margin; Alignment align; diff --git a/source/partcache.cpp b/source/partcache.cpp index 3b94457..f4c085e 100644 --- a/source/partcache.cpp +++ b/source/partcache.cpp @@ -6,22 +6,12 @@ using namespace std; namespace Msp { namespace GLtk { -CachedPart::CachedPart(): - part(0), - texture(0), - mesh(0) -{ } - CachedPart::~CachedPart() { delete mesh; } -PartCache::PartCache(): - rebuilding(false) -{ } - void PartCache::begin_rebuild() { if(rebuilding) diff --git a/source/partcache.h b/source/partcache.h index 9dc71bf..af69733 100644 --- a/source/partcache.h +++ b/source/partcache.h @@ -12,11 +12,10 @@ class Part; struct CachedPart { - const Part *part; - const GL::Texture2D *texture; - GL::Mesh *mesh; + const Part *part = 0; + const GL::Texture2D *texture = 0; + GL::Mesh *mesh = 0; - CachedPart(); ~CachedPart(); }; @@ -36,14 +35,12 @@ public: typedef std::list PartList; private: - bool rebuilding; + bool rebuilding = false; PartList parts; PartList::iterator next; PartList::iterator current; public: - PartCache(); - void begin_rebuild(); void insert_special(const Part &); GL::Mesh &create_mesh(const Part &, const GL::Texture2D &); diff --git a/source/progressbar.cpp b/source/progressbar.cpp index 9c7bcea..7c6def7 100644 --- a/source/progressbar.cpp +++ b/source/progressbar.cpp @@ -8,11 +8,6 @@ using namespace std; namespace Msp { namespace GLtk { -ProgressBar::ProgressBar(): - range(1), - fraction(0) -{ } - void ProgressBar::set_range(float r) { if(r<=0) diff --git a/source/progressbar.h b/source/progressbar.h index d167410..6e44e73 100644 --- a/source/progressbar.h +++ b/source/progressbar.h @@ -10,12 +10,10 @@ namespace GLtk { class MSPGLTK_API ProgressBar: public Widget { private: - float range; - float fraction; + float range = 1.0f; + float fraction = 0.0f; public: - ProgressBar(); - virtual const char *get_class() const { return "progressbar"; } void set_range(float); diff --git a/source/resources.cpp b/source/resources.cpp index 06fa44c..3cfd830 100644 --- a/source/resources.cpp +++ b/source/resources.cpp @@ -10,13 +10,18 @@ namespace GLtk { Resources::Resources() { - init(); + add_type().keyword("graphic"); + add_type().creator([this](const string &n){ return create_module(n); }); + add_type().creator([this](const string &n){ return create_sampler(n); }); + add_type().creator([this](const string &n){ return create_program(n); }); + add_type().keyword("texture").creator([this](const string &n){ return create_texture(n); }); + add_type().keyword("font"); + add_type