我正在尝试使用SystemC(用于系统建模的 C++ 库)为系统编写模型。我的设计由三个主要部分组成:a Server
、an Environment
、People
objects 和Robots
. 环境和服务器都需要访问系统中的所有机器人。我最初的想法是在对象和Robot
对象中都保留一个对象向量(将传递给每个对象的构造函数)。但是,向量类要求对象具有默认构造函数。根据 SystemC 的性质,“模块”没有默认构造函数,因为每个模块都需要有一个名称。此外,我需要通过Server
Environment
Robot
向量。对此的常见解决方案是使用指针向量,然后从构造函数初始化该向量,如此处所示。但是,Robot
模块还需要在其构造函数中采用额外的参数。所以我不能真正嵌套这个技巧。如果有人可以为我提供解决此困境的方法,我将不胜感激。
为简洁起见,我只发布 和 的代码Server
,Robot
因为所有模块都遇到同样的问题;如果我能把它固定在一个地方,其他人应该跟着。
服务器.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_ */
服务器.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()
{
}
机器人.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_ */
机器人.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()
{
}
这是编译器输出(我把它放在 pastebin 上,因为我超出了字符数)