Jump to content

SystemC object instanciation in C++ class


feron_jb

Recommended Posts

Hello,
 
I am trying to develop a service class allowing a user to interact with a SystemC model.
I am in trouble during my SystemC object instanciation in the service class.
Do you see any errors in my example codes hereunder?
 
File sc_user.h

#include "MySC_obj.h"
class sc_user{
public :
   // Service class constructor
   sc_user();

   // SC model instanciation
   MySC_obj* test("test");
}

File MySC_obj.h

#include <systemc.h>
SC_MODULE(MySC_obj){
   ...
   SC_HAS_PROCESS(MySC_obj);
   MySC_obj(sc_module_name instName) : sc_module(instName), ...{
      SC_THREAD(...);
      ...
   }
}

The complier complains about an expected ';' at end of member declaration in the line (MySC_obj* test("test"); ).
If I declare the same MySC_obj outside of the sc_user class, in a main function for instance, it is ok.
Any idea about my error?
 
Thank you!

Link to comment
Share on other sites

Do you see any errors in my example codes hereunder?

Yes. 

 

File sc_user.h

#include "MySC_obj.h"
class sc_user{

 

Side note: You should not prefix your classes with "sc_". These identifiers may be used in the current (or future) SystemC versions.

 

   // SC model instanciation
   MySC_obj* test("test");
}

Three problems here:

  1. In C++03, you cannot initialize a class member directly in its declaration (and the syntax would be different in C++11).
  2. You cannot convert a string literal to an MySC_obj pointer, use a direct member instead (i.e. drop the '*').
  3. You need to add a semicolon at the end of the 'sc_user' class definition)

You can then initialize the member in your constructor:

sc_user()
  : test("test")  // initializer list
{
  // ...
}

Please check your favourite C++ book on how to write classes, constructors, etc.

 

hth,

  Philipp

Link to comment
Share on other sites

Hi Philipp,

 

Thank you for your quick answer and interesting remarks.

 

I think my main problem is my misunderstanding of the SystemC constructor. I though the string passed as argument was used as instance name in simulation, I'm now doubting. Or maybe it is not necessary in a pointer to a SystemC object declaration.

By this misunderstanding, I can't declare a pointer to the SystemC object MySC_obj (what I was trying to do, not to initialize the oblect in the class :-D )

Could you, please, explain (or point me to the documentation about) :

  • the role of the string in the systemC constructor
  • how to declare a pointer to a systemC object (and how to instanciate its content using 'new' dynamic constructor). An example is sufficient.

Thank you!

J-B

 

EDIT :

It seems that I should declare and instanciate pointers and their content like it :

 

File sc_user.h

#include "MySC_obj.h"
class sc_user{
public :
   // Service class constructor
   sc_user();

   // SC model instanciation
   MySC_obj* test;
}

File sc_user.cpp

#include "sc_user.h"

   // Service class constructor
   sc_user::sc_user(){
      test = new MySC_obj("test");
   }
}

What if I want to initialize an array of MySC_obj?

test = new MySC_obj("test")[2]; 

The previous command doesn't work...

I didn't find answer to the string role in the constructor.

Link to comment
Share on other sites

As I said, you need to read up on C++ classes, class members, constructors in general first.  And of course, you need to understand the difference between a pointer and an object.

 

Of course, you can declare a pointer to an object as a member in your 'sc_user' class.  You can then set its contents in the sc_user constructor in the same way as you would do it outside of a constructor.  You just can't set it in the body of the class directly, which is what you (kind of) tried in your original code.

 

Last, but not least: Why do you want to use a pointer and a dynamic allocation (new) anyway?  If you want to instantiate the MySC_obj object in the constructor, you should definitely prefer a direct member instead of a pointer.

 

But learning the basics of C++ is a prerequisite to all of this.

 

/Philipp

Link to comment
Share on other sites

What if I want to initialize an array of MySC_obj?
test = new MySC_obj("test")[2]; 

The previous command doesn't work...

I didn't find answer to the string role in the constructor.

 

As you correctly pointed out, SystemC module constructors have a required "name" parameter.  Therefore, you can't create C(++) arrays of such classes by the C++ language rules.

 

If you want to create array-like containers of SystemC modules (or other named SystemC objects), you can use the sc_vector class template, available since IEEE 1666-2011 and SystemC 2.3.0:

// in class definition
sc_vector< MySC_obj > mysc_vec; // can be used like an array (or rather a std::vector) of MySC_obj

// constructor
sc_user::sc_user()
  : mysc_vec( "test", 2 ) // initialize with name and size
{}

I am now effectively initializing my SystemC object in the user classe constructor.

 

Yes, but you are missing a destructor, which is needed to avoid a memory leak coming from the dynamic allocation.  Remember: Whenever you write a "new" somewhere in your program, you should write a matching "delete" as well.  These are basic C++ language rules again.

 

/Philipp

Link to comment
Share on other sites

Yes. 

 

 

Side note: You should not prefix your classes with "sc_". These identifiers may be used in the current (or future) SystemC versions.

 

Three problems here:

  1. In C++03, you cannot initialize a class member directly in its declaration (and the syntax would be different in C++11).
  2. You cannot convert a string literal to an MySC_obj pointer, use a direct member instead (i.e. drop the '*').
  3. You need to add a semicolon at the end of the 'sc_user' class definition)

You can then initialize the member in your constructor:

sc_user()
  : test("test")  // initializer list
{
  // ...
}

Please check your favourite C++ book on how to write classes, constructors, etc.

 

hth,

  Philipp

In perfect agreement with what Phillipp has said, please note

that SystemC is a C++ library. So a sound understanding/working

knowledge of C++ features, especially the advanced ones (e.g.,

templates, inheritance etc.,) is essential before you can use

SystemC efficiently - that is focus on the design problem one

is trying to solve, rather than conceptual/coding errors.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...