C++ 'object'? alike the .NET's and Java's object - c++

In C# you can write the below and if the type is correct it just works. Is there something like that which exist in C++?
object o = anything;
...
var anything2=(Anything)o;

Maybe boost::any is what you are looking for? It is not quite the same but might be applicable for your particular scenario

Avoid using object use interface or templates instead. Which is the reason you need something like that?? In case if you need to store a group of objects in the same list (for example) or something like that then all of your objects probably has something common. So all of them should implement an interface and your list will be like ( std::list< IMyObject* > ).

If you want a type that is a pointer to anything, then that would be void*.
The difference is that in C#, you can safely convert (almost) anything into a reference. In C++, it's not that simple and if you have something that's not a pointer, you can't just convert it to void* and expect it to work.
But, I try to avoid using object in C# whenever possible. And the same applies to void* in C++. Try to use the type system, not work around it.

Related

How to store get a correct Glib::RefPtr to a non-widget using Gtk::Builder

It is rather simple to get a Glib::RefPtr to any widget by using get_widget function of Gtk::Builder, but when it comes to getting other objects the corresponding get_object function returns Glib::Object, which is not easily convertable to the needed class (such as Gtk::TreeView).
What is the appropriate way of doing that?
It would be best to use Glib::RefPtr<TheDerivedype>::cast_dynamic(object) .
However, Gtk::TreeView (which you mention in your question) is a Gtk::Widget, so you would use get_widget() instead of get_object().
If you meant, Gtk::TreeModel, well, defining GtkTreeModels in Glade, for use in gtkmm C++ code, is something that might work since we added some fixes for that in gtkmm recently:
https://bugzilla.gnome.org/show_bug.cgi?id=742637
But it's not something that we generally expect to work - many C++ developers would prefer the static type safety of defining the DataModel structure completely in code, and not relying on a translation between C and C++ types. For instance: https://developer.gnome.org/gtkmm-tutorial/stable/sec-treeview-model.html.en#treeview-model-liststore
Glib::RefPtr has a static template function which allows one to do what is needed. This function is logically called cast_static.
The sample code can be:
treeStore =
Glib::RefPtr< Gtk::TreeStore >::cast_static( builder->get_object("treestore1") );

C# Linq expression in C++

I am thinking to create a Linq library in C++ to do things like theVector.select( ... ), theVector.where(...).
I am wondering if there is a way to extend a C++ class that have already been declared ( like std::vector ) I would like to be able to do :
theVector.select(...)
instead of
myClass( theVector ).select(...)
Is there anyway way to accomplish this like in C# ?
Thanks,
The C++ community seems to be moving towards non-member functions for this type of "utility thing not directly related to the class at hand". e.g. if you have a std::vector v; you could always v.begin(), but now you can also std::begin(v). This would imply something like select(theVector)....
But, you might want to support joins at some point, so I would consider
select(...).from(theVector).join(theMap, ...).where(...)
// or
select(...).from(theVector).order_by(...)
// etc.

C++ Multi type map like PHP stdClass

I'm using the libjson for parsing a JSON file in C++. I was wondering if you could do something like a PHP style notation for a map:
Just some pseudo code:
mapObj["id"] = 4;
mapObj["tags"] = vector {"Foo", "Bar"};
structMapObj = {
{"name", "FooBar"},
{"size", 1234567},
{"date", "2014-12-24"}
};
mapObj["file"] = anotherMapObject;
// for the vector
mapObj["tags"][0];
mapObj["tags"][1];
mapObj["tags"].size();
mapObj["tags"].pushBack("Foo");
// for the map
mapObj["file"]["name"]
...
Is it possible to receive a result like this?
Maybe an enum for the current type in the BaseClass?
myObj["key"].getType; // returns a 1 for example an INT
I tried to make it with a BaseClass and a template class, but I wasn't able to iterate through the object. Or should I even overload the operators for my BaseClass? Or is it necessary to inherit the BaseClass for each case (a class for the map-type object, a class for the int-type, for string and so on)?
I'm a a little bit desperate right now. Just need someone who leads me into the right direction :-P
PS: I don't want to use boost :-/
Thank you very much,
Daniel
I appreciate that you don't want to use boost. However, this problem has been solved in boost.
You are essentially wanting a map of strings to variants.
Have a look at the source code for boost::variant and boost::any. Take particular note of how boost gets round the problem of recursive definitions, for example when you want to store a map inside an element of another map.
This will teach you more than you ever wanted to know on this subject :-)
There is an open source project for C++Builder programmer called JSonCBB library. This library provides a semantic like to your need: http://www.cbuilderblog.com/jsoncbuilderblog-class-library/

How to cast const FB::variant& into user defined class in c++?

I am tryintg to cast const FB::variant& sample into SampleJS* in C++.
like this:
SampleJS* info = sample.cast<SampleJS*>();
i do not know what is going wrong here.
this gives me error of:
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_any_cast> >
Thank you in advance.
You need to understand that FB::variant just stores whatever type is put into it. Your code will absolutely work... if what is inside happens to be a MouseInfoJS*. However, you'd have to go through a lot of work to put something like that inside, since the FB::variant class is designed to make it difficult, but not impossible, to store inside it types that it doesn't know.
So basically, what you're trying to do probably doesn't make any sense, so you can't do it.
Depending on what type MouseInfoJS is, it might sorta make sense. Does MouseInfoJS inherit from JSAPI? (or JSAPIAuto?) If so, then it would somewhat make sense to try what you're doing, except that you'd never have a MouseInfoJS*, you'd have a MouseInfoJSPtr, which would be a typedef for boost::shared_ptr, since it would then be partially owned by the page and thus dangerous to store a raw pointer there.
Even in this case, which you'd need to use .convert_cast<MouseInfoJSPtr>, not .cast, it won't work on most modern browsers because they wrap the NPObject returned by FireBreath in another object which doesn't allow us to get the original object back; I believe this is a security feature. For more information, see A firebreath JSAPI will not be recognized in firefox

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.