Declaring a generic list method - how does this work? - list

Im studying C# and came across a piece of code that Im quite confused about
private static List<Customer> CreateCustomerList()
List<Customer> customers = new List<Customer>{new Customer{FirstName="Orlando"}
};
return customers;
}
Im confused with the line where it starts
private static List<Customer> CreateCustomerList()....
Im used to seeing for example
private static class CreateCustomerList()....
Why would I need to use List instead of declaring the method as a class?

The line where you are confuses is the method signature and the return type of the method is of List, means that method will return the list of customers entity.
Thaanks

Related

QT C++ access to a Class Member of another Class

i really dont understand the following behavior:
3 Code snippets to explain
sqlconnection.h
class SqlConnection
{
public:
SqlConnection();
QSqlDatabase db;
} ;
sqlconnection.cpp
SqlConnection::SqlConnection()
{ if (!db.open()){ //no different with or without that
db = QSqlDatabase::addDatabase("QODBC");
...
}
artikelverwaltung.cpp
Artikelverwaltung::Artikelverwaltung(){
SqlConnection().db.open();
...(code to do works fine)
}
void Artikelverwaltung::on_pushButton_2_clicked()
{
SqlConnection().db.close(); // "HERE"
}
"HERE" it cycles the full Contructor of the Class again, but why it don't just close the connection? (this is the Main question about and how to fix that.)
I can't even access "db" from another Method inside the Class but outside of the Constructor, like:
sqlconnection.cpp:
void closeDB() {db.close();} is not possible.`
What i am doing wrong?
Thank you guys
In general, you do not need to use any wrapper class to hold database connections in Qt. You should create a database connection once using QSqlDatabase::addDatabase and open it. After that you can get a database connection anywhere by calling QSqlDatabase::database static method. So to close the database connection you should simply call QSqlDatabase::database().close().
Hello and welcome to Stackoverflow!
If you call SqlConnection().db.close();, what you are actually doing is creating a new instance of your SqlConnection class and then calling close() on its variable db. This is not the same one you opened earlier.
What you want to do is inside of your Artikelverwaltung class:
SqlConnection sqlConnection = new SqlConnection();
and then later
sqlConnection.db.open();
and afterwards
sqlConnection.db.close();
The reason why you cant add that closeDB() method is that you need to either put the method inside of the SqlConnection class or you need to operate on an instance of the class.
You cannot operate on a class like this without instanciating it (creating an instance of it).
You should look at class instantiation to understand how the concept works.
If you call SqlConnection() you are creating a new instance of the class (you can look at it like a copy of the class).
If you call SqlConnection() somewhere else you are simply creating a new instance.
These instances do NOT share the variables or the contents in them. They are completely independent.
You cannot access a member of a class (like your db variable) without creating an instance of the class first and then accessing the member of that instance.

Javassist - addMethod that returns an Object[][]

My goal is to create in runtime an additional method inside a specific .class file.
A method that returns an Object[][].
For that I found an amazing framework called - Javassist, a bytecode modifier framework, which helps you modify your compiled class in runtime in order to add more bytecode that represents a new method.
Managed to create a void method, and a method that returns a string but, for some reason, I'm unable to generate a method that returns an array or a matrix.
So far I've been struggling to find the proper way of creating such method, and got a continuous CannotCompileException.
Code:
private static CtMethod generateMethod1(CtClass declaringClass)
throws CannotCompileException {
StringBuffer sb = new StringBuffer();
sb.append("public ").append(Object[][].class.getName()).append(" ").append("method1").append("(){")
.append("return new").append(Object[][].class.getName()).append("{{ 1,2 }}").append("; }");
System.out.println(sb.toString());
return CtMethod.make(sb.toString(), declaringClass);
}
The toString of the generated method above is:
public [[Ljava.lang.Object; method1(){return [[Ljava.lang.Object;{{ 1,2 }}; }
Probably fails due to false jni syntax.
Well, solved it by just replacing the Object[][].class.getName() with Object[][] literally...

Should instance fields access be synchronized in a Tapestry page or component?

If a page or component class has one instance field which is a non-synchronized object, f.ex. an ArrayList, and the application has code that structurally modifies this field, should the access to this field be synchronized ?
F.ex.:
public class MyPageOrComponent
{
#Persist
private List<String> myList;
void setupRender()
{
if (this.myList == null)
{
this.myList = new ArrayList<>();
}
}
void afterRender(MarkupWriter writer)
{
// Should this be synchronized ?
if (someCondition)
{
this.myList.add(something);
}
else
{
this.myList.remove(something);
}
}
}
I'm asking because I seem to understand that Tapestry creates only one instance of a page or component class and it uses this instance for all the connected clients (but please correct me if this is not true).
In short the answer is no, you don't have to because Tapestry does this for you. Tapestry will transform your pages and classes for you at runtime in such a way that wherever you interact with your fields, they will not actually be working on the instance variable but on a managed variable that is thread safe. The full inner workings are beyond me, but a brief reference to the transformation can be found here.
One warning, don't instantiate your page/component variables at decleration. I have seen some strange behaviour around this. So don't do this:
private List<String> myList = new ArrayList<String>;
Tapestry uses some runtime byte code magic to transform your pages and components. Pages and components are singletons but the properties are transformed so that they are backed by a PerThreadValue. This means that each request gets it's own copy of the value so no synchronization is required.
As suggested by #joostschouten you should never initialize a mutable property in the field declaration. The strange behaviour he discusses is caused beacause this will be shared by all requests (since the initializer is only fired once for the page/component singleton). Mutable fields should instead be initialized in a render method (eg #SetupRender)

Boost.Python - Passing boost::python::object as argument to python function?

So I'm working on a little project in which I'm using Python as an embedded scripting engine. So far I've not had much trouble with it using boost.python, but there's something I'd like to do with it if it's possible.
Basically, Python can be used to extend my C++ classes by adding functions and even data values to the class. I'd like to be able to have these persist in the C++ side, so one python function can add data members to a class, and then later the same instance passed to a different function will still have them. The goal here being to write a generic core engine in C++, and let users extend it in Python in any way they need without ever having to touch the C++.
So what I thought would work was that I would store a boost::python::object in the C++ class as a value self, and when calling the python from the C++, I'd send that python object through boost::python::ptr(), so that modifications on the python side would persist back to the C++ class. Unfortunately when I try this, I get the following error:
TypeError: No to_python (by-value) converter found for C++ type: boost::python::api::object
Is there any way of passing an object directly to a python function like that, or any other way I can go about this to achieve my desired result?
Thanks in advance for any help. :)
Got this fantastic solution from the c++sig mailing list.
Implement a std::map<std::string, boost::python::object> in the C++ class, then overload __getattr__() and __setattr__() to read from and write to that std::map. Then just send it to the python with boost::python::ptr() as usual, no need to keep an object around on the C++ side or send one to the python. It works perfectly.
Edit: I also found I had to override the __setattr__() function in a special way as it was breaking things I added with add_property(). Those things worked fine when getting them, since python checks a class's attributes before calling __getattr__(), but there's no such check with __setattr__(). It just calls it directly. So I had to make some changes to turn this into a full solution. Here's the full implementation of the solution:
First create a global variable:
boost::python::object PyMyModule_global;
Create a class as follows (with whatever other information you want to add to it):
class MyClass
{
public:
//Python checks the class attributes before it calls __getattr__ so we don't have to do anything special here.
boost::python::object Py_GetAttr(std::string str)
{
if(dict.find(str) == dict.end())
{
PyErr_SetString(PyExc_AttributeError, JFormat::format("MyClass instance has no attribute '{0}'", str).c_str());
throw boost::python::error_already_set();
}
return dict[str];
}
//However, with __setattr__, python doesn't do anything with the class attributes first, it just calls __setattr__.
//Which means anything that's been defined as a class attribute won't be modified here - including things set with
//add_property(), def_readwrite(), etc.
void Py_SetAttr(std::string str, boost::python::object val)
{
try
{
//First we check to see if the class has an attribute by this name.
boost::python::object obj = PyMyModule_global["MyClass"].attr(str.c_str());
//If so, we call the old cached __setattr__ function.
PyMyModule_global["MyClass"].attr("__setattr_old__")(ptr(this), str, val);
}
catch(boost::python::error_already_set &e)
{
//If it threw an exception, that means that there is no such attribute.
//Put it on the persistent dict.
PyErr_Clear();
dict[str] = val;
}
}
private:
std::map<std::string, boost::python::object> dict;
};
Then define the python module as follows, adding whatever other defs and properties you want:
BOOST_PYTHON_MODULE(MyModule)
{
boost::python::class_<MyClass>("MyClass", boost::python::no_init)
.def("__getattr__", &MyClass::Py_GetAttr)
.def("__setattr_new__", &MyClass::Py_SetAttr);
}
Then initialize python:
void PyInit()
{
//Initialize module
PyImport_AppendInittab( "MyModule", &initMyModule );
//Initialize Python
Py_Initialize();
//Grab __main__ and its globals
boost::python::object main = boost::python::import("__main__");
boost::python::object global = main.attr("__dict__");
//Import the module and grab its globals
boost::python::object PyMyModule = boost::python::import("MyModule");
global["MyModule"] = PyMyModule;
PyMyModule_global = PyMyModule.attr("__dict__");
//Overload MyClass's setattr, so that it will work with already defined attributes while persisting new ones
PyMyModule_global["MyClass"].attr("__setattr_old__") = PyMyModule_global["MyClass"].attr("__setattr__");
PyMyModule_global["MyClass"].attr("__setattr__") = PyMyModule_global["MyClass"].attr("__setattr_new__");
}
Once you've done all of this, you'll be able to persist changes to the instance made in python over to the C++. Anything that's defined in C++ as an attribute will be handled properly, and anything that's not will be appended to dict instead of the class's __dict__.

android: cannot access method of custom list

I got the following problem:
I made a custom list CustomList which extends ArrayList and added a new method to it:
public class CustomList extends ArrayList<CustomObj> {
public CustomObj get(String searchName) {
...
}
}
now in my MainActivity.java I make a new object of this CustomList, but I'm using:
List<CustomObj> list = new CustomList(); (1)
and NOT:
CustomList list = new CustomList(); (2)
so far so good!
but when I try to access the function get(String searchName), there is no function I could use!
why? cause when I call it when creating the CustomList via (2) it'll totally work
its is because you are making List(Interface) has no any such method defined , you are making object of CustomList but reference type is List .so it will give you compile time errer if you force to call this method on List reference.This is an example of simple Polymorphism.
You can imagine a case where Mordern car extends Old age car and mordern car has GPS navigation system where as old doesn't in this case if you try to get the detail of navigation system by old car reference which doesn't know about GPS , you wont get anything.