diff --git a/src/app/file/file_data.cpp b/src/app/file/file_data.cpp index c46e1848e..c07d4be25 100644 --- a/src/app/file/file_data.cpp +++ b/src/app/file/file_data.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2022 Igara Studio S.A. // Copyright (C) 2001-2017 David Capello // // This program is distributed under the terms of @@ -86,7 +86,7 @@ doc::color_t color_from_hex(const char* str) template -void update_xml_collection(Container& container, +void update_xml_collection(const Container& container, TiXmlElement* xmlParent, const char* childElemName, const char* idAttrName, @@ -111,7 +111,7 @@ void update_xml_collection(Container& container, continue; bool found = false; - for (auto child : container) { + for (const auto& child : container) { std::string thisChildName = childNameGetter(child); if (thisChildName == xmlChildName) { existent.insert(thisChildName); @@ -128,7 +128,7 @@ void update_xml_collection(Container& container, } // Add new children - for (auto child : container) { + for (const auto& child : container) { std::string thisChildName = childNameGetter(child); if (existent.find(thisChildName) == existent.end()) { TiXmlElement xmlChild(childElemName); diff --git a/src/doc/keyframes.h b/src/doc/keyframes.h index cb0c980db..2975dd10a 100644 --- a/src/doc/keyframes.h +++ b/src/doc/keyframes.h @@ -1,4 +1,5 @@ // Aseprite Document Library +// Copyright (c) 2022 Igara Studio S.A. // Copyright (c) 2017 David Capello // // This file is released under the terms of the MIT license. @@ -10,6 +11,7 @@ #include "doc/frame.h" +#include #include namespace doc { @@ -19,14 +21,22 @@ namespace doc { public: class Key { public: - Key(const frame_t frame, T* value) : m_frame(frame), m_value(value) { } + Key(const frame_t frame, + std::unique_ptr&& value) + : m_frame(frame) + , m_value(std::move(value)) { } + Key(const Key& o) + : m_frame(o.m_frame) + , m_value(o.m_value ? new T(*o.m_value): nullptr) { } + Key(Key&& o) = default; + Key& operator=(Key&& o) = default; frame_t frame() const { return m_frame; } - T* value() const { return m_value; } + T* value() const { return m_value.get(); } void setFrame(const frame_t frame) { m_frame = frame; } - void setValue(T* value) { m_value = value; } + void setValue(std::unique_ptr&& value) { m_value = std::move(value); } private: - frame_t m_frame; - T* m_value; + frame_t m_frame = 0; + std::unique_ptr m_value = nullptr; }; typedef std::vector List; @@ -112,30 +122,25 @@ namespace doc { Keyframes(const Keyframes& other) { for (const auto& key : other.m_keys) - m_keys.push_back(Key(key.frame(), new T(*key.value()))); + m_keys.push_back(Key(key.frame(), std::make_unique(*key.value()))); } - void insert(const frame_t frame, T* value) { + void insert(const frame_t frame, std::unique_ptr&& value) { auto it = getIterator(frame); if (it == end()) - m_keys.push_back(Key(frame, value)); + m_keys.push_back(Key(frame, std::move(value))); else if (it->frame() == frame) - it->setValue(value); + it->setValue(std::move(value)); else { ++it; - m_keys.insert(it, Key(frame, value)); + m_keys.insert(it, Key(frame, std::move(value))); } } - T* remove(const frame_t frame) { + void remove(const frame_t frame) { auto it = getIterator(frame); - if (it != end()) { - T* value = it->value(); + if (it != end()) m_keys.erase(it); - return value; - } - else - return nullptr; } T* operator[](const frame_t frame) { diff --git a/src/doc/keyframes_tests.cpp b/src/doc/keyframes_tests.cpp index 7cecd387c..9282d9053 100644 --- a/src/doc/keyframes_tests.cpp +++ b/src/doc/keyframes_tests.cpp @@ -1,4 +1,5 @@ // Aseprite Document Library +// Copyright (c) 2022 Igara Studio S.A. // Copyright (c) 2017 David Capello // // This file is released under the terms of the MIT license. @@ -21,7 +22,7 @@ TEST(Keyframes, Operations) EXPECT_TRUE(k.empty()); EXPECT_EQ(0, k.size()); - k.insert(0, new int(5)); + k.insert(0, std::make_unique(5)); EXPECT_FALSE(k.empty()); EXPECT_EQ(1, k.size()); EXPECT_EQ(0, k.fromFrame()); @@ -31,7 +32,7 @@ TEST(Keyframes, Operations) EXPECT_EQ(5, *k[1]); EXPECT_EQ(5, *k[2]); - k.insert(2, new int(6)); + k.insert(2, std::make_unique(6)); EXPECT_EQ(2, k.size()); EXPECT_EQ(0, k.fromFrame()); EXPECT_EQ(2, k.toFrame()); @@ -41,7 +42,7 @@ TEST(Keyframes, Operations) EXPECT_EQ(6, *k[2]); EXPECT_EQ(6, *k[3]); - k.insert(1, new int(3)); + k.insert(1, std::make_unique(3)); EXPECT_EQ(3, k.size()); EXPECT_EQ(0, k.fromFrame()); EXPECT_EQ(2, k.toFrame()); @@ -72,10 +73,10 @@ TEST(Keyframes, Operations) TEST(Keyframes, Range) { Keyframes k; - k.insert(0, new int(5)); + k.insert(0, std::make_unique(5)); k.insert(2, nullptr); - k.insert(4, new int(8)); - k.insert(6, new int(2)); + k.insert(4, std::make_unique(8)); + k.insert(6, std::make_unique(2)); EXPECT_EQ(0, k.fromFrame()); EXPECT_EQ(6, k.toFrame()); @@ -131,7 +132,7 @@ TEST(Keyframes, Range) TEST(Keyframes, BugEmptyCount) { Keyframes k; - k.insert(7, new int(5)); + k.insert(7, std::make_unique(5)); EXPECT_EQ(0, k.range(-1, 6).countKeys()); EXPECT_EQ(1, k.range(0, 7).countKeys()); EXPECT_EQ(1, k.range(8, 9).countKeys()); diff --git a/src/doc/slice.cpp b/src/doc/slice.cpp index e50275e5e..d416589cc 100644 --- a/src/doc/slice.cpp +++ b/src/doc/slice.cpp @@ -1,4 +1,5 @@ // Aseprite Document Library +// Copyright (c) 2022 Igara Studio S.A. // Copyright (c) 2017 David Capello // // This file is released under the terms of the MIT license. @@ -61,12 +62,12 @@ int Slice::getMemSize() const void Slice::insert(const frame_t frame, const SliceKey& key) { - m_keys.insert(frame, new SliceKey(key)); + m_keys.insert(frame, std::make_unique(key)); } void Slice::remove(const frame_t frame) { - delete m_keys.remove(frame); + m_keys.remove(frame); } const SliceKey* Slice::getByFrame(const frame_t frame) const