Dynamically typed container in Qt - c++

Is there a container who can store different types (yes, I really need to work with different kind of types) in Qt? I must create a new class to do this? If so, could you give me a hint to create it?

Most of containers in Qt are template based, then you can use them for different static types.
Another option is to use use QVariant, for example: QVector<QVariant> vec;.
A more dynamic solution is to use polymorphism, you can store pointers to a base class and so on.. .
PS: As a general rule, you should avoid this patterns. From Effective C++, by Scott Meyers:
Anytime you find yourself writing code of the form "if the object is
of type T1, then do something, but if it's of type T2, then do
something else," slap yourself.

Related

Store any value and a value of a specific range of classes

I have a class that looks something like this:
class container{
private:
std::vector<physical_component> physical;
std::vector<storage_component> storage;
--some other stuff not relevant--
public:
--constructors, getters setters, methods to add to the vectors etc--
}
Now I am struggeling with making the physical_component and storage_component classes since I dont know a proper datatype to handle this sort of thing.
Physical_component should be able to:
Store a set amount of types, and fully retaining a type (something I can cast to is good enough)
Should store the objects in a way that makes them individual from the ones passed (and therefore secure from changes to the orignial class)
I remember something like that excisting in c alongside enum but I dont know the name. Also c++ probably has a better way for that.
Storage_component is supposed to:
Store any type
(optional) remember the original type
I have no idea how to achieve this properly. I saw std::any but it seems to be rather new therefore I dont know if its a good way to go about this. Also I cant make storage_component a template because I cant store it in a vector then
What is the (proper) way to implement these classes?
Store a set amount of types, and fully retaining a type
You probably want std::variant<Ts...> (or boost::variant<Ts...>). It stores one of Ts... at a particular point in time.
Store any type
If all the types share the same interface, use a traditional virtual + std::unique_ptr polymorphism approach. Otherwise std::any is the right choice here.

Use the C++ STL in Enterprise Architect

How is it possible to use parts of the C++ STL in Enterprise Architect?
It would be nice to be able to specify certain class attributes as std::string or use std::auto_ptr (or even std::tr1::shared_ptr) as types.
Another interesting thing would be how one is able to integrate container-types like std::vector and std::map into EA.
I have taught how STL containers look like to EA, I guess it can be extended to stl pointers too:
Forward engineering:
You can define collection classes for different multiplicities globally in the language settings, or for a specific class of your project (this will define how it is "contained" in other classes) this way.
Simple example setting:
Make sure you set the container classes for the target class of the association, not the source. Set the Multiplicity of the Target Role to multiple (different from 0, 0..1, 1 and empty field according to the code template). Also, set the Containment of the Target Role of the association to Value to avoid generating a pointer to a container.
Another, more flexible way would be to modify the code templates in Settings -> Code Generation Templates. I believe there is a way to override the default template for stereotyped connectors, though I never tried. This is probably the only way to generate STL pointers, as collection class definitions are only used by EA for multiplicities bigger than 1.
Reverse engineering:
Go to Tools->Options->Source code engineering->C++ and append to 'Additional Collection Classes' the following string:
vector<#TYPE#*>;deque<#TYPE#*>;list<#TYPE#*>;stack<#TYPE#*>;queue<#TYPE#*>;priority_queue<#TYPE#*>;set<#TYPE#*>;map<*,#TYPE#*>;multiset<#TYPE#*>;multimap<*,#TYPE#*>;
I've never tried, but I assume adding the STL pointers to this is trivial.
Round-trip engineering
I don't know if the above works if you do round-trip engineering. I assume the fact that the definitions are asymmetric will cause issues.
I've been toying with this sort of thing and it's doable... just.
What you'd need to do is reverse-engineer the libraries from source, but since EA does not contain a complete preprocessor, you'll end up with a lot of "You may need to define a language macro" errors. Perhaps actually running the source through a preprocessor first would help.
The other way, of course, is to just add the STL classes as you need them.
As for container types, I'm not sure if EA provides any support for constructions like the Allocator in
template < class T, class Allocator = allocator<T> > class vector;
Simple template classes, however, are defined as a Class with Template Parameters. The easiest way to create an instantiation is to create a new Class, go into its Templates tab and add a Binding to the template Class; this allows you to select values for the formal template parameters.

How to allow your data structure to take in objects of any class - C++

How do I do that? Like you know in Java, you can use an ArrayList and it will take any object as long as you cast it down to whatever it is when you're retrieving the object.
Even better, you can specify what class of objects that ArrayList would store by doing...
new ArrayList()< whateverObject >
I've implemented a linked list data structure in C++ and I'd like to know how I can allow it to do this...
At the moment, I'm just using...
typedef whateverObject ItemType
at the start of my header file for my linked list and then manipulating "ItemType" throughout the implementation of the linked list. So every time I want to change the type, e.g. instead of using the list for storing strings, I want to store an int, I'll have to change the typedef in my linked list's header but I want to be able to simply use it for any object so...
How?!
Thanks.
Templates are the answer to your question.
Define your linked list as follows :
template<typename ItemType>
class ArrayList
{
// What's inside your class definition does not need to be changed
// Include your method definitions here and you'll be fine
};
The type to use is then ArrayList<WhateverObject>.
Use templates. It's a lot to explain so I'll just give you a link where it's explained much better than I'll ever be able to do here: C++ FAQ - Templates.
While you're at it, if you have the time, I suggest you read the whole FAQ, it's really a great resource!
If I have understood well what you ask, templates is what you want.
Take a look here:
http://www.cplusplus.com/doc/tutorial/templates/
In java you can do so, because all classes are inherited from one base class Object. In C++ you do not have it. The reason is that Object base class impose overhead for all objects, while C++ do not like any unnecessary overhead.
If you want to store any object - you can store "void *" data type. The question remained - what you will be able to do with objects, without the knowledge of the type? If you do know - you can cast to the needed type and use it. The practice described above is not safe, and templates are better in most cases.

how to implement objects for toy language?

I am trying to make a toy language in c++. I have used boost spirit for the grammar, and hopefully for parser/lexer. The idea is a toy language where 'everything is an object' like javascript and some implementation of prototype based inheritance. I want to know how to implement the 'object' type for the language in c++. I saw source codes of engine spidermonkey but mostly it is done using structures, also getting more complex at later stages. As structures are more or less equivalent to classes in C++, I hope I could manage with the stdlib itself. All I want is a solid idea of how the basic object has to be implemented and how properties are created/modified/destroyed. I tried to take a look at V8, but its really confusing me a lot!
Have each class have pointers to parent classes and implement properties and methods in STL containers like <string,pointer_fun> so that you can add/remove dynamically methods.
Then you could just lookup a method in an obj, if there isn't then follow the ptr to parent and lookup there till you find one or fail non-existant method.
For properties you could have a template to wrap them in the STL container so that they share a common ancestor and you can store pointers like <string,property<type>* > where property makes created type inherit from common type.
With this approach and some runtime checks you can support dynamically anything, just need to have clear which are the lookup rules for a method when you call it in an object.
So essentially every obj instance in your system could be:
class obj{
type_class parent*;
string type;
std::map<string,pointer_fun> methods;
std::map<string,property_parent_class> properties;
}
And have constructors/destructor be normal methods with special names.
Then in obj creation you could just lookup for type_name in type_objs and copy the member and properties from the type to the impl obj.
EDIT:
About function objects, you can use functors inheriting from a common one to use the container_of_pointers approach.
For lists I'd create a simple class object that implements metods like __add__() or __len__() or __get__() like in python for example, then when you parse the language you'd substitute list_obj[3] for your_list_obj.method['__get__'] after checking that it exists of course.

What is the typical usage of boost any library?

What are the advantages of using boost.any library ? Could you please give me some real life examples ? Why the same functionality couldn't be achieved by having some generic type in the root of object's hierarchy and creating containers with that base type ?
boost::any will happily store ints and floats, types that clearly have no base classes. A real-life example where you can use it is a virtual machine for a high-level interpreted language. Your "function" objects will need an array of arguments. This can easily be implemented with a std::list<boost::any> behind the scenes.
I consider that Boost.Variant should always be preferred as it's non-intrusive and still calls for very structured programming.
But i guess the main idea behind boost.any is to provide the equivalent of java and c# object types. It's a way of saying "yes we can" ! :-)
We've used it in a property map, (std::map<std::string, boost::any>), to store a lot of things dynamically in a simple, flat dataspace.
Mostly we either stored smart-ptr-to-scriptable-objects or strings, but some entries where other types (floats, vec3f, matrices, and other non-standard objects).
It works pretty well for adding more dynamic capabilities to c++, or wherever you want some type-erasure to just add any type of data to an object.
Why the same functionality couldn't be achieved by having some generic type in the root of object's hierarchy and creating containers with that base type ?
That calls an object hierarchy -- a construct you are injecting in artificially in to the design for solving a peripheral problem. Further, such a construct is easy to get wrong and a wrong implementation can wreak havoc. Boost.Any is a community reviewed safe, well-tested alternative.
Could you please give me some real life examples ?
TinyJSON uses boost.Any.
What are the advantages of using boost.any library ?
I refer the introductory documentation.
We use boost.any as the carrier type for a type-safe tagged variadic container. Here's what that means:
We have a "raft" object, which travels through a set of filters. When a filter wants to add data to the raft, it can do something like this:
raft.addTaggedData<ETag1>(3.0);
raft.addTaggedData<ETag2>("a string")`;
std::string str = raft.getTaggedData<ETag2>();
int a = raft.getTaggedData<ETag1>(); // <-- Compile error
Where ETag1 and ETag2 are members of an enum, and we use a traits template to map tags to types.
The raft class is using a list of pair<ETagType, boost::any> as a backing store. Boost.any saved us the pain of managing raw buffers for various types.