Files

135 lines
2.3 KiB
C++
Raw Permalink Normal View History

2019-01-18 21:27:51 +01:00
#pragma once
2014-01-31 08:23:00 +13:00
#include <iostream>
2019-01-18 17:04:44 +01:00
#include <string>
2014-01-31 08:23:00 +13:00
2019-01-18 21:27:51 +01:00
#include <config.hpp>
#ifdef MEMDEBUG
2019-01-18 17:04:44 +01:00
void trackmem(bool enable);
void checkmem(std::ostream& out);
2019-01-18 21:27:51 +01:00
#else
static inline void trackmem(bool){};
static inline void checkmem(std::ostream&){};
#endif
2014-01-31 08:23:00 +13:00
//some stuff that should be in std libs
2019-01-18 17:04:44 +01:00
int atoi(const std::string& s);
double atof(const std::string& s);
std::string itoa(int n);
std::string ftoa(float n);
std::string tolower(const std::string& s);
std::string toupper(const std::string& s);
std::string fullfilename(const std::string& t);
std::string filenamepath(const std::string& t);
std::string filenamefile(const std::string& t);
2014-01-31 08:23:00 +13:00
//lazy version of auto_ptr
2019-01-19 18:31:46 +01:00
/*template<class T>
2019-01-18 17:04:44 +01:00
class a_ptr {
public:
a_ptr(T* t = 0) : t(t) {}
~a_ptr()
{
delete t;
}
a_ptr& operator=(T* t)
{
this->t = t;
return *this;
}
T& operator*() const
{
return *t;
}
T* operator->() const
{
return t;
}
operator T&() const
{
return *t;
}
operator T*() const
{
return t;
}
T* release()
{
T* tt = t;
t = 0;
return tt;
}
private:
T* t;
2019-01-19 18:31:46 +01:00
};*/
2014-01-31 08:23:00 +13:00
//Speed-up for SLOW sstream
2019-01-18 17:04:44 +01:00
class qstreambuf : public std::streambuf {
public:
2014-01-31 08:23:00 +13:00
qstreambuf();
~qstreambuf();
2019-01-18 17:04:44 +01:00
int size(); //bytes unread
char* data(); //start of bytes unread
private:
char* buf;
2014-01-31 08:23:00 +13:00
int_type underflow();
2019-01-18 17:04:44 +01:00
int_type overflow(int_type c);
2014-01-31 08:23:00 +13:00
};
template<class T>
2019-01-18 17:04:44 +01:00
class pool {
T* free;
enum { N = 512 };
public:
typedef size_t size_type;
2014-01-31 08:23:00 +13:00
typedef ptrdiff_t difference_type;
2019-01-18 17:04:44 +01:00
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
pointer address(reference q) const
{
return &q;
}
const_pointer address(const_reference q) const
{
return &q;
}
pool() : free(0) {}
pointer allocate(size_type n, const void*)
{
clog << "Allocating " << n << endl;
if (n > 1)
return new T[n];
if (!free) {
free = (T*)new char[sizeof(T) * N];
for (int k = 0; k < N - 1; ++k)
*(T**)(free + k) = free + k + 1;
*(T**)(free + N - 1) = 0;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:44 +01:00
T* t = free;
free = *(T**)t;
2014-01-31 08:23:00 +13:00
return t;
}
2019-01-18 17:04:44 +01:00
void deallocate(pointer q, size_type n)
{
clog << "Deallocating " << n << endl;
while (n-- > 0) {
*(T**)q = free;
*(T**)free = q;
2014-01-31 08:23:00 +13:00
++q;
}
}
2019-01-18 17:04:44 +01:00
void construct(pointer p, const T& q)
{
new (p) T(q);
}
void destroy(pointer p)
{
p->~T();
}
2014-01-31 08:23:00 +13:00
};