Skip to content

Commit 4710992

Browse files
committed
Improved Tuple, removed Object.
1 parent 17184b2 commit 4710992

File tree

15 files changed

+94
-289
lines changed

15 files changed

+94
-289
lines changed

docs/api/tuple.rst

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Tuples and Objects
2-
==================
1+
Tuples
2+
======
33

44
.. code-block:: cpp
55
@@ -19,7 +19,7 @@ Initialisation of a Tuple can be done with a brace-initializer-list as follows.
1919

2020
.. code-block:: cpp
2121
22-
Tuple<int, char> t = {10, 'c'};
22+
Tuple<int, char> t {10, 'c'};
2323
2424
Element retrieval and assignment is described below in the :ref:`Helper
2525
functions` section.
@@ -34,39 +34,11 @@ Class definitions
3434
:content-only:
3535

3636

37-
Objects
38-
-------
39-
40-
An Object is functionally equivalent to a Tuple, except that its internal
41-
structure is preserved after serialisation. More on serialisation of Objects
42-
can be found in the :ref:`method_discovery` section.
43-
44-
Initialisation of an Object can be done with a brace-initializer-list as
45-
follows.
46-
47-
.. code-block:: cpp
48-
49-
Object<int, char> o(10, 'c');
50-
51-
Element retrieval and assignment is described below in the :ref:`Helper
52-
functions` section.
53-
54-
Note that an Object, like any higher order data structure, should be passed by
55-
reference.
56-
57-
Class definitions
58-
~~~~~~~~~~~~~~~~~
59-
60-
.. doxygengroup:: object
61-
:content-only:
62-
63-
6437
Helper functions
6538
----------------
6639

67-
Elements of a Tuple or Object can be retrieved in two ways, either via the
68-
``head`` and ``tail`` member variables, or using with the ``get<>()`` helper
69-
function.
40+
Elements of a Tuple can be retrieved in two ways, either via the ``head`` and
41+
``tail`` member variables, or using with the ``get<>()`` helper function.
7042

7143
.. code-block:: cpp
7244
@@ -89,12 +61,12 @@ the ``get<>()`` helper function.
8961
9062
There are additional helper functions available for the creation of Tuples.
9163

92-
The function ``pack()`` can be used to create a temporary Tuple to be used in a
64+
The function ``makeTuple()`` can be used to create a temporary Tuple to be used in a
9365
function call.
9466

9567
.. code-block:: cpp
9668
97-
function(pack('a', 'b', 10));
69+
function(makeTuple('a', 'b', 10));
9870
9971
10072
Functions

examples/demo/demo.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ int inc(int a) {
1717
return a + 1;
1818
}
1919

20-
Object<char, Object<int, char> > object(Object<char, Object<int, char> >& o) {
21-
Object<char, Object<int, char> > r;
20+
Tuple<char, Tuple<int, char> > tuple(Tuple<char, Tuple<int, char> >& o) {
21+
Tuple<char, Tuple<int, char> > r;
2222

2323
get<0>(r) = get<0>(o) + 1;
2424
get<0>(get<1>(r)) = get<0>(get<1>(o)) + 1;
@@ -28,9 +28,9 @@ Object<char, Object<int, char> > object(Object<char, Object<int, char> >& o) {
2828
}
2929

3030
Vector<float> vector(Vector<int>& v) {
31-
Vector<float> r(v.size);
31+
Vector<float> r(v.size());
3232

33-
for (size_t i {0}; i < v.size; i++) {
33+
for (size_t i {0}; i < v.size(); i++) {
3434
r[i] = float(v[i]) + 0.4;
3535
}
3636

@@ -59,7 +59,7 @@ void loop() {
5959
inc, F("inc: Increment a value. @a: Value. @return: a + 1."),
6060
setLed, F("set_led: Set LED brightness. @brightness: Brightness."),
6161
milliTime, F("milli_time: Report the system time. @return: System time."),
62-
object, F("object: Example with objects. @o: Object. @return: Object."),
62+
tuple, F("tuple: Example with tuples. @o: Tuple. @return: Tuple."),
6363
vector, F("cpp_vector: Example with vectors. @v: Vector. @return: Vector."),
6464
cVector, F("c_vector: Example with C vectors. @v: C vector. @return: Vector."));
6565
}

src/del.tcc

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,8 @@ inline void rpcDel(Tuple<>*) {}
5252

5353
/*! \ingroup del
5454
* \copydoc rpcDel(T*) */
55-
template <class... Membs>
56-
void rpcDel(Tuple<Membs...>* data) {
55+
template <class... Ts>
56+
void rpcDel(Tuple<Ts...>* data) {
5757
rpcDel(&(*data).head);
5858
rpcDel(&(*data).tail);
5959
}
60-
61-
62-
/*! \ingroup del
63-
* \copydoc rpcDel(T*) */
64-
template <class... Membs>
65-
void rpcDel(Object<Membs...>* data) {
66-
rpcDel(dynamic_cast<Tuple<Membs...>*>(data));
67-
}

src/interface.tcc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ void interface(Tuple<>, Args...) {}
137137
* \param t Tuple of input / output objects.
138138
* \param args Parameter pairs (function pointer, documentation).
139139
*/
140-
template <class... Membs, class... Args>
141-
void interface(Tuple<Membs...> t, Args... args) {
140+
template <class... Ts, class... Args>
141+
void interface(Tuple<Ts...> t, Args... args) {
142142
interface(*t.head, args...);
143143
interface(t.tail, args...);
144144
}

src/read.tcc

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,8 @@ inline void rpcRead(Stream&, Tuple<>*) {}
129129

130130
/*! \ingroup read
131131
* \copydoc rpcRead(Stream&, T*) */
132-
template <class... Membs>
133-
void rpcRead(Stream& io, Tuple<Membs...>* data) {
132+
template <class... Ts>
133+
void rpcRead(Stream& io, Tuple<Ts...>* data) {
134134
rpcRead(io, &(*data).head);
135135
rpcRead(io, &(*data).tail);
136136
}
137-
138-
139-
/*! \ingroup read
140-
* \copydoc rpcRead(Stream&, T*) */
141-
template <class... Membs>
142-
void rpcRead(Stream& io, Object<Membs...>* data) {
143-
rpcRead(io, dynamic_cast<Tuple<Membs...>*>(data));
144-
}

src/tuple.tcc

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,27 @@
1111
/*! \ingroup tuple
1212
* Empty Tuple.
1313
*/
14-
template <class... Membs>
15-
struct Tuple {};
14+
template <class...>
15+
class Tuple {};
1616

1717
/*! \ingroup tuple
1818
* Non-empty Tuple.
1919
*/
20-
template <class H, class... Tail>
21-
struct Tuple<H, Tail...> {
22-
H head {}; //!< First element.
23-
Tuple<Tail...> tail {}; //!< Remaining elements.
20+
template <class T, class... Ts>
21+
class Tuple<T, Ts...> {
22+
public:
23+
Tuple() = default;
24+
Tuple(T h, Ts... t);
25+
26+
T head {}; //!< First element.
27+
Tuple<Ts...> tail {}; //!< Remaining elements.
2428
};
2529

2630

31+
template <class T, class... Ts>
32+
Tuple<T, Ts...>::Tuple(T h, Ts... t) : head {h}, tail {t...} {}
33+
34+
2735
/*!
2836
* Access the type of the *k*-th element in a Tuple.
2937
*
@@ -33,63 +41,46 @@ template <size_t, class>
3341
struct ElemTypeHolder_;
3442

3543
//! \copydoc ElemTypeHolder_
36-
template <class H, class... Tail>
37-
struct ElemTypeHolder_<0, Tuple<H, Tail...> > {
38-
typedef H type;
44+
template <class T, class... Ts>
45+
struct ElemTypeHolder_<0, Tuple<T, Ts...> > {
46+
typedef T type;
3947
};
4048

4149
//! \copydoc ElemTypeHolder_
42-
template <size_t k, class H, class... Tail>
43-
struct ElemTypeHolder_<k, Tuple<H, Tail...> > {
44-
typedef typename ElemTypeHolder_<k - 1, Tuple<Tail...> >::type type;
50+
template <size_t k, class T, class... Ts>
51+
struct ElemTypeHolder_<k, Tuple<T, Ts...> > {
52+
typedef typename ElemTypeHolder_<k - 1, Tuple<Ts...> >::type type;
4553
};
4654

4755

4856
/*! \ingroup tuplehelper
49-
* Get the *k*-th element of a Tuple or Object.
57+
* Get the *k*-th element of a Tuple.
5058
*
5159
* This can be used for both retrieval as well as assignment.
5260
*
5361
* \param t A Tuple.
5462
*
5563
* \return Reference to the *k*-th element in `t`.
5664
*/
57-
template <size_t k, class... Membs>
65+
template <size_t k, class... Ts>
5866
//! \cond
5967
typename enableIf<
60-
k == 0, typename ElemTypeHolder_<0, Tuple<Membs...> >::type&>::type
68+
k == 0, typename ElemTypeHolder_<0, Tuple<Ts...> >::type&>::type
6169
//! \endcond
62-
get(Tuple<Membs...>& t) {
70+
get(Tuple<Ts...>& t) {
6371
return t.head;
6472
}
6573

66-
template <size_t k, class... Membs>
74+
template <size_t k, class... Ts>
6775
//! \cond
6876
typename enableIf<
69-
k != 0, typename ElemTypeHolder_<k, Tuple<Membs...> >::type&>::type
77+
k != 0, typename ElemTypeHolder_<k, Tuple<Ts...> >::type&>::type
7078
//! \endcond
71-
get(Tuple<Membs...>& t) {
79+
get(Tuple<Ts...>& t) {
7280
return get<k - 1>(t.tail);
7381
}
7482

7583

76-
//! Recursion terminator for `fill()`.
77-
inline void fill(Tuple<>) {}
78-
79-
/*!
80-
* Fill a Tuple.
81-
*
82-
* \param t Tuple.
83-
* \param head First element.
84-
* \param tail Remaining elements.
85-
*/
86-
template <class H, class... Tail>
87-
void fill(Tuple<H, Tail...>& t, H head, Tail... tail) {
88-
t.head = head;
89-
fill(t.tail, tail...);
90-
}
91-
92-
9384
/*! \ingroup tuplehelper
9485
* Make a Tuple from a parameter pack.
9586
*
@@ -98,25 +89,7 @@ void fill(Tuple<H, Tail...>& t, H head, Tail... tail) {
9889
* \return Tuple containing `args`.
9990
*/
10091
template <class... Args>
101-
Tuple<Args...> pack(Args... args) {
102-
Tuple<Args...> t;
103-
fill(t, args...);
104-
92+
Tuple<Args...> makeTuple(Args... args) {
93+
Tuple<Args...> t {args...};
10594
return t;
10695
}
107-
108-
109-
/*! \ingroup object
110-
* Object.
111-
*/
112-
template <class... Membs>
113-
struct Object : Tuple<Membs...> {
114-
/*
115-
* Preferably this would have been an alias, but this is not supported in
116-
* C++11.
117-
*/
118-
Object() {}
119-
Object(Membs... args) : Tuple<Membs...>() {
120-
fill(*dynamic_cast<Tuple<Membs...>*>(this), args...);
121-
}
122-
};

src/types.tcc

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,32 +132,25 @@ inline void rpcTypeOf(Stream& io, double) {
132132
}
133133

134134

135-
//! Recursion terminator for `rpcTypeOf(Tuple&)`.
136-
inline void rpcTypeOf(Stream&, Tuple<>&) {}
135+
//! Recursion terminator for `rpcTypeOf_(Tuple&)`.
136+
inline void rpcTypeOf_(Stream&, Tuple<>&) {}
137137

138138
/*! \ingroup types
139139
* Get the types of all members of a Tuple.
140140
*
141141
* \param io Stream.
142142
* \param t Tuple.
143143
*/
144-
template <class... Membs>
145-
void rpcTypeOf(Stream& io, Tuple<Membs...>& t) {
144+
template <class... Ts>
145+
void rpcTypeOf_(Stream& io, Tuple<Ts...>& t) {
146146
rpcTypeOf(io, t.head);
147-
rpcTypeOf(io, t.tail);
147+
rpcTypeOf_(io, t.tail);
148148
}
149149

150-
151-
/*! \ingroup types
152-
* Get the types of all members of an Object.
153-
*
154-
* \param io Stream.
155-
* \param t Object.
156-
*/
157-
template <class... Membs>
158-
void rpcTypeOf(Stream& io, Object<Membs...>& o) {
150+
template <class... Ts>
151+
void rpcTypeOf(Stream& io, Tuple<Ts...>& t) {
159152
rpcPrint(io, '(');
160-
rpcTypeOf(io, dynamic_cast<Tuple<Membs...>&>(o));
153+
rpcTypeOf_(io, t);
161154
rpcPrint(io, ')');
162155
}
163156

src/write.tcc

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,8 @@ inline void rpcWrite(Stream&, Tuple<>*) {}
6767

6868
/*! \ingroup write
6969
* \copydoc rpcWrite(Stream&, T*) */
70-
template <class... Membs>
71-
void rpcWrite(Stream& io, Tuple<Membs...>* data) {
70+
template <class... Ts>
71+
void rpcWrite(Stream& io, Tuple<Ts...>* data) {
7272
rpcWrite(io, &(*data).head);
7373
rpcWrite(io, &(*data).tail);
7474
}
75-
76-
77-
/*! \ingroup write
78-
* \copydoc rpcWrite(Stream&, T*) */
79-
template <class... Membs>
80-
void rpcWrite(Stream& io, Object<Membs...>* data) {
81-
rpcWrite(io, dynamic_cast<Tuple<Membs...>*>(data));
82-
}

tests/test_interface.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ TEST_CASE("Describe class member function", "[describe][class]") {
2929

3030
// Empty description.
3131
Serial.reset();
32-
describe_(Serial, pack(&c, &C::f), "");
32+
describe_(Serial, makeTuple(&c, &C::f), "");
3333
REQUIRE(Serial.inspect<String>() == ":;");
3434

3535
// Non-empty description.
3636
Serial.reset();
37-
describe_(Serial, pack(&c, &C::f), "Function description.");
37+
describe_(Serial, makeTuple(&c, &C::f), "Function description.");
3838
REQUIRE(Serial.inspect<String>() == ":;Function description.");
3939
}
4040

@@ -49,13 +49,13 @@ TEST_CASE("Multiple functions", "[describe][class]") {
4949

5050
// Normal function first.
5151
Serial.reset();
52-
describe_(Serial, f, "f", pack(&c, &C::f), "C::f");
52+
describe_(Serial, f, "f", makeTuple(&c, &C::f), "C::f");
5353
REQUIRE(Serial.inspect<String>() == ":;f");
5454
REQUIRE(Serial.inspect<String>() == ":;C::f");
5555

5656
// Class member function first.
5757
Serial.reset();
58-
describe_(Serial, pack(&c, &C::f), "C::f", f, "f");
58+
describe_(Serial, makeTuple(&c, &C::f), "C::f", f, "f");
5959
REQUIRE(Serial.inspect<String>() == ":;C::f");
6060
REQUIRE(Serial.inspect<String>() == ":;f");
6161
}
@@ -135,7 +135,7 @@ TEST_CASE("RPC call function x", "[call][basic]") {
135135
Serial.reset();
136136
Serial.prepare('\0');
137137
Serial.prepare('\0');
138-
interface(pack(&Serial, &Serial), S::f, "");
138+
interface(makeTuple(&Serial, &Serial), S::f, "");
139139
REQUIRE(Serial.inspect<short int>() == 2);
140140
REQUIRE(Serial.inspect<short int>() == 2);
141141
REQUIRE(Serial.rx == 2 * (sizeof(byte) + sizeof(int)));

0 commit comments

Comments
 (0)