diff options
-rw-r--r-- | src/base.hpp | 1 | ||||
-rw-r--r-- | src/ui/content_control.cpp | 4 | ||||
-rw-r--r-- | src/ui/control.cpp | 3 | ||||
-rw-r--r-- | src/ui/layout_control.cpp | 21 | ||||
-rw-r--r-- | src/ui/render/render_object.cpp | 21 | ||||
-rw-r--r-- | src/ui/render/text_render_object.cpp | 2 | ||||
-rw-r--r-- | src/ui/render/window_render_object.cpp | 2 | ||||
-rw-r--r-- | src/ui/window.cpp | 25 | ||||
-rw-r--r-- | src/ui/window_class.cpp | 5 |
9 files changed, 27 insertions, 57 deletions
diff --git a/src/base.hpp b/src/base.hpp index 2c511c4b..e3dfc1ee 100644 --- a/src/base.hpp +++ b/src/base.hpp @@ -1,6 +1,7 @@ #pragma once #include "pre.hpp" +#include <cassert> #include <chrono> #include <stdexcept> #include <string> diff --git a/src/ui/content_control.cpp b/src/ui/content_control.cpp index 960867b2..d5abca1c 100644 --- a/src/ui/content_control.cpp +++ b/src/ui/content_control.cpp @@ -9,9 +9,7 @@ ContentControl::ContentControl() ContentControl::~ContentControl() { delete child_; } void ContentControl::SetChild(Control* child) { - if (dynamic_cast<Window*>(child)) - throw std::invalid_argument("Can't add a window as child."); - + assert(!dynamic_cast<Window*>(child)); // Can't add a window as child. if (child == child_) return; const auto window = GetWindow(); diff --git a/src/ui/control.cpp b/src/ui/control.cpp index e19754dc..5c629fd6 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -1,11 +1,8 @@ #include "control.hpp" -#include <cassert> - #include "window.hpp" namespace cru::ui { - void Control::_SetParent(Control* parent) { const auto old_parent = GetParent(); parent_ = parent; diff --git a/src/ui/layout_control.cpp b/src/ui/layout_control.cpp index d2b430dd..c0c4a7fe 100644 --- a/src/ui/layout_control.cpp +++ b/src/ui/layout_control.cpp @@ -3,24 +3,14 @@ #include "window.hpp" namespace cru::ui { -void ControlAddChildCheck(Control* control) { - if (control->GetParent() != nullptr) - throw std::invalid_argument("The control already has a parent."); - - if (dynamic_cast<Window*>(control)) - throw std::invalid_argument("Can't add a window as child."); -} - LayoutControl::~LayoutControl() { for (const auto child : children_) delete child; } void LayoutControl::AddChild(Control* control, const int position) { - ControlAddChildCheck(control); - - if (position < 0 || static_cast<decltype(children_.size())>(position) > - this->children_.size()) - throw std::invalid_argument("The position is out of range."); + assert(control->GetParent() == nullptr); // The control already has a parent. + assert(!dynamic_cast<Window*>(control)); // Can't add a window as child. + assert(position >= 0 || position <= this->children_.size()); // The position is out of range. children_.insert(this->children_.cbegin() + position, control); @@ -31,9 +21,8 @@ void LayoutControl::AddChild(Control* control, const int position) { } void LayoutControl::RemoveChild(const int position) { - if (position < 0 || static_cast<decltype(this->children_.size())>(position) >= - this->children_.size()) - throw std::invalid_argument("The position is out of range."); + assert(position >= 0 && + position < this->children_.size()); // The position is out of range. const auto i = children_.cbegin() + position; const auto child = *i; diff --git a/src/ui/render/render_object.cpp b/src/ui/render/render_object.cpp index 0be44c1e..6380c2fe 100644 --- a/src/ui/render/render_object.cpp +++ b/src/ui/render/render_object.cpp @@ -4,15 +4,10 @@ namespace cru::ui::render { void RenderObject::AddChild(RenderObject* render_object, const int position) { - if (render_object->GetParent() != nullptr) - throw std::invalid_argument("Render object already has a parent."); - - if (position < 0) - throw std::invalid_argument("Position index is less than 0."); - - if (static_cast<std::vector<RenderObject*>::size_type>(position) > - children_.size()) - throw std::invalid_argument("Position index is out of bound."); + assert(render_object->GetParent() == + nullptr); // Render object already has a parent. + assert(position >= 0); // Position index is less than 0. + assert(position <= children_.size()); // Position index is out of bound. children_.insert(children_.cbegin() + position, render_object); render_object->SetParent(this); @@ -20,12 +15,8 @@ void RenderObject::AddChild(RenderObject* render_object, const int position) { } void RenderObject::RemoveChild(const int position) { - if (position < 0) - throw std::invalid_argument("Position index is less than 0."); - - if (static_cast<std::vector<RenderObject*>::size_type>(position) >= - children_.size()) - throw std::invalid_argument("Position index is out of bound."); + assert(position >= 0); // Position index is less than 0. + assert(position < children_.size()); // Position index is out of bound. const auto i = children_.cbegin() + position; const auto removed_child = *i; diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp index e8032f78..b90dae71 100644 --- a/src/ui/render/text_render_object.cpp +++ b/src/ui/render/text_render_object.cpp @@ -1,7 +1,5 @@ #include "text_render_object.hpp" -#include <cassert> - #include "exception.hpp" #include "graph/graph.hpp" diff --git a/src/ui/render/window_render_object.cpp b/src/ui/render/window_render_object.cpp index 52452bd4..f198c2fa 100644 --- a/src/ui/render/window_render_object.cpp +++ b/src/ui/render/window_render_object.cpp @@ -1,7 +1,5 @@ #include "window_render_object.hpp" -#include <cassert> - #include "graph/graph.hpp" #include "ui/window.hpp" diff --git a/src/ui/window.cpp b/src/ui/window.cpp index b976ca6a..ca3356ff 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -117,19 +117,14 @@ WindowManager::WindowManager() { } void WindowManager::RegisterWindow(HWND hwnd, Window* window) { - const auto find_result = window_map_.find(hwnd); - if (find_result != window_map_.end()) - throw std::runtime_error("The hwnd is already in the map."); - + assert(window_map_.count(hwnd) == 0); // The hwnd is already in the map. window_map_.emplace(hwnd, window); } void WindowManager::UnregisterWindow(HWND hwnd) { const auto find_result = window_map_.find(hwnd); - if (find_result == window_map_.end()) - throw std::runtime_error("The hwnd is not in the map."); + assert(find_result != window_map_.end()); // The hwnd is not in the map. window_map_.erase(find_result); - if (window_map_.empty()) Application::GetInstance()->Quit(0); } @@ -188,14 +183,15 @@ Window::Window(tag_overlapped_constructor) { CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, Application::GetInstance()->GetInstanceHandle(), nullptr); - if (hwnd_ == nullptr) throw std::runtime_error("Failed to create window."); + if (hwnd_ == nullptr) + throw Win32Error(::GetLastError(), "Failed to create window."); AfterCreateHwnd(window_manager); } Window::Window(tag_popup_constructor, Window* parent, const bool caption) { - if (parent != nullptr && !parent->IsWindowValid()) - throw std::runtime_error("Parent window is not valid."); + assert(parent == nullptr || + parent->IsWindowValid()); // Parent window is not valid. BeforeCreateHwnd(); @@ -210,7 +206,8 @@ Window::Window(tag_popup_constructor, Window* parent, const bool caption) { parent == nullptr ? nullptr : parent->GetWindowHandle(), nullptr, Application::GetInstance()->GetInstanceHandle(), nullptr); - if (hwnd_ == nullptr) throw std::runtime_error("Failed to create window."); + if (hwnd_ == nullptr) + throw Win32Error(::GetLastError(), "Failed to create window."); AfterCreateHwnd(window_manager); } @@ -474,10 +471,8 @@ Point Window::GetMousePosition() { } bool Window::RequestFocusFor(Control* control) { - if (control == nullptr) - throw std::invalid_argument( - "The control to request focus can't be null. You can set it as the " - "window."); + assert(control != nullptr); // The control to request focus can't be null. + // You can set it as the window. if (!IsWindowValid()) return false; diff --git a/src/ui/window_class.cpp b/src/ui/window_class.cpp index 456d9492..5e8b3454 100644 --- a/src/ui/window_class.cpp +++ b/src/ui/window_class.cpp @@ -1,5 +1,7 @@ #include "window_class.hpp" +#include "exception.hpp" + namespace cru::ui { WindowClass::WindowClass(const String& name, WNDPROC window_proc, HINSTANCE h_instance) @@ -20,6 +22,7 @@ WindowClass::WindowClass(const String& name, WNDPROC window_proc, window_class.hIconSm = NULL; atom_ = ::RegisterClassExW(&window_class); - if (atom_ == 0) throw std::runtime_error("Failed to create window class."); + if (atom_ == 0) + throw Win32Error(::GetLastError(), "Failed to create window class."); } } // namespace cru::ui |