Path: csiph.com!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: german diago Newsgroups: comp.std.c++ Subject: Towards an object class for C++ Date: Wed, 2 May 2012 11:46:42 -0700 (PDT) Organization: http://groups.google.com Lines: 139 Sender: std-cpp-request@vandevoorde.com Approved: james.dennett@gmail.com Message-ID: <18698075.117.1335982910981.JavaMail.geo-discussion-forums@pbfk7> NNTP-Posting-Host: QmlJtH0FDkN4l/OkfC2ne0b5Sg8b5Kowuf7R9S7BgLY= Content-Type: text/plain; charset=ISO-8859-1 X-Trace: news.albasani.net ICX7aGDnHCVFHVYPgoiKBOiwpH5VUgvLruUoYcWaIUZHK2JTkJK65betuaBhQ17pECRU++DMw0zth3SdvWYqHg== X-Complaints-To: abuse@albasani.net NNTP-Posting-Date: Wed, 2 May 2012 18:46:44 +0000 (UTC) X-Mailer: Perl5 Mail::Internet v2.05 X-Submission-Address: std-cpp-submit@vandevoorde.com Cancel-Lock: sha1:Lv1NW7bCNzbrsbP9HjXxLTKenSw= X-Original-Date: Wed, 2 May 2012 11:21:50 -0700 (PDT) Xref: csiph.com comp.std.c++:504 Hello all, I would like to put here, in order to be discussed, what I think it's still a missing point in the C++ standard library. I'm trying to implement an object class for c++, like C# and java have, but adapted to the c++ needs. My class is targeted at c++11 (not c++03 for now). The main goals I want to achieve are: 1.- Plays well with all kind of c++ types as much as possible. 2.- It feels comfortable and high level when used, looks as a high-level type. 3.- It's safe to use, no segfaults or whatever. Use of exceptions. For my implementation, I've already decided (although not set in stone): - Use type erasure. Client types don't need to derive from any class to use object class. - Value semantics. Should be used by value, not by pointer. - Value type requirements: at least movable type. - Able to hold both polymorphic and non-polymorphic types without the held types losing polymorphism. - Convertible to string. Planned: - Serializable. - Equality comparable. - Hashable (to be useful in containers). - Assignable to concrete types (like POCO DynamicAny class http://pocoproject.org/docs/Poco.DynamicAny.html). I'm facing one very concrete problem: - I want that function getReference() from object can cast an object to its concrete type or, if polymorphic, to any of its base classes. When the held value in object is not polymorphic, it's easy, but when you want to hold a polymorphic type and be able to cast to a base class, things get more complicated. I tried with a CastTo class like the following that is stored in a global container for class Object for each concrete type held in an object. When the constructor of Object is executed: - Since the exact type of the held type T is known, I create an entry for CastTo with key typeid(T).name(), to be able to cast back to the concrete type again later. template struct CastTo { void * operator(ObjectInterface & o) const; }; The problem is that I must return a void * because I need to be able to hold a map of the CastTo function objects in a global container for the class to be able to cast back to ObjectModel type. After that, I can cast to the base class type, but I cannot apply dynamic_cast to a void pointer, so I need alternative implementations or a solution for this problem. It's working now, but it's not a safe cast. The interface for the class right now. (Still needs a lot of review) class Object { public: template Object(T obj); Object(const Object &) = delete; Object & operator=(const Object &) = delete; Object(Object &&) = default; Object & operator=(Object &&) = default; template bool isA() const; template T extractAs() const; template T & getReference() const; template T & getConstReference() const; bool isPolymorphic() const; std::string toString() const; private: std::unique_ptr held_object_; }; My ObjectInterface is a class like the following: class ObjectInterface { public: virtual std::string toString() const = 0; virtual bool isPolymorphic() const = 0; virtual ~ObjectInterface() {} }; My holder for concrete types of Objects is like this: template class ObjectModel : public ObjectInterface { public: ObjectModel(T value); ObjectModel(const ObjectModel &) = delete; ObjectModel(ObjectModel &&) = delete; ObjectModel & operator=(const ObjectModel) = delete; ObjectModel & operator=(ObjectModel &&) = delete; bool isPolymorphic() const; std::string toString() const; bool isPolymorphic_; T value_; }; Regards, Germán Diago Gómez -- [ comp.std.c++ is moderated. To submit articles, try posting with your ] [ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]