scool r151: mini-std: Improve the hierarchy

URL: https://svn.lrde.epita.fr/svn/scool/branches/scool-ng ChangeLog: 2009-04-29 Warren Seine <soow@lrde.epita.fr> mini-std: Improve the hierarchy. * mini-std.hh: Still not compiling. Added Object (root of the hierarchy). Replaced Exact with E in templates (was SCOOP2). Added standard traits. Removed "automatic implementations" (was SCOOP2). Added Iterator abstraction. Added comments refering to Milena hierarchy. Replaced stc_typename's with typedef's when needed (was SCOOP2). Removed "impl_" prefix (was SCOOP2). Removed "bridge" (what?!). --- mini-std.hh | 474 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 419 insertions(+), 55 deletions(-) Index: branches/scool-ng/examples/mini-std/c++/mini-std.hh =================================================================== --- branches/scool-ng/examples/mini-std/c++/mini-std.hh (revision 150) +++ branches/scool-ng/examples/mini-std/c++/mini-std.hh (revision 151) @@ -34,7 +34,29 @@ namespace mstd { - // Add #include's here + // Fwd decl. + template <typename E> struct Object; + + // Object category flag type. + template <> + struct Object<void> + { + // typedef Unknown<void> super; // LOL !? + }; + + + /*! \brief Base class for almost every class defined in Milena. + * + * The parameter \a E is the exact type. + */ + template <typename E> + struct Object + { + typedef E exact_t; + typedef Object<void> category; // Default. + protected: + Object() {} + }; } // End of namespace mstd @@ -52,13 +74,10 @@ namespace mstd { // Forward declaration. - template <typename Exact> + template <typename E> struct Container; - template <typename Exact> - struct ForwardContainer; - - template <typename Exact> + template <typename E> struct Iterator; // Container category flag type. @@ -67,6 +86,13 @@ { typedef Object<void> super; }; + + // Container category flag type. + template <> + struct Iterator<void> + { + typedef Object<void> super; + }; } @@ -76,14 +102,64 @@ namespace mstd { - namespace trait // mln/core/image/image2d.hh:94 + namespace trait // See: mln/trait/image/props.hh:352 { // Undefined property tag struct undef { std::string name() const { return "undef"; } }; + namespace container + { + struct category + { + struct any + { protected: any() {} }; + + struct primary : any + { std::string name() const { return "category::primary"; } }; + + struct morpher : any + { protected: morpher() {} }; - template <typename C> - struct undefined_container_ // mln/trait/images.hh:134 + struct x_morpher : morpher + { std::string name() const { return "category::x_morpher"; } }; + }; + + struct speed + { + struct any + { protected: any() {} }; + + struct slow : any + { std::string name() const { return "speed::slow"; } }; + + struct fast : any + { std::string name() const { return "speed::fast"; } }; + + struct fastest : fast + { std::string name() const { return "speed::fastest"; } }; + }; + + struct size + { + struct any + { protected: any() {} }; + + struct huge : any + { std::string name() const { return "size::huge"; } }; + + struct regular : any + { std::string name() const { return "size::regular"; } }; + }; + } + } +} + +namespace mstd +{ + namespace trait // See: mln/core/image/image2d.hh:94 + { + template <typename T> + struct undefined_container_ // See: mln/trait/images.hh:134 { // misc typedef undef category; @@ -91,35 +167,46 @@ typedef undef size; // data (I::value) - typedef undef kind; - typedef undef nature; - typedef undef quant; + // typedef undef kind; + // typedef undef nature; + // typedef undef quant; }; - template <typename C> - struct container_ : undefined_container_<C> + template <typename T> + struct container_ : undefined_container_<T> { }; - template <typename T, typename C> - struct default_container_ : undefined_container_<C> + template <typename T> + struct default_container_ : undefined_container_<T> { + public: + typedef trait::container::speed::fast speed; }; - } -} -/*----------------------------. -| Automatic implementations. | -`----------------------------*/ + template <typename T> + struct undefined_iterator_ + { + }; -namespace mstd + + template <typename T> + struct iterator_ : undefined_iterator_<T> { }; + template <typename T> + struct default_iterator_ : undefined_iterator_<T> + { + }; + } +} + + /*---------------. | Abstractions. | `---------------*/ @@ -130,6 +217,78 @@ // Iterator. // // ---------- // + // Base class for implementation of iterator classes + + template <typename E> + struct Iterator : + public Object<E> + { + /* + bool is_valid() const; + void invalidate(); + void start(); + void next_(); + */ + + Iterator() + { + bool (E::*m1)() const = & E::is_valid; + m1 = 0; + void (E::*m2)() = & E::invalidate; + m2 = 0; + void (E::*m3)() = & E::start; + m3 = 0; + void (E::*m4)() = & E::next_; + m4 = 0; + } + + template <typename J> + Container<E>& operator=(const J& rhs) + { + /// You are assigning an iterator of a concrete type to + /// an iterator with a concept type. It does NOT work. + /// + /// You forgot a call to exact() on the left operand! + + // FIXME: Add the following check: + // mlc_abort(E)::check(); + return *this; + } + + // value_t operator*() + // { + // return *(exact(this)); + // } + + // bool operator==(const Exact& it) + // { + // return exact(this) == it; + // } + + // bool operator!=(const Exact& it) + // { + // return exact(this) == it; + // } + + /*! \brief Go to the next element. + * + * \warning This is a final method; iterator classes should not + * re-defined this method. The actual "next" operation has to be + * defined through the \em next_ method. + * + * \pre The iterator is valid. + */ + void next() + { + assert(exact(this)->is_valid()); + exact(this)->next_(); + } + + protected: + Iterator(); + }; + + // ----------- // // Container. // // ----------- // @@ -137,59 +296,109 @@ // Base class for implementation of container classes template <typename E> - struct Container : public Object< E > + struct Container : + public Object<E> { typedef Container<void> category; + template <typename J> + Container<E>& operator=(const J& rhs) + { + /// You are assigning an container of a concrete type to + /// an container with a concept type. It does NOT work. + /// + /// You forgot a call to exact() on the left operand! + + // FIXME: Add the following check: + // mlc_abort(E)::check(); + return *this; + } + /* // provided by internal::container_base: - typedef value_t; typedef size_t; typedef stdcontainer_t; + size_t size() const; + + bool empty() const; + + void clear(); + + stdcontainer_t& get_stdcontainer(); + + + // to be provided in concrete container classes: + + typedef value_t; */ protected: Container() { - // FIXME: Add check just like in mln/core/concept/image.hh + // See: mln/core/concept/image.hh:199 + + // FIXME: Add new checks like above + + // size_t (E::*m1)() const = & E::size; + // m1 = 0; + + // provided by internal::container_base: + + typedef typename E::value_t value_t; } }; - template <typename E> struct Container; - - template <typename Exact> - struct Container : virtual public Any<Exact>, - virtual public automatic::set_impl< Container, behavior::identity, Exact > - //automatic::get_impl<Container, Exact> + namespace internal + { + /// A base class for containers. + /// Parameter \p T is the container value type. + template <typename T, typename E> + struct container_base : + public Container<E> { - stc_typename(value_t); - stc_typename(size_t); - stc_typename(stdcontainer_t); + typedef std::size_t size_t; + typedef typename mstd::trait::container_<E>::stdcontainer_t stdcontainer_t; size_t size() const { - return exact(this)->impl_size(); + return exact(this)->size(); } bool empty() const { - return exact(this)->impl_empty(); + return exact(this)->empty(); } void clear() { - exact(this)->impl_clear(); + exact(this)->clear(); } stdcontainer_t& get_stdcontainer() { - return exact(this)->impl_get_stdcontainer(); + return exact(this)->get_stdcontainer(); } + + protected: + + /// Constructor without argument. + container_base(); }; + /// A base class for primary containers. + /// Parameter \p T is the container value type. + template <typename T, typename E> + struct container_primary : public container_base<T, E> + { + protected: + container_primary() { } + }; + } + + // ----------------- // // SortedContainer. // // ----------------- // @@ -206,16 +415,16 @@ // ForwardContainer. // // ------------------ // - template <typename Exact> - struct ForwardContainer : public virtual Container<Exact>, - public virtual automatic::set_impl<ForwardContainer, behavior::identity, Exact> + template <typename E> + struct ForwardContainer : public Container<E> { - typedef Container<Exact> super; - stc_typename(iter_t); + typedef Container<E> super; + + typedef typename mstd::trait::container_<E>::iter_t iter_t; iter_t begin() { - return exact(this)->impl_begin(); + return exact(this)->begin(); } }; @@ -242,13 +451,6 @@ } // End of namespace mstd. -/*---------. -| Bridge. | -`---------*/ -namespace mstd -{ - // FIXME -} // End of namespace mstd. /*------------------. | Implementations. | @@ -256,7 +458,82 @@ namespace mstd { - // FIXME: Iterators + // ------------------ // + // Forward_Iterator. // + // ------------------ // + + // Forward declarations. + template <typename T, typename E> + struct Forward_Iterator; + + + namespace trait + { + template <typename E> + struct iterator_< Forward_Iterator<E> > : + public default_iterator_< Forward_Iterator<E> > + { + typedef T container_t; + + typedef typename container_t::value_t value_t; + }; + } + + + template <typename T, typename E> + struct Forward_Iterator : + public Iterator< Forward_Iterator<T> > + { + typedef mstd::trait::iterator_<E>::container_t container_t; + + Forward_Iterator(container_t& c) : + c_ (c), + iter_ (c.get_stdcontainer().begin()) + { + } + + value_t& operator*() + { + return *iter_; + } + + Forward_Iterator<T, E>& operator++() + { + iter_++; + return *exact(this); + } + + Forward_Iterator<T, E> operator++(int) + { + current ret = *exact(this); + ++iter_; + return ret; + } + + void to_begin() + { + iter_ = c_.get_stdcontainer().begin(); + } + + void to_end() + { + iter_ = c_.get_stdcontainer().end(); + } + + bool operator==(const Forward_Iterator<T, E>& it) + { + return this->iter_ == it.iter_; + } + + bool operator!=(const Forward_Iterator<T, E>& it) + { + return this->iter_ != it.iter_; + } + + protected: + container_t& c_; + iter_t iter_; + }; // ------ // @@ -267,18 +544,105 @@ template <typename T> struct list; - namespace trait // mln/core/image/image2d.hh:94 + + namespace trait // See: mln/core/image/image2d.hh:94 { template <typename T> - struct container_< list<T> > : default_container_< T, list<T> > + struct container_< list<T> > : + public default_container_< T, list<T> > { // misc typedef trait::container::category::primary category; + + // typedef mlc::true_ is_isfrontinsertion; + // typedef mlc::true_ is_isbackinsertion; + + // typedef mlc::true_ is_mutable; + + typedef T value_t; + + typedef bi_dir_iterator< list<T> > iter_t; + + typedef std::list<T> stdcontainer_t; }; } - // FIXME: List implementation + template <typename T> + class list : + public internal::container_primary< T, list<T> > + { + public: + typedef T value_t; + + /// Constructor without argument. + list(); + + size_t size() const + { + return list_.size(); + } + + bool empty() const + { + return list_.empty(); + } + + void clear() + { + list_.clear(); + } + + stdcontainer_t& get_stdcontainer() + { + return list_; + } + + void push_front(value_t& v) + { + list_.push_front(v); + } + + void pop_front() + { + list_.pop_front(); + } + + void push_back(value_t& v) + { + list_.push_back(v); + } + + void pop_back() + { + list_.pop_back(); + } + + iter_t begin() + { + return iter_t(*this); + } + + iter_t end() + { + iter_t it(*this); + it.to_end(); + return it; + } + + value_t& front() + { + return list_.front(); + } + + value_t& back() + { + return list_.back(); + } + + protected: + stdcontainer_t list_; + }; // ---------- // // Morphers. // -- :: Warren Seine // SooW :: :: warren.seine @ gmail.com :: :: EPITA CSI 2010 ::
participants (1)
-
Warren Seine