PythonQt how to override virtual function - function

I am trying to add Python scripting capability to my Qt application. So far, I have been able to write wrappers for many classes. However, one class has a virtual 'init()' function which can be overridden by a derived class if necessary.
// C++ class
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass()
{
init(); // perform user initialization
}
virtual void init() {}
};
The MyClass constructor calls the virtual function 'init()'. It is intended that when MyClass is sub-classed that the init function can be overridden if necessary to provide user initialization.
I wish to use 'MyClass' in PythonQt so I have written a wrapper class:
class MyClass_Wrapper : public QObject
{
Q_OBJECT
public Q_SLOTS:
// add a constructor
MyClass* new_MyClass() {return new MyClass(); }
void init(MyClass* mc) { mc->init(); }
};
The class 'MyClass' is now registered with PythonQt:
PythonQt::self()->registerCPPClass("MyClass",NULL,"myModule", PythonQtCreateObject<MyClass_Wrapper>);
Now, I create a Python script to use 'MyClass':
import myModule
Class TestClass(MyClass) :
def init(self):
# init() is called exactly once at the beginning to do
# any necessary configuration.
print "Inside init(self)"
.
.
.
return
The problem is that the print statement (and the following code) is never executing. The init() function is not being overridden by the PythonQt code.

Related

Google Mock and protected copy constructor

I have a class with a protected copy constructor:
class ThingList
{
public:
ThingList() {}
virtual ~ThingList() {}
std::vector<Thing> things;
protected:
ThingList(const ThingList &copy) {}
};
I have another class the uses this one:
class AnotherThing
{
public:
AnotherThing()
{
}
virtual ~AnotherThing() {}
void DoListThing(const ThingList &list)
{
}
};
and a Mock version of this class:
class MockAnotherThing : public AnotherThing
{
public:
MOCK_METHOD1(DoListThing, void(const ThingList &list));
};
I want to call this method DoListThing with a real argument to supply a real list:
TEST(Thing, DoSomeThingList)
{
MockThing thing;
ThingList list;
MockAnotherThing anotherThing;
list.things.push_back(Thing());
EXPECT_CALL(anotherThing, DoListThing(list));
anotherThing.DoListThing(list);
}
I get an error compiling this:
1>..\mockit\googletest\googlemock\include\gmock\gmock-matchers.h(3746): error C2248: 'ThingList::ThingList': cannot access protected member declared in class 'ThingList'
Yet if I make a non-Mock call it works just fine:
ThingList list;
AnotherThing theRealThing;
theRealThing.DoListThing(list);
If in the Mock test I call with '_', it works:
TEST(Thing, DoSomeThingList)
{
MockThing thing;
ThingList list;
MockAnotherThing anotherThing;
list.things.push_back(Thing());
EXPECT_CALL(anotherThing, DoListThing(_));
anotherThing.DoListThing(list);
}
However, how can I pass a list in this case? If the list was returned by DoListThing, I could use Return but what do I for an argument that get modified like this?
I was unable to get past a protected copy constructor so my answer was to create a fake (dummy) version of a class and ignore Google Mock. This worked well enough for me to test the class in question. The example I provided here is a simplified version of the bigger package.

AS3 - how to extend this function

This is first class with "gordz()" function
public class Model extends Object implements IModel
{
public static function gordz() : void
{
newobject = gallas.pop();
}
}
Now i try to override the function but i still want that old code is executed... How can i extend this function correctly?
public class LOL extends Model
{
override public static function gordz() : void
{
... //New code + execute old code
}
}
Neither super
You cannot use the super statement in a static method.
nor override
You cannot use the override attribute on any of the following:
[...]
Static methods
can be used in a static method.
Whatever you are trying to do should be accomplished in a different way.

AS3 Inheritance

When 'SubClass' extends 'SuperClass', when it inherit its methods and properties, it creates methods and properties that distinguishes from the 'SuperClass'?
Or if I create an instance of 'SubClass' and I try to modify a property, that was inherited from 'SuperClass', am I modificating the super class property also?
Thanks.
EDIT
package {
public class SubClass extends SuperClass {
public function SubClass() {
trace('superclass value n='+superClass.n+'\n');
trace('subclass changes inherited n'+'\n');
n = 3;
trace('subclass value n='+n+'\n');
trace('superclass value n='+superClass.n+'\n');
}
}
}
Returns me:
superclass value n=-1;
subclass changes inherited n;
subclass value n=3;
superclass value n=3;
I will explain it in short.
We have two classes - Subclass and SuperClass.
SuperClass have four methods:
private function methodPrivate():void;
protected function methodProtected():void;
public function methodPublic():void;
internal function methodInternal():void;
From the Subclass you:
Cannot access methodPrivate():void;
Can access methodProtected():void; but just like your private method, it means, you cannot access it from outside of Subclass.
Can access methodPublic():void; and everything can access if from outside of Subclass also.
methodInternal():void; is available for classes from the package of SuperClass.
You can however override these methods. Overriding doesn't change a methods of SuperClass but change them only in SubClass.
override public function methodPublic() : void {
// your additional code
super.methodPublic(); // eventually calling the same method of SuperClass, you can pass arguments to it also
}
As you know, your SuperClass can also have variables, that also can be public, protected, private or internal. You cannot override them, but you can do this with getters or setters however.
You can access variables that are created as public or protected by using a word "super" like this: super.someVariable .
So everything is up to you, if you want to create a different variables of the same name in SuperClass and SubClass, just declare one as private in SuperClass. If you want to have one variable that SuperClass and SubClass both can access - just declare it as protected or public.
Hope that was clear.
When you create a blank SubClass the extends SuperClass, you are creating a new class that provides the same interface (with the same implementation) to the parent class.
That is to say, if your parent class contains a method doSomething, your SubClass, without ever actually writing it, will have the doSomething method available as well. The one caveat to this is if the method is marked private, in which case the inheriting class, SubClass, will not have access.
package {
public class SuperClass {
public function SuperClass():void {
self.doSomething();
}
public function doSomething():void {
trace("doing something");
}
}
package {
import SuperClass;
public class SubClass extends SuperClass {
public function SubClass():void {}
}
}
Once you have this relationship established, you can decide whether calling doSomething on an instance of SubClass will behave differently than the default implementation, defined in SuperClass. If you want the same behavior, you leave it as is. If you want different behavior, then you override the parent class' method, using the keyword override.
package {
import SuperClass;
public class SubClass extends SuperClass {
public function SubClass():void {}
override public function doSomething():void {
trace("doing another thing instead");
}
}
}
Now something that calls doSomething on an instance of SubClass will get modified behavior. But the default implementation has not been touched. Instanced of SuperClass are not modified by this overriding of a method. Only instances of SubClass will be affected.
This is the same case for properties.
There is one exception to this, and that is static properties. A static property is a property of the class, not of an instance of the class. Static properties are not inherited. A static property looks like this:
package {
public class SuperClass {
public static var i:int = 0;
public function SuperClass():void {
}
public function doSomething():void {
trace("doing something");
}
}
The SubClass class will not have a reference to a static property i. However, a SubClass instance can change the static value of the SuperClass. As an example:
package {
import SuperClass;
public class SubClass extends SuperClass {
public function SubClass():void {}
override public function doSomething():void {
trace("changing something in SuperClass");
SuperClass.i = 1;
}
}
}
Now, the SuperClass's static variable i has a value of 1, instead of 0. In this way a SubClass has the potential (although it is the same potential any code has with the right access privileges) to change the properties of SuperClass.
I hope this helps.

How can I subclass a class loaded from a swf

I want to subclass "superClass" and override one of it's functions.
public class superClass {
protected function f1(...) : Boolean {...}
protected function f2(...) : Boolean {...}
...
protected function f100(...) : Boolean {...}
}
public class subClass extends superClass {
// override f1 in the subclass
protected override function f1(...) : Boolean {...}
}
The problem is that the "superClass" is loaded from a swf. I do not have a reference to the class definition in my project. Is there a way to do something like below?
public class subClass {
private var superClassObject: Object;
// construct from an instance of superClass
public function subClass (s : Object) {
superClassObject = s;
}
private function myF1(...) : Boolean {...}
override InvokeFunction (fname: string, args: Array) : Object {
if (fname == "f1") {
return myF1(args);
} else {
return superClassObject.InvokeFunction(fname, args);
}
}
}
I'm not sure how function call is implemented in AS. What should "InvokeFunction" be?
You should use .swc , not .swf. The class you want to extend must be available at compile time, not at runtime. Anyway if using of swf is mandatory for you, you could use different techniques to 'override' functionality.
Besides inheritance there are other OOP concepts (Composition, Aggregation,etc...) and design patterns(Proxy,Adapter,etc...) for changing functionality of class. You could use them.

Run code before class instanciation in ActionScript 3

I need to run code in a class declaration before its instanciation. This would be especially useful to automatically register classes in a factory. See:
// Main.as
public class Main extends Sprite
{
public function Main() : void
{
var o : Object = Factory.make(42);
}
}
// Factory.as
public class Factory
{
private static var _factory : Array = new Array();
public static function registerClass(id : uint, c : Class) : void
{
_factory[id] = function () : Object { return new c(); };
}
public static function make(id : uint) : Object
{
return _factory[id]();
}
}
// Foo.as
public class Foo
{
// Run this code before instanciating Foo!
Factory.registerClass(CLASS_ID, Foo);
public static const CLASS_ID : uint = 42;
}
AFAIK, the JIT machine for the ActionScript language won't let me do that since no reference to Foo is made in the Main method. The Foo class being generated, I can't (and don't want to) register the classes in Main: I'd like to register all the exported classes in a specific package (or library). Ideally, this would be done through package introspection, which doesn't exist in ActionScript 3.
Do you know any fix (or other solution) to my design issue?
I'm not 100% sure sure if this is what you're after, but have you tried using a Static Initializer?
public class Foo
{
// Static Initializer
{
Factory.registerClass(CLASS_ID, Foo);
}
public static const CLASS_ID : uint = 42;
}
http://life.neophi.com/danielr/2006/12/static_initializers_in_as3.html
You can use compiler options to include class byte code in the resulting SWF or SWC. But you have to compile with MXMLC (or COMPC for SWCs).