using C++11 attributes - c++

Could you please explain how to get information from attributes in C++? For example, I want to write C++ to python binding. For this I need to annotate all methods in class with some specific binding info. Now I need to generate some code by attributes. Or another example, map class to db entity. Or C++11 attributes is not the same as in Java or C# annotations?

Attributes (a new C++11 feature) are just a standardized syntax for compiler extensions. To do what you want you would need a compiler with the proper extensions. So far, I don't think any compiler even implements attribute syntax, much less any specific attributes for Python bindings.
Because they're intended for compiler extensions, there's no standard way of creating your own attributes, like you can with Java annotations or C# attributes. Of course, a compiler could provide this ability as an extension... ;)

An update with some more recent information:
GCC now (as of 4.8) implements C++11 attributes as an alternative syntax for __attribute__((XXX)).
You can also use the GCC plugin mechanism to define new attributes - see https://gcc.gnu.org/onlinedocs/gccint/Plugins.html.
You can also do this in python using the gcc-python-plugin - see https://gcc-python-plugin.readthedocs.org/en/latest/attributes.html.

Related

Can Dropbox Djinni be used with C++98

I am trying to evaluate Djinni, for generating Java and Obj-C wrappers from our C++ code.
We currently use SWIG and are evaluating other tools for wrapper generation.
Due to some restriction at my work place our C++ code is in C++-98.
We cannot migrate to C++-11 due to some customer needs.
As such I wanted to know if I can use Djinni, with C++-98.
Djinni github readme states:
Interfaces are objects with defined methods to call (in C++, passed by shared_ptr). Djinni produces code allowing an interface implemented in C++ to be transparently used from ObjC or Java, and vice versa.
I saw couple of sample's using Djinni over the internet and they all seem to use shared_ptrs with their interfaces.
Is C++-11 mandatory for using Djinni?
No, Djinni can not be used with C++98. It uses C++11 pretty extensively, both in the generated code and in the support library, so it can't support C++98.

Is there a QPair class, but for three+ items instead of two?

QPair is nice, but what if I need 3 items? Should I just make a struct, or does Qt have me covered?
As QTBUG-22441 indicates, the developers have no intention of adding a Qt analog of std::tuple. (Even though QTBUG-3283 gives us hope that it could be done, it's dated Dec'09, while the newer report, with a WONTFIX, is dated May'15). Thus, you need to fall back to std::tuple or come up with your own data structure.
Quote from Marc Mutz:
A hypothetical QTuple wouldn't do anything differently, anyway, except drain Qt developer resources.
Moreover, the docs for Qt 5 Algorithms module state the following explicitly:
Historically, Qt used to provide functions which were direct equivalents of many STL algorithmic functions. Starting with Qt 5.0, you are instead encouraged to use directly the implementations available in the STL; most of the Qt ones have been deprecated (although they are still available to keep the old code compiling).
So using STL when programming with Qt 5 is officially encouraged, should it become a necessity.
You can create your own structure using Qpair<Qpair<item1, item2>, item3>. Last time I used something like this to achieve what you say.
Note that for all the operations to work properly, you need to override them. The first item is a composed item (the item1+item2 pair).
For some simpler cases, you can use a QVector<QVariant> or QList<QVariant>, provided you only use QVariant-supported data types.
An interesting note in the docs say that
QVariant can be extended to support other types than those mentioned in the Type enum. See Creating Custom Qt Types for details.
That second link includes how to create a QVariant-storable custom type. There's also the Custom Type Example.

Possible to instantiate a class given class name?

Is it possible to perform reflection in c++, and instantiate a class given the name of it as a string?
Cheers,
There's no language feature that lets you do this. You can, however, write your own set of factory functions and put those in a string-indexed map.
If you can use MFC, it has a object serialization framework that allows you to do this. This is documented here. Just to clarify, MFC can be used for a UI less application and I have seen it being used in this way quite successfully.
If you can't use MFC, you can consider boost serialization library. But to my knowledge it does not provide a factory function that allows you to create classes given their names. However, it does have the mechanisms to dynamically create classes from their names which you may be able to adapt to your unique needs. The relevant doc is here.

Can XSD restriction be platform specific?

I have to specify a restriction for one of xml attribute, I know I can use the syntax below.
But the minInclusive and MaxInclusive depends on platform. How do I specify such syntax? Are they supported in XSD?
Thanks,
Ram
Saxon's XSD processor introduces the idea of validation parameters, which you can specify when invoking the schema processor from the command line or from an application - you can't use them in facets like minInclusive and maxInclusive, but you can use them in xs:assert and other places where XPath expressions are used.
It's good to see that this meets a real requirement, though to be honest, until such a feature becomes widely implemented in other processors it would be a little unwise to base your strategy on it.

How can I pass map<string,string> into py with API?

C/C++ can use python API to load py.
But, only simple type is supported.
How can I pass map into py to be a dict with API?
Or, which methods are better?
Use SWIG, which has some ready-made templates for various STL types. See this, for example.
The Python C API supports C-level functionality (not C++ level one) -- basically, you can easily expose to Python things you could put in an extern C block (which doesn't include std::map &c) -- for other stuff, you need a bit more work. The nature of that work depends on what you're using to wrap your C++ code for Python consumption -- there are many options, including the bare C API, SWIG, SIP, Boost Python, Cython, ...
In the bare C API (which I assume is what you're using, judging from your question and tags), I would recommend making a custom object type -- maybe, these days, one subclassing collections.Mapping (MutableMapping if mutable, of course), as you would when implementing a mapping in Python -- and implementing the Mapping Object Structures plus the needed bits of a general type structure such as tp_iter and tp_iternext slots.
Of course, the key idea is that you'll implement item setting and getting, as well as iteration, by simply delegating to your chosen std::map and performing the needed type conversion and low-level fiddling (object allocation, reference counting) -- the latter is the part that higher-level frameworks for extending Python save you from having to do, essentially, but the "wrap and delegate" underlying architecture won't change by much.