C++/CLI Shell Exercise Code

A number of years ago, I was helping someone on DaniWeb with some code that was (probably) more than they expected.
I never got closure on the code because it seemed the requester didn’t understand to use Visual Studio.
I recently revisited that code and realized how differently I would have done things if I created it today.
It’s like that old cartoon from NotInventedHere.com on 4/20/2010 where one character can’t stand to see his old code:


Well, I had the same reaction to mine (as I frequently do) and so I rewrote portions of it.
Luckily, it was short and I knew SPECIFICALLY what I wanted done (for now).

I figured I’d go ahead and post it for future reference (when I once again can’t stand it).

This code puts into practice multiple techniques for using Linq and IEnumerable in C++/CLI.
The program itself is a simple shell-program where the user has a command-line and can enter
some simple commands.

This was probably someone’s homework, but the implementation I demonstrated might not have been
something he/she could have turned in.  LOL.

// DW_393014.cpp : main project file.
#include "stdafx.h"
using namespace System;
using namespace System::Collections::Generic;
using namespace System::IO;
using namespace System::Linq;
public ref class CCommands
	static void _doShowDir(void)
	static void _doHelp()
		Console::WriteLine("Commands are: " +
			String::Join(", ",
					Enumerable::Select(arr_kvp_s2aCommands, getKeyFromKvpS2a))) + ", quit, exit");
	static Action^ _getActFromKvpS2aJoin(KeyValuePair<String^, Action^> kvpS2a, String^) { return kvpS2a.Value; }
	static Action^ _clear = gcnew Action(Console::Clear);
	static Action^ _help = gcnew Action(_doHelp);
	static Action^ _showDir = gcnew Action(_doShowDir);
	static String^ _getKeyFromKvpS2a(KeyValuePair<String^, Action^> kvp_s2a) { return kvp_s2a.Key; }
	static String^ _getStringAsString(String^ str) { return str; }
	static bool _notNothing(Action^ act) { return nullptr != act; }
	static void _doNothing(){}
	static Func<KeyValuePair<String^, Action^>, String^>^ getKeyFromKvpS2a =
		gcnew Func<KeyValuePair<String^, Action^>, String^>(_getKeyFromKvpS2a);
	static Func<String^, String^>^ getStringAsString = gcnew Func<String^, String^>(_getStringAsString);
	static Func<KeyValuePair<String^, Action^>, String^, Action^>^ getActFromKvpS2aJoin =
		gcnew Func<KeyValuePair<String^, Action^>, String^, Action^>(_getActFromKvpS2aJoin);
	static String^ NowTime(DateTime^ dt)
		return String::Format("{0}:{1}:{2}",
	static IEnumerable<KeyValuePair<String^, Action^>>^ arr_kvp_s2aCommands =
		gcnew array<KeyValuePair<String^, Action^>>
			KeyValuePair<String^, Action^>("spy", _showDir),
			KeyValuePair<String^, Action^>("wipe", _clear),
			KeyValuePair<String^, Action^>("help", _help),
	static Action^ DoNothing = gcnew Action(_doNothing);
	static Func<Action^, bool>^ IsSomething = gcnew Func<Action^, bool>(_notNothing);
int main(void)
{	//array<System::String ^> ^args)
	Console::WriteLine(L"Welcome to CLI Shell (v1.0)\n");
	array<String^>^ arr_strQuitExit = { "exit", "quit" };
	String^ strCommand = "";
	while (!Enumerable::Contains<String^>(arr_strQuitExit, strCommand))
		Console::Write("{0}>: ", CCommands::NowTime(DateTime::Now));
		strCommand = Console::ReadLine()->ToLower();
		// Invoke the first non-null Action that matches the command
		// If no match, call the DoNothing action.
		(	// construct pieces ensures no nulls
			gcnew array <Action^>
			{	// [0]=desired command, [1]=fall-back
				( // returns nullptr if no matching command
					Enumerable::Join<KeyValuePair<String^, Action^>, String^, String^, Action^>
						gcnew array <String^> { strCommand }, // array of 1
						CCommands::getKeyFromKvpS2a,		// get the Key (string)
						CCommands::getStringAsString,    // return the string
						CCommands::getActFromKvpS2aJoin  // (KVP, STR) => KVP->Key
				CCommands::DoNothing // when no match or nullptr
			CCommands::IsSomething // not nullptr
	return 0;
posted @ Wednesday, August 19, 2015 12:59 PM

Comments on this entry:

No comments posted yet.

Your comment:

(not displayed)


Live Comment Preview: