I'm trying to write a model for a system using SystemC (a C++ library for systems modeling). My design consists of three main parts: a Server
, an Environment
, People
objects and Robots
. The environment and the server both need access to all the robots in the system. My original idea was to keep a vector of Robot
objects in both the Server
and the Environment
objects (which would be passed into the constructors of each). However, the vector class requires that the object have a default constructor. By the nature of SystemC, "modules" do not have default constructors as each module needs to have a name. Further, I need to pass in the Robot
vector. The common solution to this is to use an vector of pointers and then initialize the vector from the constructor as illustrated here. However, the Robot
modules also need to take additional parameters in their constructors. So I can't really nest this trick. I'd appreciate it if somebody could offer me a solution to this dilemma.
For brevity, I'm only going to post the code for the Server
and the Robot
, as all the modules are suffering from the same problem; if I can get it fixed in one place, the others should follow.
server.h
#ifndef SERVER_H_
#define SERVER_H_
#include <vector>
#include <systemc.h>
#include "Person.h"
#include "Robot.h"
SC_MODULE (Server)
{
public:
sc_in<bool> clk;
SC_HAS_PROCESS(Server);
Server(sc_module_name name_, std::vector<Robot> robots);
void prc_server();
};
#endif /* SERVER_H_ */
server.cpp
#include "Server.h"
Server::Server(sc_module_name name_, std::vector<Robot> robots) : sc_module(name_)
{
SC_METHOD(prc_server);
sensitive << clk.pos();
}
void Server::prc_server()
{
}
robot.h
#ifndef ROBOT_H_
#define ROBOT_H_
#include <vector>
#include <systemc.h>
#define ROBOT_SPEED 0.1
SC_MODULE (Robot)
{
private:
std::vector<unsigned> path;
public:
sc_in<bool> clk; // system clock
sc_in<bool> canMove; // true = safe to move
sc_in<unsigned> position; // current position
sc_out<bool> arrived; // true = arrived at next position
SC_HAS_PROCESS(Robot);
Robot(sc_module_name name_, std::vector<unsigned> path);
void prc_robot();
};
#endif /* ROBOT_H_ */
robot.cpp
#include "Robot.h"
Robot::Robot(sc_module_name name_, std::vector<unsigned> path) : sc_module(name_)
{
this->path = path;
SC_METHOD(prc_robot);
sensitive<<clk.pos();
}
void Robot::prc_robot()
{
}
Here's the compiler output (I put it on pastebin because I overran the character count)
According to your statement, your original idea (and the fragment of codes you show) would lead you to have all robots of your environment duplicated, as each Server and Environment would keep a COPY of your vector.
This is not want you intend to do, unless you want to add code to painfully synchronize the duplicate robots in each object.
So you'd better manage a shared vector of robots, and pass it at construction to your System/Environment/etc.. BY REFERENCE. Then you would have only one single and consistent vector of robots. If it is updated in one place, all the other objects would see the update as well.
Now, vector doesn't require default consturctor. It depends on the operations you do:
So you could perfecly well create an empty vector, and populate it as needed, construcing each robot individually with all the required parameters.