optimize: code optimization

This commit is contained in:
Haibo 2018-10-29 21:10:03 +08:00 committed by Nomango
parent 32251365d6
commit 0f1ba104dd
9 changed files with 103 additions and 81 deletions

View File

@ -385,8 +385,8 @@ namespace easy2d
bool operator< (const Duration &) const;
bool operator<= (const Duration &) const;
Duration operator + (Duration const &) const;
Duration operator - (Duration const &) const;
Duration operator + (const Duration &) const;
Duration operator - (const Duration &) const;
Duration operator - () const;
Duration operator * (int) const;
Duration operator * (float) const;
@ -395,8 +395,8 @@ namespace easy2d
Duration operator / (float) const;
Duration operator / (double) const;
Duration& operator += (Duration const &);
Duration& operator -= (Duration const &);
Duration& operator += (const Duration &);
Duration& operator -= (const Duration &);
Duration& operator *= (int);
Duration& operator *= (float);
Duration& operator *= (double);
@ -432,19 +432,34 @@ namespace easy2d
public:
Time();
explicit Time(
std::chrono::steady_clock::time_point
);
Time(
const Time& other
);
Time(
Time&& other
);
// 获取时间戳
time_t GetTimeStamp() const;
// 是否是零时
bool IsZero() const;
Time operator + (Duration const &) const;
Time operator - (Duration const &) const;
Time operator + (const Duration &) const;
Time operator - (const Duration &) const;
Time& operator += (Duration const &);
Time& operator -= (Duration const &);
Time& operator += (const Duration &);
Time& operator -= (const Duration &);
Duration operator - (Time const &) const;
Duration operator - (const Time &) const;
Time& operator = (const Time &) E2D_NOEXCEPT;
Time& operator = (Time &&) E2D_NOEXCEPT;
// 获取当前时间
static Time Now();
@ -641,7 +656,6 @@ namespace easy2d
{
if (FAILED(hr))
{
// 在此处设置断点以捕获系统异常.
static char s_str[64] = {};
sprintf_s(s_str, "Failure with HRESULT of %08X", static_cast<unsigned int>(hr));
throw std::runtime_error(s_str);

View File

@ -388,13 +388,12 @@ easy2d::Rect easy2d::Game::Locate(int width, int height)
width = std::min(width, max_width);
height = std::min(height, max_height);
Rect client_rect(
return Rect(
static_cast<float>((max_width - width) / 2),
static_cast<float>((max_height - height) / 2),
static_cast<float>(width),
static_cast<float>(height)
);
return std::move(client_rect);
}
int easy2d::Game::GetWidth() const
@ -409,11 +408,10 @@ int easy2d::Game::GetHeight() const
easy2d::Size easy2d::Game::GetSize() const
{
easy2d::Size size(
return easy2d::Size(
static_cast<float>(width_),
static_cast<float>(height_)
);
return std::move(size);
}
HWND easy2d::Game::GetHWnd() const

View File

@ -147,14 +147,12 @@ float easy2d::Image::GetSourceHeight() const
easy2d::Size easy2d::Image::GetSourceSize() const
{
Size source_size;
if (bitmap_)
{
auto bitmap_size = bitmap_->GetSize();
source_size.width = bitmap_size.width;
source_size.height = bitmap_size.height;
return Size{ bitmap_size.width, bitmap_size.height };
}
return std::move(source_size);
return Size{};
}
float easy2d::Image::GetCropX() const

View File

@ -379,7 +379,7 @@ float easy2d::Node::GetPivotY() const
easy2d::Size easy2d::Node::GetSize() const
{
return std::move(Size(GetWidth(), GetHeight()));
return Size{ GetWidth(), GetHeight() };
}
float easy2d::Node::GetScaleX() const
@ -667,7 +667,7 @@ easy2d::Node::Nodes easy2d::Node::GetChildren(const std::wstring& name) const
children.push_back(child);
}
}
return std::move(children);
return children;
}
easy2d::Node * easy2d::Node::GetChild(const std::wstring& name) const

View File

@ -94,7 +94,7 @@ std::wstring easy2d::File::GetExtension() const
// 转换为小写字母
std::transform(file_ext.begin(), file_ext.end(), file_ext.begin(), std::towlower);
}
return std::move(file_ext);
return file_ext;
}
bool easy2d::File::Delete()
@ -118,7 +118,7 @@ easy2d::File easy2d::File::Extract(Resource& res, const std::wstring& dest_file_
);
if (file_handle == INVALID_HANDLE_VALUE)
return std::move(file);
return file;
if (res.Load())
{
@ -135,7 +135,7 @@ easy2d::File easy2d::File::Extract(Resource& res, const std::wstring& dest_file_
::DeleteFile(dest_file_name.c_str());
}
return std::move(file);
return file;
}
void easy2d::File::AddSearchPath(const std::wstring & path)
@ -218,7 +218,7 @@ easy2d::File easy2d::File::ShowOpenDialog(const std::wstring & title, const std:
}
::CoUninitialize();
}
return std::move(File(file_path));
return File(file_path);
}
easy2d::File easy2d::File::ShowSaveDialog(const std::wstring & title, const std::wstring& def_file, const std::wstring & def_ext)
@ -288,5 +288,5 @@ easy2d::File easy2d::File::ShowSaveDialog(const std::wstring & title, const std:
}
::CoUninitialize();
}
return std::move(File(file_path));
return File(file_path);
}

View File

@ -78,6 +78,5 @@ easy2d::Color::Color(const D2D1_COLOR_F& color)
easy2d::Color::operator D2D1_COLOR_F() const
{
D2D1::ColorF color_f(r, g, b, a);
return std::move(color_f);
return D2D1::ColorF(r, g, b, a);
}

View File

@ -77,10 +77,10 @@ easy2d::Duration easy2d::Duration::Parse(const std::wstring & str)
if (!std::regex_match(str, regex))
{
E2D_WARNING("Duration::Parse: invalid duration");
return std::move(Duration{});
return d;
}
if (str.empty() || str == L"0") { return std::move(Duration{}); }
if (str.empty() || str == L"0") { return d; }
// ·ûºÅλ
if (str[0] == L'-' || str[0] == L'+')
@ -108,7 +108,7 @@ easy2d::Duration easy2d::Duration::Parse(const std::wstring & str)
if (num_str.empty() || num_str == L".")
{
E2D_WARNING("Duration::Parse: invalid duration");
return std::move(Duration{});
return Duration();
}
// µ¥Î»
@ -127,7 +127,7 @@ easy2d::Duration easy2d::Duration::Parse(const std::wstring & str)
if (unit_map.find(unit_str) == unit_map.end())
{
E2D_WARNING("Duration::Parse: invalid duration");
return std::move(Duration{});
return Duration();
}
double num = std::stod(num_str);
@ -139,7 +139,7 @@ easy2d::Duration easy2d::Duration::Parse(const std::wstring & str)
{
d.milliseconds_ = -d.milliseconds_;
}
return std::move(d);
return d;
}
bool easy2d::Duration::operator==(const Duration & other) const
@ -172,71 +172,58 @@ bool easy2d::Duration::operator<=(const Duration & other) const
return milliseconds_ <= other.milliseconds_;
}
easy2d::Duration easy2d::Duration::operator+(Duration const & other) const
easy2d::Duration easy2d::Duration::operator+(const Duration & other) const
{
Duration d(milliseconds_ + other.milliseconds_);
return std::move(d);
return Duration(milliseconds_ + other.milliseconds_);
}
easy2d::Duration easy2d::Duration::operator-(Duration const & other) const
easy2d::Duration easy2d::Duration::operator-(const Duration & other) const
{
Duration d(milliseconds_ - other.milliseconds_);
return std::move(d);
return Duration(milliseconds_ - other.milliseconds_);
}
easy2d::Duration easy2d::Duration::operator-() const
{
Duration d(-milliseconds_);
return std::move(d);
return Duration(-milliseconds_);
}
easy2d::Duration easy2d::Duration::operator*(int value) const
{
Duration d(milliseconds_ * value);
return std::move(d);
return Duration(milliseconds_ * value);
}
easy2d::Duration easy2d::Duration::operator/(int value) const
{
Duration d(milliseconds_ / value);
return std::move(d);
return Duration(milliseconds_ / value);
}
easy2d::Duration easy2d::Duration::operator*(float value) const
{
int ms = static_cast<int>(milliseconds_ * value);
Duration d(ms);
return std::move(d);
return Duration(static_cast<int>(milliseconds_ * value));
}
easy2d::Duration easy2d::Duration::operator/(float value) const
{
int ms = static_cast<int>(milliseconds_ / value);
Duration d(ms);
return std::move(d);
return Duration(static_cast<int>(milliseconds_ / value));
}
easy2d::Duration easy2d::Duration::operator*(double value) const
{
int ms = static_cast<int>(milliseconds_ * value);
Duration d(ms);
return std::move(d);
return Duration(static_cast<int>(milliseconds_ * value));
}
easy2d::Duration easy2d::Duration::operator/(double value) const
{
int ms = static_cast<int>(milliseconds_ / value);
Duration d(ms);
return std::move(d);
return Duration(static_cast<int>(milliseconds_ / value));
}
easy2d::Duration & easy2d::Duration::operator+=(Duration const &other)
easy2d::Duration & easy2d::Duration::operator+=(const Duration &other)
{
milliseconds_ += other.milliseconds_;
return (*this);
}
easy2d::Duration & easy2d::Duration::operator-=(Duration const &other)
easy2d::Duration & easy2d::Duration::operator-=(const Duration &other)
{
milliseconds_ -= other.milliseconds_;
return (*this);
@ -280,30 +267,30 @@ easy2d::Duration & easy2d::Duration::operator/=(double value)
easy2d::Duration easy2d::operator*(int value, const Duration & dur)
{
return std::move(dur * value);
return dur * value;
}
easy2d::Duration easy2d::operator/(int value, const Duration & dur)
{
return std::move(dur / value);
return dur / value;
}
easy2d::Duration easy2d::operator*(float value, const Duration & dur)
{
return std::move(dur * value);
return dur * value;
}
easy2d::Duration easy2d::operator/(float value, const Duration & dur)
{
return std::move(dur / value);
return dur / value;
}
easy2d::Duration easy2d::operator*(double value, const Duration & dur)
{
return std::move(dur * value);
return dur * value;
}
easy2d::Duration easy2d::operator/(double value, const Duration & dur)
{
return std::move(dur / value);
return dur / value;
}

View File

@ -27,6 +27,21 @@ easy2d::Time::Time()
{
}
easy2d::Time::Time(std::chrono::steady_clock::time_point time)
: time_(time)
{
}
easy2d::Time::Time(const Time & other)
: time_(other.time_)
{
}
easy2d::Time::Time(Time && other)
: time_(std::move(other.time_))
{
}
time_t easy2d::Time::GetTimeStamp() const
{
auto& duration = time_point_cast<milliseconds>(time_).time_since_epoch();
@ -38,42 +53,53 @@ bool easy2d::Time::IsZero() const
return time_.time_since_epoch().count() == 0LL;
}
easy2d::Time easy2d::Time::operator+(Duration const & other) const
easy2d::Time easy2d::Time::operator+(const Duration & other) const
{
Time t;
t.time_ = time_ + milliseconds(other.Milliseconds());
return std::move(t);
return Time(time_ + milliseconds(other.Milliseconds()));
}
easy2d::Time easy2d::Time::operator-(Duration const & other) const
easy2d::Time easy2d::Time::operator-(const Duration & other) const
{
Time t;
t.time_ = time_ - milliseconds(other.Milliseconds());
return std::move(t);
return Time(time_ - milliseconds(other.Milliseconds()));
}
easy2d::Time & easy2d::Time::operator+=(Duration const & other)
easy2d::Time & easy2d::Time::operator+=(const Duration & other)
{
time_ += milliseconds(other.Milliseconds());
return (*this);
}
easy2d::Time & easy2d::Time::operator-=(Duration const &other)
easy2d::Time & easy2d::Time::operator-=(const Duration &other)
{
time_ -= milliseconds(other.Milliseconds());
return (*this);
}
easy2d::Duration easy2d::Time::operator-(Time const & other) const
easy2d::Duration easy2d::Time::operator-(const Time & other) const
{
auto ms = duration_cast<milliseconds>(time_ - other.time_).count();
Duration d(static_cast<int>(ms));
return std::move(d);
return Duration(static_cast<int>(ms));
}
easy2d::Time& easy2d::Time::operator=(const Time & other) E2D_NOEXCEPT
{
if (this == &other)
return *this;
time_ = other.time_;
return *this;
}
easy2d::Time& easy2d::Time::operator=(Time && other) E2D_NOEXCEPT
{
if (this == &other)
return *this;
time_ = std::move(other.time_);
return *this;
}
easy2d::Time easy2d::Time::Now()
{
Time t;
t.time_ = steady_clock::now();
return std::move(t);
return Time(steady_clock::now());
}

View File

@ -52,7 +52,7 @@ easy2d::Transform::operator D2D1::Matrix3x2F() const
position.x - pivot.x,
position.y - pivot.y
);
return std::move(matrix);
return matrix;
}
bool easy2d::Transform::operator==(const Transform & other) const