PIT/include/pit/iters.h

90 lines
2.5 KiB
C++

#include <cstdlib>
#include <string>
#include <istream>
namespace pit{
template<typename T>
class passthrough{
T underlying;
public:
passthrough<T>(T underlying): underlying(underlying){}
passthrough<T>(passthrough<T>& from): underlying(from.underlying){}
passthrough<T> operator+(std::size_t amount){
return passthrough<T>(this->underlying+amount);
}
void operator+=(std::size_t amount){
this->underlying += amount;
}
passthrough<T> operator-(std::size_t amount){
return passthrough<T>(this->underlying-amount);
}
void operator-=(std::size_t amount){
this->underlying -= amount;
}
T const& operator*(){
return this->underlying;
}
bool operator==(passthrough<T> const& rhs){
return this->underlying == rhs.underlying;
}
};
template<typename T>
class range{
private:
T beginning;
T ending;
public:
range<T>(T begin, T end):beginning(begin), ending(end){}
passthrough<T> begin(){
return passthrough<T>(this->beginning);
}
passthrough<T> end(){
return passthrough<T>(this->ending);
}
};
template<typename T>
class repeat{
T val;
public:
// noops
repeat operator+(std::size_t ){}
repeat operator-(std::size_t ){}
void operator+=(std::size_t ){}
void operator-=(std::size_t ){}
// always return false on equality to ensure it repeats forever
bool operator==(repeat const& rhs){return false;}
// if T isn't copyable then make the template arg a reference
T operator*(){
return this->val;
}
// convenience for for loops and what not
repeat begin(){
return *this;
}
repeat end(){
return *this;
}
repeat rbegin(){
return *this;
}
repeat rend(){
return *this;
}
};
template<class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT>>
class basic_stream_split {
bool ended;
std::basic_istream<CharT, Traits> stream;
public:
bool operator==(basic_stream_split const& rhs){
return this->ended == rhs;
}
// todo: +,-, +=, -=, *
};
using stream_split = basic_stream_split<char>;
using wstream_split = basic_stream_split<wchar_t>;
}