Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | german diago <germandiago@gmail.com> |
|---|---|
| Newsgroups | comp.std.c++ |
| Subject | Towards an object class for C++ |
| Date | 2012-05-02 11:46 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <18698075.117.1335982910981.JavaMail.geo-discussion-forums@pbfk7> (permalink) |
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<T> 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<T> with key typeid(T).name(), to be able to cast back to
the concrete type again later.
template <class T>
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<T> function objects in a global container
for the class to be able to cast back to ObjectModel<T> type.
After that, I can cast to the base class type, but I cannot apply
dynamic_cast<BaseClass*> 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 <class T>
Object(T obj);
Object(const Object &) = delete;
Object & operator=(const Object &) = delete;
Object(Object &&) = default;
Object & operator=(Object &&) = default;
template <class T>
bool isA() const;
template <class T>
T extractAs() const;
template <class T>
T & getReference() const;
template <class T>
T & getConstReference() const;
bool isPolymorphic() const;
std::string toString() const;
private:
std::unique_ptr<ObjectInterface> 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 T>
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 ]
Back to comp.std.c++ | Previous | Next — Next in thread | Find similar
Towards an object class for C++ german diago <germandiago@gmail.com> - 2012-05-02 11:46 -0700 Re: Towards an object class for C++ Daniel Krügler<daniel.kruegler@googlemail.com> - 2012-05-04 11:23 -0700
csiph-web