Skip to content

Commit faf0202

Browse files
authored
Merge pull request #764 from ckormanyos/more_11_07a
More 11_07a
2 parents 5a9ca5b + ecb5fd5 commit faf0202

38 files changed

Lines changed: 1040 additions & 424 deletions

‎examples/chapter11_07a/chapter11_07a.vcxproj.filters‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,12 +533,6 @@
533533
<ClInclude Include="src\mcal\host\mcal_dev.h">
534534
<Filter>src\mcal\host</Filter>
535535
</ClInclude>
536-
<ClInclude Include="src\mcal_memory\mcal_memory_nonconst_address_ptr.h">
537-
<Filter>src\mcal_memory</Filter>
538-
</ClInclude>
539-
<ClInclude Include="src\mcal_memory\mcal_memory_progmem_access.h">
540-
<Filter>src\mcal_memory</Filter>
541-
</ClInclude>
542536
<ClInclude Include="src\mcal_memory\mcal_memory_progmem_array.h">
543537
<Filter>src\mcal_memory</Filter>
544538
</ClInclude>
@@ -581,6 +575,12 @@
581575
<ClInclude Include="src\mcal_memory\mcal_memory_const_address_ptr.h">
582576
<Filter>src\mcal_memory</Filter>
583577
</ClInclude>
578+
<ClInclude Include="src\mcal_memory\mcal_memory_nonconst_address_ptr.h">
579+
<Filter>src\mcal_memory</Filter>
580+
</ClInclude>
581+
<ClInclude Include="src\mcal_memory\mcal_memory_progmem_access.h">
582+
<Filter>src\mcal_memory</Filter>
583+
</ClInclude>
584584
</ItemGroup>
585585
<ItemGroup>
586586
<None Include="src\util\STL_C++XX_stdfloat\cstdfloat">

‎examples/chapter11_07a/src/mcal_memory/mcal_memory_const_address_ptr.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
auto operator[](const difference_type i) const noexcept -> reference
5252
{
53-
return *(my_ptr + (i * difference_type(static_size)));
53+
return *offset_pointer(my_ptr, i);
5454
}
5555

5656
auto operator++() noexcept -> const_address_ptr& { my_ptr += static_size; return *this; }

‎examples/chapter11_07a/src/mcal_memory/mcal_memory_nonconst_address_ptr.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@
5353

5454
auto operator[](const difference_type i) noexcept -> reference
5555
{
56-
reference value = *(my_ptr + (i * difference_type(static_size)));
56+
reference value = *offset_pointer(my_ptr, i);
5757

5858
return value;
5959
}
6060

6161
auto operator[](const difference_type i) const noexcept -> const_reference
6262
{
63-
const_reference value = *(my_ptr + (i * difference_type(static_size)));
63+
const_reference value = *offset_pointer(my_ptr, i);
6464

6565
return value;
6666
}
@@ -97,8 +97,8 @@
9797

9898
private:
9999
static constexpr pointer offset_pointer(pointer ptr,
100-
difference_type n,
101-
bool subtract = false) noexcept
100+
difference_type n,
101+
bool subtract = false) noexcept
102102
{
103103
return ((n < 0) ? (subtract ? ptr + difference_type((size_type(0U) - size_type(n)) * static_size)
104104
: ptr - difference_type((size_type(0U) - size_type(n)) * static_size))

‎examples/chapter11_07a/src/mcal_memory/mcal_memory_progmem_access.h‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
#ifndef MCAL_MEMORY_PROGMEM_ACCESS_2019_08_17_H
99
#define MCAL_MEMORY_PROGMEM_ACCESS_2019_08_17_H
1010

11+
#include <mcal_memory_progmem.h>
12+
1113
#include <cstddef>
1214
#include <cstdint>
1315
#include <type_traits>
1416

15-
#include <mcal_memory_progmem.h>
16-
1717
namespace mcal { namespace memory { namespace progmem {
1818

1919
template<typename ValueType>
@@ -24,9 +24,12 @@
2424
&& (sizeof(ValueType) != 4U)
2525
&& (sizeof(ValueType) != 8U))>::type* = nullptr) noexcept -> ValueType
2626
{
27+
static_assert(std::is_trivially_copyable<ValueType>::value,
28+
"Program-memory values must be trivially copyable.");
29+
2730
using local_value_type = ValueType;
2831

29-
local_value_type dest;
32+
local_value_type dest { };
3033

3134
for(std::size_t i = 0U; i < sizeof(ValueType); ++i)
3235
{

‎examples/chapter11_07a/src/mcal_memory/mcal_memory_progmem_array.h‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
#ifndef MCAL_MEMORY_PROGMEM_ARRAY_2019_05_04_H
99
#define MCAL_MEMORY_PROGMEM_ARRAY_2019_05_04_H
1010

11+
#include <mcal_memory/mcal_memory_progmem_iterator.h>
12+
1113
#include <algorithm>
1214
#include <cstddef>
1315
#include <iterator>
1416
#include <type_traits>
1517

16-
#include <mcal_memory/mcal_memory_progmem_iterator.h>
17-
18-
// Implement most an std::array-like container for read-only program memory.
18+
// Implement most of an std::array-like container for read-only program memory.
1919
// See ISO/IEC 14882:2011 Chapter 23.3.2.
2020

2121
namespace mcal { namespace memory { namespace progmem {
@@ -136,28 +136,34 @@
136136
return (!(right < left));
137137
}
138138

139-
template<typename T>
140-
struct tuple_size;
141-
142-
template<typename T, const mcal_progmem_uintptr_t N>
143-
struct tuple_size<mcal::memory::progmem::array<T, N>>
144-
: public std::integral_constant<mcal_progmem_uintptr_t, N> { };
145-
146-
template<mcal_progmem_uintptr_t N, typename T>
147-
struct tuple_element;
148-
149139
template<mcal_progmem_uintptr_t I,
150140
typename T,
151141
mcal_progmem_uintptr_t N>
152-
struct tuple_element<I, mcal::memory::progmem::array<T, N>>
142+
auto get(const array<T, N>& source) noexcept -> typename array<T, N>::const_reference
153143
{
154-
static_assert(I < N, "Sorry, tuple_element index is out of bounds.");
155-
156-
typedef T type;
157-
};
144+
static_assert(I < N, "Sorry, get index is out of bounds.");
145+
return source[I];
146+
}
158147

159148
} } } // namespace mcal::memory::progmem
160149

150+
namespace std
151+
{
152+
template<typename T, mcal_progmem_uintptr_t N>
153+
struct tuple_size<mcal::memory::progmem::array<T, N>>
154+
: integral_constant<std::size_t, static_cast<std::size_t>(N)> { };
155+
156+
template<std::size_t I,
157+
typename T,
158+
mcal_progmem_uintptr_t N>
159+
struct tuple_element<I, mcal::memory::progmem::array<T, N>>
160+
{
161+
static_assert(I < N, "Sorry, tuple_element index is out of bounds.");
162+
163+
using type = T;
164+
};
165+
}
166+
161167
template<typename T, mcal_progmem_uintptr_t N>
162168
constexpr mcal_progmem_uintptr_t mcal::memory::progmem::array<T, N>::static_size;
163169

‎examples/chapter11_07a/src/mcal_memory/mcal_memory_progmem_iterator.h‎

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
#ifndef MCAL_MEMORY_PROGMEM_ITERATOR_2019_05_04_H
99
#define MCAL_MEMORY_PROGMEM_ITERATOR_2019_05_04_H
1010

11-
#include <iterator>
12-
13-
#include <type_traits>
14-
1511
#include <mcal_memory/mcal_memory_const_address_ptr.h>
1612
#include <mcal_memory/mcal_memory_progmem_ptr.h>
1713
#include <mcal_memory/mcal_memory_random_access_iterator_operations.h>
1814

15+
#include <iterator>
16+
#include <type_traits>
17+
1918
// Implement specialized iterator types for read-only program memory.
2019

2120
namespace mcal { namespace memory { namespace progmem {
@@ -53,15 +52,15 @@
5352

5453
progmem_iterator(const pointer x) noexcept : current(x) { }
5554

56-
template<typename OtherIteratorType,
55+
template<typename OtherValueType,
5756
typename OtherAddressType,
5857
typename OtherAddressDifferenceType,
59-
typename std::enable_if<
60-
std::is_convertible<OtherIteratorType, ValueType>::value &&
58+
typename std::enable_if_t<
59+
std::is_convertible<OtherValueType, ValueType>::value &&
6160
std::is_convertible<OtherAddressType, AddressType>::value &&
6261
std::is_convertible<OtherAddressDifferenceType, AddressDifferenceType>::value
63-
>::type* = nullptr>
64-
progmem_iterator(const progmem_iterator<OtherIteratorType, OtherAddressType, OtherAddressDifferenceType>& other) noexcept
62+
>* = nullptr>
63+
progmem_iterator(const progmem_iterator<OtherValueType, OtherAddressType, OtherAddressDifferenceType>& other) noexcept
6564
: current(static_cast<const pointer>(other.current)) { }
6665

6766
auto operator*() const noexcept -> reference
@@ -91,7 +90,50 @@
9190
}
9291

9392
auto operator+=(difference_type n) noexcept -> progmem_iterator& { operations::increment(current, n); return *this; }
94-
auto operator-=(difference_type n) noexcept -> progmem_iterator& { current = operations::subtract(current, n); return *this; }
93+
auto operator-=(difference_type n) noexcept -> progmem_iterator& { operations::increment(current, -n); return *this; }
94+
95+
template<typename OtherValueType, typename OtherAddressType, typename OtherAddressDifferenceType>
96+
auto operator==(const progmem_iterator<OtherValueType, OtherAddressType, OtherAddressDifferenceType>& other) const noexcept -> bool
97+
{
98+
return (const_pointer(current) == const_pointer(other.current));
99+
}
100+
101+
template<typename OtherValueType, typename OtherAddressType, typename OtherAddressDifferenceType>
102+
auto operator!=(const progmem_iterator<OtherValueType, OtherAddressType, OtherAddressDifferenceType>& other) const noexcept -> bool
103+
{
104+
return !(*this == other);
105+
}
106+
107+
template<typename OtherValueType, typename OtherAddressType, typename OtherAddressDifferenceType>
108+
auto operator-(const progmem_iterator<OtherValueType, OtherAddressType, OtherAddressDifferenceType>& other) const noexcept
109+
-> difference_type
110+
{
111+
return const_pointer(current) - const_pointer(other.current);
112+
}
113+
114+
template<typename OtherValueType, typename OtherAddressType, typename OtherAddressDifferenceType>
115+
auto operator<(const progmem_iterator<OtherValueType, OtherAddressType, OtherAddressDifferenceType>& other) const noexcept -> bool
116+
{
117+
return const_pointer(current) < const_pointer(other.current);
118+
}
119+
120+
template<typename OtherValueType, typename OtherAddressType, typename OtherAddressDifferenceType>
121+
auto operator<=(const progmem_iterator<OtherValueType, OtherAddressType, OtherAddressDifferenceType>& other) const noexcept -> bool
122+
{
123+
return const_pointer(current) <= const_pointer(other.current);
124+
}
125+
126+
template<typename OtherValueType, typename OtherAddressType, typename OtherAddressDifferenceType>
127+
auto operator>(const progmem_iterator<OtherValueType, OtherAddressType, OtherAddressDifferenceType>& other) const noexcept -> bool
128+
{
129+
return const_pointer(current) > const_pointer(other.current);
130+
}
131+
132+
template<typename OtherValueType, typename OtherAddressType, typename OtherAddressDifferenceType>
133+
auto operator>=(const progmem_iterator<OtherValueType, OtherAddressType, OtherAddressDifferenceType>& other) const noexcept -> bool
134+
{
135+
return const_pointer(current) >= const_pointer(other.current);
136+
}
95137

96138
private:
97139
pointer current{};

‎examples/chapter11_07a/src/mcal_memory/mcal_memory_progmem_ptr.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <mcal_memory/mcal_memory_progmem_ref.h>
1212

13+
#include <iterator>
1314
#include <type_traits>
1415

1516
// Implement a specialized pointer type for read-only program memory.
@@ -33,19 +34,19 @@
3334

3435
static constexpr size_type value_size = sizeof(value_type);
3536

36-
progmem_ptr() noexcept = default;
37+
constexpr progmem_ptr() noexcept = default;
3738

3839
explicit constexpr progmem_ptr(address_type addr) noexcept
3940
: my_address(addr) { }
4041

4142
template<typename OtherValueType,
4243
typename OtherAddressType,
4344
typename OtherAddressDifferenceType,
44-
typename std::enable_if<
45+
typename std::enable_if_t<
4546
std::is_convertible<OtherValueType, ValueType>::value &&
4647
std::is_convertible<OtherAddressType, address_type>::value &&
4748
std::is_convertible<OtherAddressDifferenceType, AddressDifferenceType>::value
48-
>::type* = nullptr>
49+
>* = nullptr>
4950
progmem_ptr(const progmem_ptr<OtherValueType, OtherAddressType, OtherAddressDifferenceType>& other) noexcept
5051
: my_address(other.my_address) { }
5152

‎examples/chapter11_07a/src/mcal_memory/mcal_memory_sram_access.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@
1919
auto read(const mcal_sram_uintptr_t src_addr,
2020
const typename std::enable_if<(sizeof(ValueType) == 1U)>::type* = nullptr) noexcept -> ValueType
2121
{
22+
static_assert(std::is_trivially_copyable<ValueType>::value,
23+
"SRAM values must be trivially copyable.");
24+
2225
auto byte_to_read = std::uint8_t { };
2326

27+
// The backend status is intentionally ignored. SRAM proxy operations are
28+
// value-oriented and do not expose a transaction status.
2429
const auto result_read_is_ok = mcal_memory_sram_device().read(src_addr, &byte_to_read);
2530

2631
static_cast<void>(result_read_is_ok);
@@ -32,10 +37,15 @@
3237
auto read(const mcal_sram_uintptr_t src_addr,
3338
const typename std::enable_if<(sizeof(ValueType) != 1U)>::type* = nullptr) noexcept -> ValueType
3439
{
40+
static_assert(std::is_trivially_copyable<ValueType>::value,
41+
"SRAM values must be trivially copyable.");
42+
3543
using local_value_type = ValueType;
3644

3745
auto value_to_read = local_value_type { };
3846

47+
// The backend status is intentionally ignored. SRAM proxy operations do
48+
// not expose a transaction status.
3949
const auto result_read_is_ok =
4050
mcal_memory_sram_device().read_n(src_addr, reinterpret_cast<std::uint8_t*>(&value_to_read), sizeof(local_value_type));
4151

@@ -49,6 +59,11 @@
4959
const mcal_sram_uintptr_t dest_addr,
5060
const typename std::enable_if<(sizeof(ValueType) == 1U)>::type* = nullptr) noexcept -> void
5161
{
62+
static_assert(std::is_trivially_copyable<ValueType>::value,
63+
"SRAM values must be trivially copyable.");
64+
65+
// The backend status is intentionally ignored. SRAM proxy operations do
66+
// not expose a transaction status.
5267
const auto result_write_is_ok = mcal_memory_sram_device().write(dest_addr, &src_value);
5368

5469
static_cast<void>(result_write_is_ok);
@@ -59,6 +74,11 @@
5974
const mcal_sram_uintptr_t dest_addr,
6075
const typename std::enable_if<(sizeof(ValueType) != 1U)>::type* = nullptr) noexcept -> void
6176
{
77+
static_assert(std::is_trivially_copyable<ValueType>::value,
78+
"SRAM values must be trivially copyable.");
79+
80+
// The backend status is intentionally ignored. SRAM proxy operations do
81+
// not expose a transaction status.
6282
using local_value_type = ValueType;
6383

6484
const auto result_write_is_ok = mcal_memory_sram_device().write_n(dest_addr, reinterpret_cast<const std::uint8_t*>(&src_value), sizeof(local_value_type));

0 commit comments

Comments
 (0)