sc_export.hh revision 13324:c8b709468e61
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#ifndef __SYSTEMC_EXT_CORE_SC_EXPORT_HH__
31#define __SYSTEMC_EXT_CORE_SC_EXPORT_HH__
32
33#include "../channel/messages.hh"
34#include "../utils/sc_report_handler.hh"
35#include "sc_module.hh" // for sc_gen_unique_name
36#include "sc_object.hh"
37
38namespace sc_core
39{
40
41class sc_interface;
42
43class sc_export_base : public sc_object
44{
45  public:
46    sc_export_base(const char *n);
47    ~sc_export_base();
48
49    virtual sc_interface *get_iterface() = 0;
50    virtual const sc_interface *get_interface() const = 0;
51
52  protected:
53    friend class sc_gem5::Module;
54
55    virtual void before_end_of_elaboration() = 0;
56    virtual void end_of_elaboration() = 0;
57    virtual void start_of_simulation() = 0;
58    virtual void end_of_simulation() = 0;
59};
60
61template <class IF>
62class sc_export : public sc_export_base
63{
64  public:
65    sc_export() :
66        sc_export_base(sc_gen_unique_name("export")), interface(nullptr)
67    {}
68    explicit sc_export(const char *n) :
69        sc_export_base(n), interface(nullptr)
70    {}
71    virtual ~sc_export() {}
72
73    virtual const char *kind() const { return "sc_export"; }
74
75    void operator () (IF &i) { bind(i); }
76    virtual void
77    bind(IF &i)
78    {
79        if (interface) {
80            SC_REPORT_ERROR(SC_ID_SC_EXPORT_ALREADY_BOUND_, name());
81            return;
82        }
83        interface = &i;
84    }
85    operator IF & ()
86    {
87        if (!interface)
88            SC_REPORT_ERROR(SC_ID_SC_EXPORT_HAS_NO_INTERFACE_, name());
89        return *interface;
90    }
91    operator const IF & () const { return *interface; }
92
93    IF *
94    operator -> ()
95    {
96        if (!interface)
97            SC_REPORT_ERROR(SC_ID_SC_EXPORT_HAS_NO_INTERFACE_, name());
98        return interface;
99    }
100    const IF *
101    operator -> () const
102    {
103        if (!interface)
104            SC_REPORT_ERROR(SC_ID_SC_EXPORT_HAS_NO_INTERFACE_, name());
105        return interface;
106    }
107
108    sc_interface *get_iterface() override { return interface; }
109    const sc_interface *get_interface() const override { return interface; }
110
111  protected:
112    void before_end_of_elaboration() {}
113    void
114    end_of_elaboration()
115    {
116        if (!interface) {
117            std::string msg = "export not bound: export '";
118            msg = msg + name() + "' (" + kind() + ")";
119            SC_REPORT_ERROR("(E109) complete binding failed", msg.c_str());
120        }
121    }
122    void start_of_simulation() {}
123    void end_of_simulation() {}
124
125  private:
126    IF *interface;
127
128    // Disabled
129    sc_export(const sc_export<IF> &);
130    sc_export<IF> &operator = (const sc_export<IF> &);
131};
132
133} // namespace sc_core
134
135#endif  //__SYSTEMC_EXT_CORE_SC_EXPORT_HH__
136