Skip to content

Commit

Permalink
layer optimizaton
Browse files Browse the repository at this point in the history
  • Loading branch information
profezzorn committed Dec 31, 2023
1 parent 1211344 commit 3098fc3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
16 changes: 11 additions & 5 deletions styles/layers.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
// opaque. If the base color is transparent, the final result may also be transparent,
// depending on what the layers paint on top of the base color.

template<class BASE, class... LAYERS> class Layers : public BASE {};

template<class BASE, class L1>
class Layers<BASE, L1> {
class Compose {
public:
LayerRunResult run(BladeBase* blade) {
LayerRunResult base_run_result = RunLayer(&base_, blade);
Expand All @@ -44,7 +42,15 @@ class Layers<BASE, L1> {
}
};

template<class BASE, class L1, class L2, class... LAYERS>
class Layers<BASE, L1, L2, LAYERS...> : public Layers<Layers<BASE, L1>, L2, LAYERS...> {};

template<class BASE, class ... LAYERS> struct LayerSelector {};
template<class BASE> struct LayerSelector<BASE> { typedef BASE type; };
template<class BASE, class L1> struct LayerSelector<AlphaL<BASE, Int<0>>, L1> { typedef L1 type; };
template<class BASE, class L1> struct LayerSelector<BASE, L1> { typedef Compose<BASE, L1> type; };
template<class BASE, class L1, class ... REST> struct LayerSelector<BASE, L1, REST...> {
typedef typename LayerSelector<Compose<BASE, L1>, REST...>::type type;
};

template<class BASE, class... LAYERS> using Layers = typename LayerSelector<BASE, LAYERS...>::type;

#endif
10 changes: 10 additions & 0 deletions styles/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,17 @@ void test_mix() {
CHECK_COLOR(mock_blade.colors[0], 0, 65535, 0, 1);
}

#define CHECK(X) do { \
if (!(X)) { fprintf(stderr, "%s failed, line %d\n", #X, __LINE__); exit(1); } \
} while(0)

void test_layers() {
CHECK( (is_same_type<Layers<AlphaL<Red, Int<0>>, Blue>, Blue>::value) );
CHECK( (!is_same_type<Layers<AlphaL<Red, Int<1>>, Blue>, Blue>::value) );
}

int main() {
test_layers();
test_mix();
test_gradient();
test_style6();
Expand Down

0 comments on commit 3098fc3

Please sign in to comment.