diff --git a/src/amdis/common/CMakeLists.txt b/src/amdis/common/CMakeLists.txt index 89f2d370f56993ad5dc3eb2d6d9541797220dd09..676d886932e962d4b39fec9335e87c0ce8c8e46e 100644 --- a/src/amdis/common/CMakeLists.txt +++ b/src/amdis/common/CMakeLists.txt @@ -10,6 +10,7 @@ install(FILES Concepts.hpp ConceptsBase.hpp ConcurrentCache.hpp + FakeContainer.hpp FieldMatVec.hpp FieldMatVec.inc.hpp Filesystem.hpp diff --git a/src/amdis/common/FakeContainer.hpp b/src/amdis/common/FakeContainer.hpp new file mode 100644 index 0000000000000000000000000000000000000000..52048b461c9d1c725711a26cfec898f27e6528bd --- /dev/null +++ b/src/amdis/common/FakeContainer.hpp @@ -0,0 +1,72 @@ +#pragma once + +#include <cstddef> + +namespace AMDiS +{ + struct FakeAssigner + { + template <class T> + FakeAssigner& operator=(T&&) { return *this; } + + template <class T> + FakeAssigner& operator+=(T&&) { return *this; } + + template <class T> + FakeAssigner& operator-=(T&&) { return *this; } + + template <class T> + FakeAssigner& operator*=(T&&) { return *this; } + + template <class T> + FakeAssigner& operator/=(T&&) { return *this; } + }; + + /// A container-like data-structure not storing anything and with empty + /// implementations in many container-interface functions. + /** + * This container that *does nothing* can be used as a dummy argument to + * functions expecting a container, in order to ommit specializations of + * these functions for not providing a container. + **/ + class FakeContainer + : public FakeAssigner + { + public: + using size_type = std::size_t; + + template <class... Args> + FakeContainer(Args&&...) {} + + template <class Arg> + void push_back(Arg&&) {} + + template <class... Args> + void emplace_back(Args&&...) {} + + void reserve(size_type) {} + void resize(size_type) {} + + /// This container is always empty + bool empty() const { return true; } + size_type size() const { return 0u; } + + /// Mutable *element* access does return the container itself + /// This allows to emulate nested containers + FakeContainer& operator[](size_type) { return *this; } + + /// Assignment operators from a fake assigner + using FakeAssigner::operator=; + using FakeAssigner::operator+=; + using FakeAssigner::operator-=; + using FakeAssigner::operator*=; + using FakeAssigner::operator/=; + + FakeAssigner front() { return {}; } + FakeAssigner back() { return {}; } + + friend inline FakeAssigner front(FakeContainer&) { return {}; } + friend inline FakeAssigner back(FakeContainer&) { return {}; } + }; + +} // end namespace AMDiS diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 82bc31b7a3f9b06f6c17f75c590d8e96bff22789..3eef3b941528456f55ae2f2273e23db5324d284b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,6 +24,9 @@ dune_add_test(SOURCES DiscreteFunctionTest.cpp dune_add_test(SOURCES ExpressionsTest.cpp LINK_LIBRARIES amdis) +dune_add_test(SOURCES FakeContainerTest.cpp + LINK_LIBRARIES amdis) + dune_add_test(SOURCES FieldMatVecTest.cpp LINK_LIBRARIES amdis) diff --git a/test/FakeContainerTest.cpp b/test/FakeContainerTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..758e158f71b49d4c807489e7612bada19b232e2c --- /dev/null +++ b/test/FakeContainerTest.cpp @@ -0,0 +1,37 @@ +#include <amdis/AMDiS.hpp> +#include <amdis/common/FakeContainer.hpp> + +#include "Tests.hpp" + +using namespace AMDiS; + +int main(int argc, char** argv) +{ + // Environment env(argc, argv); + + FakeContainer vec1; + FakeContainer vec2(vec1); + FakeContainer vec3(std::move(vec2)); + + FakeContainer vec4 = vec1; + FakeContainer vec5 = std::move(vec3); + + vec1.reserve(7); + vec1.resize(1); + vec4.resize(1); + + vec1[0] = 0.0; + vec4[1] = vec1[0]; + + vec1.push_back(42); + vec1.emplace_back(42); + + AMDIS_TEST(vec1.empty()); + AMDIS_TEST(vec1.size() == 0u); + + vec1.front() = 1; + front(vec4) = 2; + back(vec4) = vec1.back(); + + return 0; +}