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.
Related
Recently I want use all new features of c++(<=c++17) in MQL5 , Or using c++ stl containers , like std::map , std::vector.
I can do something like this:
creating a wrapper on them and compile them , hence using dll in mql5
But it's so bad idea , Can anyone suggest better way? thx for answer.
I know that features like template overloading and more features should be suppoerted by meta trader code base , but they are so slow in this way , I wanna use new features in indirect way now, if it's possible.
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/
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.
Is there a way to implement functionality like Class Categories (of Objective-C) or Extension Methods (of C# 3.0) in C and/or C++?
C++ has free functions, but sometimes extension methods work better when you nest many functions together. Take a look at this C# code:
var r = numbers.Where(x => x > 2).Select(x => x * x);
If we write this in C++ using free function it would look like this:
auto r = select(where(numbers, [](int x) { return x > 2; }), [](int x) { return x * x; });
Not only is this difficult to read, but it is difficult to write. The common way to solve this is to create what is called a pipable function. These functions are created by overloading the | pipe operator(which is just really the or operator). So the code above could be written like this:
auto r = numbers | where([](int x) { return x > 2; }) | select([](int x) { return x * x; });
Which is much easier to read and write. Many libraries use pipable function for ranges, but it could be expanded to other classes as well. Boost uses it in their range library, pstade oven uses it, and also this C++ linq library uses it as well.
If you would like to write your own pipable function, boost explain how to do that here. Other libraries, however, provide function adaptors to make it easier. Pstade egg has a pipable adaptor, and linq provides the range_extension adaptor to create a pipable function for ranges as least.
Using linq, you first just create your function as a function object like this:
struct contains_t
{
template<class Range, class T>
bool operator()(Range && r, T && x) const
{ return (r | linq::find(x)) != boost::end(r); };
};
Then you initialize the function using static initialization like this:
range_extension<contains_t> contains = {};
Then you can use your pipable function like this:
if (numbers | contains(5)) printf("We have a 5");
Not really. It's not the C++ way to treat classes like this.
Amongst others, Meyers argue that it's best to have a small class with the minimal set of operations that make it fully useful. If you want to expand the feature set, you may add an utility namespace (e.g. namespace ClassUtil) that contains non-member utility functions that operate on that minimal class. It's easy to add functions to a namespace from anywhere.
You can check a discussion on the subject here.
C++ doesn't have sealed classes or single class inheritance, so in most cases you can subclass the base class. There are creative ways to make a class non-inheritable, but they are few and far in between. In general, C++ doesn't have the problems C# does that gave birth to extension methods.
C is not Object Orientated, so the question doesn't really apply.
With regard to C#'s extension methods: Not directly. C++ has less need for these things because C++ supports free functions. I've never used Objective-C so I can't comment there.
Can you use an interface? Extension methods are an easy way to avoid subclassing, but they are rendered semi-useless when proper OO techniques are used. The reason that they are used with Linq so much is so that the VS team did not have to go and update code that would most likely break a lot of legacy applications.
Per MSDN:
"In general, we recommend that you implement extension methods sparingly and only when you have to. Whenever possible, client code that must extend an existing type should do so by creating a new type derived from the existing type."
http://msdn.microsoft.com/en-us/library/bb383977.aspx
What's the best way to output the public contents of an object to a human-readable file? I'm looking for a way to do this that would not require me to know of all the members of the class, but rather use the compiler to tell me what members exist, and what their names are. There have to be macros or something like that, right?
Contrived example:
class Container
{
public:
Container::Container() {/*initialize members*/};
int stuff;
int otherStuff;
};
Container myCollection;
I would like to be able to do something to see output along the lines of "myCollection: stuff = value, otherStuff = value".
But then if another member is added to Container,
class Container
{
public:
Container::Container() {/*initialize members*/};
int stuff;
string evenMoreStuff;
int otherStuff;
};
Container myCollection;
This time, the output of this snapshot would be "myCollection: stuff = value, evenMoreStuff=value, otherStuff = value"
Is there a macro that would help me accomplish this? Is this even possible? (Also, I can't modify the Container class.)
Another note: I'm most interested about a potential macros in VS, but other solutions are welcome too.
What you're looking for is "[reflection](http://en.wikipedia.org/wiki/Reflection_(computer_science)#C.2B.2B)".
I found two promising links with a Google search for "C++ reflection":
http://www.garret.ru/cppreflection/docs/reflect.html
http://seal-reflex.web.cern.ch/seal-reflex/index.html
Boost has a serialization library that can serialize into text files. You will, however, not be able to get around with now knowing what members the class contains. You would need reflection, which C++ does not have.
Take a look at this library .
What you need is object serialization or object marshalling. A recurrent thema in stackoverflow.
I'd highly recommend taking a look at Google's Protocol Buffers.
There's unfortunately no macro that can do this for you. What you're looking for is a reflective type library. These can vary from fairly simple to home-rolled monstrosities that have no place in a work environment.
There's no real simple way of doing this, and though you may be tempted to simply dump the memory at an address like so:
char *buffer = new char[sizeof(Container)];
memcpy(buffer, containerInstance, sizeof(Container));
I'd really suggest against it unless all you have are simple types.
If you want something really simple but not complete, I'd suggest writing your own
printOn(ostream &) member method.
XDR is one way to do this in a platform independent way.