From 1d2391e0b4dfac2513355cde7105bdd1d7463162 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 29 May 2020 12:49:48 +0200 Subject: [PATCH] ENH: add swallow assignment to nullObject - similar to the behaviour of std::ignore and consistent with the no input / no output nature of nullObject. Similarly accept a const reference for its Istream operator. - make most nullObject methods constexpr --- .../test/nullObject/Test-nullObject.C | 13 ++++++--- .../primitives/nullObject/nullObject.H | 28 +++++++++++++------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/applications/test/nullObject/Test-nullObject.C b/applications/test/nullObject/Test-nullObject.C index 5f0f171cea..5c86e4e500 100644 --- a/applications/test/nullObject/Test-nullObject.C +++ b/applications/test/nullObject/Test-nullObject.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2014 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -47,9 +47,8 @@ class SimpleClass { public: - //- Null constructor - SimpleClass() - {} + //- Default construct + SimpleClass() {} }; @@ -149,6 +148,12 @@ int main() printInfo(pointField::null()); } + // Test swallow assigment (like std::ignore) + // Looks pretty ugly though! + + NullObject::nullObject = "hello world"; + NullObject::nullObject = labelList({1, 2, 3}); + Info<< nl; return 0; diff --git a/src/OpenFOAM/primitives/nullObject/nullObject.H b/src/OpenFOAM/primitives/nullObject/nullObject.H index 78f428dbd6..09788dd96c 100644 --- a/src/OpenFOAM/primitives/nullObject/nullObject.H +++ b/src/OpenFOAM/primitives/nullObject/nullObject.H @@ -105,31 +105,41 @@ public: } //- Zero valued integer content - inline unsigned long value() const + inline constexpr unsigned long value() const { - return data_[0].val; + return 0; } //- No elements - inline bool empty() const + inline constexpr bool empty() const { return true; } //- Zero elements - inline label size() const + inline constexpr label size() const { return 0; } //- No-op method (for HashTable replacement) - inline const NullObject& toc() const + inline constexpr const NullObject& toc() const { return *this; } //- No-op method (for HashTable replacement) - inline const NullObject& sortedToc() const + inline constexpr const NullObject& sortedToc() const + { + return *this; + } + + + // Member Operators + + //- Swallow assignment (cf, std::ignore) + template + inline constexpr const NullObject& operator=(const T&) const { return *this; } @@ -146,13 +156,13 @@ extern const NullObject* nullObjectPtr; // IOstream Operators -//- Read from Istream consumes no content. -inline Istream& operator>>(Istream& is, NullObject&) noexcept +//- Read from Istream consumes no content +inline Istream& operator>>(Istream& is, const NullObject&) noexcept { return is; } -//- Write to Ostream emits no content. +//- Write to Ostream emits no content inline Ostream& operator<<(Ostream& os, const NullObject&) noexcept { return os;