ENH: provide direct pointer access to dictionaryEntry

This commit is contained in:
Mark Olesen
2017-07-21 12:05:32 +02:00
parent 2c69b7d7c4
commit c0c7c39031
9 changed files with 75 additions and 51 deletions

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -88,6 +88,18 @@ Foam::ITstream& Foam::dictionaryEntry::stream() const
} }
const Foam::dictionary* Foam::dictionaryEntry::dictPtr() const
{
return this;
}
Foam::dictionary* Foam::dictionaryEntry::dictPtr()
{
return this;
}
const Foam::dictionary& Foam::dictionaryEntry::dict() const const Foam::dictionary& Foam::dictionaryEntry::dict() const
{ {
return *this; return *this;

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -52,10 +52,8 @@ namespace Foam
{ {
// Forward declaration of friend functions and operators // Forward declaration of friend functions and operators
class dictionaryEntry; class dictionaryEntry;
Ostream& operator<<(Ostream& os, const dictionaryEntry& e);
Ostream& operator<<(Ostream& os, const dictionaryEntry& de);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
@ -111,13 +109,13 @@ public:
// Member functions // Member functions
//- Return the dictionary name (scoped, e.g. dictA::dictB::dictC) //- Return the scoped dictionary name (eg, dictA.dictB.dictC)
const fileName& name() const const fileName& name() const
{ {
return dictionary::name(); return dictionary::name();
} }
//- Return the dictionary name (scoped, e.g. dictA::dictB::dictC) //- Return the scoped dictionary name (eg, dictA.dictB.dictC)
fileName& name() fileName& name()
{ {
return dictionary::name(); return dictionary::name();
@ -133,11 +131,12 @@ public:
// calling this function generates a FatalError // calling this function generates a FatalError
ITstream& stream() const; ITstream& stream() const;
//- Return true because this entry is a dictionary
bool isDict() const //- Return pointer to this dictionary
{ virtual const dictionary* dictPtr() const;
return true;
} //- Return non-const pointer to this dictionary
virtual dictionary* dictPtr();
//- Return dictionary //- Return dictionary
const dictionary& dict() const; const dictionary& dict() const;
@ -159,7 +158,7 @@ public:
// Ostream operator // Ostream operator
friend Ostream& operator<<(Ostream& os, const dictionaryEntry& de); friend Ostream& operator<<(Ostream& os, const dictionaryEntry& e);
}; };

View File

@ -23,7 +23,6 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "keyType.H"
#include "dictionaryEntry.H" #include "dictionaryEntry.H"
#include "IOstreams.H" #include "IOstreams.H"
@ -66,9 +65,9 @@ void Foam::dictionaryEntry::write(Ostream& os) const
// * * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * //
Foam::Ostream& Foam::operator<<(Ostream& os, const dictionaryEntry& de) Foam::Ostream& Foam::operator<<(Ostream& os, const dictionaryEntry& e)
{ {
de.write(os); e.write(os);
return os; return os;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -52,8 +52,7 @@ namespace Foam
// Forward declaration of friend functions and operators // Forward declaration of friend functions and operators
class dictionaryListEntry; class dictionaryListEntry;
Ostream& operator<<(Ostream& os, const dictionaryListEntry& e);
Ostream& operator<<(Ostream&, const dictionaryListEntry&);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
@ -66,9 +65,6 @@ class dictionaryListEntry
{ {
// Private Member Functions // Private Member Functions
//- Returns size of dictionary without FoamFile
static label realSize(const dictionary&);
//- Disallow bitwise copy //- Disallow bitwise copy
dictionaryListEntry(const dictionaryListEntry&) = delete; dictionaryListEntry(const dictionaryListEntry&) = delete;
@ -78,13 +74,13 @@ public:
// Constructors // Constructors
//- Construct from the parent dictionary and Istream //- Construct from the parent dictionary and Istream
dictionaryListEntry(const dictionary& parentDict, Istream&); dictionaryListEntry(const dictionary& parentDict, Istream& is);
//- Construct as copy for the given parent dictionary //- Construct as copy for the given parent dictionary
dictionaryListEntry dictionaryListEntry
( (
const dictionary& parentDict, const dictionary& parentDict,
const dictionaryListEntry& const dictionaryListEntry& dictEnt
); );
autoPtr<entry> clone(const dictionary& parentDict) const autoPtr<entry> clone(const dictionary& parentDict) const
@ -96,7 +92,7 @@ public:
// Member functions // Member functions
//- Write //- Write
virtual void write(Ostream&) const; virtual void write(Ostream& os) const;
//- Return info proxy. //- Return info proxy.
// Used to print token information to a stream // Used to print token information to a stream
@ -108,12 +104,12 @@ public:
// Ostream operator // Ostream operator
friend Ostream& operator<<(Ostream&, const dictionaryListEntry&); friend Ostream& operator<<(Ostream& os, const dictionaryListEntry& e);
}; };
template<> template<>
Ostream& operator<<(Ostream&, const InfoProxy<dictionaryListEntry>&); Ostream& operator<<(Ostream& os, const InfoProxy<dictionaryListEntry>& ip);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -24,12 +24,12 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "dictionaryListEntry.H" #include "dictionaryListEntry.H"
#include "keyType.H"
#include "IOstreams.H" #include "IOstreams.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
Foam::label Foam::dictionaryListEntry::realSize(const dictionary& dict) // File-scope: The dictionary size without the "FoamFile" entry
static Foam::label realSize(const Foam::dictionary& dict)
{ {
if (dict.size() < 1 || dict.first()->keyword() != "FoamFile") if (dict.size() < 1 || dict.first()->keyword() != "FoamFile")
{ {
@ -127,9 +127,9 @@ void Foam::dictionaryListEntry::write(Ostream& os) const
// * * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * //
Foam::Ostream& Foam::operator<<(Ostream& os, const dictionaryListEntry& de) Foam::Ostream& Foam::operator<<(Ostream& os, const dictionaryListEntry& e)
{ {
de.write(os); e.write(os);
return os; return os;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -153,37 +153,54 @@ public:
//- Return line number of last token in dictionary //- Return line number of last token in dictionary
virtual label endLineNumber() const = 0; virtual label endLineNumber() const = 0;
//- Return true if this entry is a stream //- Return true if this entry is a stream
virtual bool isStream() const virtual bool isStream() const
{ {
return false; return false;
} }
//- Return token stream if this entry is a primitive entry //- Return token stream, if entry is a primitive entry
virtual ITstream& stream() const = 0; virtual ITstream& stream() const = 0;
//- Return true if this entry is a dictionary //- Return true if this entry is a dictionary
virtual bool isDict() const virtual bool isDict() const
{ {
return false; return !this->dictPtr();
} }
//- Return dictionary if this entry is a dictionary //- Return pointer to dictionary, if entry is a dictionary.
// Return nullptr if the entry is not a dictionary.
virtual const dictionary* dictPtr() const
{
return nullptr;
}
//- Return non-const pointer to dictionary, if entry is a dictionary
// Return nullptr if the entry is not a dictionary.
virtual dictionary* dictPtr()
{
return nullptr;
}
//- Return dictionary, if entry is a dictionary
virtual const dictionary& dict() const = 0; virtual const dictionary& dict() const = 0;
//- Return non-const access to dictionary if this entry is a dictionary //- Return non-const access to dictionary, if entry is a dictionary
virtual dictionary& dict() = 0; virtual dictionary& dict() = 0;
//- Write //- Write
virtual void write(Ostream&) const = 0; virtual void write(Ostream& os) const = 0;
// Member operators // Member operators
void operator=(const entry&); void operator=(const entry& e);
bool operator==(const entry&) const; bool operator==(const entry& e) const;
bool operator!=(const entry&) const; bool operator!=(const entry& e) const;
// Ostream operator // Ostream operator

View File

@ -32,9 +32,9 @@ License
void Foam::primitiveEntry::append(const UList<token>& varTokens) void Foam::primitiveEntry::append(const UList<token>& varTokens)
{ {
forAll(varTokens, i) for (const token& tok : varTokens)
{ {
newElmt(tokenIndex()++) = varTokens[i]; newElmt(tokenIndex()++) = tok;
} }
} }

View File

@ -160,7 +160,7 @@ public:
return true; return true;
} }
//- Return token stream if this entry is a primitive entry //- Return token stream for this primitive entry
ITstream& stream() const; ITstream& stream() const;
//- This entry is not a dictionary, //- This entry is not a dictionary,

View File

@ -227,9 +227,15 @@ void Foam::primitiveEntry::write(Ostream& os, const bool contentsOnly) const
os.writeKeyword(keyword()); os.writeKeyword(keyword());
} }
for (label i=0; i<size(); ++i) bool space = false; // Separate from previous tokens with a space
for (const token& t : *this)
{ {
const token& t = operator[](i); if (space)
{
os << token::SPACE;
}
space = true; // Prefix any following tokens
if (t.type() == token::VERBATIMSTRING) if (t.type() == token::VERBATIMSTRING)
{ {
// Bypass token output operator to avoid losing verbatimness. // Bypass token output operator to avoid losing verbatimness.
@ -240,11 +246,6 @@ void Foam::primitiveEntry::write(Ostream& os, const bool contentsOnly) const
{ {
os << t; os << t;
} }
if (i < size()-1)
{
os << token::SPACE;
}
} }
if (!contentsOnly) if (!contentsOnly)