test02.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/*****************************************************************************
21
22  test02.cpp -- Test sc_vector
23
24  Original Author: Philipp A. Hartmann, OFFIS, 2010-01-10
25
26 *****************************************************************************/
27
28/*****************************************************************************
29
30  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
31  changes you are making here.
32
33      Name, Affiliation, Date:
34  Description of Modification:
35
36 *****************************************************************************/
37
38//#define USE_BOOST
39
40#ifndef USE_BOOST
41#  define SC_INCLUDE_DYNAMIC_PROCESSES
42#endif
43
44#include "systemc.h"
45
46#include "sysc/utils/sc_vector.h"
47using sc_core::sc_vector;
48
49#ifdef USE_BOOST
50#include <boost/bind.hpp>
51#include <boost/bind/placeholders.hpp>
52#endif
53
54// ---- some classes
55//
56struct base : sc_object
57{
58  base( const char* n ) : sc_object(n) {}
59};
60
61struct derived_0 : base
62{
63  derived_0(const char* name) : base(name) {}
64  const char* kind() const { return "derived_0"; }
65};
66
67struct derived_1 : public base
68{
69  derived_1(const char* name) : base(name) {}
70  const char* kind() const { return "derived_1"; }
71};
72
73// plain function pointer
74base* fill_array( const char* n, size_t i )
75{
76  if( i%2 ) return new derived_1( n );
77  return new derived_0( n );
78}
79
80SC_MODULE(DUT)
81{
82  sc_vector< base >            arr;
83  sc_vector< sc_in<bool> >     inps;
84  sc_vector< sc_signal<bool> > sigs;
85
86  SC_CTOR(DUT);
87
88  // member function as creator (use with sc_bind())
89  sc_signal<bool>* init_sig_bind( const char* n, unsigned i )
90  {
91    sc_signal<bool>* sig = new sc_signal<bool>(n);
92    inps[i]( *sig );
93    return sig;
94  }
95};
96
97
98DUT::DUT( sc_module_name )
99  : arr("array")
100  , inps("inps", 5)
101  , sigs("sigs")
102{
103  arr.init( 3, fill_array );
104
105  sigs.init( inps.size()
106#if defined( SC_INCLUDE_DYNAMIC_PROCESSES )
107	, sc_bind( &DUT::init_sig_bind, this, sc_unnamed::_1, sc_unnamed::_2 )
108#elif defined( USE_BOOST )
109	, boost::bind( &DUT::init_sig_bind, this, _1, _2 )
110#endif
111  );
112}
113
114int sc_main(int , char* [])
115{
116  DUT dut("dut");
117
118  std::vector<sc_object*> children = dut.get_child_objects();
119
120  for (size_t i=0; i<children.size(); ++i )
121    cout << children[i]->name() << " - "
122         << children[i]->kind()
123         << endl;
124
125  cout << "Program completed" << endl;
126  return 0;
127}
128