Commit 5e90a0dd authored by Mark Olesen's avatar Mark Olesen
Browse files

dictionary gets xfer constructor and transfer() method(s)

parent a0101214
......@@ -42,11 +42,20 @@ using namespace Foam;
int main(int argc, char *argv[])
{
{
dictionary dict(IFstream("testDict")());
Info<< "dict: " << dict << nl
<< "toc: " << dict.toc() << nl
<< "keys: " << dict.keys() << nl
<< "patterns: " << dict.keys(true) << endl;
dictionary dict1(IFstream("testDict")());
Info<< "dict1: " << dict1 << nl
<< "toc: " << dict1.toc() << nl
<< "keys: " << dict1.keys() << nl
<< "patterns: " << dict1.keys(true) << endl;
dictionary dict2(dict1.transfer());
Info<< "dict1.toc(): " << dict1.name() << " " << dict1.toc() << nl
<< "dict2.toc(): " << dict2.name() << " " << dict2.toc() << endl;
// copy back
dict1 = dict2;
Info<< "dict1.toc(): " << dict1.name() << " " << dict1.toc() << endl;
}
......
......@@ -89,6 +89,12 @@ baz
}
"anynumber.*"
{
$active
}
// this should work
#remove active
......
......@@ -22,24 +22,15 @@ License
along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Description
A simple wrapper around bool so that it can
be read as on/off, yes/no or y/n.
\*---------------------------------------------------------------------------*/
#include "Switch.H"
#include "error.H"
#include "dictionary.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
Switch Switch::lookupOrAddToDict
Foam::Switch Foam::Switch::lookupOrAddToDict
(
const word& name,
dictionary& dict,
......@@ -52,52 +43,39 @@ Switch Switch::lookupOrAddToDict
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
word Switch::wordValue(const bool l) const
Foam::word Foam::Switch::wordValue(const bool val) const
{
word w("off");
if (l)
{
w = "on";
}
return w;
return word(val ? "on" : "off");
}
bool Switch::boolValue(const word& w) const
bool Foam::Switch::boolValue(const word& val) const
{
bool l = true;
if (w == "on" || w == "yes" || w == "y" || w == "true")
if (val == "on" || val == "true" || val == "yes" || val == "y")
{
l = true;
return true;
}
else if (w == "off" || w == "no" || w == "n" || w == "false")
else if (val == "off" || val == "false" || val == "no" || val == "n")
{
l = false;
return false;
}
else
{
FatalErrorIn("Switch::boolValue(const word& w) const")
<< "unknown switch word " << w
FatalErrorIn("Switch::boolValue(const word&) const")
<< "unknown switch word " << val
<< abort(FatalError);
}
return l;
return false;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
bool Switch::readIfPresent(const word& name, const dictionary& dict)
bool Foam::Switch::readIfPresent(const word& name, const dictionary& dict)
{
return dict.readIfPresent(name, logicalSwitch_);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //
......@@ -26,8 +26,8 @@ Class
Foam::Switch
Description
A simple wrapper around bool so that it can be read as
on/off, yes/no or y/n.
A simple wrapper around bool so that it can be read as a word:
on/off, true/false, yes/no or y/n.
SourceFiles
Switch.C
......@@ -68,6 +68,7 @@ class Switch
// Private member functions
// could be static instead:
word wordValue(const bool) const;
bool boolValue(const word&) const;
......
......@@ -22,23 +22,14 @@ License
along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Description
A simple wrapper around bool so that it can
be read as on/off, yes/no or y/n.
\*---------------------------------------------------------------------------*/
#include "Switch.H"
#include "IOstreams.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Switch::Switch(Istream& is)
Foam::Switch::Switch(Istream& is)
:
wordSwitch_(is),
logicalSwitch_(boolValue(wordSwitch_))
......@@ -47,29 +38,25 @@ Switch::Switch(Istream& is)
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Istream& operator>>(Istream& is, Switch& s)
Foam::Istream& Foam::operator>>(Istream& is, Switch& s)
{
is >> s.wordSwitch_;
s.logicalSwitch_ = s.boolValue(s.wordSwitch_);
is.check("Istream& operator>>(Istream& is, Switch& s)");
is.check("Istream& operator>>(Istream&, Switch&)");
return is;
}
Ostream& operator<<(Ostream& os, const Switch& s)
Foam::Ostream& Foam::operator<<(Ostream& os, const Switch& s)
{
os << s.wordSwitch_;
os.check("Ostream& operator<<(Ostream& os, const Switch& s)");
os.check("Ostream& operator<<(Ostream&, const Switch&)");
return os;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //
......@@ -167,6 +167,29 @@ Foam::dictionary::dictionary
}
Foam::dictionary::dictionary
(
const dictionary& parentDict,
const xfer<dictionary>& dict
)
:
parent_(parentDict)
{
transfer(dict());
}
Foam::dictionary::dictionary
(
const xfer<dictionary>& dict
)
:
parent_(dictionary::null)
{
transfer(dict());
}
Foam::autoPtr<Foam::dictionary> Foam::dictionary::clone() const
{
return autoPtr<dictionary>(new dictionary(*this));
......@@ -756,17 +779,15 @@ bool Foam::dictionary::merge(const dictionary& dict)
++iter
)
{
const word& keyword = iter().keyword();
HashTable<entry*>::iterator fnd = hashedEntries_.find(iter().keyword());
HashTable<entry*>::iterator iter2 = hashedEntries_.find(keyword);
if (iter2 != hashedEntries_.end())
if (fnd != hashedEntries_.end())
{
// Recursively merge sub-dictionaries
// TODO: merge without copying
if (iter2()->isDict() && iter().isDict())
if (fnd()->isDict() && iter().isDict())
{
if (iter2()->dict().merge(iter().dict()))
if (fnd()->dict().merge(iter().dict()))
{
changed = true;
}
......@@ -798,6 +819,27 @@ void Foam::dictionary::clear()
}
void Foam::dictionary::transfer(dictionary& dict)
{
// changing parents probably doesn't make much sense,
// but what about the names?
name_ = dict.name_;
IDLList<entry>::transfer(dict);
hashedEntries_.transfer(dict.hashedEntries_);
patternEntries_.transfer(dict.patternEntries_);
patternRegexps_.transfer(dict.patternRegexps_);
}
Foam::xfer<Foam::dictionary> Foam::dictionary::transfer()
{
Foam::xfer<dictionary> xf;
xf().transfer(*this);
return xf;
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
Foam::ITstream& Foam::dictionary::operator[](const word& keyword) const
......@@ -806,17 +848,17 @@ Foam::ITstream& Foam::dictionary::operator[](const word& keyword) const
}
void Foam::dictionary::operator=(const dictionary& dict)
void Foam::dictionary::operator=(const dictionary& rhs)
{
// Check for assignment to self
if (this == &dict)
if (this == &rhs)
{
FatalErrorIn("dictionary::operator=(const dictionary&)")
<< "attempted assignment to self for dictionary " << name()
<< abort(FatalError);
}
name_ = dict.name();
name_ = rhs.name();
clear();
// Create clones of the entries in the given dictionary
......@@ -824,8 +866,8 @@ void Foam::dictionary::operator=(const dictionary& dict)
for
(
IDLList<entry>::const_iterator iter = dict.begin();
iter != dict.end();
IDLList<entry>::const_iterator iter = rhs.begin();
iter != rhs.end();
++iter
)
{
......@@ -834,10 +876,10 @@ void Foam::dictionary::operator=(const dictionary& dict)
}
void Foam::dictionary::operator+=(const dictionary& dict)
void Foam::dictionary::operator+=(const dictionary& rhs)
{
// Check for assignment to self
if (this == &dict)
if (this == &rhs)
{
FatalErrorIn("dictionary::operator+=(const dictionary&)")
<< "attempted addition assignment to self for dictionary " << name()
......@@ -846,8 +888,8 @@ void Foam::dictionary::operator+=(const dictionary& dict)
for
(
IDLList<entry>::const_iterator iter = dict.begin();
iter != dict.end();
IDLList<entry>::const_iterator iter = rhs.begin();
iter != rhs.end();
++iter
)
{
......@@ -856,10 +898,10 @@ void Foam::dictionary::operator+=(const dictionary& dict)
}
void Foam::dictionary::operator|=(const dictionary& dict)
void Foam::dictionary::operator|=(const dictionary& rhs)
{
// Check for assignment to self
if (this == &dict)
if (this == &rhs)
{
FatalErrorIn("dictionary::operator|=(const dictionary&)")
<< "attempted assignment to self for dictionary " << name()
......@@ -868,8 +910,8 @@ void Foam::dictionary::operator|=(const dictionary& dict)
for
(
IDLList<entry>::const_iterator iter = dict.begin();
iter != dict.end();
IDLList<entry>::const_iterator iter = rhs.begin();
iter != rhs.end();
++iter
)
{
......@@ -881,10 +923,10 @@ void Foam::dictionary::operator|=(const dictionary& dict)
}
void Foam::dictionary::operator<<=(const dictionary& dict)
void Foam::dictionary::operator<<=(const dictionary& rhs)
{
// Check for assignment to self
if (this == &dict)
if (this == &rhs)
{
FatalErrorIn("dictionary::operator<<=(const dictionary&)")
<< "attempted assignment to self for dictionary " << name()
......@@ -893,8 +935,8 @@ void Foam::dictionary::operator<<=(const dictionary& dict)
for
(
IDLList<entry>::const_iterator iter = dict.begin();
iter != dict.end();
IDLList<entry>::const_iterator iter = rhs.begin();
iter != rhs.end();
++iter
)
{
......
......@@ -166,11 +166,17 @@ public:
//- Construct top-level dictionary as copy
dictionary(const dictionary&);
//- Construct by transferring parameter contents given parent dictionary
dictionary(const dictionary& parentDict, const xfer<dictionary>&);
//- Construct top-level dictionary by transferring parameter contents
dictionary(const xfer<dictionary>&);
//- Construct and return clone
Foam::autoPtr<dictionary> clone() const;
autoPtr<dictionary> clone() const;
//- Construct top-level dictionary on freestore from Istream
static Foam::autoPtr<dictionary> New(Istream&);
static autoPtr<dictionary> New(Istream&);
// Destructor
......@@ -385,6 +391,12 @@ public:
//- Clear the dictionary
void clear();
//- Transfer the contents of the argument and annull the argument.
void transfer(dictionary&);
//- Transfer the contents to the xfer container
xfer<dictionary> transfer();
// Write
......
......@@ -71,22 +71,22 @@ bool Foam::dictionary::substituteKeyword(const word& keyword)
{
word varName = keyword(1, keyword.size()-1);
// lookup the variable name in the given dictionary....
// lookup the variable name in the given dictionary
const entry* ePtr = lookupEntryPtr(varName, true, true);
// ...if defined insert its entries into this dictionary...
// if defined insert its entries into this dictionary
if (ePtr != NULL)
{
const dictionary& addDict = ePtr->dict();
for
(
IDLList<entry>::const_iterator addIter = addDict.begin();
addIter != addDict.end();
++addIter
IDLList<entry>::const_iterator iter = addDict.begin();
iter != addDict.end();
++iter
)
{
add(addIter());
add(iter());
}
return true;
......
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