From 1cbe8e41ea5d8b09f852766e2a7f83a469187d23 Mon Sep 17 00:00:00 2001 From: Thunder422 Date: Wed, 8 Oct 2014 20:57:30 -0400 Subject: [PATCH] dictionary: replaced debug text function with put stream operator removed the dictionary class debug text function and replaced it with a nearly identical function in the tester class source file that overloads the put stream operator ("<<") for a dictionary pointer except that is outputs directly to the output stream argument (instead of appending to a local string), and returns the output stream reference (instead of the string in the string stream); however, the new function no longer outputs the header string (the caller does this now) removed the program model dictionaries debug test function and put this functionality in the test class run function where the function was called from (and outputs the header for each dictionary) --- dictionary.cpp | 38 -------------------------------------- dictionary.h | 7 ++++--- programmodel.cpp | 15 --------------- programmodel.h | 1 - test_ibcp.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 49 insertions(+), 58 deletions(-) diff --git a/dictionary.cpp b/dictionary.cpp index 419f72d..c00b2a3 100644 --- a/dictionary.cpp +++ b/dictionary.cpp @@ -95,44 +95,6 @@ int Dictionary::remove(uint16_t index) } -QString Dictionary::debugText(const QString header) -{ - QString string {QString("\n%1:\n").arg(header)}; - for (size_t i {}; i < m_iterator.size(); i++) - { - if (m_iterator[i] != m_keyMap.end()) - { - auto iterator = m_iterator[i]; - string.append(QString("%1: %2 |%3|\n").arg(i) - .arg(iterator->second.m_useCount) - .arg(QString::fromStdString(iterator->first))); - } - } - string.append("Free:"); - if (m_freeStack.empty()) - { - string.append(" none"); - } - else - { - std::stack tempStack = m_freeStack; - while (!tempStack.empty()) - { - int index {tempStack.top()}; - tempStack.pop(); - string.append(QString(" %1").arg(index)); - if (m_iterator[index] != m_keyMap.end()) - { - string.append(QString("|%1|") - .arg(QString::fromStdString(m_iterator[index]->first))); - } - } - } - string.append("\n"); - return string; -} - - //////////////////////////////////////////////////////////////////////////////// // // // INFORMATION DICTIONARY FUNCTIONS // diff --git a/dictionary.h b/dictionary.h index fccb5ca..26f2430 100644 --- a/dictionary.h +++ b/dictionary.h @@ -25,13 +25,12 @@ #ifndef DICTIONARY_H #define DICTIONARY_H +#include #include #include #include #include -#include - class Token; using TokenPtr = std::shared_ptr; @@ -58,7 +57,9 @@ class Dictionary { return m_iterator[index]->first; } - QString debugText(const QString header); + + friend std::ostream &operator<<(std::ostream &os, + const Dictionary *const dictionary); private: struct EntryValue diff --git a/programmodel.cpp b/programmodel.cpp index e46e557..1c3c19b 100644 --- a/programmodel.cpp +++ b/programmodel.cpp @@ -148,21 +148,6 @@ QString ProgramModel::debugText(int lineIndex, bool fullInfo) const } -// NOTE temporary function to return debug text of all the dictionary -QString ProgramModel::dictionariesDebugText(void) -{ - QString string; - - string.append(m_remDictionary->debugText("Remarks")); - string.append(m_constNumDictionary->debugText("Number Constants")); - string.append(m_constStrDictionary->debugText("String Constants")); - string.append(m_varDblDictionary->debugText("Double Variables")); - string.append(m_varIntDictionary->debugText("Integer Variables")); - string.append(m_varStrDictionary->debugText("String Variables")); - return string; -} - - // function to return text for a given program line QString ProgramModel::lineText(int lineIndex) { diff --git a/programmodel.h b/programmodel.h index 7e1e38d..1777dee 100644 --- a/programmodel.h +++ b/programmodel.h @@ -226,7 +226,6 @@ class ProgramModel : public QAbstractListModel } } QString debugText(int lineIndex, bool fullInfo = false) const; - QString dictionariesDebugText(void); // program model access functions QString lineText(int lineIndex); diff --git a/test_ibcp.cpp b/test_ibcp.cpp index 3e8690a..0d8d68b 100644 --- a/test_ibcp.cpp +++ b/test_ibcp.cpp @@ -203,6 +203,42 @@ std::ostream &operator<<(std::ostream &os, const RpnList &rpnList) } +// overloaded output stream operator for abbreviated contents of rpn list +std::ostream &operator<<(std::ostream &os, const Dictionary *const dictionary) +{ + for (size_t i {}; i < dictionary->m_iterator.size(); i++) + { + if (dictionary->m_iterator[i] != dictionary->m_keyMap.end()) + { + auto iterator = dictionary->m_iterator[i]; + os << i << ": " << iterator->second.m_useCount << " |" + << iterator->first << "|\n"; + } + } + os << "Free:"; + if (dictionary->m_freeStack.empty()) + { + os << " none"; + } + else + { + std::stack tempStack = dictionary->m_freeStack; + while (!tempStack.empty()) + { + int index {tempStack.top()}; + tempStack.pop(); + os << ' ' << index; + if (dictionary->m_iterator[index] != dictionary->m_keyMap.end()) + { + os << '|' << dictionary->m_iterator[index]->first << '|'; + } + } + } + os << '\n'; + return os; +} + + // function to process a test input file specified on the command line // or accept input lines from the user Tester::Tester(const QStringList &args, std::ostream &cout) : @@ -426,7 +462,15 @@ bool Tester::run(CommandLine *commandLine) m_cout << i << ": " << m_programUnit->debugText(i, true).toStdString() << '\n'; } - m_cout << m_programUnit->dictionariesDebugText().toStdString(); + + m_cout << "\nRemarks:\n" << m_programUnit->remDictionary(); + m_cout << "\nNumber Constants:\n" + << m_programUnit->constNumDictionary(); + m_cout << "\nString Constants:\n" + << m_programUnit->constStrDictionary(); + m_cout << "\nDouble Variables:\n" << m_programUnit->varDblDictionary(); + m_cout << "\nInteger Variables:\n" << m_programUnit->varIntDictionary(); + m_cout << "\nString Variables:\n" << m_programUnit->varStrDictionary(); if (m_recreate) {