aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/oo/ObjTypes.h
diff options
context:
space:
mode:
Diffstat (limited to 'meowpp/oo/ObjTypes.h')
-rw-r--r--meowpp/oo/ObjTypes.h202
1 files changed, 202 insertions, 0 deletions
diff --git a/meowpp/oo/ObjTypes.h b/meowpp/oo/ObjTypes.h
new file mode 100644
index 0000000..4092d63
--- /dev/null
+++ b/meowpp/oo/ObjTypes.h
@@ -0,0 +1,202 @@
+#ifndef oo_ObjType_H__
+#define oo_ObjType_H__
+
+#include "../Self.h"
+#include "ObjBase.h"
+
+#include <cstdlib>
+#include <cstdio>
+
+namespace meow {
+
+/*!
+ * @brief 純粹把給定的 \c Type 包起來, 變成繼承自 ObjBase
+ *
+ * @author cathook
+ */
+template<class Type, class ReaderWriter>
+class ObjType: public ObjBase {
+private:
+ struct Myself {
+ Type data_;
+ Myself() {
+ }
+ ~Myself() {
+ }
+ Myself copyFrom(Myself const& b) {
+ data_ = b.data_;
+ }
+ };
+ Self<data_> const self;
+public:
+ //! @brief constructor
+ ObjType(): self(true) {
+ }
+
+ //! @brief constructor, 並且copy資料
+ ObjType(ObjType const& a): self(false) {
+ self().copyFrom(a.self);
+ }
+
+ //! @brief constructor, 並且給值
+ ObyType(Type const& t): self(true) {
+ self().data_ = t;
+ }
+
+ ~ObjType() {
+ }
+
+ ObjType& copyFrom(ObjType const& a) {
+ self().copyFrom(a.self);
+ return *this;
+ }
+
+ ObjType& referenceFrom(ObjType const& a) {
+ self().referenceFrom(a.self);
+ return *this;
+ }
+
+ Type const& access() const {
+ return self->data_;
+ }
+
+ Type& modify() {
+ return self()->data_;
+ }
+
+ ObjType& operator=(ObjType const& a) {
+ return copyFrom(a);
+ }
+
+ Type const& operator()() const {
+ return access();
+ }
+
+ Type& operator()() {
+ return modify();
+ }
+
+ bool write(FILE* f, bool bin, unsigned int fg) const {
+ return ReaderWriter::write(f, bin, fg, self->data_);
+ }
+
+ bool read(FILE* f, bool bin, unsigned int fg) {
+ return ReaderWriter::read(f, bin, fg, &(self()->data_));
+ }
+
+ ObjBase* create() const {
+ return new ObjType();
+ }
+
+ ObjBase* copyFrom(ObjBase const* b) {
+ return &(copyFrom(&(ObjType const*)b));
+ }
+
+ char const* ctype() const {
+ static char const* ptr = typeid(*this).name();
+ return ptr;
+ }
+
+ std::string type() const {
+ return std::string(ctype());
+ }
+};
+
+class ReaderWriter_int {
+public:
+ static bool write(FILE* f, bool bin, unsigned int fg, int const& k) {
+ if (bin) {
+ return (fwrite(&k, sizeof(k), 1, f) == 1);
+ }
+ else {
+ return (fprintf(f, "%d\n", k) == 1);
+ }
+ }
+ static bool read(FILE* f, bool bin, unsigned int fg, int* k) {
+ if (bin) {
+ return (fread(k, sizeof(k), 1, f) == 1);
+ }
+ else {
+ return (fscanf(f, "%d", k) == 1);
+ }
+ }
+};
+
+class ReaderWriter_size_t {
+public:
+ static bool write(FILE* f, bool bin, unsigned int fg, size_t const& k) {
+ if (bin) {
+ return (fwrite(&k, sizeof(k), 1, f) == 1);
+ }
+ else {
+ return (fprintf(f, "%lu\n", k) == 1);
+ }
+ }
+ static bool read(FILE* f, bool bin, unsigned int fg, size_t* k) {
+ if (bin) {
+ return (fread(k, sizeof(k), 1, f) == 1);
+ }
+ else {
+ return (fscanf(f, "%lu", k) == 1);
+ }
+ }
+};
+
+class ReaderWriter_double {
+public:
+ static bool write(FILE* f, bool bin, unsigned int fg, double const& k) {
+ if (bin) {
+ return (fwrite(&k, sizeof(k), 1, f) == 1);
+ }
+ else {
+ return (fprintf(f, "%.15f\n", k) == 1);
+ }
+ }
+ static bool read(FILE* f, bool bin, unsigned int fg, double* k) {
+ if (bin) {
+ return (fread(k, sizeof(k), 1, f) == 1);
+ }
+ else {
+ return (fscanf(f, "%lf", k) == 1);
+ }
+ }
+};
+
+class ReaderWriter_string {
+public:
+ static bool write(FILE* f, bool bin, unsigned int fg, std::string const& k) {
+ size_t len = k.size();
+ char const* buf = k.c_str();
+ if (bin) {
+ if (fwrite(&len, sizeof(len) , 1, f) < 1) return false;
+ if (fwrite( buf, sizeof(char), len, f) < len) return false;
+ }
+ else {
+ if (fprintf(f, "%s\n", buf) < 1) return false;
+ }
+ return true;
+ }
+ static bool read(FILE* f, bool bin, unsigned int fg, std::string* k) {
+ size_t len;
+ char buf[2048];
+ if (bin) {
+ if (fread(&len, sizeof(len) , 1, f) < 1) return false;
+ if (fread( buf, sizeof(char), len, f) < len) return false;
+ buf[len] = '\0';
+ }
+ else {
+ if (fscanf(f, "%s", buf) < 1) return false;
+ }
+ (*k) = buf;
+ return true;
+ }
+};
+
+typedef ObjType<int , ReaderWriter_int > ObjInt;
+typedef ObjType<size_t , ReaderWriter_size_t> ObjSizeT;
+typedef ObjType<double , ReaderWriter_double> ObjDouble;
+typedef ObjType<std::string, ReaderWriter_string> ObjString;
+
+}
+
+#endif // oo_ObjType_H__