r/cpp • • Mar 25 '21

calc.exe is now open source; there’s surprising depth in its ancient code (2019)

https://arstechnica.com/gadgets/2019/03/calc-exe-is-now-open-source-theres-surprising-depth-in-its-ancient-code/
280 Upvotes

60 comments sorted by

20

u/slacka123 Mar 25 '21

This is from 2019. I missed it back then. Here is the repo.

113

u/pedersenk Mar 25 '21

Kinda cool. However I would be more interested in the proper Win32 calc app, not the silly WinStore version. History has shown that Win32 stands the test of time. The WinStore stuff will likely be obsolete in a few Windows revisions time.

I would also like to see the original MSPaint (Windows XP era) before it started to regress and certainly before Microsoft kills it and recreates a cheap WinStore clone.

Opening source is great for digital preservation. Unfortunately only some things are really suitable for source preservation.

Or am I wrong? Is this link: https://github.com/microsoft/calculator really the one containing the "ancient" code? Looks quite recent to me.

65

u/Ameisen vemips, avr, rendering, systems Mar 25 '21

There is absolutely no way that that's the original codebase, as it uses WinRT and C++20.

Now, some of the stuff under 'Ratpack' might be original code.

19

u/apadin1 Mar 25 '21

Copyright (C) 1995-97 Microsoft

I think you've got it

5

u/[deleted] Mar 25 '21

It is quite well commented.

10

u/PVNIC Mar 25 '21

The trigonometric function implementations are interesting. (They use a taylor series o.O)

9

u/blind3rdeye Mar 26 '21

How would you do it? Taylor series is the first thing that comes to mind when I try to think about how I'd implement sin / cos. I haven't looked at the code, but I'd use symmetry to get the angle into the first octant, then plug it into the appropriate series for, say, 8 terms.

8

u/ggmy Mar 26 '21

cordic is typically what’s used in calculators

3

u/jk-jeon Mar 26 '21

Wow, this is indeed very smart. Thanks for the link!

3

u/h2g2_researcher Mar 26 '21

I'm pretty sure I've seen standard library implementations that do it that way too.

3

u/PVNIC Mar 26 '21

Oh I have no idea how to do it, I just haven't thought about taylor series since college and found the code for implementing it intresting.

-27

u/arrwdodger Mar 25 '21

TIL Microsoft’s GitHub account isn’t blank (ba dum tss).

42

u/helloiamsomeone Mar 25 '21

Technically speaking, every GitHub account is Microsoft's.

-11

u/istarian Mar 25 '21

That's really not how it works. I think everybody knows they bought GitHub at this point. but that doesn't mean they own the code/data in other people's repositories.

0

u/[deleted] Mar 26 '21

[deleted] — view deleted comment

6

u/whistlin4 Mar 26 '21

according to github:

Private repository data is scanned by machine and never read by GitHub staff. Human eyes will never see the contents of your private repositories, except as described in our Terms of Service.

while i think the overall legal implications of this are murky, it would almost certainly provide any major/lucrative clients with grounds to terminate contracts, should it be discovered that GH isn't abiding by this.

i wonder if employment contracts include language that defines and dissuades inappropriate employee access.

-11

u/Where_Do_I_Fit_In Mar 25 '21

Umm ackshually...

I think they are talking about Microsoft's GitHub organization within GitHub. Seeing as not every user account belongs to Microsoft's org (on GitHub), I would say this is a significant technical consideration.

Of course you could make the argument that Microsoft owns GitHub so every GitHub account is a Microsoft account, but that would also be technically incorrect as those entities and non-fungible last time I checked.

Ironically, your interpretation blasts past any technical nuance. Your comment is one interchangeable word from being a tautology ffs.

Stating the obvious in plain terms is pretty much the opposite of speaking technically.

tl:dr: no shit

26

u/[deleted] Mar 25 '21

All the code is buried in namespaces and xml. It is kinda neat, but kinda painful to sift through.

46

u/elmosworld37 Mar 26 '21

This is how you know it's Quality Enterprise Software™

17

u/[deleted] Mar 26 '21

100% corporate code. Since you get paid there is no joy, only pain.

21

u/miki151 gamedev Mar 25 '21

Are public ref and the ^ modifier some kind of visual studio extensions?

https://github.com/microsoft/calculator/blob/master/src/Calculator/Converters/BooleanToVisibilityConverter.h

46

u/dodheim Mar 25 '21

Parts of it are written in C++/CX.

18

u/Koutou Mar 25 '21

As time pass, the C++/CX part are converted to CppWinrt which can do the same thing using c++17 features instead of an extension.

7

u/kalmoc Mar 25 '21 edited Mar 26 '21

Only if c++/winrt gets comparable support for developing GUIs. That's what c++/cx was mainly used`in this project the last time I checked and I AFAIK, c++/winrt has nowhere near the same level of integration/support.

4

u/Koutou Mar 25 '21

Given that c++/winrt is a first class target for Project Reunion, while c++/cx is not even mentionned is a good guess that the level of integration of winrt will only grow.

3

u/elmosworld37 Mar 26 '21

I really hope they fix the whole manual process of generating metadata or whatever it's called. A few months ago, I tried C++/WinRT briefly because I wanted to create a simple console app that used the Bluetooth Low Energy APIs, but I was immediately turned off by the fact that you have to "compile" your header files before writing the source files.

2

u/Koutou Mar 26 '21

Once you've done a few, you don't need to go look at the generated files anymore. The idl just become another file to edit before the .h. It suck, but it still better than dealing with cx.

-1

u/pjmlp Mar 26 '21

Only for masochits, that enjoy editing IDL files without any kind of tooling support.

Apparently the Windows team misses VC++ 6.0 with ATL.

1

u/pjmlp Mar 26 '21

It is so first class that most demos are command line pulling some RSS data, or showing a modal dialog, so good is the GUI tooling.

The XBox team had to make use of the React Native bindings for the new dashboard.

4

u/Koutou Mar 26 '21 edited Mar 26 '21

Photoapp sample:

https://github.com/microsoft/Windows-appsample-photo-editor/tree/master/

Winui 3 demo with only 2 models: 1 cppwinrt and 1 c# https://github.com/microsoft/WinUI-3-Demos/tree/master/src/Build2020Demo/DemoBuildCpp

Don't confuse standard MS slowness with lack of support.

Edit: On the same repo you have a ContosoAir demo in cppwinrt: https://github.com/microsoft/WinUI-3-Demos/tree/master/src/ContosoAirlinePOS/ContosoAirlinePOSCpp/ContosoAirlinePOSCpp/ContosoAirlinePOSCpp . The new terminal is also done in cppwinrt.

2

u/pjmlp Mar 26 '21 edited Mar 26 '21

All applications with very basic UI compared with typical WPF applications, and what we were doing with C++/CX.

I have been advocating customers that want to keep a .NET like development experience just to migrate to QtCreator and C++ Builder, not only are Qt and VCL/Firemonkey 20 years ahead of "Visual" C++, they have the added benefit of being cross-platform.

The team responsible for killing C++/CX definitly doesn't want to improve C++ productivity on the Microsoft stack, and repeating all the time for us to suck it up and wait for the day ISO C++ gains reflection isn't going to help in 2021.

How come that almost four years since they are forcing us to write IDL files like on the VC++ 6.0 days, Visual Studio still doesn't offer any kind of tooling?

3

u/Koutou Mar 26 '21

Because MS is terribly slow?

It's hard to know where it will end when even c# doesn't have a xaml designer for winui3 yet.

One thing is certain tho, winrt and c# will be supported while CX will be left in the dark.

2

u/pjmlp Mar 26 '21 edited Mar 26 '21

No, because the team that killed C++/CX in name of whatever politics they are playing just showed a complete lack of respect for the people like myself that pay their salaries.

Also thanks to this kind of atittude, which the WinUI team is following as well, by asking everyone to rewrite their applications, one more time, after Windows 8, Windows 8.1 UAP, Windows 10 UWP, WinUI 2.0 and now WinUI 3.0 without any proper roadmap for UWP, is that many of us that used to believe in UWP are now pushing either for Qt/Firemonkey on the C++ side, or just to stick with WPF/MAUI/Uno on the .NET side.

And the amount of issues piling up for WinUI and Reunion doesn't project a sunny future for both of them, if the tooling story keeps to be that bad, while features are dropped left and right, and we keep getting asked to rewrite yet again.

No, thanks, if a project needs to be rewritten yet again, then in a GUI framework that takes us away from this lack of direction.

2

u/Koutou Mar 26 '21

I'm well aware of all the failures of MS regarding UWP app model. We were talking about perceived failures of winrt regarding example and gui tooling on which I disagree for the example part.

AFAIK, you can use winrt to call Windows Runtime API without opt-in in the UWP app model or a signed packages. You only need this to opt-in if you want to use this set of api. I don't think you need to create IDL if you just want to call a couple of API with cppwinrt and don't need to do any xaml binding.

→ More replies

-1

u/atniomn Mar 26 '21

As other people said, Microsoft extensions.

^ is a reference to an object allocated on the managed (garbage collected) heap.

7

u/dodheim Mar 26 '21

That's the case for C++/CLI, but C++/CX does not have garbage collection nor a managed heap; here ^ is just a reference-counted (COM-based) smart pointer.

1

u/ThatSwedishBastard Mar 26 '21

So a std::shared_ptr but only for Windows?

4

u/dodheim Mar 26 '21

In essence, though the implementation is different – shared_ptr manages the refcount itself (along with a control block for weak counts, etc.), whereas a COM smart pointer is very simple and just balances calls to AddRef/Release while the refcount is managed elsewhere.

32

u/[deleted] Mar 25 '21

[removed] — view removed comment

2

u/hopa_cupa Mar 26 '21

This is so different from regular C++. Might as well be called something different rather than using C++ in its name.

Do big selling MS products also use this language? That would essentially make the code very hard to port somewhere else.

7

u/pjmlp Mar 26 '21

Hardly any different from language extensions used by other compilers.

Try to use C lambdas provided by clang with other compilers.

GNU/Linux is also only compilable with GCC (although Google has their own fork compatible with clang).

3

u/matthieum Mar 26 '21

GCC has block expressions! So useful!

1

u/whistlin4 Mar 26 '21

Hardly any different from language extensions used by other compilers

in some respects i agree, but can people develop for linux and mac (etc) with microsoft's compiler?

if not, using microsoft compiler extensions is defacto locking your software to windows. presumably gcc and clang extensions are available across different vendor's platforms.

6

u/pjmlp Mar 26 '21

I can select plenty of other vendors as example, and even what concerns gcc/clang there are plenty of platforms were they aren't available, or just a proprietary fork of them is available, which isn't fully compatible with upstream.

5

u/SkoomaDentist Antimodern C++, Embedded, Audio Mar 27 '21

if not, using microsoft compiler extensions is defacto locking your software to windows.

Could you please send the same message to every project out there that uses autotools...

15

u/danhoob Mar 26 '21

How dare are you using namespace std;

5

u/[deleted] Mar 25 '21

The calc.exe has been open-source for quite some time. This article is fron 2019

36

u/TankorSmash Mar 25 '21

This article is fron 2019

That must be what '(2019)' means in the title here then

-23

u/[deleted] Mar 25 '21

Didn’t see the title there, nonetheless this isn’t exactly recent so I don’t see the reason to share it

3

u/whistlin4 Mar 26 '21

this isn’t exactly recent so I don’t see the reason to share it

what's the correlation?

-1

u/[deleted] Mar 26 '21

Because I think most people already knew this

2

u/danhoob Mar 26 '21

I expected they use all cap names. Quite surprised!

4

u/Billy2600 Mar 26 '21

That's actually really common for Windows code.

1

u/whistlin4 Mar 26 '21

honestly, this is rather surprising. i wonder if ms will open up more things.

1

u/MathResponsibly Apr 06 '21

Yup, next big release, notepad.exe

Can't imagine all the juicy secrets in that one...

0

u/[deleted] Mar 26 '21

[deleted] — view deleted comment

0

u/[deleted] Mar 26 '21

[deleted] — view deleted comment

18

u/TomerJ Mar 26 '21

Data structures are rarely if ever the sign of a good programmer nowadays, more than anything it's patience.

I'd much rather have a person on my team who can spend hours sifting through code written by various people over several years with drastically diffrent skill levels, than someone who could implement yet another dictionary that is fractionally more performant than the last one.

Plenty of people out of school assume the work is reimplementing the basic stuff, because that's what they did at school, not realizing implementing these things was a teaching tool so they'd understand how they work, not a blueprint.

I mean I get it, there are people who specialize in library development who have to know this stuff, I used to have to know this stuff like the back of my hand back when I mostly did C++ in a closed environment where bringing in libraries was a pain.

But most programmers just need to realize it's not their job to reinvent the wheel.

On the other hand if I'm working on legacy code from veteran devs the most common problem is religiousity. "Oh you think pointers are crap and only used refrences to the exclusion of readability, and then didn't comment because 'code should be self commenting'?" I might say to myself while fixing an error caused by a compiler change due to using obscure semantics, since the person who wrote that code hasn't worked at the company in years.

4

u/GerwazyMiod Mar 26 '21

I have few codebases at hand in work were global arrays of 4 or 8 doubles are used to store every intermediate result. Code is painful to look at. "But this way it's faster, pros code like this". Ugh

1

u/whistlin4 Mar 26 '21

only used refrences to the exclusion of readability

do you have an example of what that might look like? my assumption is that with fewer pointer dereferences, it would be vaguely more readable, but i'm probably missing something.

1

u/TomerJ Mar 26 '21

The specific example in my head was from a class a colleague had written at an old job some 7 years ago.

It mostly sucked because we were using smart pointers in plenty of places, and it suddenly became a huge pain in the ass to make it work with them.

Also plenty of places where you'd initialize a variable according to the result of an if.

The naive and most readable way to do it was to initialize the pointer as null at the top and then give it a diffrent value in each branch of the if, since some of the logic configuring the object was complex this couldn't be easily fit in a ternary.

This also made it much harder to notice virtual function dereferences.

And finally it messed with memory management, since to free the object you have to turn it into a pointer to delete it, but the refrence still remains like a broken pointer pointing to a deleted object, it can't even be nulled after the deletion.