Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Tuple.h
1 /*
2  * Tuple interface and tuple providers.
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2017, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_RTTI_TUPLE_H_
22 #define ELM_RTTI_TUPLE_H_
23 
24 #include <elm/rtti/type_of.h>
25 #include <elm/rtti/Class.h>
26 
27 namespace elm { namespace rtti {
28 
29 class Tuple {
30 public:
31  virtual ~Tuple();
32  virtual const Vector<const rtti::Type *>& types() const = 0;
33  virtual void split(void *ptr, Vector<Variant>& values) const = 0;
34  virtual void *make(const Vector<Variant>& values, int i = 0) const = 0;
35  inline int count() const { return types().length(); }
36 };
37 
38 template <class T>
39 class AbstractTuple: public AbstractClass, public Tuple {
40 public:
42  void *instantiate(void) const override { return new T; }
43  void free(void *obj) const override { delete static_cast<T *>(obj); }
44  const Tuple *toTuple() const override { return this; }
45  const Vector<const rtti::Type *>& types() const override { return _types; }
46 protected:
47  inline void add(const rtti::Type *t) { _types.add(t); }
48 private:
50 };
51 
52 template <class T, class A>
53 class Tuple1: public AbstractTuple<T> {
54 public:
55  typedef A (T::*acc_t)() const;
56  Tuple1(cstring name, acc_t acc): AbstractTuple<T>(name), _acc(acc)
57  { AbstractTuple<T>::add(&type_of<A>()); }
58  void split(void *ptr, Vector<Variant>& values) const override
59  { values.add(Variant((static_cast<const T *>(ptr)->*_acc)())); }
60  void *make(const Vector<Variant>& values, int i) const override
61  { cerr << "DEBUG: " << values[i].as<A>() << io::endl;
62  return new T(values[i].as<A>()); }
63 private:
64  acc_t _acc;
65 };
66 
67 template <class T, class A>
68 AbstractClass *tuple1(cstring name, A (T::*a)() const) { return new Tuple1<T, A>(name, a); }
69 
70 class Declare {
71 public:
72  inline Declare(const AbstractClass *cls): _cls(cls) { }
73  inline const AbstractClass& get() const { return *_cls; }
74  inline const AbstractClass& operator*() const { return get(); }
75  inline const AbstractClass *operator->() const { return &get(); }
76 private:
77  const AbstractClass *_cls;
78 };
79 
80 } } // elm::rtti
81 
82 #endif /* ELM_RTTI_TUPLE_H_ */
elm::rtti::Tuple
Definition: Tuple.h:29
elm::Variant
Definition: Variant.h:92
elm::rtti::Type::name
string name(void) const
Definition: Type.h:91
elm::rtti::Tuple::split
virtual void split(void *ptr, Vector< Variant > &values) const =0
elm::rtti::Declare::operator*
const AbstractClass & operator*() const
Definition: Tuple.h:74
elm::CString
Definition: CString.h:17
elm::rtti::AbstractTuple::free
void free(void *obj) const override
Definition: Tuple.h:43
elm::rtti::Tuple1::split
void split(void *ptr, Vector< Variant > &values) const override
Definition: Tuple.h:58
elm
Definition: adapter.h:26
elm::Vector::add
void add(const T &v)
Definition: Vector.h:101
elm::Vector
Definition: Vector.h:34
elm::rtti::Tuple1::acc_t
A(T::* acc_t)() const
Definition: Tuple.h:55
elm::rtti::Tuple::~Tuple
virtual ~Tuple()
Definition: rtti.cpp:814
elm::io::endl
const EOL endl
Definition: io_Output.cpp:880
elm::rtti::AbstractTuple::AbstractTuple
AbstractTuple(cstring name)
Definition: Tuple.h:41
elm::rtti::Declare::get
const AbstractClass & get() const
Definition: Tuple.h:73
elm::rtti::AbstractTuple::add
void add(const rtti::Type *t)
Definition: Tuple.h:47
elm::rtti::AbstractTuple::toTuple
const Tuple * toTuple() const override
Definition: Tuple.h:44
elm::rtti::Tuple1
Definition: Tuple.h:53
elm::cerr
io::Output cerr
elm::rtti::Declare::Declare
Declare(const AbstractClass *cls)
Definition: Tuple.h:72
elm::rtti::Declare
Definition: Tuple.h:70
elm::rtti::Tuple::make
virtual void * make(const Vector< Variant > &values, int i=0) const =0
elm::rtti::tuple1
AbstractClass * tuple1(cstring name, A(T::*a)() const)
Definition: Tuple.h:68
elm::rtti::Declare::operator->
const AbstractClass * operator->() const
Definition: Tuple.h:75
elm::rtti::AbstractClass
Definition: Class.h:289
elm::rtti::Tuple1::make
void * make(const Vector< Variant > &values, int i) const override
Definition: Tuple.h:60
elm::rtti::AbstractTuple::types
const Vector< const rtti::Type * > & types() const override
Definition: Tuple.h:45
elm::rtti::Tuple1::Tuple1
Tuple1(cstring name, acc_t acc)
Definition: Tuple.h:56
elm::rtti::AbstractTuple::instantiate
void * instantiate(void) const override
Definition: Tuple.h:42
elm::rtti::Tuple::types
virtual const Vector< const rtti::Type * > & types() const =0
elm::rtti::Type
Definition: Type.h:83
elm::rtti::Tuple::count
int count() const
Definition: Tuple.h:35
elm::rtti::AbstractTuple
Definition: Tuple.h:39