I have this WRL declaration (taken from Grayscale filter from samples provided by MS):
class CGrayscale: public RuntimeClass<
RuntimeClassFlags<WinRtClassicComMix>,
IMediaExtension,
IMFTransform
>
How do I convert that class to C++/CX?
The reason I need to do so is because in the same DLL I have classes defined using C++/CX which automatically under the hood generates class factory for these classes (The DllGetActivationFactory export from my dll). That automatic C++/CX class factory declaration conflicts with class factory declaration that I have to do for a runtime class defined by WRL.
So, the easiest way it seems to convert that WRL declaration to C++/CX. If it's impossible to define that class using C++/CX, what can I do to make WRL and C++/CX defined runtime classes to co-exist (e.g. be discoverable) in the same dll?
Related
My C++ class:
class Base {
public:
virtual void foo(int);
};
In the python module, I have a class that derives from above and overrides foo.
class PyDerived(Base):
...
def foo(self):
...
Then I create an object of this derived class using a factory method that is defined in python module like this:
def createObject():
m = PyDerived()
return m
With this PyObject in C++ code, I want to call foo and I want foo in python module to be executed. Is this possible? If so how?
(I already tried calling virtual methods from python which dispatch the actual call to a C++ method, but that does not match my requirements)
Yes, you need to enable the SWIG "directors" feature for the class Base containing the virtual method. You can read about it in the documentation here.
The documentation will tell you that you need to add two things to the SWIG interface file:
At the very beginning, edit the %module directive to enable the use of directors at all:
%module(directors="1") your_modulename
Before the declaration of the class Base or the corresponding %include directive, put the following to enable the directors feature for that class:
%feature("director") Base;
For example "Using System.Console". Here "System" is the namespace and "Console" is the class"
Okay that makes sence but what about directives such as "System.IO.Compression".
In the above example would "Compression" be the method?
In visual studio projects why does the IDE add using system; and then using system.console?
Would using system automatically call all the classes in the namespace anyway making the using system.console redundant?
System, System.Console, System.IO, System.IO.Compression are all namespaces.
namespace System
{
class Foo1{}
namespace Console
{
}
namespace IO
{
class Foo2{}
namespace Compression{}
}
}
Compression namespace is nested inside IO, and all nested inside System
If you Using System, you only can access class Foo1(), in the other words, Using... only access class and variable, not the namespace that nested inside
***EDIT: This article defines quite clearly about namespace: https://msdn.microsoft.com/en-us/library/dfb3cx8s(v=vs.140).aspx
I'm writing a program that has a portable C++ backend that we want to create a WinRT Xaml frontend for. I want to keep our models in pure C++ code and create a view in C++/CX that mirrors the C++ model that we can pass around and consume on the frontend. Is there any standard method for this?
For example:
class Person
{
public:
const std::wstring GetName() const;
void SetName(const std::wstring& value);
private:
std::wstring m_Name;
}
ref class PersonMirror : INotifyPropertyChanged
{
public:
property Platform::String^ Name
{
Platform::String^ get();
void set(Platform::String^ value);
}
private:
std::shared_ptr<Person> m_Person;
}
Some things I don't know how to do in this are:
How I get the associated ref mirror class from the native class. can I convert a void* to a PersonMirror^ and talk to the GC about holding a reference to the object?
Are there C++ implementations for triggering events? Or I guess if I can maintain that this is a 1-to-1 relationship, I can just use a function pointer perhaps?
You cannot simply convert a void* to a PersonMirror^. You have to use "ref new" to create a WinRT instance of the class and then copy the data (or a pointer to it) from Person into the PersonMirror. You can have a PersonMirror constructor that copies individual fields from the ISO C++ class, or you can just copy a shared_ptr as you have done, and provide public accessors for it in your ref class. This is a nice approach. Note: There is no "GC" in C++/CX; a ref class is basically just a smart pointer.
• You can use whatever eventing mechanism you like between a ref class and an ISO C++ class in the same process. The only restriction is that any public types in the ref class must be WinRT compatible. To expose a callback or function object event handler in PersonMirror, give it internal accessibilty so that it is invisible to the WinRT interface, but your ISO code can still access it.
The Reversi sample on MSDN demonstrates an ISO C++ class that is wrapped by a C++/CX wrapper class.
I'm using SWIG to generate a Java JNI wrapper for my DLL.
As part of it, I want to generate a custom exception class, but I want to provide the complete Java class implementation for my exception class myself.
I can of course just put my Java class in a separate file, but is it possible to embed such a hand-rolled Java class into a SWIG script?
Unless the class is an inner class of some sort you're pretty much left with writing it as a separate file since that's what it needs to be when you come to compile the Java.
I'm slightly puzzled why you would want to write your own pure Java exception class though - the normal thing to do with SWIG would be derive from std::exception, even if it's through a %inline directive and merge the C++ exception hierarchy with the Java one naturally and for free.
There is a workaround you could use if you really want (although I personally would neverdo it) to generate a pure Java class from the SWIG interface though:
%module test
%nodefaultctor MyException;
%typemap(javabody) MyException %{
// Java stuff goes here (or in javacode typemap)
%}
%typemap(javafinalize) MyException ""
%typemap(javadestruct) MyException ""
struct MyException {};
Which generates:
public class MyException {
// stuff goes here
}
But since that is clearly an ugly hack I'd strongly recommend avoiding it entirely and just writing the class like normal in your source distribution.
Please retag this question to include languages to which it is relevant
So my java book had a whole chapter on nested classes, but ended on the note that you should only really use them when it comes to "modeling composition relationships and implementing internals of a class you want to hide". So lets discuss when you would want to use nested classes and some examples.
A nested/inner class is just a class that's only ever used specifically in the context of another class, which doesn't have it's own class file. If it's linked to an instance, it can only be instantiated in the context of a parent class instance; it can see private data, or only private static data if it's a static class.
The java developer site has a nested classes tutorial with one example:
http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
A couple examples of usage:
Hide a concrete implementation of an
interface:
(Thinking of a database session for a tool like Hibernate): Suppose you have a Session interface, and a SessionFactory which returns an instance of a Session. The SessionImpl concrete class that implements the Session interface could be an innner class of the factory that knows how to construct and initialize it.
Supply information by implementing an
interface:
In the Wicket web framework, each GUI component has an associated "model", whose job is to wire data to the component. The interface looks something like:
public interface IModel extends IDetachable {
public Object getObject();
public Object setObject();
}
Suppose you have some special logic to retrieve data for a custom GUI component that you've written. Since no other component retrieves data the same way, you could use an anonymous class at the point where the IModel is supplied to take care of the data retrieval. If you have another point in the same class where you need to reuse your IModel implementation, you could make it an inner class. Later, if you need the model elsewhere, you could convert it to a top-level class.
Generally you use an inner class in a situation where you need a class definition, but that class is only usable or only makes sense in the context of the parent class.
A real life usage i had with nested classes, was in a global settings object.
The parent class was a Singleton, with nested classes as settings categories.
Settings
File settings
Print settings
Etc.
There was no real point in making the inner object as separate classes, as their would be no use for them outside the settings class scope.
I use nested classes for encapsulating algorithms that would be usually done as a method with lots of arguments. I use class that has raw data and I put algorithms into separate file in nested class (using partial keyword). That way I can put properties for that algorithm and its (working) data lives after algorithm is done.
I know that can be easily done without nested classes but this feels right because algorithm is purposely built for parent class.
public partial class Network
{
partial void initFDLF()
{
fdlf=new FDLF(this);
}
public FDLF fdlf;
public class FDLF
{
internal bool changed=true;
internal bool pvchange=true;
public double epsilon = 0.001;
public bool fdlfOk=false;
public void init(){...}
public void run(){...}
...