Commit 12c903bb authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: define nameOp<>, typeOp<>, sizeOp<> functors (issue #1013)

parent 47519b2e
......@@ -617,6 +617,7 @@ int main(int argc, char *argv[])
fileName pathName(wrdList);
Info<< "pathName = " << pathName << nl
<< "nameOp = " << nameOp<fileName>()(pathName) << nl
<< "pathName.name() = >" << pathName.name() << "<\n"
<< "pathName.path() = " << pathName.path() << nl
<< "pathName.ext() = >" << pathName.ext() << "<\n"
......
......@@ -271,32 +271,31 @@ int main(int argc, char *argv[])
Info<< "hash: = " << word::printf("0x%012X", string::hash()(s2)) << endl;
// test formatting on int
// Test formatting on int
{
label val = 25;
Info<<"val: " << val << "\n";
Info<< "int " << val << " as word >"
<< Foam::name(val) << "< or "
<< word::printf("formatted >%08d<", val) << "\n";
Info<< "int " << val << " nameOp='"
<< nameOp<label>()(val) << "', name='"
<< Foam::name(val) << "' or "
<< word::printf("formatted '%08d'", val) << "\n";
}
// test formatting on scalar
// Test formatting on scalar
{
scalar val = 3.1415926535897931;
Info<< "scalar " << val << " as word >"
<< Foam::name(val) << "< or "
<< word::printf("formatted >%.9f<", val) << "\n";
Info<< "scalar " << val << " nameOp='"
<< nameOp<scalar>()(val) << "', name='"
<< Foam::name(val) << "' or "
<< word::printf("formatted '%.9f'", val) << "\n";
}
// test formatting on uint
// Test formatting on uint
{
uint64_t val = 25000000ul;
Info<<"val: " << val << "\n";
Info<< "uint64 " << val << " as word >"
<< Foam::name(val) << "< or "
<< word::printf("formatted >%08d<", val) << "\n";
Info<< "uint64 " << val << " nameOp='"
<< nameOp<uint64_t>()(val) << "', name='"
<< Foam::name(val) << "' or "
<< word::printf("formatted '%08d'", val) << "\n";
}
// test startsWith, endsWith methods
......
......@@ -87,12 +87,12 @@ static const Enum<restoreMethod> methodEndings
// Files in given directory at time instant
wordList getFiles(const fileName& dir, const word& instance)
inline wordList getFiles(const fileName& dir, const word& instance)
{
return ListOps::create<word>
(
Foam::readDir(dir/instance, fileName::FILE),
[](const fileName& f){ return f.name(); }
nameOp<fileName>()
);
}
......
......@@ -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 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -612,6 +612,17 @@ struct Hash<UList<T>>
};
//- Extract size (as label) from an object, typically using its size() method.
template<class T>
struct sizeOp
{
inline label operator()(const T& obj) const
{
return obj.size();
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
......
......@@ -105,10 +105,22 @@ public:
// * * * * * * * * * * * * * * * IO/Conversion * * * * * * * * * * * * * * * //
//- Return a word representation of a Scalar.
//- A word representation of a floating-point value.
// Uses stringstream instead of std::to_string for more consistent formatting.
word name(const Scalar val);
//- A word representation of a floating-point value.
template<>
struct nameOp<Scalar>
{
inline word operator()(const Scalar val) const
{
return Foam::name(val);
}
};
//- Parse entire buffer as a float/double, skipping leading/trailing whitespace.
// \return Parsed value or FatalIOError on any problem
Scalar ScalarRead(const char* buf);
......
......@@ -216,9 +216,9 @@ public:
// * * * * * * * * * * * * * * Global functions * * * * * * * * * * * * * * //
//- Return a string representation of a VectorSpace
//- A word representation of a VectorSpace
template<class Form, class Cmpt, direction Ncmpts>
word name(const VectorSpace<Form, Cmpt, Ncmpts>&);
word name(const VectorSpace<Form, Cmpt, Ncmpts>& vs);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -48,13 +48,22 @@ class Ostream;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Return a word representation of an int16
//- A word representation of int16 value
inline word name(const int16_t val)
{
// No stripping required
return word(std::to_string(int(val)), false);
return word(std::to_string(int(val)), false); // Needs no stripping
}
//- A word representation of int16 value
template<>
struct nameOp<int16_t>
{
inline word operator()(const int16_t val) const
{
return word(std::to_string(int(val)), false); // Needs no stripping
}
};
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
......
......@@ -54,14 +54,24 @@ class Ostream;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Return a word representation of an int32
//- A word representation of int32 value
inline word name(const int32_t val)
{
// No stripping required
return word(std::to_string(val), false);
return word(std::to_string(val), false); // Needs no stripping
}
//- A word representation of int32 value
template<>
struct nameOp<int32_t>
{
inline word operator()(const int32_t val) const
{
return word(std::to_string(val), false); // Needs no stripping
}
};
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Read int32_t from stream
......
......@@ -55,14 +55,24 @@ class Ostream;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Return a word representation of an int64
//- A word representation of int64 value
inline word name(const int64_t val)
{
// No stripping required
return word(std::to_string(val), false);
return word(std::to_string(val), false); // Needs no stripping
}
//- A word representation of int64 value
template<>
struct nameOp<int64_t>
{
inline word operator()(const int64_t val) const
{
return word(std::to_string(val), false); // Needs no stripping
}
};
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Read int64_t from stream
......
......@@ -48,14 +48,24 @@ class Ostream;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Return a word representation of a uint16
//- A word representation of uint16 value
inline word name(const uint16_t val)
{
// No stripping required
return word(std::to_string(int(val)), false);
return word(std::to_string(int(val)), false); // Needs no stripping
}
//- A word representation of uint16 value
template<>
struct nameOp<uint16_t>
{
inline word operator()(const uint16_t val) const
{
return word(std::to_string(int(val)), false); // Needs no stripping
}
};
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Istream& operator>>(Istream& is, uint16_t& val);
......
......@@ -54,13 +54,22 @@ class Ostream;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Return a word representation of a uint32
//- A word representation of uint32 value
inline word name(const uint32_t val)
{
// No stripping required
return word(std::to_string(val), false);
return word(std::to_string(val), false); // Needs no stripping
}
//- A word representation of uint32 value
template<>
struct nameOp<uint32_t>
{
inline word operator()(const uint32_t val) const
{
return word(std::to_string(val), false); // Needs no stripping
}
};
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
......
......@@ -54,14 +54,24 @@ class Ostream;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Return a word representation of a uint64
//- A word representation of uint64 value
inline word name(const uint64_t val)
{
// No stripping required
return word(std::to_string(val), false);
return word(std::to_string(val), false); // Needs no stripping
}
//- A word representation of uint64 value
template<>
struct nameOp<uint64_t>
{
inline word operator()(const uint64_t val) const
{
return word(std::to_string(val), false); // Needs no stripping
}
};
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Read uint64_t from stream.
......
......@@ -589,7 +589,7 @@ Foam::fileName Foam::operator/(const string& a, const string& b)
Foam::fileName Foam::search(const word& file, const fileName& directory)
{
// Search the current directory for the file
fileNameList files(fileHandler().readDir(directory));
fileNameList files(fileHandler().readDir(directory, fileName::FILE));
for (const fileName& item : files)
{
if (item == file)
......
......@@ -52,11 +52,11 @@ SourceFiles
namespace Foam
{
// Forward declarations
template<class T> class List;
template<class T> class UList;
typedef List<word> wordList;
// Forward declaration of friend functions and operators
class wordRe;
class fileName;
......
......@@ -47,7 +47,7 @@ SourceFiles
namespace Foam
{
// Forward declaration of friend functions and operators
// Forward declarations
class word;
Istream& operator>>(Istream& is, word& w);
Ostream& operator<<(Ostream& os, const word& w);
......@@ -218,13 +218,37 @@ public:
};
// Global Operators
// * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
//- Join words as camelCase, capitalizing the first letter of b.
// No effect if either argument is empty.
word operator&(const word& a, const word& b);
// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
//- Extract name (as a word) from an object, typically using its name() method.
template<class T>
struct nameOp
{
inline word operator()(const T& obj) const
{
return obj.name();
}
};
//- Extract type (as a word) from an object, typically using its type() method.
template<class T>
struct typeOp
{
inline word operator()(const T& obj) const
{
return obj.type();
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment