diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2013-06-11 21:55:13 -0700 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2013-06-11 23:27:33 -0700 |
commit | 8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13 (patch) | |
tree | 15e8a4841af992e17794f26fc7991ed40c35bd51 /src/common/dumb_ptr.hpp | |
parent | 8c6072df499ef9068346fbe8313b63dbba1e4e82 (diff) | |
download | tmwa-8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13.tar.gz tmwa-8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13.tar.bz2 tmwa-8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13.tar.xz tmwa-8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13.zip |
Allegedly remove all manual memory management
Diffstat (limited to 'src/common/dumb_ptr.hpp')
-rw-r--r-- | src/common/dumb_ptr.hpp | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/src/common/dumb_ptr.hpp b/src/common/dumb_ptr.hpp index 1a0b4a1..e10ca3a 100644 --- a/src/common/dumb_ptr.hpp +++ b/src/common/dumb_ptr.hpp @@ -21,8 +21,12 @@ #include "sanity.hpp" +#include <cstring> + #include <algorithm> +#include "const_array.hpp" + // unmanaged new/delete-able pointer // should be replaced by std::unique_ptr<T> template<class T> @@ -176,4 +180,199 @@ public: } }; +struct dumb_string +{ + dumb_ptr<char[]> impl; + + dumb_string() + : impl() + {} + dumb_string(char *) = delete; + // copy ctor, copy assign, and dtor are all default + + static dumb_string copy(const char *b, const char *e) + { + dumb_string rv; + rv.impl.new_((e - b) + 1); + std::copy(b, e, &rv.impl[0]); + return rv; + } + static dumb_string copy(const char *sz) + { + return dumb_string::copy(sz, sz + strlen(sz)); + } + static dumb_string copys(const std::string& s) + { + return dumb_string::copy(&*s.begin(), &*s.end()); + } + static dumb_string copyn(const char *sn, size_t n) + { + return dumb_string::copy(sn, sn + strnlen(sn, n)); + } + static dumb_string copyc(const_string s) + { + return dumb_string::copy(s.begin(), s.end()); + } + + static + dumb_string fake(const char *p) + { + dumb_string rv; + rv.impl = dumb_ptr<char[]>(const_cast<char *>(p), strlen(p)); + return rv; + } + + dumb_string dup() const + { + return dumb_string::copy(&impl[0]); + } + void delete_() + { + impl.delete_(); + } + + const char *c_str() const + { + return &impl[0]; + } + + std::string str() const + { + return c_str(); + } + + operator const_string() const + { + return const_string(c_str()); + } + + char& operator[](size_t i) const + { + return impl[i]; + } + + explicit + operator bool() const + { + return bool(impl); + } + bool operator !() const + { + return !impl; + } + +#if 0 + friend bool operator == (dumb_string l, dumb_string r) + { + return l.impl == r.impl; + } + friend bool operator != (dumb_string l, dumb_string r) + { + return !(l == r); + } +#endif +}; + +namespace operators +{ + inline + bool operator == (dumb_string l, dumb_string r) + { + return strcmp(l.c_str(), r.c_str()) == 0; + } + inline + bool operator != (dumb_string l, dumb_string r) + { + return strcmp(l.c_str(), r.c_str()) != 0; + } + inline + bool operator < (dumb_string l, dumb_string r) + { + return strcmp(l.c_str(), r.c_str()) < 0; + } + inline + bool operator <= (dumb_string l, dumb_string r) + { + return strcmp(l.c_str(), r.c_str()) <= 0; + } + inline + bool operator > (dumb_string l, dumb_string r) + { + return strcmp(l.c_str(), r.c_str()) > 0; + } + inline + bool operator >= (dumb_string l, dumb_string r) + { + return strcmp(l.c_str(), r.c_str()) >= 0; + } + + inline + bool operator == (const char *l, dumb_string r) + { + return strcmp(l, r.c_str()) == 0; + } + inline + bool operator != (const char *l, dumb_string r) + { + return strcmp(l, r.c_str()) != 0; + } + inline + bool operator < (const char *l, dumb_string r) + { + return strcmp(l, r.c_str()) < 0; + } + inline + bool operator <= (const char *l, dumb_string r) + { + return strcmp(l, r.c_str()) <= 0; + } + inline + bool operator > (const char *l, dumb_string r) + { + return strcmp(l, r.c_str()) > 0; + } + inline + bool operator >= (const char *l, dumb_string r) + { + return strcmp(l, r.c_str()) >= 0; + } + + inline + bool operator == (dumb_string l, const char *r) + { + return strcmp(l.c_str(), r) == 0; + } + inline + bool operator != (dumb_string l, const char *r) + { + return strcmp(l.c_str(), r) != 0; + } + inline + bool operator < (dumb_string l, const char *r) + { + return strcmp(l.c_str(), r) < 0; + } + inline + bool operator <= (dumb_string l, const char *r) + { + return strcmp(l.c_str(), r) <= 0; + } + inline + bool operator > (dumb_string l, const char *r) + { + return strcmp(l.c_str(), r) > 0; + } + inline + bool operator >= (dumb_string l, const char *r) + { + return strcmp(l.c_str(), r) >= 0; + } +} + +inline +const char *convert_for_printf(dumb_string ds) +{ + return ds.c_str(); +} + #endif // TMWA_COMMON_DUMB_PTR_HPP |