
2007-01-30 Thierry GERAUD <theo@tegucigalpa.lrde.epita.fr> Add mini-std. * samples/mini-std: New. * samples/mini-oln/design.hh: New. Index: samples/mini-oln/design.hh =================================================================== --- samples/mini-oln/design.hh (revision 0) +++ samples/mini-oln/design.hh (revision 0) @@ -0,0 +1,461 @@ + +// Point + +Point : class = +{ +} + +point2d : class < Point = +{ + row : int; + col : int; +} + +point3d : class < Point = +{ + row : int; + col : int; + sli : int; +} + + +// Iterator + +Iterator : class = +{ + point_t : typedef < Point; + + start : () -> void; + next : () -> void; + is_valid : const () -> bool; + + auto : -> point_t; +} + + + +// Image + +Image : class = +{ + { + point_t < Point; + iter_t < Iterator; + value_t; + } + : typedef; + + "()" : const (p : point_t const&) -> value_t; + has : const (p : point_t const&) -> bool; +} + + +// Image = Image_2D | Image_3D + +Image_2D : class < Image = +{ + "()" : const (row : int, col : int) -> value_t; + nrows : const () -> int; + ncols : const () -> int; +} + +Image_3D : class < Image = +{ + "()" : const (row : int, col : int, sli : int) -> value_t; + nrows : const () -> int; + ncols : const () -> int; + nslis : const () -> int; +} + + +// Image = Image_with_Nbh | not + +Image_with_Nbh : class < Image +{ + { + niter_t < Iterator; + nbh_t < Neighborhood; + } + : typedef; + nbh : const () -> nbh_t; +} + + + +// plugs ! + +image_dim_plug : [I : type] -> type = { + switch (I.point_t) { + case point2d: return Image_2D; + case point3d: return Image_3D; + } +} + +image_nbh_plug : [I : type] -> type = { + if (I.nbh_t) + return Image_with_Nbh; + else + return Image; +} + + + +// image_entry + +image_base : class < image_dim_plug[@], + image_nbh_plug[@] = +{ + { + point_t; + iter_t; + value_t; + } + : typedef; + + "()" : const (p : point_t const&) -> value_t; + has : const (p : point_t const&) -> bool; +} + + + +// iterator2d + +iterator2d : class < Iterator = +{ + point_t : typedef := point2d; + + make : (ima : Image_2D) = { + nrows := ima.nrows; + ncols := ima.ncols; + } + + start : () -> void = { + p.row := 0; + p.col := 0; + } + + next : () -> void = { + p.col := p.col + 1; + if (p.col = ncols) { + p.col := 0; + p.row := p.row + 1; + } + } + + is_valid : const () -> bool = { + return p.row < p.nrows; + } + + auto : -> point_t = + { + return p; + } + + protected { + nrows : int; + ncols : int; + p : point_t; + } +} + + + +// image2d[T] + +image2d : [T : class] -> class < image_base = +{ + { + point_t := point2d; + iter_t := iterator2d; + value_t := T; + } + : typedef; + + make : (nrows : int, ncols : int) = + { + data.make(nrows * ncols); + } + + "()" : const (p : point_t const&) -> value_t = + { + precondition { has(p) = true } + return data.element(p.row * ncols + p.col); + } + + "()" : const (row : int, col : int) -> value_t = + { + p : point2d; + p.make(row, col); + return @[p]; + } + + has : const (p : point_t const&) -> bool = + { + precondition { data != 0 } + return p.row >= 0 and p.row < nrows and p.col >= 0 and p.col < ncols; + } + + data : vector[T]; + nrows : int; + ncols : int; +} + + + +// image_morpher + +image_morpher : class < image_base = +{ + delegatee_t : typedef; +} + + + +// fun + +fun_ : [I : class < Image, F : type] where (F.arg_t = I.value_t) -> class = +{ + { + delegatee_t := I; + value_t := F.res_t; + } + : typedef; + + ima : I; + f : F; + + "()" : const (p : point_t const&) -> value_t = + { + precondition { has(p) = true } + return f(ima(p)); + } +} + +fun1 : class = +{ + { + arg_t; + res_t; + } + : typedef; + + "()" : (ima : I < Image) -> fun_[I,@] = + { + fun_[I,@] tmp; // '@' is exact type + tmp.make(ima, @); // here '@' is *this + return tmp; + } +} + +pow : class < fun1 = +{ + n : unsigned; + + { + arg_t := double; + res_t := double; + } + : typedef; + + "()" : (x : double) -> double = + { + return std::pow(x, n); + } +} + + +// stack + +vec : [n : unsigned, T : type] -> class = +{ + data : T[n]; + + "[]" : (i : unsigned) -> T = + { + precondition { i < n } + return data[i]; + } +} + +stack_ : [n : unsigned, I : class < Image] -> class = +{ + { + delegatee_t := I; + value_t := vec[n, I.value_t]; + } + : typedef; + + ima : vec[n,I]; + + "()" : const (p : point_t const&) -> value_t = + { + precondition { has(p) = true } + value_t tmp; + for (unsigned i := 0; i < n; ++i) + tmp[i] := ima[i](p); + return tmp; + } +} + +stack : [I : class < Image] (ima_0 : I const&, ima_1 : I const&) -> stack_[2,I] = +{ + stack_[2,I] tmp; + tmp.ima[0] := ima_0; + tmp.ima[1] := ima_1; + return tmp; +} + + + +// "image + neighborhood" + +plus : [I : class < Image, N : type] where N < Neighborhood +-> class < image_morpher = +{ + { + delegatee_t := I; + } + : typedef; + + ima : I; + nbh : N; +} + + +// operator plus + +"+" : [I : class < Image, N : class < Neighborhood] (ima : I, nbh : N) -> plus[I, N] = +{ + plus[I, N] tmp; + return tmp.make(ima, nbh); +} + + + + +// ---------------------------------------------------------------------- + + + + +// "image + neighborhood" + +plus : [I : class < Image, N : type] where N < Neighborhood +-> class < image_morpher = +{ + { + delegatee_t := I; + } + : typedef; + + ima : I; + nbh : N; +} + + +// operator plus + +"+" : [I : class < Image, + T : type] (lhs : I, rhs : T) -> plus[I, T] = +{ + plus[I, T] ima; + return ima.make(lhs, rhs); +} + + + + +// unsub + +unsubset : (ima : Image_with_Subset) = { FIXME } + + + +// predicate + +predicate : [I : class < Image] -> type = { + return (I.point_t const &) -> I.value_t; +} + + + +// sub[I,P] + +sub : [I, P] where I < Iterator and P = predicate[I] +-> class < Iterator = +{ + point_t : typedef := I.point_t; + + iter : I; + pred : P; + p : point_t const&; + + make : (ima : Image) { + iter.make(unsubset(ima)); + pred := ima.pred(); + p := iter.p; + } + + invariant { + iter.is_valid() => pred(p); + } + + start : () -> void = { + iter.start(); + while (iter.is_valid() and not pred(p)) + iter.next(); + } + + next : () -> void = { + iter.next(); + while (iter.is_valid() and not pred(p)) + iter.next(); + } + + is_valid : const () -> bool = { + return iter.is_valid(); + } + + auto : -> point_t = + { + return p; + } +} + + + +// 'pipe' is "image | predicate -> sub-image" + +pipe : [I : class < Image, P : type] where P = predicate[I] +-> class < image_morpher = +{ + { + delegatee_t := I; + iter_t : sub[I.iter_t, P]; + } + : typedef; + + pred : P; + + unsubset : () -> I = + { + return ref_ima; + } + + has : const (p : point_t const&) -> bool = + { + return ref_ima.has(p) and pred(p); + } + +} + + + +// operator pipe + +"|" : [I : class < Image, + T : type] (lhs : I, rhs : T) -> pipe[I, T] = +{ + pipe[I, T] ima; + return ima.make(lhs, rhs); +} Index: samples/mini-std/cpp/impl.hh =================================================================== --- samples/mini-std/cpp/impl.hh (revision 0) +++ samples/mini-std/cpp/impl.hh (revision 0) @@ -0,0 +1,199 @@ +#ifndef MINI_STD_IMPL_HH +# define MINI_STD_IMPL_HH + +# include "concepts.hh" + + +namespace abc +{ + + + // container SWITCH-CASE + + namespace internal + { + + // front insertion + template <typename b, typename E> struct front_insertion_sequence; + template <typename E> struct front_insertion_sequence <mlc::true_, E> : virtual Front_Insertion_Sequence<E> {}; + template <typename E> struct front_insertion_sequence <mlc::false_, E> : virtual Sequence<E> {}; + + // back insertion + template <typename b, typename E> struct back_insertion_sequence; + template <typename E> struct back_insertion_sequence <mlc::true_, E> : virtual Back_Insertion_Sequence<E> {}; + template <typename E> struct back_insertion_sequence <mlc::false_, E> : virtual Sequence<E> {}; + + // random access + template <typename b, typename E> struct random_access_container; + template <typename E> struct random_access_container <mlc::true_, E> : virtual Random_Access_Container<E> {}; + template <typename E> struct random_access_container <mlc::false_, E> : virtual Container<E> {}; + + template <typename E> + struct container_switch + : front_insertion_sequence< abc_vtype(E, has_front_insertion), E >, + back_insertion_sequence < abc_vtype(E, has_back_insertion), E >, + random_access_container < abc_vtype(E, has_random_access), E > + { + protected: + container_switch() {} + }; + + + } // end of abc::internal + + + + +// container_base +// | +// +-- morpher_container +// | +// +-- primary_container +// | +// +-- primary_std_container + + + + + // Class container_base<E>. + + template <typename E> class container_base; + + template <typename E> + struct vtypes< container_base<E> > + { + typedef mlc::none super_type; + + typedef stc::abstract value_type; + typedef stc::abstract iterator; + typedef stc::abstract const_iterator; + typedef stc::abstract reference; // FIXME: or stc::final<value_type&>? + typedef stc::abstract const_reference; // FIXME: or stc::final<const value_type&>? + typedef stc::abstract size_type; + + typedef stc::abstract has_random_access; + typedef stc::abstract has_front_insertion; + typedef stc::abstract has_back_insertion; + + typedef stc::abstract tag; + }; + + template <typename E> + class container_base : public internal::container_switch<E> + { + public: + // FIXME: remove below. + typedef typename Back_Insertion_Sequence<E>::reference reference; + typedef typename Back_Insertion_Sequence<E>::const_reference const_reference; + protected: + container_base() {} + }; + + + + // Class morpher_container<E>. + + template <typename E> class morpher_container; + + template <typename E> + struct vtypes< morpher_container<E> > + { + typedef container_base<E> super_type; + typedef stc::abstract delegatee_type; + }; + + template <typename E> + class morpher_container : public container_base<E> + { + public: + stc_using_from(Container<E>, value_type); + abc_typename(delegatee_type); + + const delegatee_type& delegatee() const { return this->exact().impl_delegatee(); } + delegatee_type& delegatee() { return this->exact().impl_delegatee(); } + + protected: + morpher_container() {} + }; + + + + // Class primary_container<E>. + + template <typename E> class primary_container; + + template <typename E> + struct vtypes< primary_container<E> > + { + typedef container_base<E> super_type; + typedef mlc::none tag; + }; + + template <typename E> + class primary_container : public container_base<E> + { + public: + stc_using_from(Container<E>, value_type); + protected: + primary_container() {} + }; + + + + // Class primary_std_container<E>. + + template <typename E> class primary_std_container; + + template <typename E> + struct vtypes< primary_std_container<E> > + { + typedef primary_container<E> super_type; + typedef stc::abstract std_type; + + typedef stc_defer(std_type) container_t; + typedef stc::final<typename container_t::value_type> value_type; + typedef stc::final<typename container_t::iterator> iterator; + typedef stc::final<typename container_t::const_iterator> const_iterator; + typedef stc::final<typename container_t::reference> reference; + typedef stc::final<typename container_t::const_reference> const_reference; + typedef stc::final<typename container_t::size_type> size_type; + }; + + template <typename E> + class primary_std_container : public primary_container<E> + { + public: + typedef primary_container<E> super; + + stc_using(iterator); + stc_using(const_iterator); + stc_using(size_type); + stc_using(value_type); + + // Container. + iterator impl_begin() { return data_.begin(); } + const_iterator impl_begin() const { return data_.begin(); } + iterator impl_end() { return data_.end(); } + const_iterator impl_end() const { return data_.end(); } + size_type impl_size() const { return data_.size(); } + bool impl_empty() const { return data_.empty(); } + + // Forward_Container. + bool impl_equal(const E& rhs) const { return data_ == rhs.data_; } + bool impl_less (const E& rhs) const { return data_ < rhs.data_; } + + protected: + + primary_std_container() {} + primary_std_container(size_type n) : data_(n) {} + primary_std_container(size_type n, const value_type& t) : data_(n, t) {} + primary_std_container(const primary_std_container& rhs) : data_(rhs.data_) {} + + abc_vtype(E, std_type) data_; + }; + + +} // end of namespace abc + + +#endif // ! MINI_STD_IMPL_HH Index: samples/mini-std/cpp/vector.hh =================================================================== --- samples/mini-std/cpp/vector.hh (revision 0) +++ samples/mini-std/cpp/vector.hh (revision 0) @@ -0,0 +1,85 @@ +#ifndef MINI_STD_VECTOR_HH +# define MINI_STD_VECTOR_HH + +# include <vector> +# include "impl.hh" + + +namespace abc +{ + + + template <typename T> class vector; + + + template <typename T> + struct vtypes< vector<T> > + { + typedef vector<T> E; + typedef primary_std_container<E> super_type; + + typedef std::vector<T> std_type; + + // As Reversible_Container: + typedef typename std::vector<T>::reverse_iterator reverse_iterator; + typedef typename std::vector<T>::const_reverse_iterator const_reverse_iterator; + + // Properties. + typedef mlc::true_ has_random_access; + typedef mlc::false_ has_front_insertion; + typedef mlc::true_ has_back_insertion; + }; + + + + template <typename T> + class vector : public primary_std_container< vector<T> > + { + public: + + typedef vector<T> self_type; + typedef primary_std_container<self_type> super; + + stc_using(iterator); + stc_using(const_iterator); + stc_using(reverse_iterator); + stc_using(const_reverse_iterator); + stc_using(size_type); + stc_using(reference); + stc_using(const_reference); + stc_using(value_type); + + using super::data_; + + // Constructors: + vector() {} + vector(size_type n) : super(n) {} + vector(size_type n, const T& t) : super(n, t) {} + vector(const vector& rhs) : super(rhs) {} + + vector& operator=(const vector& rhs) { data_ = rhs.data_; return *this; } + void reserve(size_t n) { data_.reserve(n); } + + // FIXME: swap, resize + + // As Reversible_Container: + reverse_iterator impl_rbegin() { return data_.rbegin(); } + const_reverse_iterator impl_rbegin() const { return data_.rbegin(); } + reverse_iterator impl_rend() { return data_.rend(); } + const_reverse_iterator impl_rend() const { return data_.rend(); } + + // As Random_Access_Container: + reference impl_brackets(size_type n) { return data_[n]; } + const_reference impl_brackets(size_type n) const { return data_[n]; } + + // As Sequence: + iterator impl_insert(iterator p, value_type t) { return data_.insert(p, t); } + iterator impl_erase(iterator p) { return data_.erase(p); } + iterator impl_erase(iterator p, iterator q) { return data_.erase(p, q); } + }; + + +} // end of namespace abc + + +#endif // ! MINI_STD_VECTOR_HH Index: samples/mini-std/cpp/deque.hh =================================================================== --- samples/mini-std/cpp/deque.hh (revision 0) +++ samples/mini-std/cpp/deque.hh (revision 0) @@ -0,0 +1,81 @@ +#ifndef MINI_STD_DEQUE_HH +# define MINI_STD_DEQUE_HH + +# include <deque> +# include "impl.hh" + + +namespace abc +{ + + + template <typename T> class deque; + + + + template <typename T> + struct vtypes< deque<T> > + { + typedef deque<T> E; + typedef primary_std_container<E> super_type; + + typedef std::deque<T> std_type; + + // As Reversible_Container: + typedef typename std::deque<T>::reverse_iterator reverse_iterator; + typedef typename std::deque<T>::const_reverse_iterator const_reverse_iterator; + + // Properties. + typedef mlc::true_ has_random_access; + typedef mlc::true_ has_front_insertion; + typedef mlc::true_ has_back_insertion; + }; + + + + template <typename T> + class deque : public primary_std_container< deque<T> > + { + public: + + typedef deque<T> self_type; + typedef primary_std_container<self_type> super; + + stc_using(iterator); + stc_using(const_iterator); + stc_using(reverse_iterator); + stc_using(const_reverse_iterator); + stc_using(size_type); + stc_using(reference); + stc_using(const_reference); + stc_using(value_type); + + using super::data_; + + // Constructors: + deque() {} + deque(size_type n) : super(n) {} + deque(size_type n, const T& t) : super(n, t) {} + deque(const deque& rhs) : super(rhs) {} + + deque& operator=(const deque& rhs) { data_ = rhs.data_; return *this; } + + // FIXME: swap, resize + + // As Reversible_Container: + reverse_iterator impl_rbegin() { return data_.rbegin(); } + const_reverse_iterator impl_rbegin() const { return data_.rbegin(); } + reverse_iterator impl_rend() { return data_.rend(); } + const_reverse_iterator impl_rend() const { return data_.rend(); } + + // As Sequence: + iterator impl_insert(iterator p, value_type t) { return data_.insert(p, t); } + iterator impl_erase(iterator p) { return data_.erase(p); } + iterator impl_erase(iterator p, iterator q) { return data_.erase(p, q); } + }; + + +} // end of namespace abc + + +#endif // ! MINI_STD_DEQUE_HH Index: samples/mini-std/cpp/stack.hh =================================================================== --- samples/mini-std/cpp/stack.hh (revision 0) +++ samples/mini-std/cpp/stack.hh (revision 0) @@ -0,0 +1,71 @@ +#ifndef MINI_STD_CONTAINERS_HH +# define MINI_STD_CONTAINERS_HH + +# include "impl.hh" + + +namespace abc +{ + + + template <typename T, typename S> class stack; + + + + template <typename T, typename S> + struct vtypes< stack<T,S> > + { + typedef stack<T,S> E; + typedef morpher_container<E> super_type; + + typedef S delegatee_type; + + // Properties. + typedef mlc::false_ has_front_insertion; + typedef mlc::true_ has_back_insertion; + + typedef tag::identity tag; + }; + + + + template <typename T, typename S> + class stack : public morpher_container< stack<T,S> > + { + public: + + typedef stack<T,S> self_type; + typedef morpher_container<self_type> super; + + stc_using(reference); + stc_using(value_type); + stc_using(delegatee_type); + + // Renaming. + reference top() + { + assert(not this->empty()); + return this->back(); + } + void push(value_type t) + { + this->push_back(t); + } + void pop() + { + assert(not this->empty()); + this->pop_back(); + } + + const delegatee_type& impl_delegatee() const { return this->seq_; } + delegatee_type& impl_delegatee() { return this->seq_; } + + protected: + S seq_; + }; + + +} // end of namespace abc + + +#endif // ! MINI_STD_CONTAINERS_HH Index: samples/mini-std/cpp/algorithm.hh =================================================================== --- samples/mini-std/cpp/algorithm.hh (revision 0) +++ samples/mini-std/cpp/algorithm.hh (revision 0) @@ -0,0 +1,64 @@ +#ifndef MINI_STD_ALGORITHM_HH +# define MINI_STD_ALGORITHM_HH + +# include <algorithm> +# include "concepts.hh" + + +namespace abc +{ + + /// \{ + /// Facades. + + template <typename C> + void sort(Container<C>& c); + + template <typename C, typename I> + void copy(const Container<C>& c, I iter); + + /// \} + + + + /// \{ + /// sort + + namespace impl + { + + template <typename C> + void sort(Random_Access_Container<C>& c) + { + std::sort(c.begin(), c.end()); + } + + } // impl + + template <typename C> + void sort(Container<C>& c) + { + impl::sort(c.exact()); + } + + /// \} + + + + /// \{ + /// copy + + template <typename C, typename I> + void copy(const Container<C>& c, I iter) + { + std::copy(c.begin(), c.end(), iter); + } + + /// \} + + + +} // end of namespace abc + + +#endif // ! MINI_STD_ALGORITHM_HH Index: samples/mini-std/cpp/value_cast.hh =================================================================== --- samples/mini-std/cpp/value_cast.hh (revision 0) +++ samples/mini-std/cpp/value_cast.hh (revision 0) @@ -0,0 +1,65 @@ +#ifndef MINI_STD_VALUE_CAST_HH +# define MINI_STD_VALUE_CAST_HH + +# include "impl.hh" +# include "identity.hh" + + +namespace abc +{ + + + template <typename T, typename C> class value_cast_; + + + template <typename T, typename C> + struct vtypes< value_cast_<T,C> > + { + typedef value_cast_<T,C> E; + typedef morpher_container<E> super_type; + + typedef C delegatee_type; + + typedef T value_type; + typedef T& reference; + typedef const T& const_reference; + + typedef tag::identity tag; + }; + + + template <typename T, typename C> + class value_cast_ : public morpher_container< value_cast_<T,C> > + { + public: + + typedef value_cast_<T,C> self_type; + typedef morpher_container<self_type> super; + + stc_using(delegatee_type); + + value_cast_(Container<C>& con) + : con_(con.exact()) + { + } + + const delegatee_type& impl_delegatee() const { return this->con_; } + delegatee_type& impl_delegatee() { return this->con_; } + + protected: + C& con_; + }; + + + template <typename T, typename C> + value_cast_<T, C> value_cast(Container<C>& c) + { + value_cast_<T, C> tmp(c); + return tmp; + } + + +} // end of namespace abc + + +#endif // ! MINI_STD_VALUE_CAST_HH Index: samples/mini-std/cpp/equipment.hh =================================================================== --- samples/mini-std/cpp/equipment.hh (revision 0) +++ samples/mini-std/cpp/equipment.hh (revision 0) @@ -0,0 +1,89 @@ +#ifndef MINI_STD_EQUIPMENT_HH +# define MINI_STD_EQUIPMENT_HH + +# include <cassert> +# include <stc/any.hh> +# include <stc/scoop.hh> + + +stc_scoop_equipment_for_namespace(abc); + + + +// Patch. + +# define abc_vtype_(From, Target) abc::vtype<From, abc::typedef_::Target>::ret +# define abc_vtype(From, Target) typename abc_vtype_(From, Target) + +# define abc_typename(Vtype) typedef abc_vtype(E, Vtype) Vtype + +# define stc_defer(Target) typename abc::deferred_vtype<E, abc::typedef_::Target>::ret + +# define stc_using(Vtype) typedef typename super::Vtype Vtype +# define stc_using_from(From, Vtype) typedef typename From::Vtype Vtype + +# define stc_introducing(Vtype) typedef abc_vtype(self_type, Vtype) Vtype + +// stc_deferred_vtype(test, E, dim) + + + +namespace abc +{ + + /// \{ + /// Typedef declarations. + + // From std. + mlc_decl_typedef(value_type); + mlc_decl_typedef(value_compare); + mlc_decl_typedef(reference); + mlc_decl_typedef(const_reference); + mlc_decl_typedef(data_type); + mlc_decl_typedef(size_type); + mlc_decl_typedef(distance_type); + mlc_decl_typedef(iterator); + mlc_decl_typedef(const_iterator); + mlc_decl_typedef(reverse_iterator); + mlc_decl_typedef(const_reverse_iterator); + mlc_decl_typedef(key_type); + mlc_decl_typedef(key_compare); + mlc_decl_typedef(std_type); + + // Properties. + mlc_decl_typedef(has_random_access); + mlc_decl_typedef(has_front_insertion); + mlc_decl_typedef(has_back_insertion); + + mlc_decl_typedef(tag); + + /// \} + + + + namespace automatic + { + + template < template <class> class abstraction, typename E, typename tag = void > + struct impl; + + template < template <class> class abstraction, typename E > + struct impl< abstraction, E, mlc::none > + { + // none means nothing! + }; + + template < template <class> class abstraction, typename E > + struct impl< abstraction, E, void > : impl< abstraction, E, abc_vtype(E, tag) > + { + // fetch impl w.r.t. tag + }; + + } // end of namespace abc::automatic + + + +} // end of namespace abc + + +#endif // ! MINI_STD_EQUIPMENT_HH Index: samples/mini-std/cpp/identity.hh =================================================================== --- samples/mini-std/cpp/identity.hh (revision 0) +++ samples/mini-std/cpp/identity.hh (revision 0) @@ -0,0 +1,164 @@ +#ifndef MINI_STD_IDENTITY_HH +# define MINI_STD_IDENTITY_HH + +# include "equipment.hh" +# include "fwddecls.hh" + + + +namespace abc +{ + + // tag::identity. + + namespace tag { struct identity; } + + + namespace automatic + { + + // FIXME: Hack! (to be removed) + + template < template <class> class abstraction, typename E > + struct impl< abstraction, E, tag::identity > + { + // ... + }; + + + + // Container + + template <typename E> + class impl< Container, E, tag::identity > + : + public virtual stc::any__simple<E> + { + protected: + abc_typename(value_type); + abc_typename(iterator); + abc_typename(const_iterator); + abc_typename(size_type); + public: + iterator impl_begin() { return this->exact().delegatee().begin(); } + const_iterator impl_begin() const { return this->exact().delegatee().begin(); } + iterator impl_end() { return this->exact().delegatee().end(); } + const_iterator impl_end() const { return this->exact().delegatee().end(); } + size_type impl_size() const { return this->exact().delegatee().size(); } + bool impl_empty() const { return this->exact().delegatee().empty(); } + }; + + + + // Forward_Container + + template <typename E> + class impl< Forward_Container, E, tag::identity > + : + public virtual stc::any__simple<E> + { + protected: + typedef Forward_Container<E> self_type; + public: + bool impl_equal(const self_type& rhs) const { return this->exact().delegatee().operator==(rhs); } + bool impl_less (const self_type& rhs) const { return this->exact().delegatee().operator< (rhs); } + }; + + + + // Reversible_Container + + template <typename E> + class impl< Reversible_Container, E, tag::identity > + : + public virtual stc::any__simple<E> + { + protected: + abc_typename(reverse_iterator); + abc_typename(const_reverse_iterator); + public: + reverse_iterator impl_rbegin() { return this->exact().delegatee().rbegin(); } + const_reverse_iterator impl_rbegin() const { return this->exact().delegatee().rbegin(); } + reverse_iterator impl_rend() { return this->exact().delegatee().rend(); } + const_reverse_iterator impl_rend() const { return this->exact().delegatee().rend(); } + }; + + + + // Random_Access_Container + + template <typename E> + class impl< Random_Access_Container, E, tag::identity > + : + public virtual stc::any__simple<E> + { + protected: + abc_typename(reference); + abc_typename(const_reference); + abc_typename(size_type); + public: + reference impl_brackets(size_type n) { return this->exact().delegatee()[n]; } + const_reference impl_brackets(size_type n) const { return this->exact().delegatee()[n]; } + }; + + + + // Sequence + + template <typename E> + class impl< Sequence, E, tag::identity > + : + public virtual stc::any__simple<E> + { + protected: + abc_typename(value_type); + abc_typename(iterator); + public: + iterator impl_insert(iterator p, value_type t) { return this->exact().delegatee().insert(p, t); } + iterator impl_erase(iterator p) { return this->exact().delegatee().erase(p); } + iterator impl_erase(iterator p, iterator q) { return this->exact().delegatee().erase(p, q); } + }; + + + + // Front_Insertion_Sequence + + template <typename E> + class impl< Front_Insertion_Sequence, E, tag::identity > + : + public virtual stc::any__simple<E> + { + protected: + abc_typename(value_type); + public: + void impl_push_front(value_type t) { this->exact().delegatee().push_front(t); } + void impl_pop_front() { this->exact().delegatee().pop_front(); } + }; + + + + // Back_Insertion_Sequence + + template <typename E> + class impl< Back_Insertion_Sequence, E, tag::identity > + : + public virtual stc::any__simple<E> + { + protected: + abc_typename(value_type); + abc_typename(reference); + public: + reference impl_back() { this->exact().delegatee().back(); } + void impl_push_back(value_type t) { this->exact().delegatee().push_back(t); } + void impl_pop_back() { this->exact().delegatee().pop_back(); } + }; + + + + } // end of namespace abc::automatic + + +} // end of namespace abc + + +#endif // ! MINI_STD_IDENTITY_HH Index: samples/mini-std/cpp/main.cc =================================================================== --- samples/mini-std/cpp/main.cc (revision 0) +++ samples/mini-std/cpp/main.cc (revision 0) @@ -0,0 +1,19 @@ + +#include <iostream> +#include <iterator> + +#include "vector.hh" +#include "algorithm.hh" + + + +int main() +{ + abc::vector<int> v; + v.push_back(3); + v.push_back(1); + v.push_back(4); + + abc::sort(v); + abc::copy(v, std::ostream_iterator<int>(std::cout, "\n")); +} Index: samples/mini-std/cpp/fwddecls.hh =================================================================== --- samples/mini-std/cpp/fwddecls.hh (revision 0) +++ samples/mini-std/cpp/fwddecls.hh (revision 0) @@ -0,0 +1,84 @@ +#ifndef MINI_STD_FWDDECLS_HH +# define MINI_STD_FWDDECLS_HH + + +namespace abc +{ + + + // Container + // | + // +-- Forward_Container + // | + // | + // +-- Reversible_Container + // | | + // | +-- Random_Access_Container + // | + + template <typename E> class Container; + template <typename E> class Forward_Container; + template <typename E> class Reversible_Container; + template <typename E> class Random_Access_Container; + + // | + // +-- Sequence + // | | + // | +-- Front_Insertion_Sequence + // | | + // | +-- Back_Insertion_Sequence + // | + + template <typename E> class Sequence; + template <typename E> class Front_Insertion_Sequence; + template <typename E> class Back_Insertion_Sequence; + + // | + // +-- Associative_Container + // | + // +-- Simple_Associative_Container + // +-- Pair_Associative_Container + // | + // +-- Sorted_Associative_Container + // +-- Hashed_Associative_Container + // | + // +-- Unique_Associative_Container + // +-- Multiple_Associative_Container + + template <typename E> class Associative_Container; + template <typename E> class Simple_Associative_Container; + template <typename E> class Pair_Associative_Container; + template <typename E> class Sorted_Associative_Container; + template <typename E> class Hashed_Associative_Container; + template <typename E> class Unique_Associative_Container; + template <typename E> class Multiple_Associative_Container; + + + + // Iterator + // | + // +-- Trivial_Iterator + // | | + // | +-- Input_Iterator + // | | + // +-- Output_Iterator | + // | | + // +---------+-- Forward_Iterator + // | + // +-- Bidirectional_Iterator + // | + // +-- Random_Access_Iterator + + template <typename E> class Iterator; + template <typename E> class Trivial_Iterator; + template <typename E> class Input_Iterator; + template <typename E> class Output_Iterator; + template <typename E> class Forward_Iterator; + template <typename E> class Bidirectional_Iterator; + template <typename E> class Random_Access_Iterator; + + +} // end of namespace abc + + +#endif // ! MINI_STD_FWDDECLS_HH Index: samples/mini-std/cpp/list.hh =================================================================== --- samples/mini-std/cpp/list.hh (revision 0) +++ samples/mini-std/cpp/list.hh (revision 0) @@ -0,0 +1,83 @@ +#ifndef MINI_STD_LIST_HH +# define MINI_STD_LIST_HH + +# include <list> +# include "impl.hh" + + +namespace abc +{ + + + template <typename T> class list; + + + template <typename T> + struct vtypes< list<T> > + { + typedef list<T> E; + typedef primary_std_container<E> super_type; + + typedef std::list<T> std_type; + + // As Reversible_Container: + typedef typename std::list<T>::reverse_iterator reverse_iterator; + typedef typename std::list<T>::const_reverse_iterator const_reverse_iterator; + + // Properties. + typedef mlc::false_ has_random_access; + typedef mlc::true_ has_front_insertion; + typedef mlc::true_ has_back_insertion; + }; + + + + template <typename T> + class list : public primary_std_container< list<T> > + { + public: + + typedef list<T> self_type; + typedef primary_std_container<self_type> super; + + stc_using(iterator); + stc_using(const_iterator); + stc_using(size_type); + stc_using(reference); + stc_using(const_reference); + stc_using(value_type); + + stc_introducing(reverse_iterator); + stc_introducing(const_reverse_iterator); + + using super::data_; + + // Constructors: + list() {} + list(size_type n) : super(n) {} + list(size_type n, const T& t) : super(n, t) {} + list(const list& rhs) : super(rhs) {} + + list& operator=(const list& rhs) { data_ = rhs.data_; return *this; } + + void sort() { data_.sort(); } + + // FIXME: swap, resize + + // As Reversible_Container: + reverse_iterator impl_rbegin() { return data_.rbegin(); } + const_reverse_iterator impl_rbegin() const { return data_.rbegin(); } + reverse_iterator impl_rend() { return data_.rend(); } + const_reverse_iterator impl_rend() const { return data_.rend(); } + + // As Sequence: + iterator impl_insert(iterator p, value_type t) { return data_.insert(p, t); } + iterator impl_erase(iterator p) { return data_.erase(p); } + iterator impl_erase(iterator p, iterator q) { return data_.erase(p, q); } + }; + + +} // end of namespace abc + + +#endif // ! MINI_STD_LIST_HH Index: samples/mini-std/cpp/concepts.hh =================================================================== --- samples/mini-std/cpp/concepts.hh (revision 0) +++ samples/mini-std/cpp/concepts.hh (revision 0) @@ -0,0 +1,356 @@ +#ifndef MINI_STD_CONCEPTS_HH +# define MINI_STD_CONCEPTS_HH + +# include "equipment.hh" + + +namespace abc +{ + + // Container + + template <typename E> + class Container : public virtual stc::any__simple<E>, + public automatic::impl<Container, E> + { + public: + + // Typedefs. + abc_typename(value_type); + abc_typename(iterator); + abc_typename(const_iterator); + abc_typename(reference); + abc_typename(const_reference); + abc_typename(size_type); + + // Methods. + iterator begin() { return this->exact().impl_begin(); } + const_iterator begin() const { return this->exact().impl_begin(); } + iterator end() { return this->exact().impl_end(); } + const_iterator end() const { return this->exact().impl_end(); } + size_type size() const { return this->exact().impl_size(); } + bool empty() const { return this->exact().impl_empty(); } + + protected: + Container() {} + }; + + + + // Forward_Container + + template <typename E> + class Forward_Container : public virtual Container<E>, + public automatic::impl<Forward_Container, E> + { + public: + + typedef Forward_Container<E> self_type; + + // Methods. + bool operator==(const self_type& rhs) const { return this->exact().impl_equal(rhs.exact()); } + bool operator< (const self_type& rhs) const { return this->exact().impl_less(rhs.exact()); } + + // Concrete. // FIXME: Use the default impl mechanism so that the user can override. + bool operator!=(const self_type& rhs) const { return not (*this == rhs); } + bool operator> (const self_type& rhs) const { return rhs < *this; } + bool operator<=(const self_type& rhs) const { return not (*this > rhs); } + bool operator>=(const self_type& rhs) const { return not (*this < rhs); } + + protected: + Forward_Container() {} + }; + + + + // Reversible_Container + + template <typename E> + class Reversible_Container : public virtual Forward_Container<E>, + public automatic::impl<Reversible_Container, E> + { + public: + + // Typedefs. + abc_typename(reverse_iterator); + abc_typename(const_reverse_iterator); + + // Methods. + reverse_iterator rbegin() { return this->exact().impl_rbegin(); } + const_reverse_iterator rbegin() const { return this->exact().impl_rbegin(); } + reverse_iterator rend() { return this->exact().impl_rend(); } + const_reverse_iterator rend() const { return this->exact().impl_rend(); } + + protected: + Reversible_Container() {} + }; + + + + // Random_Access_Container + + template <typename E> + class Random_Access_Container : public virtual Reversible_Container<E>, + public automatic::impl<Random_Access_Container, E> + { + public: + + typedef Reversible_Container<E> super; + stc_using(reference); + stc_using(const_reference); + stc_using(size_type); + + // Methods. + reference operator[](size_type n) { assert(0 <= n and n <= this->size()); + return this->exact().impl_brackets(n); } + const_reference operator[](size_type n) const { assert(0 <= n and n <= this->size()); + return this->exact().impl_brackets(n); } + protected: + Random_Access_Container() {} + }; + + + + // Sequence + + template <typename E> + class Sequence : public virtual Forward_Container<E>, + public automatic::impl<Sequence, E> + { + public: + + typedef Forward_Container<E> super; + stc_using(iterator); + stc_using(value_type); + stc_using(reference); + stc_using(const_reference); + + // Methods. + iterator insert(iterator p, value_type t) { return this->exact().impl_insert(p, t); } + iterator erase(iterator p) { return this->exact().impl_erase(p); } + iterator erase(iterator p, iterator q) { return this->exact().impl_erase(p, q); } + + // Concrete. + reference front() + { + assert(not this->empty()); + return *(this->first()); + } + const_reference front() const + { + assert(not this->empty()); + return *(this->first()); + } + void clear() + { + this->erase(this->begin(), this->end()); + } + + protected: + Sequence() {} + }; + + + + + // Front_Insertion_Sequence + + template <typename E> + class Front_Insertion_Sequence : public virtual Sequence<E>, + public automatic::impl<Front_Insertion_Sequence, E> + { + public: + + typedef Sequence<E> super; + stc_using(value_type); + + // Concrete. + void push_front(value_type t) + { + this->insert(this->begin(), t); + } + void pop_front() + { + assert(not this->empty()); + this->erase(this->begin()); + } + + protected: + Front_Insertion_Sequence() {} + }; + + + + // Back_Insertion_Sequence + + template <typename E> + class Back_Insertion_Sequence : public virtual Sequence<E>, + public automatic::impl<Back_Insertion_Sequence, E> + { + public: + + typedef Sequence<E> super; + stc_using(reference); + stc_using(value_type); + + // Concrete. + reference back() + { + assert(not this->empty()); + return *(--this->end()); + } + void push_back(value_type t) + { + this->insert(this->end(), t); + } + void pop_back() + { + assert(not this->empty()); + this->erase(--this->end()); + } + protected: + Back_Insertion_Sequence() {} + }; + + + + // Iterator + + template <typename E> + class Iterator : public virtual stc::any__simple<E>, + public automatic::impl<Iterator, E> + { + public: + + // FIXME + + protected: + Iterator() {} + }; + + + + // Trivial_Iterator + + template <typename E> + class Trivial_Iterator : public virtual Iterator<E>, + public automatic::impl<Trivial_Iterator, E> + { + public: + + typedef Trivial_Iterator<E> self_type; + + // Typedef. + abc_typename(value_type); + + // Methods. + value_type operator*() const { return this->exact().impl_star(); } + const value_type& operator->() const { return this->exact().impl_arrow(); } + bool operator==(const self_type& rhs) const { return this->exact().impl_equal(rhs.exact()); } + + // Concrete. + bool operator!=(const self_type& rhs) const + { + return not (*this == rhs); + } + + protected: + Trivial_Iterator() {} + }; + + + + // Input_Iterator + + template <typename E> + class Input_Iterator : public virtual Trivial_Iterator<E>, + public automatic::impl<Input_Iterator, E> + { + public: + + // Typedef. + abc_typename(distance_type); + + // Method. + E& operator++() { return this->exact().impl_preinc(); } + + // Concrete. + E operator++(int) const + { + E cpy(*this); + ++(*this); + return cpy; + } + + protected: + Input_Iterator() {} + }; + + + + // Output_Iterator + + template <typename E> + class Output_Iterator : public virtual Trivial_Iterator<E>, + public automatic::impl<Output_Iterator, E> + { + public: + + typedef Trivial_Iterator<E> super; + stc_using(value_type); + + using super::operator*; + using super::operator->; + + // Methods. + value_type& operator*() { return this->exact().impl_star(); } + value_type& operator->() { return this->exact().impl_arrow(); } + + protected: + Output_Iterator() {} + }; + + + + // Forward_Iterator + + template <typename E> + class Forward_Iterator : public virtual Input_Iterator<E>, public virtual Output_Iterator<E>, + public automatic::impl<Forward_Iterator, E> + { + public: + // nothing + protected: + Forward_Iterator() {} + }; + + + + // Bidirectional_Iterator + + template <typename E> + class Bidirectional_Iterator : public virtual Forward_Iterator<E>, + public automatic::impl<Bidirectional_Iterator, E> + { + public: + + // Method. + E& operator--() { return this->exact().impl_predec(); } + + // Concrete. + E operator--(int) const + { + E cpy(*this); + --(*this); + return cpy; + } + + protected: + Bidirectional_Iterator() {} + }; + + +} // end of namespace abc + + +#endif // ! MINI_STD_CONCEPTS_HH Index: samples/mini-std/design.hh =================================================================== --- samples/mini-std/design.hh (revision 0) +++ samples/mini-std/design.hh (revision 0) @@ -0,0 +1,248 @@ + +// Container + +Container : class = +{ + value_type : type; + + iterator : type; + const_iterator : type; + + reference : type; + const_reference : type; + + size_type : type; + + begin : () -> iterator; + begin : const () -> const_iterator; + + end : () -> iterator; + end : const () -> const_iterator; + + size : const () -> size_type; + empty : const () -> bool; +} + + +// Forward_Container + +Forward_Container : class < Container = +{ + "=" : const (rhs : @type) -> bool; + "!=" : const (rhs : @type) -> bool; + "<" : const (rhs : @type) -> bool; + ">" : const (rhs : @type) -> bool; + "<=" : const (rhs : @type) -> bool; + ">=" : const (rhs : @type) -> bool; +} + + +// Reversible_Container + +Reversible_Container : class < Forward_Container = +{ + reverse_iterator : type; + const_reverse_iterator : type; + + rbegin : () -> reverse_iterator; + rbegin : const () -> const_reverse_iterator; + + rend : () -> reverse_iterator; + rend : const () -> const_reverse_iterator; + + /* Models: vector, list, deque */ +} + + +// Random_Access_Container + +Random_Access_Container : class < Reversible_Container = +{ + "[]" : (n : size_type) -> reference { + precondition { 0 <= n and n <= @size() } + } + "[]" : const (n : size_type) -> const_reference { + precondition { 0 <= n and n <= @size() } + } +} + + + +// Sequence + +Sequence : class < Forward_Container = +{ + make : (n : size_type, t : value_type) { + precondition { n >= 0 } + postcondition { @size() = n } // FIXME: and... + } + + // FIXME: some ctors are missing. + + front : () -> reference = + { + precondition { not @empty() } + return *(@first()); + } + front : const () -> const_reference = + { + precondition { not @empty() } + return *(@first()); + } + + insert : (p : iterator, t : value_type) -> iterator; + + erase : (p : iterator) -> iterator; + erase : (p : iterator, q : iterator) -> iterator; + + clear : () -> void = + { + @erase(@begin(), @end()); + } + + /* Models: vector, deque, list */ +} + + + +// Front_Insertion_Sequence + +Front_Insertion_Sequence : class < Sequence = +{ + push_front : (t : value_type) -> void = + { + @insert(@begin(), t); + } + pop_front : () -> void = + { + precondition { not @empty() } + @erase(@begin()); + } + /* Models: deque, list */ +} + + +// Back_Insertion_Sequence + +Back_Insertion_Sequence : class < Sequence = +{ + back : () -> reference = + { + precondition { not @empty() } + return *(--@end()); + } + push_back : (t : value_type) -> void = + { + @insert(@end(), t); + } + pop_back : () -> void = + { + precondition { not @empty() } + @erase(--@end()); + } + /* Models: vector, deque, list */ +} + + + +// vector[T] + +vector : [T : type] -> class < Back_Insertion_Sequence, Random_Access_Container = +{ + // FIXME: impl +} + + +// Associative_Container + +Associative_Container : class < Forward_Container = +{ + key_type : type; + + erase : (k : key_type) -> size_type; + erase : (p : iterator) -> void; + erase : (p : iterator, q : iterator) -> void; + + clear : () -> void = + { + @erase(@begin(), @end()); + } + + find : (k : key_type) -> iterator; + find : const (k : key_type) -> const_iterator; + + count : (k : key_type) -> size_type; + + // FIXME: equal_range + + /* Models: set, multiset, map, multimap */ +} + + +// Simple_Associative_Container + +Simple_Associative_Container : class < Associative_Container = +{ + check { + key_type = value_type and iterator = const_iterator + } + /* Models: set, multiset */ +} + + +// Pair_Associative_Container + +Pair_Associative_Container : class < Associative_Container = +{ + data_type : type; + value_type : type = pair[key_type const, data_type]; + /* Models: map, multimap */ +} + + + +// Sorted_Associative_Container + +Sorted_Associative_Container : class < Associative_Container = +{ + key_compare : type < Strict_Weak_Ordering; + value_compare : type < Strict_Weak_Ordering; + + // FIXME: ctors, key_comp, value_compare, lower_bound, upper_bound, equal_range +} + + + +// FIXME: Hashed_Associative_Container +// FIXME: Unique_Associative_Container +// FIXME: Multiple_Associative_Container + + + +// Trivial_Iterator + +Trivial_Iterator : class = +{ + value_type : type; + "*" : const () : value_type const&; + "->" : const () : value_type const&; + "=" : const (rhs : @type const&) : bool; +} + +// Input_Iterator + +Input_Iterator : class < Trivial_Iterator = +{ + "++@" : () -> @type &; // pre + "@++" : () -> @type &; // post +} + + + +// Output_Iterator + +Output_Iterator : class < Trivial_Iterator = +{ + "*" : () : value_type &; + "->" : () : value_type &; +} Index: samples/mini-std/README =================================================================== --- samples/mini-std/README (revision 0) +++ samples/mini-std/README (revision 0) @@ -0,0 +1,29 @@ + -*- outline -*- + +* compilation + +cd cpp +g++ -I../../.. -I../../../../metalic -Wall -ansi -pedantic main.cc + +* contents + +** utility files + +concepts.hh +equipment.hh +fwddecls.hh +identity.hh +impl.hh + +** based on existing std tools + +deque.hh +list.hh +stack.hh +vector.hh + +algorithm.hh + +** morphers + +value_cast.hh
participants (1)
-
Thierry GERAUD