1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#include <utility>
31
32#include "systemc/ext/core/sc_attr.hh"
33
34namespace sc_core
35{
36
37sc_attr_base::sc_attr_base(const std::string &_name) : _name(_name) {}
38sc_attr_base::sc_attr_base(const sc_attr_base &other) : _name(other._name) {}
39sc_attr_base::~sc_attr_base() {}
40
41const std::string &sc_attr_base::name() const { return _name; }
42
43sc_attr_cltn::iterator
44sc_attr_cltn::begin()
45{
46    return cltn.begin();
47}
48
49sc_attr_cltn::const_iterator
50sc_attr_cltn::begin() const
51{
52    return cltn.begin();
53}
54
55sc_attr_cltn::iterator
56sc_attr_cltn::end()
57{
58    return cltn.end();
59}
60
61sc_attr_cltn::const_iterator
62sc_attr_cltn::end() const
63{
64    return cltn.end();
65}
66
67sc_attr_cltn::sc_attr_cltn() {}
68sc_attr_cltn::sc_attr_cltn(const sc_attr_cltn &other) : cltn(other.cltn) {}
69sc_attr_cltn::~sc_attr_cltn() {}
70
71bool
72sc_attr_cltn::push_back(sc_attr_base *attr)
73{
74    if (!attr || (*this)[attr->name()])
75        return false;
76
77    cltn.push_back(attr);
78    return true;
79}
80
81sc_attr_base *
82sc_attr_cltn::operator [] (const std::string &name)
83{
84    for (auto &attr: cltn)
85        if (attr->name() == name)
86            return attr;
87    return nullptr;
88}
89
90const sc_attr_base *
91sc_attr_cltn::operator [] (const std::string &name) const
92{
93    for (auto &attr: cltn)
94        if (attr->name() == name)
95            return attr;
96    return nullptr;
97}
98
99sc_attr_base *
100sc_attr_cltn::remove(const std::string &name)
101{
102    for (auto &attr: cltn) {
103        if (attr->name() == name) {
104            sc_attr_base *ret = attr;
105            std::swap(attr, cltn.back());
106            cltn.pop_back();
107            return ret;
108        }
109    }
110    return nullptr;
111}
112
113void sc_attr_cltn::remove_all() { cltn.clear(); }
114
115int sc_attr_cltn::size() const { return cltn.size(); }
116
117} // namespace sc_core
118