I was reading Qt Coding Conventions
and one thing confused me
Things to avoid:
Do not inherit from template/tool classes
What is tool class?
I disagree with Vahid Farahmand answer, a template class is not the same as a tool class.
The documentation for QT tool classes says:
Qt is equipped with a range of capable tool classes, from containers
and iterators to classes for string handling and manipulation. Other
classes provide application infrastructure support, handling plugin
loading and managing configuration files.
These are concrete classes designed to perform a particular function. Template classes provide generic types.
AFAIK it's same. Two names for same thing.
Related
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.
I'm designing (brainstorming) a C++ plugin framework for an extensible architecture.
Each plugin registers an interface, which is implemented by the plugin itself.
Such framework may be running on relatively capable embedded devices (e.g. Atom/ARM) so I can use STL and Boost.
At the moment I've managed to write a similar framework, in which interfaces are known in advance and plugins (loaded from dynamic libraries) register the objects implementing them. Those objects are instantiated as needed by their factory methods, and methods are called correctly.
Now I want to make it more flexible, having plugins register new interfaces (not just implementing the existing ones) thus extending the API available to the framework users.
I thought of using a std::map<std::string, FunctionPtr>, which is also mentioned by several articles and stackoverflow replies I've read. Unfortunately it doesn't seem to capture the case of different method interfaces.
I feel it might have something to do with template metaprogramming, or traits perhaps, but I can't figure out how it should work exactly. Can anyone help?
Try looking at XPCOM which solves these problems for you - by sortof re-implementing COM.
You have the issue of not knowing what interface the plugin provides to your application, so you need a way for the developer to access it, without the compiler knowing what it is (though, if you supply a header file, then suddenly you do know what it is and you can compile it without any need for plugin unknown-interface fanciness)
so, you're going to have to rely on runtime determinism of the interface, that roughly requires you to define the interface in some way so that the framework can call arbitrary methods on it, and I think the only realistic way you can do that is to define each interface as a set of function pointers that are loaded individually and then stored in data for the user to call. And that pretty much means a map of function pointers to names. It also means you can only user compiler niceties (such as overloading) by making the function names unique. The compiler does this for you by 'mangling' all functions to unique, coded names.
Type Traits will help you wrap your imported functions in your framework, so you can inspect them and create classes that work with any imported type, but it isn't going to solve the main problem of importing arbitrary functions.
Possibly one approach that you'll want to read is Metaclasses and Reflection by Vollmann. This was referenced by the C++ standard body, though I don't know if it will become part of a future spec. Alternatively you can look at Boost.Extension
Maybe the first thing you need check is COM.
Anything that can be done with templates, can be done without, though perhaps in a much less convenient way, by writing "template instances" by hand.
If your framework was compiled without seeing a declaration of class MyNewShinyInterface, it cannot store pointers of type MyNewShinyInterface *, and cannot return them to the framework users. No amount of template wizardry can change that. The framework can only store an pass around pointers to some base class. The users will have to do a dynamic_cast to retrieve the correctly typed pointer.
The same is true about function pointers, only functions have no base classes and one will have to do the error-prone reinterpret_cast to retrieve the right type. (This is just another reason to prefer proper objects over function pointers.)
I see that the designer generated UI classes be embedded using any of the following methods in Qt,
Aggregation as a pointer member
Aggregation
Multiple, Private inheritance
but it is said that the second method doesn't support custom slots. Can someone elaborate on this? Why can't we implement custom slots, while using aggregation?
Also, elaborate on the advantages and disadvantages in each of the methods.
I don't think it's true that the second option doesn't support custom slots.
The choice is discussed in the official Qt documentation. See http://doc.qt.io/qt-4.8/designer-using-a-ui-file.html#compile-time-form-processing
However, note that the three approaches presented in this document do not correspond to the three options presented in Qt Creator. The first approach presented in the document, The Direct Approach, is not one of the three choices here -- this approach is not available through the Qt Creator feature which this setting controls. The second option (aggregation, or "the single inheritance approach") is available in two varieties, the slight variation being whether the ui class member is as a data member (the second option) or as a pointer member (the first option).
My preference is the third option, multiple inheritance. This is also the way used throughout C++ GUI Programming with Qt 4 (first edition available for free online), which calls this approach the cleanest. When I'm writing my class, I'm not really thinking in terms of two objects, one with the UI and the rest with the functionality, I'm thinking about just one, and multiple inheritance matches that the best. But the document gives the reasons why "aggregation as a pointer member" is the default.
A QT5 Update:
Nowadays you can support custom slots in more fashions like using the lambda and normal function support for slots (check here).
Also, notice that the recommended option is the Pointer Member, also known as the Pimpl idiom. The advantage is the forward declaration of the UI object will allow faster compilation times for bigger projects and also shared libraries will be easily to package (as stated here).
I'm reading in my text book about virtual functions in C++ and my book doesn't elaborate on what exactly run-time binding is. It doesn't give me any information as to why I would need run-time binding.
Maybe the nice people at SO can provide me with some links or information?
Thanks :]
How about this one? ;D
http://www.google.ca/search?hl=en&source=hp&q=virtual+function+runtime+binding+c%2B%2B&aq=f&aqi=&aql=&oq=&gs_rfai=
In all seriousness though... the first link looks decent.
Here's a preview:
The most prominent reason why a virtual function will be used is to have a different functionality in the derived class. The difference between a non-virtual member function and a virtual member function is, the non-virtual member functions are resolved at compile time.
And from another site:
In large, complex programs, virtual functions allow the programmer to simplify the programming process. If used correctly, the base class will successfully define the interface of its derived classes. However, the program will leave the implementation of this interface up to the derived classes. Therefore, the programmer can use one interface for multiple implementations. This capability also allows for the creation of class libraries, which establish standard interfaces, but allow the programmer to tailor these interfaces to any unique implementation situations that may arise. One of the most popular libraries around is the Microsoft Foundation Classes (MFC) library, which provides the interfaces necessary for programming in the Windows environment. This library frees the programmer from having to reinvent the Windows interfaces, instead allowing him or her to focus on the specific implementation of these interfaces.
The simplest form of run-time binding is polymorphism. In context of C++ polymorphism is achieved through virtual functions. The basic purpose of this is to call methods on instances of derived classes through a pointer or a reference to a base class.
Googling virtual functions should give you plenty of good results on how and why to do this.
Please read Uncle Bobs articles on the SOLID principles of Object Orientated Design: http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod
Obviously, they are not about run time binding but they do describe the type of design problems that you are trying to solve which require to use run time binding.
I think the article on the open closed principle brobably best demonstrates (again, the article isn't about run time binding)when you would need to do this:
http://www.objectmentor.com/resources/articles/ocp.pdf
I am looking for recommendation for object serialization/deserialization library in c++? Which one are the most advanced and open-sourced?
Can it handle
Any class that users defined?
Object hierarchy (parent and child classes)?
A Tree of objects? Class A has an attribute of Class B which has an attribute of Class C?
STL containers? Class A has a vector of Class B?
A cyclic of objects? Class A has a pointer pointing to B which has a pointer to A?
I find boost serialization library. I am not sure what is its limitation from http://www.boost.org/doc/libs/1_42_0/libs/serialization/doc/tutorial.html
It really depends what you're looking for. If you're looking for super-fast speed and rapid development within a library, Boost is awesome. If you're looking for super-fast speed, a little more customizability and cross-library binary compatibility, then Qt is a great solution (not saying that Boost can't be made to do this, too). If you're looking for crazy interoperability, then look for a text-based serialization system like JSON (jsoncpp), YAML (yamlcpp) or XML (way too many), each of which have about 8 billion independent libraries.
Protocol buffers is a library developed and used by Google for object serialization that is cross language. It may be a bit different in concept from what you're describing, but it's worth taking a look at.
The Linderdaum Engine Core (iObject, iStaticClass and clLinker objects) provide the custom RTTI for C++.
The idea behind the serialization there is simple: we use an automated source code postprocessor (LSDC) to generate all the save/load code and the registration for all of the metaclassses and properties.
Any object can be serialized to and from the abstract tree-based markup language script. XML and custom JSON-like (we call it XLML) script is supported.
The implementation details are described in this answer: https://stackoverflow.com/a/10332336/1182653
Any class derived from iObject is supported
Object hierarchies are supported
"Trees" of objects are supported
std::vector-like containers (supporting push_back/size semantics) are supported
Well, the properties are defined explicitly and the "pointer fixup" can be performed in a custom iObject::EndLoad() method (redefined in user classes)