Skip to content

Commit 81e99b4

Browse files
committed
Add axisId
1 parent f9b5680 commit 81e99b4

File tree

19 files changed

+140
-84
lines changed

19 files changed

+140
-84
lines changed

src/base/refl/auto_enum.h

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,27 @@ namespace Detail
3434
{
3535
template <class T> using real_t = std::underlying_type_t<T>;
3636

37-
template <class E, real_t<E> From, real_t<E> To>
38-
requires(!std::same_as<bool, real_t<E>>)
37+
template <class E>
38+
concept UniqueNames = requires {
39+
static_cast<std::string_view>(unique_enum_names(E{}));
40+
};
41+
42+
template <class E>
43+
concept UniqueRange = requires {
44+
static_cast<std::pair<real_t<E>, real_t<E>>>(unique_from_to(E{}));
45+
};
46+
47+
template <class E>
48+
requires(UniqueRange<E>)
49+
consteval std::pair<real_t<E>, real_t<E>> from_to()
50+
{
51+
constexpr auto res = unique_from_to(E{});
52+
static_assert(res.first <= res.second, "Invalid range");
53+
return res;
54+
}
55+
56+
template <class E, real_t<E> From = {}, real_t<E> To = {}>
57+
requires(!UniqueRange<E> && !std::same_as<bool, real_t<E>>)
3958
consteval std::pair<real_t<E>, real_t<E>> from_to()
4059
{
4160
if constexpr (std::is_signed_v<real_t<E>>
@@ -44,39 +63,40 @@ consteval std::pair<real_t<E>, real_t<E>> from_to()
4463
return from_to<E, From - 1, To>();
4564
else if constexpr (!Name::name<E, static_cast<E>(To)>().empty())
4665
return from_to<E, From, To + 1>();
47-
else
48-
return {From, To};
66+
else {
67+
static_assert(From != To, "Not found any enum string");
68+
return {From, To - 1};
69+
}
4970
}
5071

5172
template <class E, int, int>
52-
requires(std::same_as<bool, real_t<E>>)
73+
requires(!UniqueRange<E> && std::same_as<bool, real_t<E>>)
5374
consteval std::pair<real_t<E>, real_t<E>> from_to()
5475
{
55-
return {false, false};
76+
constexpr auto has_false =
77+
!Name::name<E, static_cast<E>(false)>().empty();
78+
constexpr auto has_true =
79+
!Name::name<E, static_cast<E>(true)>().empty();
80+
static_assert(has_false || has_true, "Not found any enum string");
81+
return {!has_false, has_true};
5682
}
5783

5884
template <class E>
5985
requires(!std::same_as<bool, real_t<E>>)
6086
consteval real_t<E> count()
6187
{
62-
auto [from, to] = from_to<E, 0, 0>();
63-
return static_cast<real_t<E>>(to - from);
88+
auto [from, to] = from_to<E>();
89+
return static_cast<real_t<E>>(to - from + 1);
6490
}
6591

6692
template <class E>
6793
requires(std::same_as<bool, real_t<E>>)
6894
consteval int count()
6995
{
70-
return Name::name<E, static_cast<E>(false)>().empty()
71-
? 0
72-
: 1 + !Name::name<E, static_cast<E>(true)>().empty();
96+
auto [from, to] = from_to<E>();
97+
return to - from + 1;
7398
}
7499

75-
template <class E>
76-
concept UniqueNames = requires {
77-
static_cast<std::string_view>(unique_enum_names(E{}));
78-
};
79-
80100
template <class E, class F = real_t<E>, F... Ix>
81101
consteval auto whole_array(std::integer_sequence<F, Ix...> = {})
82102
{
@@ -87,7 +107,7 @@ consteval auto whole_array(std::integer_sequence<F, Ix...> = {})
87107
return res;
88108
}
89109
else {
90-
constexpr auto first = Detail::from_to<E, 0, 0>().first;
110+
constexpr auto first = Detail::from_to<E>().first;
91111
std::array<char,
92112
(Name::name<E, static_cast<E>(Ix + first)>().size() + ...
93113
+ (sizeof...(Ix) - 1))>
@@ -109,7 +129,7 @@ constexpr std::array enum_name_holder = Detail::whole_array<E>(
109129
Detail::count<E>()>{});
110130

111131
template <class E>
112-
constexpr std::array enum_name_holder<E, true> =
132+
constinit const std::array enum_name_holder<E, true> =
113133
Detail::whole_array<E, int>(
114134
std::make_integer_sequence<int, Detail::count<E>()>{});
115135

@@ -143,7 +163,7 @@ template <class E> constexpr std::array enum_names = get_names<E>();
143163
template <class Type = std::string_view, class E>
144164
Type enum_name(E name)
145165
{
146-
constexpr auto first = Detail::from_to<E, 0, 0>().first;
166+
constexpr auto first = Detail::from_to<E>().first;
147167
constexpr auto n = std::size(enum_names<E>);
148168
if (static_cast<std::size_t>(
149169
static_cast<Detail::real_t<E>>(name) - first)
@@ -159,7 +179,7 @@ Type enum_name(E name)
159179

160180
template <class E> constexpr E get_enum(const std::string_view &data)
161181
{
162-
constexpr auto first = Detail::from_to<E, 0, 0>().first;
182+
constexpr auto first = Detail::from_to<E>().first;
163183
Detail::real_t<E> ix{};
164184
for (auto v : enum_names<E>) {
165185
if (v == data) break;
@@ -204,7 +224,7 @@ struct EnumArray : std::array<V, std::size(enum_names<E>)>
204224
template <class E, class... Args>
205225
requires(std::is_enum_v<E>
206226
&& sizeof...(Args) == Detail::count<E>()
207-
&& Detail::from_to<E, 0, 0>().first == 0)
227+
&& Detail::from_to<E>().first == 0)
208228
struct EnumVariant : std::variant<Args...>
209229
{
210230
using base_variant = std::variant<Args...>;

src/chart/animator/planner.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,8 @@ bool Planner::needVertical() const
425425
!= target->axises.at(Gen::ChannelId::y).measure
426426
|| source->axises.at(Gen::ChannelId::y).dimension
427427
!= target->axises.at(Gen::ChannelId::y).dimension
428-
|| source->guides.at(Gen::ChannelId::y)
429-
!= target->guides.at(Gen::ChannelId::y)
428+
|| source->guides.at(Gen::AxisId::y)
429+
!= target->guides.at(Gen::AxisId::y)
430430
|| (isAnyLegend(Gen::ChannelId::size)
431431
&& (source->axises.at(Gen::ChannelId::size).common
432432
!= target->axises.at(Gen::ChannelId::size).common
@@ -472,8 +472,8 @@ bool Planner::needHorizontal() const
472472
!= target->axises.at(Gen::ChannelId::x).measure
473473
|| source->axises.at(Gen::ChannelId::x).dimension
474474
!= target->axises.at(Gen::ChannelId::x).dimension
475-
|| source->guides.at(Gen::ChannelId::x)
476-
!= target->guides.at(Gen::ChannelId::x)
475+
|| source->guides.at(Gen::AxisId::x)
476+
!= target->guides.at(Gen::AxisId::x)
477477
|| source->anyAxisSet != target->anyAxisSet
478478
|| source->keepAspectRatio != target->keepAspectRatio
479479
|| (source->markerConnectionOrientation

src/chart/generator/axis.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,22 @@ struct Axises
154154
return axises.at(channelType);
155155
}
156156

157-
[[nodiscard]] const Axis &other(ChannelId channelType) const
157+
[[nodiscard]] const Axis &at(AxisId axisType) const
158158
{
159-
switch (channelType) {
160-
case ChannelId::x: return axises.at(ChannelId::y);
161-
case ChannelId::y: return axises.at(ChannelId::x);
162-
default: throw std::logic_error("not an axis channel");
159+
return axises.at(asChannel(axisType));
160+
}
161+
162+
[[nodiscard]] Axis &at(AxisId axisType)
163+
{
164+
return axises.at(asChannel(axisType));
165+
}
166+
167+
[[nodiscard]] const Axis &other(AxisId axisType) const
168+
{
169+
switch (axisType) {
170+
default:
171+
case AxisId::x: return at(AxisId::y);
172+
case AxisId::y: return at(AxisId::x);
163173
}
164174
}
165175

src/chart/generator/guides.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "guides.h"
22

3-
#include <stdexcept>
4-
53
#include "base/math/interpolation.h"
64
#include "chart/options/align.h"
75
#include "chart/options/channel.h"
@@ -92,18 +90,22 @@ Guides::Guides(const Options &options)
9290
|| (!yIsMeasure && !yOpt.isEmpty())));
9391
}
9492

95-
GuidesByAxis &Guides::at(ChannelId channel)
93+
GuidesByAxis &Guides::at(AxisId channel)
9694
{
97-
if (channel == ChannelId::x) return x;
98-
if (channel == ChannelId::y) return y;
99-
throw std::out_of_range("guides index out of range");
95+
switch (channel) {
96+
default:
97+
case AxisId::x: return x;
98+
case AxisId::y: return y;
99+
}
100100
}
101101

102-
const GuidesByAxis &Guides::at(ChannelId channel) const
102+
const GuidesByAxis &Guides::at(AxisId channel) const
103103
{
104-
if (channel == ChannelId::x) return x;
105-
if (channel == ChannelId::y) return y;
106-
throw std::out_of_range("guides index out of range");
104+
switch (channel) {
105+
default:
106+
case AxisId::x: return x;
107+
case AxisId::y: return y;
108+
}
107109
}
108110

109111
bool Guides::hasAnyGuides() const

src/chart/generator/guides.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ struct Guides
3232
GuidesByAxis y;
3333

3434
explicit Guides(const Options &options);
35-
[[nodiscard]] const GuidesByAxis &at(ChannelId channel) const;
36-
GuidesByAxis &at(ChannelId channel);
35+
[[nodiscard]] const GuidesByAxis &at(AxisId channel) const;
36+
GuidesByAxis &at(AxisId channel);
3737
[[nodiscard]] bool hasAnyGuides() const;
3838
};
3939

src/chart/generator/plotbuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ void PlotBuilder::calcDimensionAxis(ChannelId type)
433433

434434
if (scale.isMeasure() || !scale.hasDimension()) return;
435435

436-
auto &&isTypeAxis = isAxis(type);
436+
auto &&isTypeAxis = asAxis(type).has_value();
437437
if (auto merge = scale.labelLevel == 0; isTypeAxis) {
438438
for (const auto &marker : plot->markers) {
439439
if (!marker.enabled) continue;

src/chart/main/style.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,9 @@ struct PlotParams
363363

364364
struct Plot : Padding, Box, PlotParams
365365
{
366-
[[nodiscard]] const Axis &getAxis(Gen::ChannelId id) const
366+
[[nodiscard]] const Axis &getAxis(Gen::AxisId id) const
367367
{
368-
return id == Gen::ChannelId::x ? xAxis : yAxis;
368+
return id == Gen::AxisId::x ? xAxis : yAxis;
369369
}
370370
};
371371

src/chart/options/advancedoptions.cpp

Whitespace-only changes.

src/chart/options/advancedoptions.h

Whitespace-only changes.

src/chart/options/channel.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@
1616
namespace Vizzu::Gen
1717
{
1818

19-
bool isAxis(ChannelId type)
19+
std::optional<AxisId> asAxis(ChannelId type)
2020
{
21-
return type == ChannelId::x || type == ChannelId::y;
21+
return type == ChannelId::x || type == ChannelId::y
22+
? std::make_optional(static_cast<AxisId>(
23+
static_cast<ChannelIdType>(type)))
24+
: std::nullopt;
25+
}
26+
27+
ChannelId asChannel(AxisId type)
28+
{
29+
return static_cast<ChannelId>(static_cast<ChannelIdType>(type));
2230
}
2331

2432
Channel Channel::makeChannel(Type id)

0 commit comments

Comments
 (0)