test03.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 test03.cpp -- Test sc_vector 23 24 Original Author: Philipp A. Hartmann, OFFIS, 2010-03-05 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#include "systemc.h" 39 40#include "sysc/utils/sc_vector.h" 41using sc_core::sc_vector; 42 43SC_MODULE( sub_module ) 44{ 45 // constructor with additional parameters 46 sub_module( sc_core::sc_module_name, int param ); 47 // ... 48}; 49 50SC_MODULE( module ) 51{ 52 sc_core::sc_vector< sub_module > m_sub_vec; // vector of sub-modules 53 54 module( sc_core::sc_module_name, unsigned n_sub ); // constructor 55 56 struct mod_creator // Creator struct 57 { 58 mod_creator( int p ) : param(p) {} // store parameter to forward 59 60 sub_module* operator()( const char* name, size_t ) // actual creator function 61 { 62 return new sub_module( name, param ); // forward param to sub-module 63 } 64 int param; 65 }; 66 67}; 68 69sub_module::sub_module( sc_core::sc_module_name, int ) 70 { /* empty */ } 71 72module::module( sc_core::sc_module_name, unsigned n_sub ) 73 : m_sub_vec( "sub_modules" ) // set name prefix 74{ 75 m_sub_vec.init( n_sub, mod_creator(42) ); // init with custom creator 76 // ... 77} 78 79int sc_main(int , char* []) 80{ 81 module dut( "dut", 5 ); 82 83 typedef sc_core::sc_vector<sub_module> vec_type; 84 vec_type const & children = dut.m_sub_vec; 85 86 for( vec_type::const_iterator it = children.begin(); 87 it != children.end(); ++it ) 88 { 89 cout << it->name() << " - " 90 << (*it).kind() 91 << endl; 92 } 93 94 for (size_t i=0; i < children.size(); ++i ) 95 cout << children[i].basename() << " - " 96 << children[i].get_parent_object()->name() 97 << endl; 98 99 cout << "Program completed" << endl; 100 return 0; 101} 102