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 <typeindex>
24
25#include "systemc/ext/tlm_core/2/generic_payload/phase.hh"
26#include "systemc/ext/utils/sc_report_handler.hh"
27
28namespace tlm
29{
30
31namespace
32{
33
34struct tlm_phase_registry
35{
36    typedef unsigned int key_type;
37
38    static tlm_phase_registry &
39    instance()
40    {
41        static tlm_phase_registry inst;
42        return inst;
43    }
44
45    unsigned int
46    register_phase(std::type_index type, std::string name)
47    {
48        type_map::const_iterator it = ids_.find(type);
49
50        if (name.empty()) {
51            SC_REPORT_FATAL( sc_core::SC_ID_INTERNAL_ERROR_,
52                    "unexpected empty tlm_phase name" );
53            return UNINITIALIZED_PHASE;
54        }
55
56        if (it == ids_.end()) {
57            // new phase - generate/store ID and name
58            type_map::value_type v(type, static_cast<key_type>(names_.size()));
59            names_.push_back(name_table::value_type(name.data(), name.size()));
60            ids_.insert(v);
61            return v.second;
62        }
63
64        if (names_[it->second] != name) {
65            SC_REPORT_FATAL(sc_core::SC_ID_INTERNAL_ERROR_,
66                    "tlm_phase registration failed: duplicate type info" );
67            sc_core::sc_abort();
68        }
69        return it->second;
70    }
71
72    const char *
73    get_name(key_type id) const
74    {
75        sc_assert(id < names_.size());
76        return names_[id].c_str();
77    }
78
79  private:
80    typedef std::map<std::type_index, key_type> type_map;
81    typedef std::vector<std::string> name_table;
82
83    type_map ids_;
84    name_table names_;
85
86    tlm_phase_registry() : names_(END_RESP + 1)
87    {
88        names_[UNINITIALIZED_PHASE] = "UNINITIALIZED_PHASE";
89        names_[BEGIN_REQ] = "BEGIN_REQ";
90        names_[END_REQ] = "END_REQ";
91        names_[BEGIN_RESP] = "BEGIN_RESP";
92        names_[END_RESP] = "END_RESP";
93    }
94};
95
96} // anonymous namespace
97
98tlm_phase::tlm_phase(unsigned int id) : m_id(id)
99{}
100
101tlm_phase::tlm_phase(const std::type_info &type, const char *name) :
102    m_id(tlm_phase_registry::instance().register_phase(type, name))
103{}
104
105const char *
106tlm_phase::get_name() const
107{
108    return tlm_phase_registry::instance().get_name(m_id);
109}
110
111} // namespace tlm
112