Created
December 11, 2014 19:12
-
-
Save dgodfrey206/4ae48035770ab8f23629 to your computer and use it in GitHub Desktop.
Size of buffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sstream> | |
#include <iostream> | |
template<typename Tag, typename Tag::type M> | |
struct grant_access | |
{ | |
friend typename Tag::type get(Tag) { return M; } | |
}; | |
template<class cT, long n> | |
struct streambuf_tag | |
{ | |
typedef cT* (std::basic_streambuf<cT>::*type)() const; | |
friend type get(streambuf_tag<cT, n>); | |
}; | |
template struct grant_access<streambuf_tag<char, 0>, &std::streambuf::pbase>; | |
template struct grant_access<streambuf_tag<char, 1>, &std::streambuf::pptr>; | |
template<class cT> | |
std::ptrdiff_t buffer_size(std::basic_ostream<cT> const& os) | |
{ | |
std::basic_streambuf<cT>* rdbuf = os.rdbuf(); | |
auto pbase = (rdbuf->*get(streambuf_tag<cT, 0>{}))(), | |
pptr = (rdbuf->*get(streambuf_tag<cT, 1>{}))(); | |
return pptr - pbase; | |
} | |
template<class cT> | |
std::ptrdiff_t buffer_size(std::basic_istringstream<cT> const& is) | |
{ | |
return const_cast<std::basic_istringstream<cT>&>(is).rdbuf()->in_avail(); | |
} | |
int main() | |
{ | |
std::istringstream os("fdfd"); | |
std::cout << buffer_size(os); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment