Commit 087e4b69 authored by Thierry Geraud's avatar Thierry Geraud
Browse files

2007-02-01 Thierry GERAUD <theo@tegucigalpa.lrde.epita.fr>

	Add a tiny example to stress differences between paradigms.
	
	* stc/doc/tiny/: New.
	* stc/scoop.hh (stc_scoop_equipment_for_namespace): Rename
	SCOOPED_NAMESPACE::internal as SCOOPED_NAMESPACE::stc_vtype_internal.



git-svn-id: https://svn.lrde.epita.fr/svn/oln/trunk@729 4aad255d-cdde-0310-9447-f3009e2ae8c0
parent daeea474
2007-02-01 Thierry GERAUD <theo@tegucigalpa.lrde.epita.fr>
Add a tiny example to stress differences between paradigms.
* stc/doc/tiny/: New.
* stc/scoop.hh (stc_scoop_equipment_for_namespace): Rename
SCOOPED_NAMESPACE::internal as SCOOPED_NAMESPACE::stc_vtype_internal.
2007-01-30 Roland Levillain <roland@lrde.epita.fr>
Rename stc/scoop2.hh as stc/scoop-alt.hh.
......
#include "local_scoop.hh"
stc_equip_namespace(abc);
namespace abc
{
// Iterator<Exact>
template <typename Exact> class Iterator;
template <typename Exact>
struct vtypes< Iterator<Exact> > : vtypes< Any<Exact> >
{
typedef stc::abstract value;
};
template <typename Exact>
class Iterator : public Any<Exact>
{
public:
stc_typename(value);
void next() { this->exact().impl_next(); }
bool is_valid() const { return this->exact().impl_is_valid(); }
void set(const value& v) { this->exact().impl_set(v); }
};
// array_iterator<T>
template <typename T> class array_iterator;
template <typename T>
struct vtypes< array_iterator<T> > : vtypes< Iterator< array_iterator<T> > >
{
typedef T value;
};
template <typename T>
class array_iterator : public Iterator< array_iterator<T> >
{
typedef Iterator< array_iterator<T> > Super;
public:
stc_using(value);
void impl_next() { i_ = i_ + 1; }
bool impl_is_valid() const { return i_ >= 0 and i_ < n_; }
void impl_set(const value& v) { v_ = v; }
array_iterator(int n) : i_(0), n_(n) {}
protected:
int i_, n_;
value v_;
};
// algo
template <typename I>
void algo(Iterator<I>& iter, typename I::value val)
{
if (iter.is_valid())
{
iter.set(val);
iter.next();
}
}
} // abc
int main()
{
abc::array_iterator<int> i(7);
int val = 51;
abc::algo(i, val);
}
g++ -Wall -ansi -pedantic -I../../../ -I../../../../metalic $1
namespace stc
{
class abstract;
template <typename Exact>
class any
{
public:
Exact& exact() { return *(Exact*)(void*)this; }
const Exact& exact() const { return *(const Exact*)(const void*)this; }
};
}
#define stc_typename(Type) typedef typename vtypes<Exact>::Type Type
#define stc_using(Type) typedef typename Super::Type Type
#define stc_equip_namespace(Namespace) \
\
namespace Namespace \
{ \
template <typename T> struct vtypes; \
\
/* Any. */ \
\
template <typename Exact> class Any; \
\
template <typename Exact> \
struct vtypes< Any<Exact> > \
{ \
typedef Exact exact; \
}; \
\
template <typename Exact> \
class Any : public stc::any<Exact> \
{ \
}; \
\
} \
\
struct end_with_semicolumn
#include "local_scoop.hh"
stc_equip_namespace(abc);
namespace abc
{
// Iterator
# define Current Iterator<Exact>
# define Super Any<Exact>
template <typename Exact> class Iterator;
template <typename Exact>
struct vtypes< Current > : vtypes< Super >
{
typedef stc::abstract value;
};
template <typename Exact>
class Iterator : public Super
{
public:
stc_typename(value);
void next() { this->exact().impl_next(); }
bool is_valid() const { return this->exact().impl_is_valid(); }
void set(const value& v) { this->exact().impl_set(v); }
};
#undef Super
#undef Current
// array_iterator
# define Current array_iterator<T>
# define Super Iterator<Current>
template <typename T> class array_iterator;
template <typename T>
struct vtypes< Current > : vtypes< Super >
{
typedef T value;
};
template <typename T>
class array_iterator : public Super
{
public:
stc_using(value);
void impl_next() { i_ = i_ + 1; }
bool impl_is_valid() const { return i_ >= 0 and i_ < n_; }
void impl_set(const value& v) { v_ = v; }
array_iterator(int n) : i_(0), n_(n) {}
protected:
int i_, n_;
value v_;
};
#undef Super
#undef Current
template <typename I>
void algo(Iterator<I>& iter, typename I::value val)
{
if (iter.is_valid())
{
iter.set(val);
iter.next();
}
}
} // abc
int main()
{
abc::array_iterator<int> i(7);
int val = 51;
abc::algo(i, val);
}
#include "local_scoop.hh"
stc_equip_namespace(abc);
namespace abc
{
// List of associated types.
mlc_decl_typedef(value);
// Iterator
template <typename Exact>
struct Iterator : public Concept<Exact>,
public automatic::impl<Iterator, Exact>
{
stc_typename(value);
void next() { this->exact().impl_next(); }
bool is_valid() const { return this->exact().impl_is_valid(); }
void set(const value& v) { this->exact().impl_set(v); }
};
// array_iterator
template <typename T> class array_iterator;
template <typename T>
struct vtypes< array_iterator<T> >
{
typedef top< array_iterator<T> > super_type;
typedef stc::is<Iterator> category;
typedef T value;
};
template <typename T>
class array_iterator : public top< array_iterator<T> >
{
public:
typedef top< array_iterator<T> > super;
stc_using(value);
void impl_next() { i_ = i_ + 1; }
bool impl_is_valid() const { return i_ >= 0 and i_ < n_; }
void impl_set(const value& v) { v_ = v; }
array_iterator(int n) : i_(0), n_(n) {}
protected:
int i_, n_;
value v_;
};
// algo
template <typename I>
void algo(Iterator<I>& iter, typename I::value val)
{
if (iter.is_valid())
{
iter.set(val);
iter.next();
}
}
} // abc
int main()
{
abc::array_iterator<int> i(7);
int val = 51;
abc::algo(i, val);
}
g++ -Wall -ansi -pedantic -I../../../ -I../../../../metalic $1
This diff is collapsed.
#include "local_scoop.hh"
stc_equip_namespace(abc);
namespace abc
{
// List of associated types.
mlc_decl_typedef(value);
// Iterator
template <typename Exact>
struct Iterator : public Concept<Exact>,
public automatic::impl<Iterator, Exact>
{
stc_typename(value);
void next() { this->exact().impl_next(); }
bool is_valid() const { return this->exact().impl_is_valid(); }
void set(const value& v) { this->exact().impl_set(v); }
};
// array_iterator
# define current array_iterator<T>
// -----------------
# define templ template <typename T>
# define classname array_iterator
# define super top< current >
stc_Header;
typedef stc::is<Iterator> category;
typedef T value;
stc_End;
template <typename T>
class array_iterator : public super
{
public:
stc_using(value);
// Impl.
void impl_next() { i_ = i_ + 1; }
bool impl_is_valid() const { return i_ >= 0 and i_ < n_; }
void impl_set(const value& v) { v_ = v; }
// Ctor.
array_iterator(int n) : i_(0), n_(n) {}
protected:
int i_, n_;
value v_;
};
# include "undefs.hh"
// algo
template <typename I>
void algo(Iterator<I>& iter, typename I::value val)
{
if (iter.is_valid())
{
iter.set(val);
iter.next();
}
}
} // abc
int main()
{
abc::array_iterator<int> i(7);
int val = 51;
abc::algo(i, val);
}
#undef current
#undef super
#undef templ
#undef classname
namespace abc
{
class Iterator
{
public:
virtual void next() = 0;
virtual bool is_valid() const = 0;
virtual void set(const void* v) = 0;
};
template <typename T>
class array_iterator : public Iterator
{
public:
typedef T value;
virtual void next() { i_ = i_ + 1; }
virtual bool is_valid() const { return i_ >= 0 and i_ < n_; }
virtual void set(const void* v) { v_ = *(int*)v; }
array_iterator(int n) : i_(0), n_(n) {}
protected:
int i_, n_;
value v_;
};
void algo(Iterator& iter, void* val)
{
if (iter.is_valid())
{
iter.set(val);
iter.next();
}
}
} // abc
int main()
{
abc::array_iterator<int> i(7);
int val = 51;
abc::algo(i, &val);
}
#include <iostream>
namespace abc
{
// Iterator
auto concept Iterator <typename I>
{
typename value = I::value;
void I::next();
bool I::is_valid() const;
void I::set(const value& v);
};
// array_iterator<T>
template <typename T>
class array_iterator
{
public:
typedef T value;
void next() { i_ = i_ + 1; }
bool is_valid() const { return i_ >= 0 and i_ < n_; }
void set(const value& v) { v_ = v; }
array_iterator(int n) : i_(0), n_(n) {}
protected:
int i_, n_;
value v_;
};
// algo
template <Iterator I> // or: template <typename I> where Iterator<I>
void algo(I& iter, typename I::value val)
{
if (iter.is_valid())
{
iter.set(val);
iter.next();
}
}
} // abc
int main()
{
abc::array_iterator<int> i(7);
int val = 51;
abc::algo(i, val);
}
namespace abc
{
// Iterator
class Iterator
{
public:
virtual typedef value = 0;
virtual void next() = 0;
virtual bool is_valid() const = 0;
virtual void set(const value& v) = 0;
};
// array_iterator<T>
template <typename T>
class array_iterator : public Iterator
{
public:
virtual typedef T value;
virtual void next() { i_ = i_ + 1; }
virtual bool is_valid() const { return i_ >= 0 and i_ < n_; }
virtual void set(const value& v) { v_ = v; }
array_iterator(int n) : i_(0), n_(n) {}
protected:
int i_, n_;
value v_;
};
// algo
void algo(Iterator& iter, Iterator::value val)
{
if (iter.is_valid())
{
iter.set(val);
iter.next();
}
}
} // abc
int main()
{
abc::array_iterator<int> i(7);
int val = 51;
abc::algo(i, val);
}
namespace abc
{
Iterator : type =
{
value : type
next : () -> void
is_valid : const bool
set : (v : value const&) -> void