I know that dynamic libraries can be loaded by Poco::SharedLibrary as below:
using PocoLibraryPtr = std::shared_ptr<Poco::SharedLibrary>;
poco_library = PocoLibraryPtr(new Poco::SharedLibrary(library_path));
I see in the code that the class is instantiated directly after loading. Is this feasible? Currently this code works fine.
template <typename ClassObject, typename Base>
class ClassFactory : public AbstractClassFactory<Base> {
public:
ClassFactory(const std::string& class_name,
const std::string& base_class_name)
: AbstractClassFactory<Base>(class_name, base_class_name) {}
Base* CreateObj() const { return new ClassObject; }
};
When the class is loaded, then the code use
classobj = factory->CreateObj();
to create the class. I did not find similar usage online. Is there any code I didn't notice?
It looks like you want a shared library to create an object using a templatized factory class.
Template instantiations are static polymorphic so the factory object needs to be determined at compile time.
In other words, no, that won't work.
If you specifically want Poco SharedLibrary help, take a look at the Poco documentation for more help: https://pocoproject.org/docs/Poco.SharedLibrary.html.
Or look at the slides here: https://pocoproject.org/slides/120-SharedLibraries.pdf
Related
Say I have a class:
class Icons {
static const IconData threesixty = IconData(0xe577, fontFamily: 'MaterialIcons');
}
now I have a string variable with value of "threesixty":
String fieldName = "threesixty";
how can I get the value of the threesixty in Icons class by the fieldName variable?
I am using the reflectable package and already have used other features of ClassMirrors in flutter but don't know how to do this.
What you want requires use of reflection. In flutter reflection is not supported because of tree shaking. Tree shaking is the process of removing unused code from your app package (apk, ipa) in order to reduce the package size. When reflection is in use all code can be used implicitly so flutter won't be able to know which parts of code to get rid of, so they opted to not support reflection (mirrors in dart context).
You should try to solve your problem with inheritance if possible, or depending on your specific problem you can try to utilize static code generation.
Edit: You can invoke a static getter with reflectable like this;
import 'package:reflectable/reflectable.dart';
class Reflector extends Reflectable {
const Reflector() : super(staticInvokeCapability);
}
const reflector = const Reflector();
#reflector
class ClassToReflect {
static double staticPropertyToInvoke = 15;
}
Main.dart
import 'package:reflectable/reflectable.dart';
import 'main.reflectable.dart';
void main() {
initializeReflectable();
ClassMirror x = reflector.reflectType(ClassToReflect);
var y = x.invokeGetter('staticPropertyToInvoke');
debugPrint(y);
}
P.S. main.reflectable.dart is the file generated by reflectable package.
As far as I know this is not possible unless using a mirror library.
See:
Get access to an object's property using bracket notation in dart
We are in the process of converting C# code to C++, but we need to do so in phases. I am at a point now where I need to instantiate several native objects from within managed code. These native objects I cannot change, and their declaration looks like this:
public class NativeA();
public class NativeB(std::shared_ptr<NativeA> obj);
Both NativeA and NativeB need to be instantiated from managed code as:
void main() {
ManagedA ObjectA = gcnew ManagedA();
ManagedB ObjectB = gcnew ManagedB(ObjectA);
}
The problem comes in with getting the shared_ptr of NativeA in the constructor of NativeB. Niether NativeA nor NativeB will be manipulated in managed code, they just need to be instantiated. Ideally, something like this:
public ref class ManagedA {
public:
ManagedA() { _object = new NativeA(); }
~ManagedA() { delete _object; }
NativeA * Get() { return _object; }
private:
NativeA *_object;
};
public ref class ManagedB {
public:
ManagedB(ManagedA^ objectA ) {
_object = new NativeB(std::make_shared<NativeA>(*objectA->Get());
}
~ManagedB() { delete _object; }
private:
NativeB *_object;
};
But, this is not allowed in c++/cli because native types are declared as private. Defining #pragma make_public(NativeA) does not solve this either.
My intent is not to work with the native objects in managed code, they just need to be instantiated, so I really don't care about trying to marshal the native pointers and deal with .NET GC if I don't have to, and I don't want to perform a copy. I just want to wrap the classes in order to pass them around.
Is there a clean and simple way to do this?
It appears that the answer was not due to a syntax or usage problem. The two managed objects were in different DLLs and could not be passed across them via .NET. Once the code was compiled in the same project, the issue was resolved.
Although the error message indicated the problem was an accessibility issue in VS 2015, and because it reported it during the link phase, I suspect the cause was because the linker would not have known about the implementation of the NativeA in NativeB without declaring an extern. Being wrapped in CLR, it surfaced as a different issue.
I am having problem with getting the class names of all classes which are inherited from a base class.
Class Base
{
};
Class A:public Base
{
};
Class B:public Base
{
};
Class C:public Base
{
};
.....so on. My requirement is that I want to know all the class names which are inherited from class Base. Is there any way or method from base class that I can know all the classes? Thanks in advance.
Qt allows to do this with the following limitations:
All involved classes should be derived from QObject.
Each class should be registered in a global class list. Qt doesn't provide a way to get full list of classes.
Meta information about a QObject-derived class is stored in a QMetaObject that can be accessed via T::staticMetaObject. First of all, you want to have a list of all metaobjects in your application. You can use something like this:
QList<const QMetaObject*> global_list;
template<class T> register_class() {
global_list << &T::staticMetaObject;
}
//execute the following at program initialization
register_class<Base>();
register_class<A>();
register_class<B>();
register_class<C>();
To get list of class names derived from Base, you need to:
Get const QMetaObject* of the target class as &Base::staticMetaObject.
Iterate over global list of metaobjects and check if QMetaObject::​superClass returns target meta object. You may need to perform recursive check if indirect inheritance should also be detected.
Get names of found subclasses with QMetaObject::​className.
I've got an object declared and instantiated in my Flex application's singular MXML file:
public var CDN:CDNClass = new CDNClass;
I would like to access this same CDN object (and its public methods and properties) in another class declared in a separate .as file as such:
package my.vp
{
import my.media.CDNClass;
public class SyncConnectorManager
{
private function syncMessageReceived(p_evt:SyncSwfEvent):void
{
switch (p_evt.data.msgNm)
{
case "startStream" :
// Play a stream
CDN.parsePlayList(p_evt.data.msgVal);
break;
But when I try to access the public method parsePlayList in the CDN object in a method in the class defined in the .as file, I get the following error:
Access of undefined property CDN
The reason I want to do this is to break up the logic of my application into multiple AS files and have minimal MXML files, probably only one.
Thanks - any help is much appreciated. Perhaps my OOD/OOP thinking is not correct here?
IT depends on your class architecture. For your code to work, the CDNClass instance must be defined and implemented inside your SyncConnectorManager.
Generally, you can always call down into components, but should never call up
One option is to pass the instance ofCDNClass to a variable inside SyncConnectorManager. Add this variable to your SyncConnectionManager class:
public var CDN:CDNClass = new CDNClass;
And at some point do this:
syncConnectorManagerInstance.CDN = CDN;
That way both classes will have access to the same CDN instance and can call methods on it.
Yes, your OOP thinking is not correct here. You should take in mind differences between classes and instances. This line declares a filed in a current class and initiates it with an instance:
public var CDN:CDNClass = new CDNClass;
So current instance of your MXML class (you can think about it as usual AS class with some other notation) has public field. To operate with CDN instance you need something from the following:
Read the value of CDN (as far as it is public) from the instance of your MXML class. You need some reference to it for that.
The instance of your MXML class can have a reference to the instance of SyncConnectorManager and SyncConnectorManager should have a way to inject the value of CDN there. Something like:
Your class:
package my.vp
{
import my.media.CDNClass;
public class SyncConnectorManager
{
private var CDN:CDNClass;
public function SyncConnectorManager(CDN:CDNClass)
{
this.CDN = CDN;
}
private function syncMessageReceived(p_evt:SyncSwfEvent):void
{
switch (p_evt.data.msgNm)
{
case "startStream" :
// Play a stream
CDN.parsePlayList(p_evt.data.msgVal);
break;
In your case SyncConnectorManager class hasn't CDN declared (the problem of the compiler error you mentioned) and instantiated (the problem of NPE even if you just declare field).
As the bottom line I can suggest you to follow ActionScript naming and coding conventions to talk other people and team members about your code :)
Suppose I have the following simple wrapper of a NativeClassInstance.
public ref class Wrapper
{
private:
NativeClass *_wrapped;
public:
Renderer()
{
_wrapped = new NativeClass();
}
~Renderer()
{
delete _wrapped;
}
operator NativeClass*()
{
return _wrapped;
}
}
Now, I want to create an instance of Wrapper from C# with Wrapper wrapper = new Wrapper() and use it in another native functionalities wrapper that resides in another assembly with Helper.Foo(wrapper) (nothing strange having other functionalities not directly related to the wrapped classes in another assembly, IMO):
// Utilities is in another Assembly
public ref class Helper
{
public:
static Foo(Wrapper ^wrapper)
{
// Do something in native code with wrapper->_wrapped
}
}
The results with the implicit user conversion is:
candidate function(s) not accessible
If I make _wrapped public it is:
cannot access private member declared in class ...
Now, I've learnt that native type visibility is private outside of the assembly. So, how I'm supposed to use the wrapped entity in native code outside the assembly it's defined? I've read of make_public but you can't use with template types so it seems very limiting in the general case. Am I missing something? Is there a more correct solution?
I haven't been able to successfully expose native types using make_public, however a solution I have used is to put NativeClass in its own native DLL and then a) reference the native DLL from both assemblies; and b) pass the pointer to the native class around as an IntPtr.
Under the above scenario, instead of having an operator NativeClass* you might use a property such as
property IntPtr WrappedObject {
IntPtr get() { return IntPtr(_wrapped); }
}
You then retrieve NativeObject in you helper assembly by
static void Foo(Wrapper ^wrapper)
{
NativeObject *_wrapped
= static_cast<NativeObject*>(wrapper->WrappedObject.ToPointer());
// ... do something ...
}
If you use make_public, your solution of making _wrapped public should work (it would obviously be best to make a public accessor instead). Regarding your comment "I've read of make_public but you can't use with template types so it seems very limiting in the general case." I agree--read here for the workaround I used:
http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/b43cca63-b0bf-451e-b8fe-74e9c618b8c4/
More related info:
Best workaround for compiler error C2158: make_public does not support native template types
Good luck!