# HG changeset patch # User Teemu Piippo # Date 1611746231 -7200 # Node ID e254398fcc7cafc20962ed2b7257986fb826cb5b # Parent 2e7225dbd9b27d887cfe718ea6a37aa246afa7de removed Container classes in favor of std::vector diff -r 2e7225dbd9b2 -r e254398fcc7c sources/basics.h --- a/sources/basics.h Wed Jan 27 13:08:51 2021 +0200 +++ b/sources/basics.h Wed Jan 27 13:17:11 2021 +0200 @@ -29,6 +29,7 @@ */ #pragma once +#include #include #include diff -r 2e7225dbd9b2 -r e254398fcc7c sources/coloredline.cpp --- a/sources/coloredline.cpp Wed Jan 27 13:08:51 2021 +0200 +++ b/sources/coloredline.cpp Wed Jan 27 13:17:11 2021 +0200 @@ -53,7 +53,7 @@ setColor(m_activeColor, false); if (m_boldActive) - m_data << RLINE_OFF_BOLD; + m_data.push_back(RLINE_OFF_BOLD); m_final = true; } @@ -103,7 +103,7 @@ setColor(m_activeColor, false); if (m_boldActive) - m_data << RLINE_OFF_BOLD; + m_data.push_back(RLINE_OFF_BOLD); m_boldActive = false; @@ -151,7 +151,7 @@ if (isprint(ch)) { m_string += ch; - m_data << int(ch); + m_data.push_back(static_cast(ch)); ++m_length; } } @@ -161,15 +161,17 @@ void ColoredLine::activateColor(Color color, bool bold) { if (m_boldActive) - m_data << RLINE_OFF_BOLD; - + { + m_data.push_back(RLINE_OFF_BOLD); + } m_activeColor = color; m_boldActive = bold; assert(m_activeColor < 8); setColor(m_activeColor, true); - if (m_boldActive) - m_data << RLINE_ON_BOLD; + { + m_data.push_back(RLINE_ON_BOLD); + } } // ------------------------------------------------------------------------------------------------- @@ -185,7 +187,7 @@ void ColoredLine::setColor(Color a, bool on) { assert(a < 8); - m_data << (a +(on ? RLINE_ON_COLOR : RLINE_OFF_COLOR)); + m_data.push_back(a + (on ? RLINE_ON_COLOR : RLINE_OFF_COLOR)); } // ------------------------------------------------------------------------------------------------- diff -r 2e7225dbd9b2 -r e254398fcc7c sources/interface.cpp --- a/sources/interface.cpp Wed Jan 27 13:08:51 2021 +0200 +++ b/sources/interface.cpp Wed Jan 27 13:17:11 2021 +0200 @@ -167,7 +167,7 @@ m_inputHistory.clear(); m_inputHistory.push_back(""); m_outputLines.clear(); - m_outputLines << ColoredLine(); + m_outputLines.push_back(ColoredLine()); m_session.setInterface(this); resetTitle(); @@ -304,7 +304,7 @@ if (m_outputLines.size() == 1) return; - m_outputScroll = clamp(m_outputScroll, 0, m_outputLines.size() - 1); + m_outputScroll = clamp(m_outputScroll, 0, static_cast(m_outputLines.size() - 1)); int height = LINES - 3; int width = COLS - nicklistWidth(); @@ -334,7 +334,7 @@ // See if there's any more rows to use(end may be too small) if (not tightFit) { - while (end < m_outputLines.size()) + while (end < static_cast(m_outputLines.size())) { int rows = m_outputLines[end].rows(width); @@ -963,12 +963,12 @@ { if (ch == '\n') { - m_outputLines.last().finalize(); - m_outputLines << ColoredLine(); + zfc::last(m_outputLines).finalize(); + m_outputLines.push_back({}); continue; } - if (m_outputLines.last().length() == 0) + if (zfc::last(m_outputLines).length() == 0) { time_t now; time(&now); @@ -976,14 +976,14 @@ strftime(timestamp, sizeof timestamp, "[%H:%M:%S] ", localtime(&now)); for (char ch : String(timestamp)) - m_outputLines.last().addChar(ch); + zfc::last(m_outputLines).addChar(ch); } // Remove some lines if there's too many of them. 20,000 should be enough, I hope. while (m_outputLines.size() > 20000) - m_outputLines.remove_at(0); + m_outputLines.erase(m_outputLines.begin()); - m_outputLines.last().addChar(ch); + zfc::last(m_outputLines).addChar(ch); } m_needOutputRender = true; diff -r 2e7225dbd9b2 -r e254398fcc7c sources/list.h --- a/sources/list.h Wed Jan 27 13:08:51 2021 +0200 +++ b/sources/list.h Wed Jan 27 13:17:11 2021 +0200 @@ -29,357 +29,19 @@ */ #pragma once +#include #include "basics.h" -#include -#include -#include -#include -#include #include "range.h" BEGIN_ZFC_NAMESPACE -// ------------------------------------------------------------------------------------------------- -// -template -class Container -{ -public: - typedef typename C::iterator Iterator; - typedef typename C::const_iterator ConstIterator; - typedef typename C::reverse_iterator ReverseIterator; - typedef typename C::const_reverse_iterator ConstReverseIterator; - typedef Container Self; - - Container(){} - - Container (int numvalues) : - m_container (numvalues) {} - - Container (const C& other) : - m_container (other) {} - - Container(std::initializer_list initializerList) : - m_container(initializerList) {} - - T& append (const T& value) - { - m_container.push_back (value); - return m_container[m_container.size() - 1]; - } - - void append(const T* values, size_t numValues) - { - size_t i0 = size(); - resize(size() + numValues); - - for (size_t i : range(numValues)) - (*this)[i0 + i] = values[i]; - } - - Iterator begin() - { - return m_container.begin(); - } - - ConstIterator begin() const - { - return m_container.begin(); - } - - void clear() - { - m_container.clear(); - } - - bool contains (const T& a) const - { - return std::find (m_container.begin(), m_container.end(), a) != m_container.end(); - } - - ConstReverseIterator crbegin() const - { - return m_container.rbegin(); - } - - ConstReverseIterator crend() const - { - return m_container.rend(); - } - - const C& container() const - { - return m_container; - } - - Iterator end() - { - return m_container.end(); - } - - ConstIterator end() const - { - return m_container.end(); - } - - Iterator find (const T& needle) - { - auto it = std::find (m_container.begin(), m_container.end(), needle); - - if (it == m_container.end()) - return end(); - - return it; - } - - ConstIterator find (const T& needle) const - { - auto it = std::find (m_container.begin(), m_container.end(), needle); - - if (it == m_container.end()) - return end(); - - return it; - } - - Iterator find (bool (*func)(T const&)) - { - for (Iterator it = begin(); it != end(); ++it) - { - if (func (*it)) - return it; - } - - return end(); - } - - ConstIterator find (bool (*func)(T const&)) const - { - for (ConstIterator it = begin(); it != end(); ++it) - { - if (func (*it)) - return it; - } - - return end(); - } - - T& first() - { - return *begin(); - } - - const T& first() const - { - return *begin(); - } - - void insert (int pos, const T& value) - { - m_container.insert (m_container.begin() + pos, value); - } - - bool is_empty() const - { - return size() == 0; - } - - T& last() - { - return *(end() - 1); - } - - const T& last() const - { - return *(end() - 1); - } - - void merge (const Self& other) - { - int oldsize = size(); - resize (size() + other.size()); - std::copy (other.begin(), other.end(), begin() + oldsize); - } - - bool pop (T& val) - { - if (is_empty()) - return false; - - val = m_container[size() - 1]; - m_container.erase (m_container.end() - 1); - return true; - } - - T& prepend (const T& value) - { - m_container.push_front (value); - return m_container[0]; - } - - ReverseIterator rbegin() - { - return m_container.rbegin(); - } - - void remove_at (int pos) - { - assert (pos < size()); - m_container.erase (m_container.begin() + pos); - } - - void remove_duplicates() - { - sort(); - resize (std::distance (begin(), std::unique (begin(), end()))); - } - - void remove_one (const T& valueToRemove) - { - auto it = std::find (m_container.begin(), m_container.end(), valueToRemove); - - if (it != m_container.end()) - m_container.erase (it); - } - - ReverseIterator rend() - { - return m_container.rend(); - } - - void resize (int size) - { - m_container.resize (size); - } - - Self reverse() const - { - Self rev; - std::copy (rbegin(), rend(), rev.begin()); - return rev; - } - - int size() const - { - return m_container.size(); - } - - void sort() - { - std::sort (begin(), end()); - } - - Self splice(int start, int end, int step = 1) const - { - start = clamp(start, 0, size()); - end = clamp(end, 0, size()); - - if (end <= start) - { - return Self(); - } - else - { - Self result; - - for (int i : range(start, end, step)) - result << operator[] (i); - - return result; - } - } - - Self splice(Range& range) const - { - return splice(range.min(), range.max(), range.step()); - } - - Self& operator<< (const T& value) - { - append (value); - return *this; - } - - Self& operator<< (const Self& vals) - { - merge (vals); - return *this; - } - - T& operator[] (int n) - { - assert (n < size()); - return m_container[n]; - } - - const T& operator[] (int n) const - { - assert (n < size()); - return m_container[n]; - } - - Self operator[] (Range const& n) const - { - return splice (n); - } - - Self operator+ (const Self& other) const - { - Self out (*this); - out.merge (other); - return out; - } - -protected: - C m_container; -}; - -// ------------------------------------------------------------------------------------------------- -// -template -Container& operator>> (const T& value, Container& haystack) -{ - haystack.prepend (value); - return haystack; -} - -// ------------------------------------------------------------------------------------------------- -// +template +using Vector = std::vector; template -class Vector : public Container > +auto& last(T& container) { -public: - typedef Container > Super; - - Vector(){} - - Vector(int numvalues) : - Super(numvalues){} - - Vector (T* data, size_t length) : - Super (std::vector (data, data + length)) {} - - Vector(std::initializer_list initializerList) : - Super(initializerList) {} - - Vector(const Super& other) : - Super(other) {} - - T* data() - { - return Super::m_container.data(); - } - - const T* data() const - { - return Super::m_container.data(); - } - - operator const T*() const - { - return data(); - } -}; + return *(container.begin() + container.size() - 1); +} using ByteArray = std::vector; class String quote(const ByteArray& bytes); diff -r 2e7225dbd9b2 -r e254398fcc7c sources/mystring.cpp --- a/sources/mystring.cpp Wed Jan 27 13:08:51 2021 +0200 +++ b/sources/mystring.cpp Wed Jan 27 13:17:11 2021 +0200 @@ -380,7 +380,7 @@ // vsnprintf needs more space, so we have to allocate a new buffer and try again. Vector newBuffer(length + 1); vsnprintf(newBuffer.data(), length + 1, formatString, argsCopy); - m_string = newBuffer; + m_string = newBuffer.data(); } }