virtual_bind.cpp revision 12855:588919e0e4aa
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// virtual_bind.cpp -- test for
21//
22//  Original Author: John Aynsley, Doulos, Inc.
23//
24// MODIFICATION LOG - modifiers, enter your name, affiliation, date and
25//
26// $Log: virtual_bind.cpp,v $
27// Revision 1.2  2011/05/08 19:18:46  acg
28//  Andy Goodrich: remove extraneous + prefixes from git diff.
29//
30
31// Process control method throw_it
32
33#define SC_INCLUDE_DYNAMIC_PROCESSES
34
35#include <systemc>
36
37using namespace sc_core;
38using std::cout;
39using std::endl;
40
41struct i_f: virtual sc_interface
42{
43  virtual void meth() = 0;
44};
45
46struct Chan: i_f, sc_module
47{
48  int f1;
49  Chan(sc_module_name _name)
50  {
51    f1 = 0;
52  }
53  virtual void meth() { f1 = 1; }
54};
55
56struct my_port: sc_port<i_f>
57{
58  void bind(i_f& _if)
59  {
60    sc_assert(false);
61  }
62};
63
64struct extended_port: my_port
65{
66  int f2;
67  extended_port() { f2 = 0; }
68
69  void bind(i_f& _if)
70  {
71    sc_port<i_f>::bind(_if);
72    f2 = 1;
73  }
74};
75
76struct my_export: sc_export<i_f>
77{
78  void bind(i_f& _if)
79  {
80    sc_assert(false);
81  }
82};
83
84struct extended_export: my_export
85{
86  int f3;
87  extended_export() { f3 = 0; }
88
89  void bind(i_f& _if)
90  {
91    sc_export<i_f>::bind(_if);
92    f3 = 1;
93  }
94};
95
96struct Child: sc_module
97{
98  extended_port p;
99  extended_export xp;
100
101  Chan chan;
102
103  Child(sc_module_name _name)
104  : chan("chan")
105  {
106    my_export* mxp = static_cast<my_export*>( &xp );
107    mxp->bind(chan); // bind should be virtual
108    SC_THREAD(T);
109  }
110
111  void T()
112  {
113    p->meth();
114  }
115
116  SC_HAS_PROCESS(Child);
117};
118
119struct Top: sc_module
120{
121  Child *child;
122  Chan chan;
123
124  Top(sc_module_name _name)
125  : chan("chan")
126  {
127    child = new Child("child");
128    extended_port* ep = &(child->p);
129    my_port* mp = static_cast<my_port*>(ep);
130    mp->bind(chan); // bind should be virtual
131
132    SC_THREAD(T);
133  }
134
135  void T()
136  {
137    child->xp->meth();
138  }
139
140  SC_HAS_PROCESS(Top);
141};
142
143int sc_main(int argc, char* argv[])
144{
145  Top top("top");
146
147  sc_start();
148
149  sc_assert( top.chan.f1 );
150  sc_assert( top.child->chan.f1 );
151  sc_assert( top.child->p.f2 );
152  sc_assert( top.child->xp.f3 );
153
154  cout << endl << "Success" << endl;
155  return 0;
156}
157
158