diff --git a/applications/test/List/Test-List.C b/applications/test/List/Test-List.C index 5ec2bab8438aabcfab19c84eac83ee54f567d329..5d3d5164deeedb222867372ed494e99a4cef7a6f 100644 --- a/applications/test/List/Test-List.C +++ b/applications/test/List/Test-List.C @@ -46,6 +46,7 @@ See also #include "HashOps.H" #include "ListOps.H" #include "SubList.H" +#include "ListPolicy.H" #include <list> #include <numeric> @@ -64,9 +65,22 @@ public: using List<string>::List; }; + + +namespace Detail +{ +namespace ListPolicy +{ + +// Override on a per-type basis +template<> struct short_length<short> : std::integral_constant<short,20> {}; + +} // End namespace ListPolicy +} // End namespace Detail } // End namespace Foam + using namespace Foam; template<class T, class ListType> @@ -92,6 +106,20 @@ void printMyString(const UList<string>& lst) } +template<class T> +Ostream& printListOutputType(const char* what) +{ + Info<< what + << " (contiguous=" + << contiguous<T>() << " no_linebreak=" + << Detail::ListPolicy::no_linebreak<T>::value + << " short_length=" + << Detail::ListPolicy::short_length<T>::value << ')'; + + return Info; +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: @@ -251,23 +279,54 @@ int main(int argc, char *argv[]) Info<<"scalar identity:" << flatOutput(slist) << endl; - Info<< "labels (contiguous=" << contiguous<label>() << ")" << nl; + printListOutputType<label>("labels") << nl; Info<< "normal: " << longLabelList << nl; Info<< "flatOutput: " << flatOutput(longLabelList) << nl; // Info<< "flatOutput(14): " << flatOutput(longLabelList, 14) << nl; + auto shrtList = ListOps::create<short> + ( + longLabelList, + [](const label& val){ return val; } + ); + + printListOutputType<short>("short") << nl; + Info<< "normal: " << shrtList << nl; + + stringList longStringList(12); forAll(longStringList, i) { longStringList[i].resize(3, 'a' + i); } - Info<< "string (contiguous=" << contiguous<string>() << ")" << nl; + printListOutputType<string>("string") << nl; Info<< "normal: " << longStringList << nl; Info<< "flatOutput: " << flatOutput(longStringList) << nl; - // contiguous longStringList[i].resize(3, 'a' + i); + + auto wList = ListOps::create<word> + ( + longStringList, + [](const std::string& val){ return val; } + ); + + printListOutputType<word>("word") << nl; + + Info<< "normal: " << wList << nl; + + // Shorten + longStringList.resize(8); + wList.resize(8); + + Info<< "Test shorter lists" << nl; + + printListOutputType<string>("string") << nl; + Info<< "normal: " << longStringList << nl; + + printListOutputType<word>("word") << nl; + Info<< "normal: " << wList << nl; } // test SubList and labelRange diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedList.H b/src/OpenFOAM/containers/Bits/PackedList/PackedList.H index c30710c02fd86b90b394e189ce33fdeaf48e9aec..a77213b9e8b5e6a3d2855f17f283d1b40b590291 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedList.H +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedList.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2017-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -400,10 +400,9 @@ public: //- Clear list and read from stream Istream& read(Istream& is); - //- Write the List, with line-breaks in ASCII if the list length - //- exceeds shortListLen. + //- Write List, with line-breaks in ASCII when length exceeds shortLen. // Using '0' suppresses line-breaks entirely. - Ostream& writeList(Ostream& os, const label shortListLen=0) const; + Ostream& writeList(Ostream& os, const label shortLen=0) const; //- Write as a dictionary entry with keyword void writeEntry(const word& keyword, Ostream& os) const; @@ -492,15 +491,20 @@ public: Istream& is, PackedList<Width>& list ); - - friend Ostream& operator<< <Width> - ( - Ostream& os, - const PackedList<Width>& list - ); }; +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // + +//- Write List to Ostream, as per UList::writeList() with default length. +// The default short-length is given by Detail::ListPolicy::short_length +template<unsigned Width> +Ostream& operator<<(Ostream& os, const PackedList<Width>& list) +{ + return list.writeList(os, Detail::ListPolicy::short_length<void>::value); +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C b/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C index 84b529ae2983f4e005ad825c3501883ebed9503c..a6196f334a31052c5028d16fb21edc5d17e2cfc1 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -206,7 +206,7 @@ template<unsigned Width> Foam::Ostream& Foam::PackedList<Width>::writeList ( Ostream& os, - const label shortListLen + const label shortLen ) const { const PackedList<Width>& list = *this; @@ -220,7 +220,7 @@ Foam::Ostream& Foam::PackedList<Width>::writeList // Two or more entries, and all entries have identical values. os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK; } - else if (!shortListLen || len <= shortListLen) + else if (!shortLen || len <= shortLen) { // Shorter list, or line-breaks suppressed os << len << token::BEGIN_LIST; @@ -284,13 +284,6 @@ Foam::Istream& Foam::operator>>(Istream& is, PackedList<Width>& list) } -template<unsigned Width> -Foam::Ostream& Foam::operator<<(Ostream& os, const PackedList<Width>& list) -{ - return list.writeList(os, 10); -} - - template<unsigned Width> Foam::Ostream& Foam::operator<< ( diff --git a/src/OpenFOAM/containers/Bits/bitSet/bitSet.H b/src/OpenFOAM/containers/Bits/bitSet/bitSet.H index 961450c31df5e34d6aada8ffc68388efe1e2f79b..69d78444ee1cfa00fd945b8e5df01dce97b2a01c 100644 --- a/src/OpenFOAM/containers/Bits/bitSet/bitSet.H +++ b/src/OpenFOAM/containers/Bits/bitSet/bitSet.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -541,10 +541,9 @@ public: // IO - //- Write the bitSet, with line-breaks in ASCII if the size - //- exceeds shortListLen. + //- Write bitSet, with line-breaks (ASCII) when length exceeds shortLen. // Using '0' suppresses line-breaks entirely. - Ostream& writeList(Ostream& os, const label shortListLen=0) const; + Ostream& writeList(Ostream& os, const label shortLen=0) const; //- Write as a dictionary entry with keyword void writeEntry(const word& keyword, Ostream& os) const; @@ -561,7 +560,7 @@ public: // Housekeeping - //- \deprecated(2018-04) compatibility name for PackedBoolList + //- Deprecated(2018-04) compatibility name for PackedBoolList // \deprecated(2018-04) - use toc() method inline labelList used() const { return toc(); } @@ -570,9 +569,13 @@ public: // Global Operators -Ostream& operator<<(Ostream& os, const InfoProxy<bitSet>& info); +//- Write bitset to Ostream, as per bitSet::writeList() with default length +//- of 40 items. Ostream& operator<<(Ostream& os, const bitSet& bitset); +//- Output bitset information +Ostream& operator<<(Ostream& os, const InfoProxy<bitSet>& info); + //- Bitwise-AND of two bitsets. // See bitSet::operator&= for more details. diff --git a/src/OpenFOAM/containers/Bits/bitSet/bitSetIO.C b/src/OpenFOAM/containers/Bits/bitSet/bitSetIO.C index b8cee2c871476cfd00792018e479ee6af64f0862..472700dbf3caa701f29120d3a501c6cf60ea2a27 100644 --- a/src/OpenFOAM/containers/Bits/bitSet/bitSetIO.C +++ b/src/OpenFOAM/containers/Bits/bitSet/bitSetIO.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -39,7 +39,7 @@ void Foam::bitSet::writeEntry(Ostream& os) const Foam::Ostream& Foam::bitSet::writeList ( Ostream& os, - const label shortListLen + const label shortLen ) const { const bitSet& list = *this; @@ -53,7 +53,7 @@ Foam::Ostream& Foam::bitSet::writeList // Two or more entries, and all entries have identical values. os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK; } - else if (!shortListLen || len <= shortListLen) + else if (!shortLen || len <= shortLen) { // Shorter list, or line-breaks suppressed os << len << token::BEGIN_LIST; @@ -111,7 +111,7 @@ void Foam::bitSet::writeEntry Foam::Ostream& Foam::operator<<(Ostream& os, const bitSet& bitset) { - return bitset.writeList(os, 10); + return bitset.writeList(os, 40); } diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C index 178d2871d9a2fb0f556dac8408e3b58bc705aa64..663a0630b519507cc89456552e4260e0428acb79 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -362,7 +362,7 @@ Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs) template<class Key, class Hash> Foam::Ostream& Foam::operator<<(Ostream& os, const HashSet<Key, Hash>& tbl) { - return tbl.writeList(os, 10); // 10=consistent with UList + return tbl.writeKeys(os, Detail::ListPolicy::short_length<Key>::value); } diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H index cbc166175ce9465694212c658d870b83a0eafa79..83e26974be576e13368db7dc2f39d835591b9769 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H @@ -290,12 +290,12 @@ public: // Writing - //- Write the unordered keys as a list, with line-breaks if list length - //- exceeds shortListLen. + //- Write unordered keys (list), with line-breaks + //- when length exceeds shortLen. // Using '0' suppresses line-breaks entirely. - Ostream& writeList(Ostream& os, const label shortListLen=0) const + Ostream& writeList(Ostream& os, const label shortLen=0) const { - return this->writeKeys(os, shortListLen); + return this->writeKeys(os, shortLen); } diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H index 8046f4d6a33a30f6d2d901c68865f1d345a5a99c..d7bdadc46f5b4432913b66fb5f3177db365492b5 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H @@ -870,10 +870,10 @@ public: //- Print information Ostream& printInfo(Ostream& os) const; - //- Write the unordered keys as a list, with line-breaks if list length - //- exceeds shortListLen. + //- Write unordered keys (list), with line-breaks + //- when length exceeds shortLen. // Using '0' suppresses line-breaks entirely. - Ostream& writeKeys(Ostream& os, const label shortListLen=0) const; + Ostream& writeKeys(Ostream& os, const label shortLen=0) const; // IOstream Operator diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C index 108b1f153c4e395da16f5b1c04d45a65997d40f4..6fe637eec8343458b7076f7ca544c62f9c6a1db5 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C @@ -115,7 +115,7 @@ template<class T, class Key, class Hash> Foam::Ostream& Foam::HashTable<T, Key, Hash>::writeKeys ( Ostream& os, - const label shortListLen + const label shortLen ) const { // Similar to UList::writeList version except the following: @@ -124,7 +124,11 @@ Foam::Ostream& Foam::HashTable<T, Key, Hash>::writeKeys label i = this->size(); - if (i <= 1 || !shortListLen || (i <= shortListLen)) + if + ( + (i <= 1 || !shortLen) + || (i <= shortLen) + ) { // Write size and start delimiter os << i << token::BEGIN_LIST; diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H index d964202b8261f32eb7cb5271846647411412917a..0872b6eaf8e55a4a94da03b05eb9563d5a388f17 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H @@ -294,10 +294,9 @@ public: // IOstream operators - //- Write LList with line-breaks when its length exceeds - //- shortListLen. + //- Write LList with line-breaks when length exceeds shortLen. // Using '0' suppresses line-breaks entirely. - Ostream& writeList(Ostream& os, const label shortListLen=0) const; + Ostream& writeList(Ostream& os, const label shortLen=0) const; //- Read list from Istream friend Istream& operator>> <LListBase, T> @@ -307,7 +306,7 @@ public: ); //- Write LList to Ostream with line breaks, - //- as per writeList() with shortListLen=-1 + //- as per writeList with shortLen=-1 friend Ostream& operator<< <LListBase, T> ( Ostream& os, diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C index 93ba63f6386e6ec9fcf5797680183fb4d7dd7ec4..3bebd99e3d61d5c6f75240836f3832ce2fa5aaaa 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C @@ -130,15 +130,15 @@ template<class LListBase, class T> Foam::Ostream& Foam::LList<LListBase, T>::writeList ( Ostream& os, - const label shortListLen + const label shortLen ) const { const label len = this->size(); if ( - len <= 1 || !shortListLen - || (len <= shortListLen) + (len <= 1 || !shortLen) + || (len <= shortLen) ) { // Size and start delimiter @@ -179,7 +179,7 @@ Foam::Ostream& Foam::LList<LListBase, T>::writeList template<class LListBase, class T> Foam::Ostream& Foam::operator<<(Ostream& os, const LList<LListBase, T>& lst) { - return lst.writeList(os, -1); // always with line breaks + return lst.writeList(os, -1); // Always with line breaks } diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H index b2c27aaf0b9639c5ac635668818100f64ce41002..92e315d789f00e7d50a47701f9eaf43e51139581 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H @@ -178,13 +178,12 @@ public: // IOstream operators - //- Write UILList with line-breaks when its length exceeds - //- shortListLen. + //- Write UILList with line-breaks when length exceeds shortLen. // Using '0' suppresses line-breaks entirely. - Ostream& writeList(Ostream& os, const label shortListLen=0) const; + Ostream& writeList(Ostream& os, const label shortLen=0) const; //- Write UILList to Ostream with line breaks, - //- as per writeList() with shortListLen=-1 + //- as per writeList() with shortLen=-1 friend Ostream& operator<< <LListBase, T> ( Ostream& os, diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILListIO.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILListIO.C index 843f75e39b8ee5992c58f3256e5f1cb7477913b4..65b811c21a16d77bacdff02524ba7de066d8117e 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILListIO.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILListIO.C @@ -33,15 +33,15 @@ template<class LListBase, class T> Foam::Ostream& Foam::UILList<LListBase, T>::writeList ( Ostream& os, - const label shortListLen + const label shortLen ) const { const label len = this->size(); if ( - len <= 1 || !shortListLen - || (len <= shortListLen) + (len <= 1 || !shortLen) + || (len <= shortLen) ) { // Size and start delimiter diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H index 49b9f561f2ee19167a971eb53ace2449053a8772..497e03c13a8f11fc4bf7d4f9038d9baed4d0f925 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H @@ -46,6 +46,7 @@ SourceFiles #include "Swap.H" #include "HashFwd.H" #include "SLListFwd.H" +#include "ListPolicy.H" #include <initializer_list> #include <iterator> @@ -400,8 +401,7 @@ public: //- Write the list as a dictionary entry with keyword void writeEntry(const word& keyword, Ostream& os) const; - //- Write the list, with line-breaks in ASCII if the length - //- exceeds shortLen. + //- Write List, with line-breaks in ASCII when length exceeds shortLen. // Using '0' suppresses line-breaks entirely. Ostream& writeList(Ostream& os, const label shortLen=0) const; @@ -480,11 +480,12 @@ struct Hash<FixedList<T, N>> // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // -//- Write to Ostream, as per FixedList::writeList() with shortLen=10 +//- Write List to Ostream, as per FixedList::writeList() with default length. +// The default short-length is given by Detail::ListPolicy::short_length template<class T, unsigned N> Ostream& operator<<(Ostream& os, const FixedList<T, N>& list) { - return list.writeList(os, 10); + return list.writeList(os, Detail::ListPolicy::short_length<T>::value); } diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C index 78ac42bff1aa109ce85aeae1b1c5d1f6808a4d4a..56512fe4650460bb7f01c3a016db58f7c3853386 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C @@ -62,7 +62,7 @@ template<class T, unsigned N> Foam::Ostream& Foam::FixedList<T, N>::writeList ( Ostream& os, - const label shortListLen + const label shortLen ) const { const FixedList<T, N>& list = *this; @@ -77,8 +77,16 @@ Foam::Ostream& Foam::FixedList<T, N>::writeList { if ( - N <= 1 || !shortListLen - || (N <= unsigned(shortListLen) && contiguous<T>()) + (N <= 1 || !shortLen) + || + ( + (N <= unsigned(shortLen)) + && + ( + Detail::ListPolicy::no_linebreak<T>::value + || contiguous<T>() + ) + ) ) { // Start delimiter diff --git a/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectList.H b/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectList.H index 7c3191f936a64d3c62cc80a7f2e946a485ced14d..3a2f583af2381db50112e4f81b1568e3797cc78f 100644 --- a/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectList.H +++ b/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectList.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2017-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -50,7 +50,6 @@ namespace Foam // Forward declarations template<class T> class UIndirectList; -template<class T> Ostream& operator<<(Ostream&, const UIndirectList<T>&); // Commonly required list types typedef UIndirectList<bool> boolUIndList; @@ -320,21 +319,21 @@ public: // Writing - //- Write the list, with line-breaks in ASCII if its length - //- exceeds shortListLen. + //- Write List, with line-breaks in ASCII when length exceeds shortLen. // Using '0' suppresses line-breaks entirely. - Ostream& writeList(Ostream& os, const label shortListLen=0) const; + Ostream& writeList(Ostream& os, const label shortLen=0) const; +}; - // Ostream operator +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // - //- Write list to Ostream, as per writeList() with shortListLen=10 - friend Ostream& operator<< <T> - ( - Ostream& os, - const UIndirectList<T>& list - ); -}; +//- Write List to Ostream, as per UList::writeList() with default length. +// The default short-length is given by Detail::ListPolicy::short_length +template<class T> +Ostream& operator<<(Ostream& os, const UIndirectList<T>& list) +{ + return list.writeList(os, Detail::ListPolicy::short_length<T>::value); +} // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectListIO.C b/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectListIO.C index 863288c302fa25cf87d54e6331078f2230aadef6..874c00c7636fce9d6856d787fd6230553a5658ec 100644 --- a/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectListIO.C +++ b/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectListIO.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -34,7 +34,7 @@ template<class T> Foam::Ostream& Foam::UIndirectList<T>::writeList ( Ostream& os, - const label shortListLen + const label shortLen ) const { const UIndirectList<T>& list = *this; @@ -51,8 +51,16 @@ Foam::Ostream& Foam::UIndirectList<T>::writeList } else if ( - len <= 1 || !shortListLen - || (len <= shortListLen && contiguous<T>()) + (len <= 1 || !shortLen) + || + ( + (len <= shortLen) + && + ( + Detail::ListPolicy::no_linebreak<T>::value + || contiguous<T>() + ) + ) ) { // Size and start delimiter @@ -114,17 +122,4 @@ Foam::Ostream& Foam::UIndirectList<T>::writeList } -// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * // - -template<class T> -Foam::Ostream& Foam::operator<< -( - Foam::Ostream& os, - const Foam::UIndirectList<T>& list -) -{ - return list.writeList(os, 10); -} - - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/UList/UList.H b/src/OpenFOAM/containers/Lists/UList/UList.H index 3d8302f7f714bc15848bfe4385003ddf448ec4c2..40bf4be2891b32f2446e4ab56d5d1ee5c74bb3b3 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.H +++ b/src/OpenFOAM/containers/Lists/UList/UList.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2017-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -51,6 +51,7 @@ SourceFiles #include "stdFoam.H" #include "Swap.H" #include "HashFwd.H" +#include "ListPolicy.H" #include <initializer_list> #include <iterator> @@ -66,8 +67,8 @@ class labelRange; template<class T> class List; template<class T> class SubList; template<class T> class UList; -template<class T> Ostream& operator<<(Ostream&, const UList<T>&); template<class T> Istream& operator>>(Istream&, UList<T>&); +template<class T> Ostream& operator<<(Ostream&, const UList<T>&); // Common list types typedef UList<char> charUList; @@ -471,20 +472,12 @@ public: //- Write the List as a dictionary entry with keyword void writeEntry(const word& keyword, Ostream& os) const; - //- Write the List, with line-breaks in ASCII if the list length - //- exceeds shortListLen. + //- Write List, with line-breaks in ASCII when length exceeds shortLen. // Using '0' suppresses line-breaks entirely. - Ostream& writeList(Ostream& os, const label shortListLen=0) const; - + Ostream& writeList(Ostream& os, const label shortLen=0) const; - // IOstream operators - //- Write List to Ostream, as per writeList() with shortListLen=10 - friend Ostream& operator<< <T> - ( - Ostream& os, - const UList<T>& list - ); + // IOstream Operators //- Read List contents from Istream. // Requires size to have been set before @@ -562,6 +555,17 @@ public: }; +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // + +//- Write List to Ostream, as per UList::writeList() with default length. +// The default short-length is given by Detail::ListPolicy::short_length +template<class T> +Ostream& operator<<(Ostream& os, const UList<T>& list) +{ + return list.writeList(os, Detail::ListPolicy::short_length<T>::value); +} + + // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * // template<class T> diff --git a/src/OpenFOAM/containers/Lists/UList/UListIO.C b/src/OpenFOAM/containers/Lists/UList/UListIO.C index 7b5805964b7142efbe5a8ae2bab2605691919673..3ba28d08eb1b660e70181cb0ef099804acebe8bc 100644 --- a/src/OpenFOAM/containers/Lists/UList/UListIO.C +++ b/src/OpenFOAM/containers/Lists/UList/UListIO.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -71,7 +71,7 @@ template<class T> Foam::Ostream& Foam::UList<T>::writeList ( Ostream& os, - const label shortListLen + const label shortLen ) const { const UList<T>& list = *this; @@ -88,8 +88,16 @@ Foam::Ostream& Foam::UList<T>::writeList } else if ( - len <= 1 || !shortListLen - || (len <= shortListLen && contiguous<T>()) + (len <= 1 || !shortLen) + || + ( + (len <= shortLen) + && + ( + Detail::ListPolicy::no_linebreak<T>::value + || contiguous<T>() + ) + ) ) { // Size and start delimiter @@ -141,14 +149,7 @@ Foam::Ostream& Foam::UList<T>::writeList } -// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * // - -template<class T> -Foam::Ostream& Foam::operator<<(Ostream& os, const UList<T>& list) -{ - return list.writeList(os, 10); -} - +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // template<class T> Foam::Istream& Foam::operator>>(Istream& is, UList<T>& list) diff --git a/src/OpenFOAM/containers/Lists/policy/ListPolicy.H b/src/OpenFOAM/containers/Lists/policy/ListPolicy.H new file mode 100644 index 0000000000000000000000000000000000000000..7697c61b22c1e98f77eae80b11c2bdd9d68bf9d7 --- /dev/null +++ b/src/OpenFOAM/containers/Lists/policy/ListPolicy.H @@ -0,0 +1,108 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. + +Namespace + Foam::Detail::ListPolicy + +Description + Additional compile-time controls of List behaviour + +\*---------------------------------------------------------------------------*/ + +#ifndef ListPolicy_H +#define ListPolicy_H + +#include "label.H" +#include <type_traits> + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +// Forward Declarations +class word; +class wordRe; +class keyType; + +namespace Detail +{ +namespace ListPolicy +{ + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +//- Number of items before requiring line-breaks in the list ouput. +// +// Default definition: 10 +template<class T> +struct short_length : std::integral_constant<label,10> {}; + +// Could override on a per-type basis +// Eg, +// template<> struct short_length<label> : std::integral_constant<label,20> {}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +//- Can suppress additional line breaks separate ASCII data content +//- when the data elements are primitives, or contiguous +// +// Default definition: (integral | floating-point) are contiguous and thus +// never need any line breaks +template<class T> +struct no_linebreak +: + std::integral_constant + < + bool, + std::is_integral<T>::value || std::is_floating_point<T>::value + > +{}; + + +// Specialization for word, wordRe, keyType +// These elements are normally fairly short, so ok to output a few (eg, 10) +// of them on a single line. + +//- Suppress line-breaks for word +template<> struct no_linebreak<word> : std::true_type {}; + +//- Suppress line-breaks for wordRe +template<> struct no_linebreak<wordRe> : std::true_type {}; + +//- Suppress line-breaks for keyType +template<> struct no_linebreak<keyType> : std::true_type {}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace ListPolicy +} // End namespace Detail +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* //