From 9cf23d101041d1e990f57d78e4712226b8a449c7 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Thu, 28 Jun 2018 13:32:06 +0200 Subject: move to c++11 remove console writes --- 3rdparty/python-console/Interpreter.cpp | 33 +++++++------------ .../python-console/ParseHelper.BlockParseState.cpp | 35 ++++---------------- .../ParseHelper.BracketParseState.cpp | 13 ++------ 3rdparty/python-console/ParseHelper.cpp | 37 +++------------------- 3rdparty/python-console/ParseHelper.h | 4 +-- .../python-console/test_python_interpreter.cpp | 2 +- 6 files changed, 28 insertions(+), 96 deletions(-) (limited to '3rdparty/python-console') diff --git a/3rdparty/python-console/Interpreter.cpp b/3rdparty/python-console/Interpreter.cpp index f84d9c5b..e66e6532 100644 --- a/3rdparty/python-console/Interpreter.cpp +++ b/3rdparty/python-console/Interpreter.cpp @@ -1,7 +1,7 @@ #include "Interpreter.h" #include #include -#include +#include PyThreadState* Interpreter::MainThreadState = NULL; @@ -27,9 +27,6 @@ Interpreter::Interpreter( ) Interpreter::~Interpreter( ) { -#ifndef NDEBUG - std::cout << "delete interpreter\n"; -#endif PyEval_AcquireThread( m_threadState ); Py_EndInterpreter( m_threadState ); PyEval_ReleaseLock( ); @@ -60,6 +57,15 @@ Interpreter::test( ) PyEval_ReleaseThread( m_threadState ); } +template +std::string string_format( const std::string& format, Args ... args ) +{ + size_t size = std::snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0' + std::unique_ptr buf( new char[ size ] ); + std::snprintf( buf.get(), size, format.c_str(), args ... ); + return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside +} + std::string Interpreter::interpret( const std::string& command, int* errorCode ) { @@ -69,15 +75,9 @@ Interpreter::interpret( const std::string& command, int* errorCode ) PyObject* py_result; PyObject* dum; std::string res; -#ifndef NDEBUG - std::cout << "interpreting (" << command << ")\n"; -#endif py_result = Py_CompileString(command.c_str(), "", Py_single_input); if ( py_result == 0 ) { -#ifndef NDEBUG - std::cout << "Huh?\n"; -#endif if ( PyErr_Occurred( ) ) { *errorCode = 1; @@ -110,13 +110,7 @@ const std::list& Interpreter::suggest( const std::string& hint ) PyEval_AcquireThread( m_threadState ); m_suggestions.clear(); int i = 0; - std::string command = boost::str( - boost::format("sys.completer.complete('%1%', %2%)\n") - % hint - % i); -#ifndef NDEBUG - std::cout << command << "\n"; -#endif + std::string command = string_format("sys.completer.complete('%s', %d)\n", hint.c_str(),i); std::string res; do { @@ -129,10 +123,7 @@ const std::list& Interpreter::suggest( const std::string& hint ) res = GetResultString( m_threadState ); GetResultString( m_threadState ) = ""; ++i; - command = boost::str( - boost::format("sys.completer.complete('%1%', %2%)\n") - % hint - % i); + command = string_format("sys.completer.complete('%s', %d)\n", hint.c_str(),i); if (res.size()) { // throw away the newline diff --git a/3rdparty/python-console/ParseHelper.BlockParseState.cpp b/3rdparty/python-console/ParseHelper.BlockParseState.cpp index 39c17b60..5d0cba9d 100644 --- a/3rdparty/python-console/ParseHelper.BlockParseState.cpp +++ b/3rdparty/python-console/ParseHelper.BlockParseState.cpp @@ -20,7 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "ParseHelper.h" -#include ParseHelper::BlockParseState:: BlockParseState( ParseHelper& parent ): @@ -47,10 +46,6 @@ process(const std::string& str) bool isIndented = PeekIndent( str, &ind ); if ( isIndented ) { -#ifndef NDEBUG - std::cout << "current line indent: "; - print( ind ); -#endif // check if indent matches if ( ind.Token != indent.Token ) { @@ -61,17 +56,14 @@ process(const std::string& str) parent.stateStack.pop_back( ); if ( !parent.stateStack.size( ) ) break; - boost::shared_ptr parseState = - boost::dynamic_pointer_cast( + std::shared_ptr parseState = + std::dynamic_pointer_cast( parent.stateStack.back( )); found = ( ind.Token == parseState->indent.Token ); } if ( ! found ) { -#ifndef NDEBUG - std::cout << "indent mismatch\n"; -#endif parent.reset( ); ParseMessage msg( 1, "IndentationError: unexpected indent"); parent.broadcast( msg ); @@ -85,11 +77,11 @@ process(const std::string& str) if ( str[str.size()-1] == ':' ) { parent.commandBuffer.push_back( str ); - //parent.inBlock = (boost::dynamic_pointer_cast( + //parent.inBlock = (std::dynamic_pointer_cast( // parent.stateStack.back())); //expectingIndent = true; - boost::shared_ptr parseState( + std::shared_ptr parseState( new BlockParseState( parent ) ); parent.stateStack.push_back( parseState ); return true; @@ -98,7 +90,7 @@ process(const std::string& str) if ( str[str.size()-1] == '\\' ) { parent.commandBuffer.push_back( str ); - boost::shared_ptr parseState( + std::shared_ptr parseState( new ContinuationParseState( parent ) ); parent.stateStack.push_back( parseState ); return true; @@ -107,7 +99,7 @@ process(const std::string& str) if (BracketParseState::HasOpenBrackets( str )) { // FIXME: Every parse state should have its own local buffer - boost::shared_ptr parseState( + std::shared_ptr parseState( new BracketParseState( parent, str ) ); parent.stateStack.push_back( parseState ); return true; @@ -121,18 +113,12 @@ process(const std::string& str) if ( str.size() ) { { -#ifndef NDEBUG - std::cout << "Expected indented block\n"; -#endif parent.reset( ); ParseMessage msg( 1, "IndentationError: expected an indented block" ); parent.broadcast( msg ); return false; } } -#ifndef NDEBUG - std::cout << "Leaving block\n"; -#endif parent.stateStack.pop_back(); parent.flush( ); parent.reset( ); @@ -146,8 +132,6 @@ initializeIndent(const std::string& str) bool expectingIndent = (indent.Token == ""); if ( !expectingIndent ) { - std::cout << "already initialized indent: "; - print( indent ); return true; } @@ -155,9 +139,6 @@ initializeIndent(const std::string& str) bool isIndented = parent.PeekIndent( str, &ind ); if ( !isIndented ) { -#ifndef NDEBUG - std::cout << "Expected indented block\n"; -#endif parent.reset( ); ParseMessage msg( 1, "IndentationError: expected an indented block" ); parent.broadcast( msg ); @@ -167,9 +148,5 @@ initializeIndent(const std::string& str) //parent.currentIndent = ind; //parent.indentStack.push_back( parent.currentIndent ); //parent.expectingIndent = false; -#ifndef NDEBUG - std::cout << "initializing indent: "; - print( ind ); -#endif return true; } diff --git a/3rdparty/python-console/ParseHelper.BracketParseState.cpp b/3rdparty/python-console/ParseHelper.BracketParseState.cpp index 017e6a71..73694235 100644 --- a/3rdparty/python-console/ParseHelper.BracketParseState.cpp +++ b/3rdparty/python-console/ParseHelper.BracketParseState.cpp @@ -22,7 +22,7 @@ THE SOFTWARE. #include "ParseHelper.h" #include #include -#include + const std::string ParseHelper::BracketParseState::OpeningBrackets = "[({"; const std::string ParseHelper::BracketParseState::ClosingBrackets = "])}"; @@ -64,23 +64,17 @@ ParseHelper::BracketParseState::BracketParseState( ParseHelper& parent, const st ParseState( parent ) { bool hasOpenBrackets = LoadBrackets( firstLine, &brackets ); - assert( hasOpenBrackets ); + //assert( hasOpenBrackets ); m_buffer.push_back( firstLine ); } bool ParseHelper::BracketParseState::process(const std::string& str) { -#ifndef NDEBUG - std::cout << "(BracketParseState) processing " << str << "\n"; -#endif // update brackets stack for (int i = 0; i < str.size(); ++i) { if (OpeningBrackets.find_first_of(str[i]) != std::string::npos) { -#ifndef NDEBUG - std::cout << "push " << str[i] << "\n"; -#endif brackets.push_back(str[i]); } else @@ -88,9 +82,6 @@ bool ParseHelper::BracketParseState::process(const std::string& str) size_t t = ClosingBrackets.find_first_of(str[i]); if (t != std::string::npos) { -#ifndef NDEBUG - std::cout << "pop " << str[i] << "\n"; -#endif // reset state if unmatched brackets seen if (t != OpeningBrackets.find_first_of(brackets.back())) { diff --git a/3rdparty/python-console/ParseHelper.cpp b/3rdparty/python-console/ParseHelper.cpp index 8944a84e..08f0526a 100644 --- a/3rdparty/python-console/ParseHelper.cpp +++ b/3rdparty/python-console/ParseHelper.cpp @@ -28,26 +28,6 @@ THE SOFTWARE. #include #include "ParseListener.h" -#ifndef NDEBUG -void print(const ParseHelper::Indent& indent) -{ - std::string str = indent.Token; - for (int i = 0; i < str.size(); ++i) - { - switch (str.at(i)) - { - case ' ': - str[i] = 's'; - break; - case '\t': - str[i] = 't'; - break; - } - } - std::cout << str << "\n"; -} -#endif - ParseHelper::Indent:: Indent( ) { } @@ -93,15 +73,11 @@ ParseHelper::ParseHelper( ) void ParseHelper::process( const std::string& str ) { -#ifndef NDEBUG - std::cout << "processing: (" << str << ")\n"; -#endif - std::string top; commandBuffer.push_back(str); //std::string top = commandBuffer.back(); //commandBuffer.pop_back(); - boost::shared_ptr blockStatePtr; + std::shared_ptr blockStatePtr; while (stateStack.size()) { top = commandBuffer.back(); @@ -122,9 +98,6 @@ void ParseHelper::process( const std::string& str ) broadcast( std::string() ); return; } -#ifndef NDEBUG - std::cout << "now processing: (" << top << ")\n"; -#endif { // check for unexpected indent Indent ind; @@ -142,7 +115,7 @@ void ParseHelper::process( const std::string& str ) // enter indented block state if ( top[top.size()-1] == ':' ) { - boost::shared_ptr parseState( + std::shared_ptr parseState( new BlockParseState( *this ) ); stateStack.push_back( parseState ); return; @@ -150,7 +123,7 @@ void ParseHelper::process( const std::string& str ) if ( top[top.size()-1] == '\\' ) { - boost::shared_ptr parseState( + std::shared_ptr parseState( new ContinuationParseState( *this ) ); stateStack.push_back( parseState ); return; @@ -160,7 +133,7 @@ void ParseHelper::process( const std::string& str ) { // FIXME: Every parse state should have its own local buffer commandBuffer.pop_back( ); - boost::shared_ptr parseState( + std::shared_ptr parseState( new BracketParseState( *this, top ) ); stateStack.push_back( parseState ); return; @@ -198,7 +171,7 @@ void ParseHelper::reset( ) bool ParseHelper::isInContinuation( ) const { return (stateStack.size() - && (boost::dynamic_pointer_cast( + && (std::dynamic_pointer_cast( stateStack.back()))); } diff --git a/3rdparty/python-console/ParseHelper.h b/3rdparty/python-console/ParseHelper.h index 8824fc39..d01e2627 100644 --- a/3rdparty/python-console/ParseHelper.h +++ b/3rdparty/python-console/ParseHelper.h @@ -25,7 +25,7 @@ THE SOFTWARE. #include #include #include -#include +#include #include "ParseMessage.h" class ParseListener; @@ -120,7 +120,7 @@ protected: // TODO: Create a ContinuationParseState to handle this bool inContinuation; std::vector< ParseListener* > listeners; - std::vector< boost::shared_ptr< ParseState > > stateStack; + std::vector< std::shared_ptr< ParseState > > stateStack; std::vector< std::string > commandBuffer; public: diff --git a/3rdparty/python-console/test_python_interpreter.cpp b/3rdparty/python-console/test_python_interpreter.cpp index 69e16ed1..61b2ccef 100644 --- a/3rdparty/python-console/test_python_interpreter.cpp +++ b/3rdparty/python-console/test_python_interpreter.cpp @@ -5,7 +5,7 @@ int main( int argc, char *argv[] ) { std::string commands[] = { "from time import time,ctime\n", - "print 'Today is',ctime(time())\n" + "print('Today is',ctime(time()))\n" }; Interpreter::Initialize( ); Interpreter* interpreter = new Interpreter; -- cgit v1.2.3