finalcut/src/include/final/ftermbuffer.h

230 lines
7.3 KiB
C
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* ftermbuffer.h - Buffer for virtual terminal strings *
* *
* This file is part of the Final Cut widget toolkit *
* *
* Copyright 2017-2019 Markus Gans *
2017-11-04 07:03:53 +01:00
* *
* The Final Cut is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License *
* as published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* The Final Cut is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
/* Standalone class
*
*
*
* FTermBuffer
*
*/
#ifndef FTERMBUFFER_H
#define FTERMBUFFER_H
#if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
#error "Only <final/final.h> can be included directly."
#endif
#include <sstream> // std::stringstream
2017-09-11 03:06:02 +02:00
#include <string>
#include <vector>
namespace finalcut
{
// class forward declaration
class FColorPair;
//----------------------------------------------------------------------
// class FTermBuffer
//----------------------------------------------------------------------
class FTermBuffer
{
2017-09-11 03:06:02 +02:00
public:
// Typedef
typedef std::vector<charData> charDataVector;
typedef charDataVector::iterator iterator;
typedef charDataVector::const_iterator const_iterator;
2017-09-11 03:06:02 +02:00
// Constructor
FTermBuffer() = default;
template<typename Iterator>
FTermBuffer (Iterator, Iterator);
2017-09-11 03:06:02 +02:00
// Destructor
virtual ~FTermBuffer();
// Overloaded operators
2019-02-24 00:25:36 +01:00
template <typename typeT>
FTermBuffer& operator << (const typeT&);
FTermBuffer& operator << (const charDataVector&);
2019-02-24 00:25:36 +01:00
FTermBuffer& operator << (const std::string&);
FTermBuffer& operator << (const std::wstring&);
FTermBuffer& operator << (const FColorPair&);
2017-09-11 03:06:02 +02:00
// Non-member operators
2019-02-24 00:25:36 +01:00
friend charDataVector& operator << ( charDataVector&
, const FTermBuffer& );
2017-09-11 03:06:02 +02:00
// Accessors
virtual const char* getClassName() const;
std::size_t getLength() const;
2019-02-24 00:25:36 +01:00
const charDataVector& getBuffer() const;
2017-09-11 03:06:02 +02:00
// Inquiry
bool isEmpty() const;
// Methods
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
charData front() const;
charData back() const;
const FString toString() const;
2017-09-11 03:06:02 +02:00
void clear();
template<typename... Args>
int writef (const FString, Args&&...);
2017-09-11 03:06:02 +02:00
int write (const FString&);
int write (wchar_t);
void write (const FColorPair&);
2017-09-11 03:06:02 +02:00
FTermBuffer& write ();
private:
2019-02-24 00:25:36 +01:00
charDataVector data{};
};
// FTermBuffer inline functions
//----------------------------------------------------------------------
template<typename Iterator>
inline FTermBuffer::FTermBuffer(Iterator first, Iterator last)
{
data.assign(first, last);
}
//----------------------------------------------------------------------
2019-02-24 00:25:36 +01:00
template <typename typeT>
inline FTermBuffer& FTermBuffer::operator << (const typeT& s)
{
std::wostringstream outstream;
2019-02-24 00:25:36 +01:00
outstream << s;
if ( ! outstream.str().empty() )
write (outstream.str());
return *this;
}
//----------------------------------------------------------------------
inline FTermBuffer& FTermBuffer::operator << (const charDataVector& vec)
{
for (auto&& tc : vec)
data.push_back(tc);
return *this;
}
2019-02-24 00:25:36 +01:00
//----------------------------------------------------------------------
inline FTermBuffer& FTermBuffer::operator << (const std::string& string)
{
write (string);
return *this;
}
//----------------------------------------------------------------------
inline FTermBuffer& FTermBuffer::operator << (const std::wstring& wstring)
{
write (wstring);
return *this;
}
//----------------------------------------------------------------------
inline FTermBuffer& FTermBuffer::operator << (const FColorPair& pair)
{
write (pair);
return *this;
}
//----------------------------------------------------------------------
inline const char* FTermBuffer::getClassName() const
{ return "FTermBuffer"; }
//----------------------------------------------------------------------
inline std::size_t FTermBuffer::getLength() const
{ return data.size(); }
2019-02-24 00:25:36 +01:00
//----------------------------------------------------------------------
inline const FTermBuffer::charDataVector& FTermBuffer::getBuffer() const
{ return data; }
//----------------------------------------------------------------------
inline bool FTermBuffer::isEmpty() const
{ return data.empty(); }
//----------------------------------------------------------------------
inline FTermBuffer::iterator FTermBuffer::begin()
{ return data.begin(); }
//----------------------------------------------------------------------
inline FTermBuffer::iterator FTermBuffer::end()
{ return data.end(); }
//----------------------------------------------------------------------
inline FTermBuffer::const_iterator FTermBuffer::begin() const
{ return data.begin(); }
//----------------------------------------------------------------------
inline FTermBuffer::const_iterator FTermBuffer::end() const
{ return data.end(); }
//----------------------------------------------------------------------
inline charData FTermBuffer::front() const
{ return data.front(); }
//----------------------------------------------------------------------
inline charData FTermBuffer::back() const
{ return data.back(); }
//----------------------------------------------------------------------
inline void FTermBuffer::clear()
{
data.clear();
data.shrink_to_fit();
}
//----------------------------------------------------------------------
template<typename... Args>
inline int FTermBuffer::writef (const FString format, Args&&... args)
{
static constexpr int BUFSIZE = 4096;
wchar_t buffer[BUFSIZE]{};
if ( format.isEmpty() )
return 0;
std::swprintf ( buffer, BUFSIZE
, format.wc_str(), std::forward<Args>(args)... );
FString str(buffer);
return write(str);
}
//----------------------------------------------------------------------
inline FTermBuffer& FTermBuffer::write()
{ return *this; }
} // namespace finalcut
#endif // FTERMBUFFER_H