ACE C++ Library class ACE_TASK doesn't have any activate function? - multithreading

I have seen how to launch threads in my project, where we are using ACE library . But I have a question when ACE_TASK doesn't have any activate function how can you call it in class derived from MyClass class . The activate function is in ACE_TASK_BASE class which is derived from ACE_TASK . But MyClass class and ACE_TASK_BASE doesn't have any direct relation ?
class MyClass: public ACE_Task< ACE_MT_SYNCH > {
public:
//derived from ACE_Task
virtual int open( void *arg = NULL );
//derived from ACE_Task
virtual int svc();
};
//then we are calling
this->activate(); // ?????
in open(); //running our job in
int svc() {
while( _running )
....
}

You described how activate() is available in MyClass - it inherits from ACE_Task which inherits from ACE_Task_Base. MyClass has access to all the public and protected methods in the inheritance chain.

Related

How to create extension method like C#

I want to attach public method to the class.
This is called extension method in C#.
package extensionMethods
class A {
def testA() {}
//def testB() {} Need to add a public method to this class A but we don't have access to the class
}
class B {
def test() {
def a = new A();
a.testA()
a.testB() //Need to add a public method to the Class A without defining the method in the class A
}
}
//In C# way -> Extension method
class C {
/* void testB(this A a) {
}*/
}
How can we achieve the similar approach in Groovy?
In the above example I want to attach method testB() to class A
You will want something like this:
package something
class SomeExtensionClass {
static void testB(A self) {
// ...
}
}
Then your META-INF/services/org.codehaus.groovy.runtime.ExtensionModule extension descriptor...
moduleName=Test module for specifications
moduleVersion=1.0-test
extensionClasses=something.SomeExtensionClass
See http://groovy-lang.org/metaprogramming.html#_extension_modules for more info.

QtConcurrent::run with a virtual class member

So I'm trying to encapsulate a timer class which will handle all of the gory details of multi-threading and timers.
Here's my code:
TimedEvent.h
class TimedEvent : public QObject
{
Q_OBJECT
public:
explicit TimedEvent(QObject *parent = 0);
TimedEvent(const int intervalInMsecs);
virtual void TimeoutWorkProcedure() = 0;
private slots:
void TimeoutWorkThread();
protected:
QTimer *myTimer;
};
TimedEvent.cpp
TimedEvent::TimedEvent(QObject *parent) :
QObject(parent)
{
}
TimedEvent::TimedEvent(const int intervalInMsecs)
{
// Create timer
//
myTimer = new QTimer(this);
// Connect the timeout signal to our virtual callback function
//
connect(myTimer, SIGNAL(timeout()), this, SLOT(TimeoutWorkThread()));
myTimer->start(intervalInMsecs);
}
void TimedEvent::TimeoutWorkThread()
{
QtConcurrent::run(this, &TimedEvent::TimeoutWorkProcedure());
}
The idea was TimedEvent would be a base class and I would be able to create derived classes very easily.
class MyClass : public TimedEvent
{
public:
MyClass( const int timeoutInMsecs );
TimeoutWorkProcedure(){ do some background stuff };
};
The problem is I cannot figure out what to pass to the QtConcurrent::run call. Not sure this is even possible. I could move the QTConcurrent::run call to the derived class, but I anticipate there being several of these derived classes.
Any ideas would be appreciated.
K.
This code:
void TimedEvent::TimeoutWorkThread()
{
QtConcurrent::run(this, &TimedEvent::TimeoutWorkProcedure);
}
is perfectly fine and will do what you expect. It will call an overridden version of TimeoutWorkProcedure.

Local abstract class gives warning C4101

I have to deal with code that can be reduced as follows:
void main ()
{
class MyBase
{
public:
virtual void MyFunc () = 0;
};
class MyClass : public MyBase
{
public:
virtual void MyFunc () {}
};
MyClass x;
}
When compiling under Visual-C++ 2010 with `cl /nologo /Wall text.cxx', I get the following warning:
test.cxx(7) : warning C4101: 'main::MyBase::MyFunc' : unreferenced local variable
I want to keep MyBase in place if possible (because it is deep down inside the code hierarchy).
Is there a way to 'fix' the code in place (other than using #pragma to ignore the warning)? Or is it bad practice to have a local abstract class?
Thanks in advance!

Subclass a C++ abstract class in Java using JNI

I have a C++ library that I have to use in an existing Android implementation. I'm using Android NDK and using the C++ classes via JNI.
However, I am not able to find how to subclass a C++ abstract class in Java using JNI.
Problems I face:
My aim is to provide Java implementation for the virtual methods in C++ by subclassing the abstract C++ class.
I have loaded the native library and I'm trying to declare the native methods.
The C++ methods have keyword 'virtual'. When I declare the native functions in Java after loading the C++ library, 'virtual' is not recognized. What is wrong here?
Any help is appreciated. I'm a newbie to JNI. Thanks in advance.
Let's consider we have a C++ class:
class iVehicle
{
public:
virtual void Run() {}; // not-pure virtual here for simplicity of a wrapper, but could be pure (see the end of the post)
virtual int GetSize() const; // we want to reuse it in Java
};
We want to create a class Bot in Java that extends class iVehicle in the sense that calls to super invoke the C++ code from iVehicle::GetSize() and, from the C++ point of view, we can use the instances of Bot as iVehicle* variables. That's tough since C++ provides no good built-in functionality for reflection.
Here is one possible solution.
To use C++ class in Java we need to generate a Java wrapper, i.e:
class iVehicle
{
public void Run() { Native_Run(); }
public int GetSize() { return Native_GetSize(); }
private native void Native_Run();
private native int Native_GetSize();
// typecasted to pointer in C++
private int NativeObjectHolder;
// create C++ object
native static private int CreateNativeObject();
}
The usage in Java is simple:
class Bot extends iVehicle
{
public int GetSize()
{
if ( condition ) return 0;
// call C++ code
return super.GetSize();
}
}
However, there is a C++ part to this code:
static jfieldID gNativeObjectHolderFieldID;
JNIEXPORT void JNICALL Java_com_test_iVehicle_Run( JNIEnv* env, jobject thiz )
{
int Value = env->GetIntField(thiz, gNativeObjectHolderFieldID);
iVehicle* Obj = (iVehicle*)Obj;
// todo: add checks here, for NULL and for dynamic casting
Obj->Run();
}
The similar code is for GetSize().
Then creating an instance of Java's Bot you have to call CreateNativeObject() and assign the returned value to the NativeObjectHolder field.
JNIEXPORT int JNICALL Java_com_test_iVehicle_CreateNativeObject( JNIEnv* env, jobject thiz )
{
iVehicle* Obj = new iVehicle;
return (int)Obj;
}
So, this is the scheme. To make this work you will need to add the destruction code and to parse C++ classes to generate all this glue code.
Added:
In case where iVehicle is actually abstract you will have to generate a non-abstract wrapper that you are able to instantiate:
class iVehicle
{
virtual void Run() = 0;
}
class iVehicle_Wrapper: public iVehicle
{
virtual void Run() { ERROR("Abstract method called"); };
}
And instantiate iVehicle_Wrapper in CreateNativeObject(). Vuala! You have inherited an abstract C++ class in Java.

Calling protected method in base class via pointer to base class in derived class

I would like to understand why when I call protected method, declared and implemented in base class, from derived class via pointer to base class I get compilation error (C2248) and when I call it from derived class via pointer to derived class instance, compilation pass.
I understand it is part of the language but I want to understand why
My explanation is that when I call a protected member of base class via pointer to base class in derived class, compilation fails because the inheritance of base class can be protected or private but when I call it via pointer to derived class in a derived class it o.k because it is part of the class.
Is that right?
e.g.
class Base
{
protected:
virtual void func() {}
}
class Derived : public Base
{
public:
virtual void myMethod()
{
Base* pBase = new Base;
pBase->func(); -> compilation error (C2248)
Derived* pDerived = new Derived;
pDerived->func(); -> O.K
}
}
The failing line is not compilable because you are accessing an instance of Base - only public methods can be accessed this way. If you do this in myMethod():
Base::func();
it should compile because now we are accessing the inherited method for this. Kind of weird to have pDerived::myMethod() call the Derived constructor?

Resources