summaryrefslogtreecommitdiff
path: root/tools/bad_thing.h
diff options
context:
space:
mode:
authorJohn Denker <jsd@av8n.com>2012-08-01 01:08:21 (GMT)
committerJohn Denker <jsd@av8n.com>2012-08-01 01:08:21 (GMT)
commit159a3f448ad2caf9468921a4387c2dbafe09c3a3 (patch)
treeb16f9593b43e0cada94d6a87c4380fd0ed1d56eb /tools/bad_thing.h
parent3f3c46c8ee8ef118ce73da7d3235364edf390cfe (diff)
bring sepofra over from ~/hack/
Diffstat (limited to 'tools/bad_thing.h')
-rw-r--r--tools/bad_thing.h45
1 files changed, 45 insertions, 0 deletions
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