![]() |
|
The most useless c++ PIMPL idiom application: turning C++ to Java. |
axilmar
Member #1,204
April 2001
|
In the attachment, you will find the most useless use of the c++ PIMPL idiom ever: a way to turn c++ into Java (well, sort-of). The most important advantages are: 1) automatic memory management via reference counting. 2) implementation details completely hidden from headers. 3) no more ugly pointer indirections (-> and *). 4) increases compilation speed. 5) no need for copy constructors and assignment operators. The disadvantages include: 1) more work, since it requires 3 files per class (one is the class header, one is the class implementation, and one is the internals implementation). It works like this: 1) the root class Object is responsible for managing the lifetime of the implementation objects. 2) Classes inheriting from Object offer additional functionality. 3) polymorphism is part of the implementation classes. What is your opinion on this? do you think that it is worthy to do an entire SDK based on it, offering a Java-style API? I think it would be good for newbies. Please note the absolute absence of templates. This is on purpose: code without templates looks better and is easier on the newbies. |
bamccaig
Member #7,536
July 2006
![]() |
I think that it's pretty pointless. If you want a language that is more like Java then use Java. What you've done seems somewhat impressive, but it also seems rather complicated and probably is a nightmare to maintain. Also, at the very least, I would implement: std::ostream & operator<<(std::ostream & out, const Object & o) { out << o.toString(); } There's little point explicitly calling the toString method when you can let the language do it for you. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
kazzmir
Member #1,786
December 2001
![]() |
I guess its ok. You would probably want some way to create java-style objects as well. class MyFoo: public Object { }; ... MyFoo foo = new MyFoo(); So I think you need operator=(Object*) as well. |
Neil Walker
Member #210
April 2000
![]() |
axilmar said: What is your opinion on this? Isn't that a bit like turning wine into water Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
Goalie Ca
Member #2,579
July 2002
![]() |
Well, there is no shortage of new languages these days. If we want to improve C++ then look to D and Go. C#/Java I believe fits an entirely different niche. ------------- |
axilmar
Member #1,204
April 2001
|
bamccaig said: If anything, this kind of deception would ultimately hurt n00bs Indeed...an easy to use c++ API? who would have thought of that? it's crazy!!! it's heresy!!! :-). Not that I don't agree with you. |
bamccaig
Member #7,536
July 2006
![]() |
I think that an easy to use C++ API would be awesome. Something complete. Not the bare minimum, but an ever expanding standard library that accomplishes all of the things programmers regularly do. However, I don't think that there's any need to hide the implementation details so much. It seems to create more work and probably has as many disadvantages as it does advantages. I don't think that it's unreasonable to implement a fully functional standard library using C++ to its fullest (including templates). -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Tobias Dammers
Member #2,604
August 2002
![]() |
C++ is at its best when used as intended - that is, program in a RAII idiom instead of tacking GC onto it, and play the template functionality to its full potential. Goalie Ca said: Well, there is no shortage of new languages these days. If we want to improve C++ then look to D and Go. C#/Java I believe fits an entirely different niche.
C++ is pretty good at what it's good at. It's just that before all those nice interpreted and hybrid languages like Python, C#, Java, etc. came along and became viable alternatives, C++ was used for pretty much everything, including jobs for which it is not the right tool. axilmar said: 2) implementation details completely hidden from headers. Whether this is a good thing remains questionable. There is a good reason newer languages don't do this anymore: Every single text editor on the planet that is remotely useful for day-to-day programming is capable of folding curly-bracket syntax, which means you can get the same compact view with a single-source-file model as with the traditional C/C++ header/source model, and hence, the "compact quick reference" argument is basically moot. It's the only non-technical argument for the two-file model though, so these days, the everything-in-one-file model is, from a human's point of view, the thing that makes sense. Quote: Please note the absolute absence of templates. This is on purpose: code without templates looks better and is easier on the newbies. But templates are the most useful feature in all of C++. C++ lacks a few features such as reflection, runtime code generation, and lambda expressions; templates almost fill that gap - not always, but close enough for most things. --- |
GullRaDriel
Member #3,861
September 2003
![]() |
Tobias said: few features such as reflection, runtime code generation, and lambda expressions
Stop bragging yourself around like that. The day to day programmer never need those silly things which are only there for marketing promotion. "Code is like shit - it only smells if it is not yours" |
axilmar
Member #1,204
April 2001
|
bamccaig said: I think that an easy to use C++ API would be awesome. Something complete. Not the bare minimum, but an ever expanding standard library that accomplishes all of the things programmers regularly do. Yeah, it would be extremely useful. Quote: However, I don't think that there's any need to hide the implementation details so much. But it's so useful...code becomes much clearer. You know, one of the problems with c++ is that it's one of the hardest languages to read. Going back to your code, after a while, and it is a strange land. Quote: It seems to create more work and probably has as many disadvantages as it does advantages. It's certainly more work for the SDK programmer. Not so much more work for the end-user of the SDK. It even allows you to seal a class by not providing the implementation part in a public header. Quote: a fully functional standard library using C++ to its fullest (including templates) Data was 'fully functional', and look what he did to Lt Yar!!! Joking aside, I've come to dislike templates. After so many years about bragging about them, I now find them gross. They are necessary, because without them smart pointers wouldn't be possible, and they are also necessary for performance reasons, but it's an absolute overkill during development, especially if you use the most advanced stuff like boost. I've tried to use boost::bidirectional_map, but the compile times are so long that I did my own version of the bidirectional_map. I've tried to use boost::spirit, but the the compile times again are so long that I had to make my own parsing library and DSL that didn't have any templates. I think templates are great stuff, but only when the computing resources are so limited that performance would suffer if templates are not used. I think that a Java-like SDK is a good idea: it's like having another language together with c++, sharing the same IDE and compiler. You don't have to use the hardcore stuff, until performance suffers. When it does, then going low level would be extremely easy, since it's the same language that allows it to go low level. Tobias Dammers said: Most software projects benefit from sacrificing a bit of runtime performance to gain a truckload of developer performance and pretty much get rid of memory allocation bugs altogether. This fits exactly with my idea. Having a good and simple SDK, even if it's a little slower than what it can be, will boost developer performance, which is a great benefit. Quote: Every single text editor on the planet that is remotely useful for day-to-day programming is capable of folding curly-bracket syntax, which means you can get the same compact view with a single-source-file model as with the traditional C/C++ header/source model, and hence, the "compact quick reference" argument is basically moot. I don't think that implementation detail hiding had ever to do with compact quick reference. I think it was more related to the fragile base class idiom: change a little detail in the base class and see all the subclasses be recompiled. In the way I have done the implementation hiding, changing the implementation class of a class would affect only the host class and the inheriting classes. The rest of the code would not need to be re-compiled. For example, in the code I posted, if I need to change the STRING struct, only the code in file String.cpp would need a recompile (and the Object.cpp file, but that is not necessary if the Object class uses the String class instead of the STRING struct). Imagine how much code would need to be recompiled if there is a change the internals of std::basic_string (or any other class that is heavily used)! Quote: so these days, the everything-in-one-file model is, from a human's point of view, the thing that makes sense. If you are a solo developer, that is. If the code is to be edited for many years, from many people, the sensible thing to do is have one file per class. |
bamccaig
Member #7,536
July 2006
![]() |
Even in C#, I actually find myself splitting single classes into many files occasionally. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
gnolam
Member #2,030
March 2002
![]() |
axilmar said: Tobias Dammers said: so these days, the everything-in-one-file model is, from a human's point of view, the thing that makes sense. If you are a solo developer, that is. If the code is to be edited for many years, from many people, the sensible thing to do is have one file per class.
He wasn't referring to projects split into multiple code files, but to separate declaration and definition files for the same code. -- |
axilmar
Member #1,204
April 2001
|
gnolam said: He wasn't referring to projects split into multiple code files, but to separate declaration and definition files for the same code. That's an unfortunate drawback of C++ that we must live with. It could be fixed, but the c++ committee just does not care. |
Tobias Dammers
Member #2,604
August 2002
![]() |
GullRaDriel said: Stop bragging yourself around like that. The day to day programmer never need those silly things which are only there for marketing promotion. I happen to use reflection and lambda expressions all the time; in languages that make it painless, I also generate code all the time. These features need to be handled with care, but sometimes, they are the most elegant and straightforward solution to a given problem. A few examples: There are tons of 'features' that are really just marketing buzzwords, but these are not. gnolam said: He wasn't referring to projects split into multiple code files, but to separate declaration and definition files for the same code. Exactly. bamccaig said: I don't like code folding though. I prefer to see what's going on. Code folding makes me worry that I'll overlook something that isn't visible. The point is, you don't have to fold. You can, if you choose to. With header/source, you don't get that choice, at least not at such a fine-grained level. --- |
BAF
Member #2,981
December 2002
![]() |
You might as well use C#. You get the familiar syntax of C++ along with some of the nice things from Java (and several things which beat Java). Also, I agree that some of the htings mentioned are FAR from just 'marketing promotion.' I make heavy use of Linq and lambdas, for example. They may sound all fluffy and useless at first glance, but once you start using them, you never want to live without them. I recently had to work on some .NET 2.0 code, and going without all of the evolved features of C# was painful. You don't have to worry about that in Java, because it's just a stagnated language with none of the cool features anyway. |
bamccaig
Member #7,536
July 2006
![]() |
I actually find myself thinking of reflection-based solutions too often. Lambdas really are very nice. C# is also a very nice language. I would certainly say that I prefer C# to Java, but Java is much more free and in practice much more platform independent so it gets a lot more points for that. Also, I really like Java's anonymous classes. C# has anonymous types, but they have their limitations. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
kazzmir
Member #1,786
December 2001
![]() |
baf said: ...
bambam said: ... omg guys, get a room. |
Thomas Fjellstrom
Member #476
June 2000
![]() |
Based on the OPs description it actually sounds a bit like Qt. It has a form of GC, a base QObject class, COW data types, private implementation classes and Java iterator variants for its data types. My Canva5 project's Font class is structured like a simple data type, as in you can pass it by value or reference, and not worry about copies, it hides the actual font data in a private implementation class that is shared via shared_ptr. -- |
axilmar
Member #1,204
April 2001
|
Thomas Fjellstrom said: Based on the OPs description it actually sounds a bit like Qt. It has a form of GC, a base QObject class, COW data types, private implementation classes and Java iterator variants for its data types. My Canva5 project's Font class is structured like a simple data type, as in you can pass it by value or reference, and not worry about copies, it hides the actual font data in a private implementation class that is shared via shared_ptr. Exactly. Wouldn't it be nice if all classes were like that? |
SiegeLord
Member #7,827
October 2006
![]() |
Maybe this sounds nice... but I wouldn't use anything made by you, axilmar... you are too prone to saying "screw it" and deleting/removing support to these "C++ improvement" projects of yours. What happened to that garbage collection library of yours (several of them, in fact)? Or that new language? "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
axilmar
Member #1,204
April 2001
|
SiegeLord said: Maybe this sounds nice... but I wouldn't use anything made by you, axilmar... you are too prone to saying "screw it" and deleting/removing support to these "C++ improvement" projects of yours. Too true!!! I wish I had the time to complete these projects. Quote: What happened to that garbage collection library of yours (several of them, in fact)? I finished it, tested it (on a shoot-em-up demo that fired lots of bullets), deleted it. There wasn't anyone interested in it. Quote: Or that new language? It requires lot's of time, which I don't have. It's extremely hard for me to work more than I do. I already put 8 hours each day to work. And I am also married. |
SiegeLord
Member #7,827
October 2006
![]() |
axilmar said: There wasn't anyone interested in it. I wanted to look at it a year or so after you made that thread, only to find it gone... I'm just making a suggestion of putting all these (possibly very useful) projects in some publicly available place. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
axilmar
Member #1,204
April 2001
|
I'll see if I can find the gc code. If I do, I'll put it somewhere public. I'll let you know if I do so. |
bamccaig
Member #7,536
July 2006
![]() |
GitHub them. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|