Skip to content

Commit

Permalink
Add class TSpan<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
magiblot committed Aug 21, 2020
1 parent 477b3ae commit b376b70
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ See the [Turbo Vision 2.0 Programming Guide](https://archive.org/details/bitsave
* `TRect` methods `move`, `grow`, `intersect` and `Union` now return `TRect&` instead of being `void`, so that they can be chained.
* `TOutlineViewer` now allows the root node to have siblings.
* New class `TStringView`, which is a clone of `std::string_view`. You shouldn't need it unless you are programming in Borland C++, which has no `std::string_view`.
* New class `TSpan<T>`, a generic (and non-const) version of `TStringView`, inspired by `std::span`.
* New classes `TDrawSurface` and `TSurfaceView`, see `<include/surface.h>`.
* Unicode support, see below.

Expand Down
108 changes: 108 additions & 0 deletions include/tvision/ttypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,112 @@ extern const uchar specialChars[];
#define _genInt(i) __int__(i)
#endif

#ifdef __BORLANDC__
#define constexpr
#endif

template <class T>
class TSpan {

// This is actually a generalization of TStringView for any kind of element
// type (and with no 'const' by default).
// It exists for compatibility with Borland C++ and because std::span (C++ 20)
// may not be widely available yet.

T _FAR *ptr;
size_t len;

public:

constexpr TSpan();
constexpr TSpan(T _FAR *first, size_t n);

constexpr T _FAR * data() const;
constexpr size_t size() const;
constexpr size_t size_bytes() const;
constexpr Boolean empty() const;
constexpr T _FAR & operator[](size_t pos) const;
constexpr T _FAR & front() const;
constexpr T _FAR & back() const;

constexpr TSpan subspan(size_t pos) const;
constexpr TSpan subspan(size_t pos, size_t n) const;

};

template <class T>
inline constexpr TSpan<T>::TSpan() :
ptr(0),
len(0)
{
}

template <class T>
inline constexpr TSpan<T>::TSpan(T _FAR *first, size_t n) :
ptr(first),
len(n)
{
}

template <class T>
inline constexpr T _FAR * TSpan<T>::data() const
{
return ptr;
}

template <class T>
inline constexpr size_t TSpan<T>::size() const
{
return len;
}

template <class T>
inline constexpr size_t TSpan<T>::size_bytes() const
{
return size()*sizeof(T);
}

template <class T>
inline constexpr Boolean TSpan<T>::empty() const
{
return size() == 0;
}

template <class T>
inline constexpr T _FAR & TSpan<T>::operator[](size_t pos) const
{
return ptr[pos];
}

template <class T>
inline constexpr T _FAR & TSpan<T>::front() const
{
return ptr[0];
}

template <class T>
inline constexpr T _FAR & TSpan<T>::back() const
{
return ptr[len - 1];
}

template <class T>
inline constexpr TSpan<T> TSpan<T>::subspan(size_t pos) const
{
return TSpan<T>(ptr + pos, len - pos);
}

template <class T>
inline constexpr TSpan<T> TSpan<T>::subspan(size_t pos, size_t n) const
{
size_t tail = len - pos;
if (n > tail)
n = tail;
return TSpan<T>(ptr + pos, n);
}

#ifdef __BORLANDC__
#undef constexpr
#endif

#endif // __TTYPES_H

0 comments on commit b376b70

Please sign in to comment.