Jump to content
jinhuang12

Passing user defined object as a parameter for sc_fifo

Recommended Posts

Hello, I want to make a server module that outputs a user defined class through sc_fifo and I'm not sure if that is possible.  So far, I have this 

 

Server module header

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <systemc.h>

#include "Gaze.h"

#include "ImageObjects.h"

#include "Image.h"

#include "iostream.h"

#include "fstream.h"

#include "string.h"

#include <sstream>

 

SC_MODULE(Server)

{

    //PORTS FOR SERVER

    sc_in_clk clk; //Clock

    sc_in<sc_uint<8> > check; //Signal for client to request new image

    sc_port<sc_fifo_in<Gaze >> inClient; // input from client

    sc_port<sc_fifo_out<Image >> output; //output to client

    

    void sendImage(sc_uint<8> img); //Function to send image to client

    std::vector<Image> getImages(); //Takes in annotation files and saves them as images

    

    //Local data structures

    std::vector<Image> ImageBuffer;

    

    SC_CTOR(Server)

    {

        SC_METHOD(sendImage);

            sensitive << check;

    }

    

};

------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
Would "sc_port<sc_fifo_out<Image >> output;" that line of code work?
 
Image is just a class I created in c++
------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include "ImageObjects.h"

#include <vector>

 

class Image : public sc_interface

{

    

public:

    

    int imageNumber;

    std::vector<ImageObjects> obj;

    

   

    Image(int i, std::vector<ImageObjects> ob);

    //void setImage(int, Objects[]);

    virtual Image returnImage();

    

};

---------------------------------------------------------------------------------------------------------------------------------------------

 

 

Thanks for all the help!

 

 

Share this post


Link to post
Share on other sites

sc_fifo_in and sc_fifo_out are already specialized ports to be connected to an sc_fifo. So, you don't need to put them as a template argument to sc_port<IF>. Regarding what kind of operations a user-defined type has to support to be communicated through an sc_fifo, have a look to clause 6.23.3 in the freely available IEEE Std 1666-2011. Basically, the output stream operator<<, an assignment operator=, and a default constructor have to be defined for that it can work.

Share this post


Link to post
Share on other sites

Thank you for the reply, so in order for me to pass an Image over sc_fifo, would I have to change my Image class code to something like this?

 

template <typename T>
class Image;

template <typename T>
ostream& operator<<(ostream &, const Image<T> &);

template<typename T>
class Image
{
public:
friend ostream& operator<< <>(ostream & output, const Image<T> & theImage);
...
};

template<typename T>
ostream& operator<<(ostream & output, const Image<T> & theImage)
{
...
return output;
}

 

Can you show an example of how I would use the assignment operator and would I have to change the Image.cpp file to accommodate this change?

 

Image.cpp

________________________________________________________________________________________________________________________________________________________________________________________

#include "Image.h"
 
//void Image::setImage(int imageNumber, Objects objects[])
//{
//    this->imageNumber = imageNumber;
//    
//    int i = 0;
//    while (i < sizeof(*objects))
//    {
//        this->objects = objects;
//        i++;
//    }
//    
//}
 
Image::Image(int imageNumber, std::vector<ImageObjects> obj)
{
    this->imageNumber = imageNumber;
    this->obj = obj;

 

____________________________________________________________________________________________________________________________________________________________________________________________

 

Thanks again!

Share this post


Link to post
Share on other sites

The assignment operator just implements the copy operation when you want to assign the value of a variable to another one of the same type. By default, C++ generates a copy constructor and assignment operator, which will simply use member-wise assignment. This usually won't be enough if you use internally pointers and dynamic memory allocation.

 

The sc_fifo uses the assignment operation to copy the value passed into its input into the internal buffer of the FIFO.

 

Have a look to the Doulos FAQ on SystemC to know more about how to use user-defined data types with SystemC channels:

http://www.doulos.com/knowhow/systemc/faq/#q1

 

For examples how to implement correctly the copy constructor and assignment operation, have a look into a good C++ book or the C++ FAQ. Here are some helpful URLs:

http://www.isocpp.org/

http://www.cplusplus.com

http://www.parashift.com/c++-faq/

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×