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/*****************************************************************************
21
22  sc_export.h -- Base classes of all export classes.
23
24  Original Author: Andy Goodrich, Forte Design Systems
25                   Bishnupriya Bhattacharya, Cadence Design Systems
26
27  CHANGE LOG IS AT THE END OF THE FILE
28 *****************************************************************************/
29
30#ifndef SC_EXPORT_H
31#define SC_EXPORT_H
32#include <typeinfo>
33
34#include "sysc/communication/sc_communication_ids.h"
35#include "sysc/communication/sc_interface.h"
36#include "sysc/kernel/sc_object.h"
37
38#if ! defined( SC_DISABLE_VIRTUAL_BIND )
39#  define SC_VIRTUAL_ virtual
40#else
41#  define SC_VIRTUAL_ /* non-virtual */
42#endif
43
44namespace sc_core {
45
46//=============================================================================
47//  CLASS : sc_export_base
48//
49//  Abstract base class for class sc_export<IF>.
50//=============================================================================
51
52class sc_export_base : public sc_object
53{
54    friend class sc_export_registry;
55public:
56
57    // typedefs
58
59    typedef sc_export_base this_type;
60
61public:
62
63    virtual       sc_interface* get_interface() = 0;
64    virtual       const sc_interface* get_interface() const = 0;
65
66protected:
67
68    // constructors
69
70    sc_export_base();
71    sc_export_base(const char* name);
72
73    // destructor
74
75    virtual ~sc_export_base();
76
77protected:
78
79    // called when construction is done
80    virtual void before_end_of_elaboration();
81
82    // called when elaboration is done (does nothing by default)
83    virtual void end_of_elaboration();
84
85    // called before simulation starts (does nothing by default)
86    virtual void start_of_simulation();
87
88    // called after simulation ends (does nothing)
89    virtual void end_of_simulation();
90
91    virtual const char* if_typename() const = 0;
92
93    // error reporting
94    void report_error( const char* id, const char* add_msg = 0) const;
95
96private:
97
98    void construction_done();
99    void elaboration_done();
100    void start_simulation();
101    void simulation_done();
102
103    // disabled
104    sc_export_base(const this_type&);
105    this_type& operator = (const this_type& );
106
107};
108
109//=============================================================================
110//  CLASS : sc_export
111//
112//  Generic export class for other export classes. This
113//  class provides a binding point for access to an interface.
114//=============================================================================
115template<class IF>
116class sc_export : public sc_export_base
117{
118    typedef sc_export<IF> this_type;
119
120public: // constructors:
121    sc_export() : sc_export_base()
122    {
123	m_interface_p = 0;
124    }
125
126    explicit sc_export( const char* name_ ) : sc_export_base(name_)
127    {
128	m_interface_p = 0;
129    }
130
131public: // destructor:
132    virtual ~sc_export()
133    {
134    }
135
136public: // interface access:
137
138    virtual sc_interface* get_interface()
139    {
140	return m_interface_p;
141    }
142
143    virtual const sc_interface* get_interface() const
144    {
145        return m_interface_p;
146    }
147
148    const IF* operator -> () const {
149        if ( m_interface_p == 0 )
150        {
151            SC_REPORT_ERROR(SC_ID_SC_EXPORT_HAS_NO_INTERFACE_,name());
152        }
153        return m_interface_p;
154    }
155
156    IF* operator -> () {
157        if ( m_interface_p == 0 )
158        {
159            SC_REPORT_ERROR(SC_ID_SC_EXPORT_HAS_NO_INTERFACE_,name());
160        }
161        return m_interface_p;
162    }
163
164    operator IF& ()
165    {
166	if ( m_interface_p == 0 )
167	{
168	    SC_REPORT_ERROR(SC_ID_SC_EXPORT_HAS_NO_INTERFACE_,name());
169	}
170	return *m_interface_p;
171    }
172    operator const IF&() const
173        { return *const_cast<this_type*>(this); }
174
175
176public: // binding:
177    SC_VIRTUAL_ void bind( IF& interface_ )
178    {
179    	if ( m_interface_p )
180	{
181	    SC_REPORT_ERROR(SC_ID_SC_EXPORT_ALREADY_BOUND_,name());
182	}
183	else
184	{
185	    m_interface_p = &interface_;
186	}
187    }
188
189    void operator () ( IF& interface_ )
190    {
191        this->bind(interface_);
192    }
193
194public: // identification:
195    virtual const char* kind() const { return "sc_export"; }
196
197protected:
198  const char* if_typename() const {
199    return typeid( IF ).name();
200  }
201
202private: // disabled
203    sc_export( const this_type& );
204    this_type& operator = ( const this_type& );
205
206protected: // data fields:
207    IF* m_interface_p;		// Interface this port provides.
208};
209
210// ----------------------------------------------------------------------------
211//  CLASS : sc_export_registry
212//
213//  Registry for all exports.
214//  FOR INTERNAL USE ONLY!
215// ----------------------------------------------------------------------------
216
217class sc_export_registry
218{
219    friend class sc_simcontext;
220
221public:
222
223    void insert( sc_export_base* );
224    void remove( sc_export_base* );
225
226    int size() const
227        { return m_export_vec.size(); }
228
229private:
230
231    // constructor
232    explicit sc_export_registry( sc_simcontext& simc_ );
233
234    // destructor
235    ~sc_export_registry();
236
237    // called when construction is done
238    bool construction_done();
239
240    // called when elaboration is done
241    void elaboration_done();
242
243    // called before simulation starts
244    void start_simulation();
245
246    // called after simulation ends
247    void simulation_done();
248
249private:
250
251    int                          m_construction_done;
252    std::vector<sc_export_base*> m_export_vec;
253    sc_simcontext*               m_simc;
254
255private:
256
257    // disabled
258    sc_export_registry();
259    sc_export_registry( const sc_export_registry& );
260    sc_export_registry& operator = ( const sc_export_registry& );
261};
262
263} // namespace sc_core
264
265#undef SC_VIRTUAL_
266
267// $Log: sc_export.h,v $
268// Revision 1.7  2011/08/26 20:45:40  acg
269//  Andy Goodrich: moved the modification log to the end of the file to
270//  eliminate source line number skew when check-ins are done.
271//
272// Revision 1.6  2011/05/09 04:07:37  acg
273//  Philipp A. Hartmann:
274//    (1) Restore hierarchy in all phase callbacks.
275//    (2) Ensure calls to before_end_of_elaboration.
276//
277// Revision 1.5  2011/04/02 00:02:14  acg
278//  Philipp A. Hartmann: add const overload for sc_export::operator IF&
279//
280// Revision 1.4  2011/02/18 20:23:45  acg
281//  Andy Goodrich: Copyright update.
282//
283// Revision 1.3  2011/02/14 17:50:16  acg
284//  Andy Goodrich: testing for sc_port and sc_export instantiations during
285//  end of elaboration and issuing appropriate error messages.
286//
287// Revision 1.2  2011/01/20 16:52:15  acg
288//  Andy Goodrich: changes for IEEE 1666 2011.
289//
290// Revision 1.1.1.1  2006/12/15 20:20:04  acg
291// SystemC 2.3
292//
293// Revision 1.3  2006/01/13 18:47:42  acg
294// Added $Log command so that CVS comments are reproduced in the source.
295//
296
297#endif
298
299// Taf!
300