From 159a3f448ad2caf9468921a4387c2dbafe09c3a3 Mon Sep 17 00:00:00 2001 From: John Denker Date: Tue, 31 Jul 2012 18:08:21 -0700 Subject: bring sepofra over from ~/hack/ --- tools/bad_thing.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tools/bad_thing.h (limited to 'tools/bad_thing.h') diff --git a/tools/bad_thing.h b/tools/bad_thing.h new file mode 100644 index 0000000..ee988dc --- /dev/null +++ b/tools/bad_thing.h @@ -0,0 +1,45 @@ +#ifndef BAD_THING_H +#define BAD_THING_H +class payloader{ +public: + int refcount; + char str[0]; +}; + +class bad_thing: public std::exception{ + payloader* load; + char* &raw; + +public: + virtual const char* what() const throw() { + return load->str; + } + bad_thing(); +// copy constructor. +// It's OK to copy a pointer to the refcount, +// but it would not be OK to copy the refcount! + bad_thing(const bad_thing &other) + : load(other.load), raw((char*&) load) + { + load->refcount++; + //xx std::cerr << "Copied! --> " << load->refcount << std::endl; + } + + bad_thing(const std::string msg) + : raw((char*&) load) + { + size_t len = msg.size(); + raw = new char[1+len + sizeof(payloader)]; + load->refcount = 1; + for (unsigned int ii = 0; ii < len; ii++){ + load->str[ii] = msg[ii]; + } + load->str[len] = 0; + } + ~bad_thing() throw() { + if (--load->refcount) return; + //xx std::cerr << "delete!" << std::endl; + delete[] raw; + } +}; +#endif -- cgit v1.2.3