diff --git a/exercise/01/animal.cpp b/exercise/01/animal.cpp new file mode 100644 index 0000000..4846058 --- /dev/null +++ b/exercise/01/animal.cpp @@ -0,0 +1,27 @@ +#include "animal.h" +#include + +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(); +} + +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..c9d7103 --- /dev/null +++ b/exercise/01/animal.h @@ -0,0 +1,40 @@ +#pragma once +#include "animal_lib.h" +#include +#include +#include + +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; +const animal_id tarantula_id = 3; + +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; + +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/animal_lib.h b/exercise/01/animal_lib.h new file mode 100644 index 0000000..620f79b --- /dev/null +++ b/exercise/01/animal_lib.h @@ -0,0 +1,50 @@ +#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 +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 leg_count::value; + } +}; + +using animal_id = std::size_t; +template +struct animal_name +{ + static const std::wstring value; +}; +template const std::wstring animal_name::value = fail_name(); + +template +struct named_animal final : legged_animal +{ + 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 0c14cbf..e0aa354 100644 --- a/exercise/01/animals.vcxproj +++ b/exercise/01/animals.vcxproj @@ -1,95 +1,103 @@ - - - - - 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 + + + false + + + + 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 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