Skip to content

Commit

Permalink
+cxx/ClassObject.hxx based-on Java superclass
Browse files Browse the repository at this point in the history
  https://docs.oracle.com/javase%2Ftutorial%2F/java/IandI/objectclass.html
  https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html
  to assist future `Java` ports from C++
  Is some slowdown to use inheritance+polymorphism with all classes, https://stackoverflow.com/questions/8824587/what-is-the-purpose-of-the-final-keyword-in-c11-for-functions/78680754#78680754 shows howto use `final` to fix this

[Work-in-progress](#10), have just commit this to have a backup (versus local
branch).
Am going to replace tip tomorrow, after which you do `git pull
--rebase`, which (unless you modify `cxx/ClassObjext.hxx`) does not
require input to fix this
  • Loading branch information
SwuduSusuwu committed Jun 29, 2024
1 parent 0aa13d1 commit 080ecac
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions cxx/ClassObject.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Dual licenses: choose "Creative Commons" or "Apache 2" (allows all uses) */
#ifndef INCLUDES_cxx_ClassObject_hxx
#define INCLUDES_cxx_ClassObject_hxx
#include <iostream> /* operator+() */
#include <vector> /* std::vector */
#include <ctype.h> /* size_t */
#include <assert.h> /* assert */
typedef enum ObjectCloneAs {
objectCloneAsShallow, /* `memcopy` */
objectCloneAsDeep, /* Recursive `new` */
objectCloneAsCoW, /* Copy-on-Write */
objectCloneAsReferenceCount, /* std::shared_ptr */
objectCloneAsNone /* (!isCloneabble()) */
} ObjectCloneAs;
typedef class Object { /* `Java` superclass based-on https://docs.oracle.com/javase%2Ftutorial%2F/java/IandI/objectclass.html https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html , to assist future `Java` ports from C++ */
virtual const bool hasImplementation() const { return typeid(Object) != typeid(this); }
virtual const bool isInitialized() const;
virtual ~Object() = default; /* release resources */
virtual const bool isCloneable() { return objectCloneAsNone != cloneableAs(); }
virtual /* const requires C++26 */ bool operator==(const Object &obj) const = default;
virtual const bool equals(const Object &obj) const { return obj == *this; /* if you would override equals, just override operator==, as most C++ code uses operator== */ }
// virtual const bool equals(const Object &obj) const { return operator==(obj); /* if you would override equals, just override operator==, as most C++ code uses operator== */ }
virtual const ObjectCloneAs cloneableAs() {
return (usesDynamicAllocations() || usesFilesystem() || usesNetwork() || usesThreads()) ? objectCloneAsShallow : objectCloneAsNone; /* crude heuristic, override for complex classes */
}
virtual const bool isCloneableAs(ObjectCloneAs cloneAs) { return cloneableAs() == cloneAs; /* must override if multiple cloneable types */ }
virtual const Object clone() {
if(!isCloneable()) { throw std::runtime_error("Unsupported Object::clone() use"); }
return Object(*this);
}
virtual const Object cloneAs(ObjectCloneAs cloneAs) {
if(!isCloneableAs(cloneAs)) { throw std::runtime_error("Unsupported Object::cloneAs() use"); }
return Object(*this); /* if(objectCloneAsShallow != cloneAs) { should override this } */
}
virtual const std::string getNameOfClass() { return "cxx/Object"; }
virtual const Object *getBaseClassPtr() { return NULL; /* no base for root class */ }
virtual const Object &getBaseClassRef() { return *this; /* for promises not to "return NULL", use references */ }
virtual const size_t getStaticSize() { return sizeof(*this); /* sizeof vtable + member variables */ }
virtual const size_t getDynamicSize() { return 0; /* sizeof allocations from functions such as `malloc` / `new` */ }
virtual const bool usesDynamicAllocations() { /* if(uses functions suchas `malloc` / `new`) {return true;} */ return false; }
virtual const bool usesFilesystem() { /* if(uses functions suchas `fopen` / `new`) {return true;} */ return false; }
virtual const bool usesNetwork() { /* if(uses functions suchas `socket`) {return true;} */ return false; }
virtual const bool usesThreads() { /* if(uses functions suchas `pthread`) {return true;} */ return false; }
virtual const bool hasExtraVirtuals/*OtherThanObject*/() { /* if(has `virtual`s other than `Object`'s) {return true;}*/ return false; }
} Object;
/* Is some slowdown to use inheritance+polymorphism with all classes, https://stackoverflow.com/questions/8824587/what-is-the-purpose-of-the-final-keyword-in-c11-for-functions/78680754#78680754 shows howto use `final` to fix this */
#endif /* ndef INCLUDES_cxx_ClassObject_hxx */

0 comments on commit 080ecac

Please sign in to comment.