How can I create a simple assignment statement in codemodel? - sun-codemodel

I want to create a simple statement using codemodel like :-
String text = element.getText();
I don't want to assign it to any block for now, rather just return it because I would be needing the name of the variable to refer later. How can I create such a statement and in which type of variable to store it? Would it be a JStatement? If yes then how?

It looks like the codemodel api doesn't allow you to create an assigment without a Block. The JAssignment object is created via the JBlock .assign() method, and the constructor for JAssignment is package private. That being said, you can always make an object of your own to hold the various parts of the assignment and defer the building of the assignment until you have a block to add it to.

Related

The right way to write a if-can-add check method before create a complete object?

I have several object implement with polymorphism . Before add it to the container , I have to check if the status of container and values of the corresponding object is valid to allow adding the object, the step is:
1) Use the object's virtual method to do the test
2) If test is passed, create the object, add it to the container
My problem is:
In the first step, I need the virtual method of the object, so I have to create
the object. This can be a resource waste if the test failed since the process of construct the object is costly.
Some solution is :
Make the test method public static, but seemed not work with polymorphism .
Make a overloading constructor take no params, and use this object to do the test. This way, I have to provide two constructor, there are two shortcomings :
a. I'm afraid the misuse of this incomplete object through the whole project by other programers.
b. I provide a simple factory method, If there are two constructors, I have to write another factory method to get the different object, these duplicate codes can be a nightmare.
The brute force way is to provide some other independent method outside of the object, pass the context info to it, and use these method to do the test.
What is the proper way to solve this problem ?
Seems that you need a builder pattern since you are about to construct object step by step.
Another solution is to use strategy pattern if you have a kind of if-else decision to create each object. So you can add to your container the right object according to your algorithm of creating

Basic C++ dynamic allocation issue

There is some basic facet of this that I'm completely missing. I have an object containing a set
PropertyContainer::PropertyContainer(string comFile, string resFile)
{
set<Property*>* prCont = new set<Property*>();
...
}
In my main I'm calling
PropertyContainer* ptrPropertySet = new PropertyContainer(comFile, resFile);
This constructor goes through and populates the set with the data from the files.
If (while i'm still in the constructor) I iterate through the set all the Properties are there.
If I go back to main() and access the set it is empty (I've done it both through an accessor function and temporarily made it public and accessed it directly, to confirm i hadn't made a mistake in the accessor function)
This leads me to believe there is a very basic tenet of programming I'm missing. Any help or links would be appreciated.
You're defining a local variable prCont, which hides your member variable.
It's very rare to dynamically allocate std::sets, and you probably shouldn't.
If you declare the member as set<Property*> prCont, it will be default-constructed automatically when your PropertyContainer is created.

Can classes get by with the default constr/destructors? c++

Or do you have to manually create them? Was wondering if you can just create a class, declare some variables and method/functions. Then assign the object to your class in the int main function. Do you need to put in a constructor/deconstructor to create and destroy the memory that was used to hold to place of the variables or is there defaults that already do that?
Any additional info on constructors and destructors would be nice. Don't need a lot just some simple added info to make understanding this easier. Not trying to create programs that promote memory leakage
You do not need to explicitly provide a constructor/destructor as a default is generated for you. However, if you have any plain old data types, such as pointers to dynamically allocated memory, you will want to make certain that you provide your own manually specified destructor as the default will not be sufficient.

How to call a class method without creating an instance

In Java you can call a class method without creating a variable with the instance of the class and still call that class method:
new Database().GetSomeValuesOutOfSomeTableJava();
If I try the same with C++ I get an error:
new Database()->GetSomeValuesOutOfSomeTableCpp();
Am I doing it wrong? How can I achieve the same result?
new Database().GetSomeValuesOutOfSomeTableJava();
That does create an instance (note the new); it then abandons it for the garbage collector to clean up.
In C++, you can create a temporary object without new:
Database().GetSomeValuesOutOfSomeTableCpp();
This will do the same as the Java example, except that the temporary will be destroyed deterministically at the end of this statement.
Whether you should be creating temporaries like this is another matter.
You would do this:
(new Database())->GetSomeValuesOutOfSomeTableCpp();
But you'd be leaking memory, so that's a pretty bad idea. Also, note that that this (both in C++ and Java) does indeed create a new instance. It's just a new instance that's not stored in a variable.
A better version might be something like this:
{
Database temporary;
temporary.GetSomeValuesOutOfSomeTableCpp();
}
Using RAII to initialize and properly destroy the temporary, and scoping to ensure it doesn't exist for longer than you'd like it to and mess up any later scoping.
Alternatively, as pointed out by Mike Seymour, you could do:
Database().GetSomeValuesOutOfSomeTableCpp();
Which will also avoid the memory leak.

Is it safe to store a local declared object in a global QList?

I got a CPP program where i make a local object A and want to store it in global object B which is a QList.
Is it save to statically allocate object A or do i need to use the new keyword.
Does QList uses the copy constructor?
Thanks
QList stores copies of objects, so it should work. However make sure that copying is indeed what you want. If this isn't the case, allocate your object with new and store the pointer in the QList.
No. Inner scope variables should not be stored in outer scoped variables. You can store the values, but not the reference/pointer to that variable.
QList has only a pointer to it's contents. So whenever you make a copy of a QList it doesn't actually copy all of the contents, it just copies the pointer. Whenever you modify a list, a copy is made to ensure that it's not modifying the contents of other objects. See this: http://doc.qt.nokia.com/4.7-snapshot/qshareddatapointer.html
QList does use the copy contructor, so if your objects contain a lot of data, it might be not good to use straight objects in the QList, since copying can cause some overhead when the list needs to grow.
Another solution would be to use QSharedDataPointer to create functionality similar to that of the QList.
Note that most of Qt classes already use this, so if you class contains things listed here: http://doc.qt.nokia.com/4.7-snapshot/implicit-sharing.html It's somewhat unnecessary to use the QSharedDataPointer.
There is one thing that you didn't make clear that has of relevance, I think. Do you want the global static object A to have the same data as the object on the list?
This is achieveable by using either pointers or QExplicitlySharedDataPointer.
QExplicitlySharedDataPointer is much the same as QSharedDataPointer, with one exception.
It doesn't make a copy of the data when it's modified. Here's some documentation http://doc.qt.nokia.com/4.7-snapshot/qexplicitlyshareddatapointer.html
I'v used those classes a lot and I'v found them very useful and not hard at all to use.
nope. QList stores the list of pointers to objects, so local variables should not be stored in the global QList.
check out this link for details:
http://twl.pl/jtz/Inne/QT-Tutorial/qlist.html#details