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 *
|
|
|
|
* *
|
2019-01-30 12:17:48 +01:00
|
|
|
* 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/>. *
|
|
|
|
***********************************************************************/
|
2017-07-12 01:16:57 +02:00
|
|
|
|
2017-10-02 07:32:33 +02:00
|
|
|
/* Standalone class
|
|
|
|
* ════════════════
|
|
|
|
*
|
|
|
|
* ▕▔▔▔▔▔▔▔▔▔▔▔▔▔▏
|
|
|
|
* ▕ FTermBuffer ▏
|
|
|
|
* ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▏
|
|
|
|
*/
|
2017-07-12 01:16:57 +02:00
|
|
|
|
|
|
|
#ifndef FTERMBUFFER_H
|
|
|
|
#define FTERMBUFFER_H
|
|
|
|
|
2017-10-31 00:41:59 +01:00
|
|
|
#if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
|
|
|
|
#error "Only <final/final.h> can be included directly."
|
|
|
|
#endif
|
|
|
|
|
2017-07-12 01:16:57 +02:00
|
|
|
#include <sstream> // std::stringstream
|
2017-09-11 03:06:02 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2017-07-12 01:16:57 +02:00
|
|
|
|
2018-09-20 23:59:01 +02:00
|
|
|
namespace finalcut
|
|
|
|
{
|
2017-07-12 01:16:57 +02:00
|
|
|
|
2019-07-21 23:31:21 +02:00
|
|
|
// class forward declaration
|
|
|
|
class FColorPair;
|
|
|
|
|
2017-07-12 01:16:57 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// class FTermBuffer
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class FTermBuffer
|
|
|
|
{
|
2017-09-11 03:06:02 +02:00
|
|
|
public:
|
|
|
|
// Typedef
|
2019-09-28 03:13:06 +02:00
|
|
|
typedef std::vector<charData> charDataVector;
|
|
|
|
typedef charDataVector::iterator iterator;
|
|
|
|
typedef charDataVector::const_iterator const_iterator;
|
2017-09-11 03:06:02 +02:00
|
|
|
|
|
|
|
// Constructor
|
2018-12-03 03:22:36 +01:00
|
|
|
FTermBuffer() = default;
|
2019-09-28 03:13:06 +02:00
|
|
|
template<typename Iterator>
|
|
|
|
FTermBuffer (Iterator, Iterator);
|
2018-12-24 18:11:16 +01:00
|
|
|
|
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&);
|
2019-09-28 03:13:06 +02:00
|
|
|
FTermBuffer& operator << (const charDataVector&);
|
2019-02-24 00:25:36 +01:00
|
|
|
FTermBuffer& operator << (const std::string&);
|
|
|
|
FTermBuffer& operator << (const std::wstring&);
|
2019-01-30 12:17:48 +01:00
|
|
|
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;
|
2018-10-17 22:12:52 +02:00
|
|
|
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
|
2019-09-28 03:13:06 +02:00
|
|
|
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();
|
2019-09-28 03:13:06 +02:00
|
|
|
template<typename... Args>
|
|
|
|
int writef (const FString, Args&&...);
|
2017-09-11 03:06:02 +02:00
|
|
|
int write (const FString&);
|
2019-01-30 12:17:48 +01:00
|
|
|
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{};
|
2017-07-12 01:16:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// FTermBuffer inline functions
|
2019-09-28 03:13:06 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
template<typename Iterator>
|
|
|
|
inline FTermBuffer::FTermBuffer(Iterator first, Iterator last)
|
|
|
|
{
|
|
|
|
data.assign(first, last);
|
|
|
|
}
|
|
|
|
|
2017-07-12 01:16:57 +02:00
|
|
|
//----------------------------------------------------------------------
|
2019-02-24 00:25:36 +01:00
|
|
|
template <typename typeT>
|
|
|
|
inline FTermBuffer& FTermBuffer::operator << (const typeT& s)
|
2017-07-12 01:16:57 +02:00
|
|
|
{
|
2017-08-06 17:02:19 +02:00
|
|
|
std::wostringstream outstream;
|
2019-02-24 00:25:36 +01:00
|
|
|
outstream << s;
|
|
|
|
|
|
|
|
if ( ! outstream.str().empty() )
|
|
|
|
write (outstream.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-09-28 03:13:06 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
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);
|
2017-07-12 01:16:57 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-01-30 12:17:48 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline FTermBuffer& FTermBuffer::operator << (const FColorPair& pair)
|
|
|
|
{
|
|
|
|
write (pair);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-07-12 01:16:57 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline const char* FTermBuffer::getClassName() const
|
|
|
|
{ return "FTermBuffer"; }
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-10-17 22:12:52 +02:00
|
|
|
inline std::size_t FTermBuffer::getLength() const
|
|
|
|
{ return data.size(); }
|
2017-07-12 01:16:57 +02:00
|
|
|
|
2019-02-24 00:25:36 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline const FTermBuffer::charDataVector& FTermBuffer::getBuffer() const
|
|
|
|
{ return data; }
|
|
|
|
|
2017-07-12 01:16:57 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline bool FTermBuffer::isEmpty() const
|
|
|
|
{ return data.empty(); }
|
|
|
|
|
2019-09-28 03:13:06 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
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(); }
|
|
|
|
|
2017-07-12 01:16:57 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline void FTermBuffer::clear()
|
2019-04-27 01:23:30 +02:00
|
|
|
{
|
|
|
|
data.clear();
|
|
|
|
data.shrink_to_fit();
|
|
|
|
}
|
2017-07-12 01:16:57 +02:00
|
|
|
|
2019-09-28 03:13:06 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-07-12 01:16:57 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline FTermBuffer& FTermBuffer::write()
|
|
|
|
{ return *this; }
|
|
|
|
|
2018-09-20 23:59:01 +02:00
|
|
|
} // namespace finalcut
|
|
|
|
|
2017-07-12 01:16:57 +02:00
|
|
|
#endif // FTERMBUFFER_H
|