0

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)

SCFrench
  • 8,244
  • 2
  • 31
  • 61
audiFanatic
  • 2,296
  • 8
  • 40
  • 56
  • 2
    I'm pretty sure that vector doesn't require a default constructor unless you use certain members. – Mooing Duck Dec 03 '14 at 22:12
  • Your code works fine on my machine: http://coliru.stacked-crooked.com/a/df9bf0d388dda1a8 Please read this page and then edit the question: http://stackoverflow.com/help/mcve – Mooing Duck Dec 03 '14 at 22:17
  • @MooingDuck Interesting; it won't compile here (I am using C++11). It's giving me a `within this context` error at the beginning of the `Robot` constructor. – audiFanatic Dec 03 '14 at 22:27
  • "Within this context" is not an error, that's the middle line of an multi-line error. Every multi-line error has those words. You'll have to give the full error message at a minimum. – Mooing Duck Dec 03 '14 at 22:32
  • All right, I'll edit the question with the compiler output in a sec – audiFanatic Dec 03 '14 at 22:33
  • Ok, I posted the output: http://pastebin.com/RtavprM7 – audiFanatic Dec 03 '14 at 22:38
  • 1
    @audiFanatic well you didn't tell us that `Robot` can also not be moved or copied. You need to add either a move-constructor or a copy-constructor (if you want to ever copy, move or resize the vector beyond its initial capacity) – PeterT Dec 03 '14 at 22:42
  • 1
    You can't have `vector` passed by value as a function parameter, if `Robot` is neither movable nor copyable – M.M Dec 03 '14 at 22:44
  • Hmm, I see; that's news to me -- that fact was hidden in the library itself. I'm not sure what kinds of consequences making it moveable or copyable will have on the simulation – audiFanatic Dec 03 '14 at 22:53
  • Yeah, first error is `use of deleted function ‘Robot& Robot::operator=(const Robot&)’`, and it appears that all the `sc` classes are noncopiable. I don't know if they're movable but it appears unlikely. – Mooing Duck Dec 03 '14 at 22:57

1 Answers1

2

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).

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:

vector<Robot> robots;        // legal: empty vector, not a single robot constructed
//vector<Robot> robts(10);   // ilegal:  requires default constructor to initializethe 10 elements
robots.push_back(a);         // legal:  a robot was created, it is now copied into the vector  
robots.reserve(10);          // legal:  space is allocated for 10 reobots, but no robot is actually constructed. 
robots.resize(10);           // illegal:  space is allocated and robots are created to 

So you could perfecly well create an empty vector, and populate it as needed, construcing each robot individually with all the required parameters.

Christophe
  • 68,716
  • 7
  • 72
  • 138