1// Aseprite Document Library
2// Copyright (c) 2017 David Capello
3//
4// This file is released under the terms of the MIT license.
5// Read LICENSE.txt for more information.
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include "doc/slices.h"
12
13#include "base/debug.h"
14#include "doc/slice.h"
15
16#include <algorithm>
17
18namespace doc {
19
20Slices::Slices(Sprite* sprite)
21 : m_sprite(sprite)
22{
23}
24
25Slices::~Slices()
26{
27 for (Slice* slice : m_slices) {
28 slice->setOwner(nullptr);
29 delete slice;
30 }
31}
32
33void Slices::add(Slice* slice)
34{
35 m_slices.push_back(slice);
36 slice->setOwner(this);
37}
38
39void Slices::remove(Slice* slice)
40{
41 auto it = std::find(m_slices.begin(), m_slices.end(), slice);
42 ASSERT(it != m_slices.end());
43 if (it != m_slices.end())
44 m_slices.erase(it);
45
46 slice->setOwner(nullptr);
47}
48
49Slice* Slices::getByName(const std::string& name) const
50{
51 for (Slice* slice : *this) {
52 if (slice->name() == name)
53 return slice;
54 }
55 return nullptr;
56}
57
58Slice* Slices::getById(ObjectId id) const
59{
60 for (Slice* slice : *this) {
61 if (slice->id() == id)
62 return slice;
63 }
64 return nullptr;
65}
66
67} // namespace doc
68