From 300c43413b56067fcbd6230b93a7eb97d6984072 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 9 Jun 2017 14:40:04 +0200 Subject: [PATCH] Updated cli example --- examples/cpp/cli/CMakeLists.txt | 2 +- examples/cpp/cli/Main.cpp | 32 +++++++---- examples/cpp/cli/Utils.hpp | 56 ++++++++++++++----- .../core/tests/IntepreterTests/CMakeLists.txt | 2 +- 4 files changed, 65 insertions(+), 27 deletions(-) diff --git a/examples/cpp/cli/CMakeLists.txt b/examples/cpp/cli/CMakeLists.txt index fb7fc5d..d54ba59 100644 --- a/examples/cpp/cli/CMakeLists.txt +++ b/examples/cpp/cli/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.5) project(cliMiniQbt) set (CMAKE_CXX_STANDARD 14) set(CMAKE_C_FLAGS_RELEASE "-O3") - +find_library(MiniQbt REQUIRED) SET(srcfiles Main.cpp diff --git a/examples/cpp/cli/Main.cpp b/examples/cpp/cli/Main.cpp index 7f1af22..ed31116 100644 --- a/examples/cpp/cli/Main.cpp +++ b/examples/cpp/cli/Main.cpp @@ -73,23 +73,31 @@ int main(int argc, char** argv){ printInfo( "Interactive shell for the Qasm language\n", "Exit program -> exit \n", - "Show version -> about \n" + "Show version -> about \n", + "show a classic register (and collapse the quantum)-> collapse \n", + "Reset the quantum register to super position -> reset \n" ); continue; } - if(command == "collapse"){ - for(const std::string& reg: intepreter.getRegisters()){ - std::vector result = intepreter.readClassicRegister(reg); - std::cout << "Result registery " << reg << ": "; - for(const bool& r : result){ - std::cout << r; - } - std::cout << "\n"; - } - continue; - } + std::regex resetQuantum("\\s*(reset|collapse)\\s*([a-z|A-Z|0-9]*)", std::regex_constants::ECMAScript); + std::smatch m; + std::regex_match(command, m,resetQuantum); + if (m[1] == "reset") { + intepreter.resetSuperPosition(m[2]); + continue; + } + else if (m[1] == "collapse") { + std::vector result = intepreter.readClassicRegister(m[2]); + std::cout << "Result registery " << m[1] << ": "; + for (const bool& r : result) { + std::cout << r; + } + std::cout << "\n"; + continue; + } + if(command[0] == '!'){ std::cout.flush(); system(command.substr(1).c_str()); diff --git a/examples/cpp/cli/Utils.hpp b/examples/cpp/cli/Utils.hpp index bab3f66..67bdd7d 100644 --- a/examples/cpp/cli/Utils.hpp +++ b/examples/cpp/cli/Utils.hpp @@ -4,19 +4,46 @@ #include #include +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) +#include +#endif + template constexpr void printInfo(T& msg){ #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) + HANDLE hConsole; + hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleTextAttribute(hConsole, 2); std::cout << msg; + SetConsoleTextAttribute(hConsole, 7); #else std::cout << "\033[1;32m" << msg << "\033[0m"; #endif } +template +constexpr void printInfo(T& msg, arguments... args) { +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) + HANDLE hConsole; + hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleTextAttribute(hConsole, 2); + std::cout << msg; + SetConsoleTextAttribute(hConsole, 7); +#else + std::cout << "\033[1;32m" << msg; +#endif + printInfo(args...); +} + + template constexpr void printWarning(T& msg){ #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) - std::cout << msg; + HANDLE hConsole; + hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleTextAttribute(hConsole, 14); + std::cout << msg; + SetConsoleTextAttribute(hConsole, 7); #else std::cout << "\033[1;33m" << msg << "\033[0m"; #endif @@ -25,27 +52,26 @@ constexpr void printWarning(T& msg){ template constexpr void printWarning(T& msg, arguments... args){ #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) - std::cout<< msg; + HANDLE hConsole; + hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleTextAttribute(hConsole, 14); + std::cout << msg; + SetConsoleTextAttribute(hConsole, 7); #else std::cout << "\033[1;33m" << msg; #endif printWarning(args...); } -template -constexpr void printInfo(T& msg, arguments... args){ - #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) - std::cout<< msg; - #else - std::cout << "\033[1;32m" << msg; - #endif - printInfo(args...); -} template constexpr void printError(T& msg){ #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) - std::cout << msg; + HANDLE hConsole; + hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleTextAttribute(hConsole, 4); + std::cout << msg; + SetConsoleTextAttribute(hConsole, 7); #else std::cout << msg << "\033[0m"; #endif @@ -56,7 +82,11 @@ constexpr void printError(T& msg){ template constexpr void printError(T& msg, arguments... args){ #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) - std::cout<< msg; + HANDLE hConsole; + hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleTextAttribute(hConsole, 4); + std::cout << msg; + SetConsoleTextAttribute(hConsole, 7); #else std::cout << "\033[1;31m" << msg; #endif diff --git a/modules/core/tests/IntepreterTests/CMakeLists.txt b/modules/core/tests/IntepreterTests/CMakeLists.txt index c83f108..28282ed 100644 --- a/modules/core/tests/IntepreterTests/CMakeLists.txt +++ b/modules/core/tests/IntepreterTests/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required (VERSION 3.5) project(IntepreterTests) set (CMAKE_CXX_STANDARD 14) - +find_library(MiniQbt REQUIRED) SET(srcfiles Main.cpp SimpleAsyncTests.cpp