Cutelee 6.1.0
blockcontext.cpp
1/*
2 This file is part of the Cutelee template system.
3
4 Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either version
9 2.1 of the Licence, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library. If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21#include "blockcontext.h"
22
23#include "block.h"
24
25void BlockContext::addBlocks(const QHash<QString, BlockNode *> &blocks)
26{
27 auto it = blocks.constBegin();
28 const auto end = blocks.constEnd();
29
30 for (; it != end; ++it) {
31 m_blocks[it.key()].prepend(it.value());
32 }
33}
34
35BlockNode *BlockContext::getBlock(const QString &name) const
36{
37 auto list = m_blocks[name];
38 if (list.isEmpty())
39 return 0;
40
41 return list.last();
42}
43
44BlockNode *BlockContext::pop(const QString &name)
45{
46 QList<BlockNode *> &list = m_blocks[name];
47 if (list.isEmpty())
48 return 0;
49 return list.takeLast();
50}
51
52void BlockContext::push(const QString &name, BlockNode const *blockNode)
53{
54 m_blocks[name].append(const_cast<BlockNode *>(blockNode));
55}
56
57bool BlockContext::isEmpty() { return m_blocks.isEmpty(); }
58
59void BlockContext::remove(QList<BlockNode *> const &nodes)
60{
61 for (auto node : nodes) {
62 m_blocks[node->name()].removeOne(node);
63 if (m_blocks[node->name()].isEmpty()) {
64 m_blocks.remove(node->name());
65 }
66 }
67}