Skip to content

Commit 03d9b39

Browse files
authored
Merge pull request #581 from vizzuhq/add_axisid
Add axisId
2 parents b98eb87 + 4603ff3 commit 03d9b39

File tree

17 files changed

+140
-84
lines changed

17 files changed

+140
-84
lines changed

src/base/refl/auto_enum.h

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,29 @@ 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,
57+
real_t<E> From = real_t<E>{},
58+
real_t<E> To = real_t<E>{}>
59+
requires(!UniqueRange<E> && !std::same_as<bool, real_t<E>>)
3960
consteval std::pair<real_t<E>, real_t<E>> from_to()
4061
{
4162
if constexpr (std::is_signed_v<real_t<E>>
@@ -44,39 +65,40 @@ consteval std::pair<real_t<E>, real_t<E>> from_to()
4465
return from_to<E, From - 1, To>();
4566
else if constexpr (!Name::name<E, static_cast<E>(To)>().empty())
4667
return from_to<E, From, To + 1>();
47-
else
48-
return {From, To};
68+
else {
69+
static_assert(From != To, "Not found any enum string");
70+
return {From, To - 1};
71+
}
4972
}
5073

5174
template <class E, int, int>
52-
requires(std::same_as<bool, real_t<E>>)
75+
requires(!UniqueRange<E> && std::same_as<bool, real_t<E>>)
5376
consteval std::pair<real_t<E>, real_t<E>> from_to()
5477
{
55-
return {false, false};
78+
constexpr auto has_false =
79+
!Name::name<E, static_cast<E>(false)>().empty();
80+
constexpr auto has_true =
81+
!Name::name<E, static_cast<E>(true)>().empty();
82+
static_assert(has_false || has_true, "Not found any enum string");
83+
return {!has_false, has_true};
5684
}
5785

5886
template <class E>
5987
requires(!std::same_as<bool, real_t<E>>)
6088
consteval real_t<E> count()
6189
{
62-
auto [from, to] = from_to<E, 0, 0>();
63-
return static_cast<real_t<E>>(to - from);
90+
auto [from, to] = from_to<E>();
91+
return static_cast<real_t<E>>(to - from + 1);
6492
}
6593

6694
template <class E>
6795
requires(std::same_as<bool, real_t<E>>)
6896
consteval int count()
6997
{
70-
return Name::name<E, static_cast<E>(false)>().empty()
71-
? 0
72-
: 1 + !Name::name<E, static_cast<E>(true)>().empty();
98+
auto [from, to] = from_to<E>();
99+
return to - from + 1;
73100
}
74101

75-
template <class E>
76-
concept UniqueNames = requires {
77-
static_cast<std::string_view>(unique_enum_names(E{}));
78-
};
79-
80102
template <class E, class F = real_t<E>, F... Ix>
81103
consteval auto whole_array(std::integer_sequence<F, Ix...> = {})
82104
{
@@ -87,7 +109,7 @@ consteval auto whole_array(std::integer_sequence<F, Ix...> = {})
87109
return res;
88110
}
89111
else {
90-
constexpr auto first = Detail::from_to<E, 0, 0>().first;
112+
constexpr auto first = Detail::from_to<E>().first;
91113
std::array<char,
92114
(Name::name<E, static_cast<E>(Ix + first)>().size() + ...
93115
+ (sizeof...(Ix) - 1))>
@@ -109,7 +131,7 @@ constexpr std::array enum_name_holder = Detail::whole_array<E>(
109131
Detail::count<E>()>{});
110132

111133
template <class E>
112-
constexpr std::array enum_name_holder<E, true> =
134+
constinit const std::array enum_name_holder<E, true> =
113135
Detail::whole_array<E, int>(
114136
std::make_integer_sequence<int, Detail::count<E>()>{});
115137

@@ -143,7 +165,7 @@ template <class E> constexpr std::array enum_names = get_names<E>();
143165
template <class Type = std::string_view, class E>
144166
Type enum_name(E name)
145167
{
146-
constexpr auto first = Detail::from_to<E, 0, 0>().first;
168+
constexpr auto first = Detail::from_to<E>().first;
147169
constexpr auto n = std::size(enum_names<E>);
148170
if (static_cast<std::size_t>(
149171
static_cast<Detail::real_t<E>>(name) - first)
@@ -159,7 +181,7 @@ Type enum_name(E name)
159181

160182
template <class E> constexpr E get_enum(const std::string_view &data)
161183
{
162-
constexpr auto first = Detail::from_to<E, 0, 0>().first;
184+
constexpr auto first = Detail::from_to<E>().first;
163185
Detail::real_t<E> ix{};
164186
for (auto v : enum_names<E>) {
165187
if (v == data) break;
@@ -204,7 +226,7 @@ struct EnumArray : std::array<V, std::size(enum_names<E>)>
204226
template <class E, class... Args>
205227
requires(std::is_enum_v<E>
206228
&& sizeof...(Args) == Detail::count<E>()
207-
&& Detail::from_to<E, 0, 0>().first == 0)
229+
&& Detail::from_to<E>().first == 0)
208230
struct EnumVariant : std::variant<Args...>
209231
{
210232
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)