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