From c07388674276e6e9dcd7a60327699add5b0d24c3 Mon Sep 17 00:00:00 2001 From: Gordan Kucinar Date: Thu, 15 Nov 2018 00:18:40 +0100 Subject: [PATCH 1/9] Done --- exercise/01/animal.cpp | 24 +++++ exercise/01/animal.h | 73 ++++++++++++++ exercise/01/animals.vcxproj | 195 +++++++++++++++++++----------------- 3 files changed, 198 insertions(+), 94 deletions(-) create mode 100644 exercise/01/animal.cpp create mode 100644 exercise/01/animal.h diff --git a/exercise/01/animal.cpp b/exercise/01/animal.cpp new file mode 100644 index 0000000..1bb9557 --- /dev/null +++ b/exercise/01/animal.cpp @@ -0,0 +1,24 @@ +#include "animal.h" +#include + +std::wstring leg_counter::add_animal(std::unique_ptr&& animal) +{ + animals.emplace_back(std::move(animal)); + return animals.back()->species(); +} + +std::size_t leg_counter::legs() const noexcept +{ + return std::accumulate(std::cbegin(animals), std::cend(animals), 0u, [](auto accu, auto const & animal) { return accu + animal->legs(); }); +} + +std::unique_ptr animal_factory(animal_id animal) +{ + switch (animal) + { + case sparrow_id: return std::make_unique(); + case tarantula_id: return std::make_unique(); + case cockroach_id: return std::make_unique (); + default: return nullptr; + } +} diff --git a/exercise/01/animal.h b/exercise/01/animal.h new file mode 100644 index 0000000..d448988 --- /dev/null +++ b/exercise/01/animal.h @@ -0,0 +1,73 @@ +#pragma once +#include +#include +#include +#include + +struct animal +{ + virtual std::size_t legs() const noexcept = 0; + virtual std::wstring const & species() const noexcept = 0; + virtual ~animal() = default; +}; + +using animal_id = std::size_t; +template +struct animal_name +{ + static const std::wstring name; +}; + +template +static std::wstring fail_name() +{ + static_assert(false, "specialize name member"); +} +template const std::wstring animal_name::name = fail_name(); + +template +struct legged_animal : animal +{ + std::size_t legs() const noexcept override final + { + return LEGS; + } +}; + +template +struct named_animal final : ANIMAL_TYPE +{ + std::wstring const & species() const noexcept override final + { + return animal_name::name; + } +}; + +//--------------------------------------------------------------------------------------------------------- + +using bird = legged_animal<2u>; +using spider = legged_animal<8u>; +using insect = legged_animal<6u>; + +const animal_id cockroach_id = 1; +const animal_id sparrow_id = 2; +const animal_id tarantula_id = 3; + +template<> const std::wstring animal_name::name = L"sparrow"; +template<> const std::wstring animal_name::name = L"cockroach"; +template<> const std::wstring animal_name::name = L"tarantula"; + +using sparrow = named_animal; +using tarantula = named_animal; +using cockroach = named_animal; + +std::unique_ptr animal_factory(animal_id animal); + +struct leg_counter final +{ + std::wstring add_animal(std::unique_ptr&& animal); + std::size_t legs() const noexcept; + +private: + std::vector> animals; +}; diff --git a/exercise/01/animals.vcxproj b/exercise/01/animals.vcxproj index 0c14cbf..fe5dc0c 100644 --- a/exercise/01/animals.vcxproj +++ b/exercise/01/animals.vcxproj @@ -1,95 +1,102 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - - {BB15329E-10D8-4740-94BC-64C50BA4EE12} - Win32Proj - array - 10.0.17134.0 - - - - DynamicLibrary - true - v141 - Unicode - false - - - DynamicLibrary - false - v141 - true - Unicode - false - - - - - - - - - - - - - true - - - true - - - - NotUsing - Level3 - Disabled - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - WIN32;_DEBUG;%(PreprocessorDefinitions) - true - - - Windows - true - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - Level3 - NotUsing - MaxSpeed - true - true - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - WIN32;NDEBUG;%(PreprocessorDefinitions) - true - - - Windows - true - true - true - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - - - - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + + {BB15329E-10D8-4740-94BC-64C50BA4EE12} + Win32Proj + array + 10.0.17134.0 + + + + DynamicLibrary + true + v141 + Unicode + false + + + DynamicLibrary + false + v141 + true + Unicode + false + + + + + + + + + + + + + true + + + true + + + + NotUsing + Level4 + Disabled + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;%(PreprocessorDefinitions) + true + stdcpp17 + true + + + Windows + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Level4 + NotUsing + MaxSpeed + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;%(PreprocessorDefinitions) + true + stdcpp17 + true + + + Windows + true + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + + + + + Level3 + Level3 + + + + + \ No newline at end of file From a6522baf468cd57b5b302a382e2cc14fa7726d31 Mon Sep 17 00:00:00 2001 From: Gordan Kucinar Date: Thu, 15 Nov 2018 00:24:01 +0100 Subject: [PATCH 2/9] Gordan Kucinar --- exercise/01/animal.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/exercise/01/animal.cpp b/exercise/01/animal.cpp index 1bb9557..4846058 100644 --- a/exercise/01/animal.cpp +++ b/exercise/01/animal.cpp @@ -3,6 +3,9 @@ std::wstring leg_counter::add_animal(std::unique_ptr&& animal) { + if (!animal) + return std::wstring(); + animals.emplace_back(std::move(animal)); return animals.back()->species(); } From 0cca019b281d9e142e14989cd06dacb093afe20a Mon Sep 17 00:00:00 2001 From: Gordan Kucinar Date: Thu, 15 Nov 2018 00:50:29 +0100 Subject: [PATCH 3/9] Kucinar Gordan --- exercise/01/animal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercise/01/animal.h b/exercise/01/animal.h index d448988..d67b6ad 100644 --- a/exercise/01/animal.h +++ b/exercise/01/animal.h @@ -37,7 +37,7 @@ struct legged_animal : animal template struct named_animal final : ANIMAL_TYPE { - std::wstring const & species() const noexcept override final + std::wstring const & species() const noexcept override { return animal_name::name; } From d1a1f3be5d399025ced4b4aa11c6931e6e0cf579 Mon Sep 17 00:00:00 2001 From: Gordan Kucinar Date: Thu, 15 Nov 2018 01:13:36 +0100 Subject: [PATCH 4/9] Kucinar Gordan --- exercise/01/animal.h | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/exercise/01/animal.h b/exercise/01/animal.h index d67b6ad..3333aab 100644 --- a/exercise/01/animal.h +++ b/exercise/01/animal.h @@ -11,11 +11,20 @@ struct animal virtual ~animal() = default; }; +template +struct legged_animal : animal +{ + std::size_t legs() const noexcept override final + { + return LEGS; + } +}; + using animal_id = std::size_t; template struct animal_name { - static const std::wstring name; + static const std::wstring value; }; template @@ -23,23 +32,14 @@ static std::wstring fail_name() { static_assert(false, "specialize name member"); } -template const std::wstring animal_name::name = fail_name(); - -template -struct legged_animal : animal -{ - std::size_t legs() const noexcept override final - { - return LEGS; - } -}; +template const std::wstring animal_name::value = fail_name(); template struct named_animal final : ANIMAL_TYPE { std::wstring const & species() const noexcept override { - return animal_name::name; + return animal_name::value; } }; @@ -53,9 +53,9 @@ const animal_id cockroach_id = 1; const animal_id sparrow_id = 2; const animal_id tarantula_id = 3; -template<> const std::wstring animal_name::name = L"sparrow"; -template<> const std::wstring animal_name::name = L"cockroach"; -template<> const std::wstring animal_name::name = L"tarantula"; +template<> const std::wstring animal_name::value = L"sparrow"; +template<> const std::wstring animal_name::value = L"cockroach"; +template<> const std::wstring animal_name::value = L"tarantula"; using sparrow = named_animal; using tarantula = named_animal; From c68ecc89fc81471acb9fe386001e00acd9722708 Mon Sep 17 00:00:00 2001 From: Gordan Kucinar Date: Thu, 15 Nov 2018 01:21:59 +0100 Subject: [PATCH 5/9] Kucinar Gordan --- exercise/01/animal.h | 43 +------------------------ exercise/01/animal_lib.h | 42 +++++++++++++++++++++++++ exercise/01/animals.vcxproj | 3 +- exercise/01/animals.vcxproj.filters | 49 +++++++++++++++-------------- 4 files changed, 71 insertions(+), 66 deletions(-) create mode 100644 exercise/01/animal_lib.h diff --git a/exercise/01/animal.h b/exercise/01/animal.h index 3333aab..44162ec 100644 --- a/exercise/01/animal.h +++ b/exercise/01/animal.h @@ -1,50 +1,9 @@ #pragma once -#include +#include "animal_lib.h" #include #include #include -struct animal -{ - virtual std::size_t legs() const noexcept = 0; - virtual std::wstring const & species() const noexcept = 0; - virtual ~animal() = default; -}; - -template -struct legged_animal : animal -{ - std::size_t legs() const noexcept override final - { - return LEGS; - } -}; - -using animal_id = std::size_t; -template -struct animal_name -{ - static const std::wstring value; -}; - -template -static std::wstring fail_name() -{ - static_assert(false, "specialize name member"); -} -template const std::wstring animal_name::value = fail_name(); - -template -struct named_animal final : ANIMAL_TYPE -{ - std::wstring const & species() const noexcept override - { - return animal_name::value; - } -}; - -//--------------------------------------------------------------------------------------------------------- - using bird = legged_animal<2u>; using spider = legged_animal<8u>; using insect = legged_animal<6u>; diff --git a/exercise/01/animal_lib.h b/exercise/01/animal_lib.h new file mode 100644 index 0000000..ce52849 --- /dev/null +++ b/exercise/01/animal_lib.h @@ -0,0 +1,42 @@ +#pragma once +#include +#include + +struct animal +{ + virtual std::size_t legs() const noexcept = 0; + virtual std::wstring const & species() const noexcept = 0; + virtual ~animal() = default; +}; + +template +struct legged_animal : animal +{ + std::size_t legs() const noexcept override final + { + return LEGS; + } +}; + +using animal_id = std::size_t; +template +struct animal_name +{ + static const std::wstring value; +}; + +template +static std::wstring fail_name() +{ + static_assert(false, "specialize name member"); +} +template const std::wstring animal_name::value = fail_name(); + +template +struct named_animal final : ANIMAL_TYPE +{ + std::wstring const & species() const noexcept override + { + return animal_name::value; + } +}; \ No newline at end of file diff --git a/exercise/01/animals.vcxproj b/exercise/01/animals.vcxproj index fe5dc0c..e0aa354 100644 --- a/exercise/01/animals.vcxproj +++ b/exercise/01/animals.vcxproj @@ -46,7 +46,7 @@ true - true + false @@ -88,6 +88,7 @@ + diff --git a/exercise/01/animals.vcxproj.filters b/exercise/01/animals.vcxproj.filters index ad4d9ab..65868a5 100644 --- a/exercise/01/animals.vcxproj.filters +++ b/exercise/01/animals.vcxproj.filters @@ -1,24 +1,27 @@ - - - - - {f2ada0ee-91e7-4bbd-b33d-837567340243} - - - {3cc0fd48-6585-4969-94bd-406daa5f786b} - - - - - test - - - src - - - - - src - - + + + + + {f2ada0ee-91e7-4bbd-b33d-837567340243} + + + {3cc0fd48-6585-4969-94bd-406daa5f786b} + + + + + test + + + src + + + + + src + + + src + + \ No newline at end of file From cc59eaafdcbba460c8b349135d66f1bf125e104a Mon Sep 17 00:00:00 2001 From: Gordan Kucinar Date: Thu, 15 Nov 2018 02:07:53 +0100 Subject: [PATCH 6/9] Kucinar Gordan --- exercise/01/animal.h | 20 ++++++++++++++------ exercise/01/animal_lib.h | 30 +++++++++++++++++++----------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/exercise/01/animal.h b/exercise/01/animal.h index 44162ec..c9d7103 100644 --- a/exercise/01/animal.h +++ b/exercise/01/animal.h @@ -4,9 +4,17 @@ #include #include -using bird = legged_animal<2u>; -using spider = legged_animal<8u>; -using insect = legged_animal<6u>; +const animal_family_id bird_id = 1; +const animal_family_id spider_id = 2; +const animal_family_id insect_id = 3; + +template<> const std::size_t leg_count::value = 2u; +template<> const std::size_t leg_count::value = 8u; +template<> const std::size_t leg_count::value = 6u; + +using bird = legged_animal; +using spider = legged_animal; +using insect = legged_animal; const animal_id cockroach_id = 1; const animal_id sparrow_id = 2; @@ -16,9 +24,9 @@ template<> const std::wstring animal_name::value = L"sparrow"; template<> const std::wstring animal_name::value = L"cockroach"; template<> const std::wstring animal_name::value = L"tarantula"; -using sparrow = named_animal; -using tarantula = named_animal; -using cockroach = named_animal; +using sparrow = named_animal; +using tarantula = named_animal; +using cockroach = named_animal; std::unique_ptr animal_factory(animal_id animal); diff --git a/exercise/01/animal_lib.h b/exercise/01/animal_lib.h index ce52849..9cc9c8b 100644 --- a/exercise/01/animal_lib.h +++ b/exercise/01/animal_lib.h @@ -9,12 +9,26 @@ struct animal virtual ~animal() = default; }; -template +template +static T fail_name() +{ + static_assert(false, "specialize value member"); +} + +using animal_family_id = std::size_t; +template +struct leg_count +{ + static const std::size_t value; +}; +template const std::size_t leg_count::value = fail_name(); + +template struct legged_animal : animal { std::size_t legs() const noexcept override final { - return LEGS; + return leg_count::value; } }; @@ -24,16 +38,10 @@ struct animal_name { static const std::wstring value; }; +template const std::wstring animal_name::value = fail_name(); -template -static std::wstring fail_name() -{ - static_assert(false, "specialize name member"); -} -template const std::wstring animal_name::value = fail_name(); - -template -struct named_animal final : ANIMAL_TYPE +template +struct named_animal final : legged_animal { std::wstring const & species() const noexcept override { From c6f7ef6632996c03ac9c80b8ad5811824c604f8c Mon Sep 17 00:00:00 2001 From: Gordan Kucinar Date: Thu, 15 Nov 2018 17:20:20 +0100 Subject: [PATCH 7/9] Kucinar Gordan --- exercise/01/animal_lib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercise/01/animal_lib.h b/exercise/01/animal_lib.h index 9cc9c8b..620f79b 100644 --- a/exercise/01/animal_lib.h +++ b/exercise/01/animal_lib.h @@ -21,7 +21,7 @@ struct leg_count { static const std::size_t value; }; -template const std::size_t leg_count::value = fail_name(); +template const std::size_t leg_count::value = fail_name(); template struct legged_animal : animal From acc675307ee7507f49e22a8ac6641e81ff011ee7 Mon Sep 17 00:00:00 2001 From: Gordan Kucinar Date: Fri, 16 Nov 2018 09:05:11 +0100 Subject: [PATCH 8/9] Kucinar Gordan --- exercise/01/test.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/exercise/01/test.cpp b/exercise/01/test.cpp index f7f2c1f..79fc068 100644 --- a/exercise/01/test.cpp +++ b/exercise/01/test.cpp @@ -8,6 +8,10 @@ TEST_CLASS(test_animal_hierarchy) public: TEST_METHOD(concrete_insect) { + using some = legged_animal<15>; + using something_else = named_animal<11, 15>; + something_else se; + cockroach c; insect& i = c; animal& a = c; From b255c6ea8b1f88198f5de76fa02988f67b4074be Mon Sep 17 00:00:00 2001 From: Gordan Kucinar Date: Fri, 16 Nov 2018 09:28:51 +0100 Subject: [PATCH 9/9] Kucinar Gordan --- exercise/01/test.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/exercise/01/test.cpp b/exercise/01/test.cpp index 79fc068..f7f2c1f 100644 --- a/exercise/01/test.cpp +++ b/exercise/01/test.cpp @@ -8,10 +8,6 @@ TEST_CLASS(test_animal_hierarchy) public: TEST_METHOD(concrete_insect) { - using some = legged_animal<15>; - using something_else = named_animal<11, 15>; - something_else se; - cockroach c; insect& i = c; animal& a = c;