phase.cc revision 13524:7f0950094a4f
1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20#include <cstring>
21#include <map>
22#include <string>
23#include <systemc>
24#include <tlm>
25#include <typeindex>
26
27namespace tlm
28{
29
30namespace
31{
32
33struct tlm_phase_registry
34{
35    typedef unsigned int key_type;
36
37    static tlm_phase_registry &
38    instance()
39    {
40        static tlm_phase_registry inst;
41        return inst;
42    }
43
44    unsigned int
45    register_phase(std::type_index type, std::string name)
46    {
47        type_map::const_iterator it = ids_.find(type);
48
49        if (name.empty()) {
50            SC_REPORT_FATAL( sc_core::SC_ID_INTERNAL_ERROR_,
51                    "unexpected empty tlm_phase name" );
52            return UNINITIALIZED_PHASE;
53        }
54
55        if (it == ids_.end()) {
56            // new phase - generate/store ID and name
57            type_map::value_type v(type, static_cast<key_type>(names_.size()));
58            names_.push_back(name_table::value_type(name.data(), name.size()));
59            ids_.insert(v);
60            return v.second;
61        }
62
63        if (names_[it->second] != name) {
64            SC_REPORT_FATAL(sc_core::SC_ID_INTERNAL_ERROR_,
65                    "tlm_phase registration failed: duplicate type info" );
66            sc_core::sc_abort();
67        }
68        return it->second;
69    }
70
71    const char *
72    get_name(key_type id) const
73    {
74        sc_assert(id < names_.size());
75        return names_[id].c_str();
76    }
77
78  private:
79    typedef std::map<std::type_index, key_type> type_map;
80    typedef std::vector<std::string> name_table;
81
82    type_map ids_;
83    name_table names_;
84
85    tlm_phase_registry() : names_(END_RESP + 1)
86    {
87        names_[UNINITIALIZED_PHASE] = "UNINITIALIZED_PHASE";
88        names_[BEGIN_REQ] = "BEGIN_REQ";
89        names_[END_REQ] = "END_REQ";
90        names_[BEGIN_RESP] = "BEGIN_RESP";
91        names_[END_RESP] = "END_RESP";
92    }
93};
94
95} // anonymous namespace
96
97tlm_phase::tlm_phase(unsigned int id) : m_id(id)
98{}
99
100tlm_phase::tlm_phase(const std::type_info &type, const char *name) :
101    m_id(tlm_phase_registry::instance().register_phase(type, name))
102{}
103
104const char *
105tlm_phase::get_name() const
106{
107    return tlm_phase_registry::instance().get_name(m_id);
108}
109
110} // namespace tlm
111