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