From 75998bb25ac9f3061f3d8fddea27ffda64b18a16 Mon Sep 17 00:00:00 2001 From: CardinalPrefect Date: Thu, 24 Dec 2020 00:16:56 +0300 Subject: [PATCH] The solution of typelist task --- module-1/homework/TypeList/tests.cpp | 1 + module-1/homework/TypeList/typelist/append.h | 17 +++++++++++++- module-1/homework/TypeList/typelist/erase.h | 22 ++++++++++++++++++- .../homework/TypeList/typelist/eraseall.h | 17 +++++++++++++- module-1/homework/TypeList/typelist/indexof.h | 17 +++++++++++++- module-1/homework/TypeList/typelist/length.h | 15 +++++++++++-- .../homework/TypeList/typelist/noduplicates.h | 14 ++++++++++-- module-1/homework/TypeList/typelist/replace.h | 17 +++++++++++++- module-1/homework/TypeList/typelist/typeat.h | 17 +++++++++++++- 9 files changed, 127 insertions(+), 10 deletions(-) diff --git a/module-1/homework/TypeList/tests.cpp b/module-1/homework/TypeList/tests.cpp index cba5881b..0b4236e5 100644 --- a/module-1/homework/TypeList/tests.cpp +++ b/module-1/homework/TypeList/tests.cpp @@ -4,6 +4,7 @@ #include #include "typelist/append.h" +#include "typelist/erase.h" #include "typelist/eraseall.h" #include "typelist/indexof.h" #include "typelist/length.h" diff --git a/module-1/homework/TypeList/typelist/append.h b/module-1/homework/TypeList/typelist/append.h index ac395d4b..75b1ba37 100644 --- a/module-1/homework/TypeList/typelist/append.h +++ b/module-1/homework/TypeList/typelist/append.h @@ -3,4 +3,19 @@ #include "typelist.h" template -struct Append; \ No newline at end of file +struct Append; + +template +struct Append, NewType> { + using NewTypeList = TypeList::NewTypeList>; +}; + +template +struct Append { + using NewTypeList = TypeList; +}; + +template<> +struct Append { + using NewTypeList = NullType; +}; diff --git a/module-1/homework/TypeList/typelist/erase.h b/module-1/homework/TypeList/typelist/erase.h index 7f33b81c..698f2187 100644 --- a/module-1/homework/TypeList/typelist/erase.h +++ b/module-1/homework/TypeList/typelist/erase.h @@ -3,4 +3,24 @@ #include "typelist.h" template -struct Erase; \ No newline at end of file +struct Erase; + +template +struct Erase, TargetType> { + using NewTypeList = TypeList::NewTypeList>; +}; + +template +struct Erase, TargetType> { + using NewTypeList = Tail; +}; + +template +struct Erase, TargetType> { + using NewTypeList = NullType; +}; + +template +struct Erase { + using NewTypeList = NullType; +}; diff --git a/module-1/homework/TypeList/typelist/eraseall.h b/module-1/homework/TypeList/typelist/eraseall.h index e395227c..f9558b91 100644 --- a/module-1/homework/TypeList/typelist/eraseall.h +++ b/module-1/homework/TypeList/typelist/eraseall.h @@ -3,4 +3,19 @@ #include "typelist.h" template -struct EraseAll; \ No newline at end of file +struct EraseAll; + +template +struct EraseAll, TargetType> { + using NewTypeList = TypeList::NewTypeList>; +}; + +template +struct EraseAll, TargetType> { + using NewTypeList = typename EraseAll::NewTypeList; +}; + +template +struct EraseAll { + using NewTypeList = NullType; +}; diff --git a/module-1/homework/TypeList/typelist/indexof.h b/module-1/homework/TypeList/typelist/indexof.h index e1c34184..0afc2c45 100644 --- a/module-1/homework/TypeList/typelist/indexof.h +++ b/module-1/homework/TypeList/typelist/indexof.h @@ -3,4 +3,19 @@ #include "typelist.h" template -struct IndexOf; \ No newline at end of file +struct IndexOf; + +template +struct IndexOf, TargetType> { + static const int pos = IndexOf::pos == -1? -1 : IndexOf::pos + 1; +}; + +template +struct IndexOf, TargetType> { + static const int pos = 0; +}; + +template +struct IndexOf { + static const int pos = -1; +}; diff --git a/module-1/homework/TypeList/typelist/length.h b/module-1/homework/TypeList/typelist/length.h index 2f45c74b..1f69acee 100644 --- a/module-1/homework/TypeList/typelist/length.h +++ b/module-1/homework/TypeList/typelist/length.h @@ -1,6 +1,17 @@ #pragma once #include "typelist.h" +#include // for size_t -template -struct Length; \ No newline at end of file +template +struct Length; + +template +struct Length > { + static const std::size_t length = 1 + Length::length; +}; + +template<> +struct Length { + static const int length = 0; +}; diff --git a/module-1/homework/TypeList/typelist/noduplicates.h b/module-1/homework/TypeList/typelist/noduplicates.h index c0c696b2..4d5ac25b 100644 --- a/module-1/homework/TypeList/typelist/noduplicates.h +++ b/module-1/homework/TypeList/typelist/noduplicates.h @@ -1,7 +1,17 @@ #pragma once -#include "erase.h" +#include "eraseall.h" #include "typelist.h" template -struct NoDuplicates; \ No newline at end of file +struct NoDuplicates; + +template +struct NoDuplicates> { + using NewTypeList = TypeList::NewTypeList>::NewTypeList>; +}; + +template<> +struct NoDuplicates { + using NewTypeList = NullType; +}; diff --git a/module-1/homework/TypeList/typelist/replace.h b/module-1/homework/TypeList/typelist/replace.h index 392595b7..ee757271 100644 --- a/module-1/homework/TypeList/typelist/replace.h +++ b/module-1/homework/TypeList/typelist/replace.h @@ -3,4 +3,19 @@ #include "typelist.h" template -struct Replace; \ No newline at end of file +struct Replace; + +template +struct Replace, OldType, NewType> { + using NewTypeList = TypeList::NewTypeList>; +}; + +template +struct Replace, OldType, NewType> { + using NewTypeList = TypeList::NewTypeList>; +}; + +template +struct Replace { + using NewTypeList = NullType; +}; diff --git a/module-1/homework/TypeList/typelist/typeat.h b/module-1/homework/TypeList/typelist/typeat.h index 805f391c..a974e053 100644 --- a/module-1/homework/TypeList/typelist/typeat.h +++ b/module-1/homework/TypeList/typelist/typeat.h @@ -3,4 +3,19 @@ #include "typelist.h" template -struct TypeAt; \ No newline at end of file +struct TypeAt; + +template +struct TypeAt, index> { + using TargetType = typename TypeAt::TargetType; +}; + +template +struct TypeAt, 0> { + using TargetType = Head; +}; + +template +struct TypeAt { + using TargetType = NullType; +};