aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/Self.h
diff options
context:
space:
mode:
Diffstat (limited to 'meowpp/Self.h')
-rw-r--r--meowpp/Self.h47
1 files changed, 44 insertions, 3 deletions
diff --git a/meowpp/Self.h b/meowpp/Self.h
index 94db352..6f24a06 100644
--- a/meowpp/Self.h
+++ b/meowpp/Self.h
@@ -6,8 +6,43 @@
namespace meow {
+#define DO_NOT_USE_SELF
+#ifdef DO_NOT_USE_SELF
+
+template<class Data>
+class Self {
+public:
+ enum DuplicateType {
+ COPY_FROM
+ };
+private:
+ Data data_;
+public:
+ Self( ) { }
+ Self(Data const& d ): data_(d) { }
+ Self(Self const& b, DuplicateType d): data_(b.data_) { }
+ Self(Self const& b);
+ ~Self() { }
+ Data const* operator->() const { return &data_; }
+ Data * operator->() { return &data_; }
+ Self& operator()() const { return *((Self*)this); }
+ Self const& copyFrom(Self const& s) { data_ = s.data_; return *this; }
+ Self const& referenceFrom(Self const& s) {
+ return copyFrom(s);
+ }
+ Self const& duplicateFrom(Self const& s, DuplicateType t) {
+ return copyFrom(s);
+ }
+ bool same(Self const& s) const { return false; }
+ bool equal(Self const& s) const { return data_ == s.data_; }
+ bool referenceLess(Self const& s) const { return (this < &s); }
+ void operator=(Self const& a);
+};
+
+#else
+
/*!
- *@brief A little class use for packing the data part of another class.
+ *@brief A tiny class use for packing the data part of another class.
* With this technique, it can achieve Copy-On-Write(COR) mechanism at
* background and have a reference mechanism which much more flexible
* then the one C++ has.
@@ -98,7 +133,7 @@ namespace meow {
*@author cathook
*
*@warning This class disabled the method \c operator= and copy constructor
- * in order to prevent upexplicit default behavior, so if you want
+ * in order to prevent unexplicit default behavior, so if you want
* to have one of them (or both), you must implement yourself
*/
template<class Data>
@@ -128,6 +163,7 @@ private:
}
~Kernel() {
+ delete data_;
}
};
@@ -139,6 +175,9 @@ private:
if (pointer_->counter_ <= 0) {
delete pointer_;
}
+ else if (pointer_->master_ == this) {
+ pointer_->master_ = NULL;
+ }
}
public:
Body( ): pointer_(new Kernel(this )), counter_(1) { }
@@ -166,7 +205,7 @@ private:
if (pointer_->counter_ > 1) {
--(pointer_->counter_);
Kernel* dupl = new Kernel(this, *pointer_->data_);
- if (pointer_->master_ == this || pointer_->master_ == NULL) {
+ if (pointer_->master_ == this) {
std::swap(pointer_->data_, dupl->data_);
pointer_->master_ = NULL;
}
@@ -332,6 +371,8 @@ public:
void operator=(Self const& a);
};
+#endif
+
} // meow
#endif // Self_h__