1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 Write primitive and binary block from UOPstream
27 \*---------------------------------------------------------------------------*/
31 #include "UOPstream.H"
37 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
40 inline void Foam::UOPstream::writeToBuffer(const T& t)
42 writeToBuffer(&t, sizeof(T), sizeof(T));
46 inline void Foam::UOPstream::writeToBuffer(const char& c)
48 if (!sendBuf_.capacity())
50 sendBuf_.setCapacity(1000);
56 inline void Foam::UOPstream::writeToBuffer
63 if (!sendBuf_.capacity())
65 sendBuf_.setCapacity(1000);
68 label alignedPos = sendBuf_.size();
72 // Align bufPosition. Pads sendBuf_.size() - oldPos characters.
73 alignedPos = align + ((sendBuf_.size() - 1) & ~(align - 1));
76 // Extend if necessary
77 sendBuf_.setSize(alignedPos + count);
79 register const char* dataPtr = reinterpret_cast<const char*>(data);
80 register size_t i = count;
81 while (i--) sendBuf_[alignedPos++] = *dataPtr++;
86 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
88 Foam::UOPstream::UOPstream
90 const commsTypes commsType,
92 DynamicList<char>& sendBuf,
94 const bool sendAtDestruct,
100 Ostream(format, version),
104 sendAtDestruct_(sendAtDestruct)
111 Foam::UOPstream::UOPstream(const int toProcNo, PstreamBuffers& buffers)
113 UPstream(buffers.commsType_),
114 Ostream(buffers.format_, buffers.version_),
116 sendBuf_(buffers.sendBuf_[toProcNo]),
118 sendAtDestruct_(buffers.commsType_ != UPstream::nonBlocking)
125 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
127 Foam::UOPstream::~UOPstream()
143 FatalErrorIn("UOPstream::~UOPstream()")
144 << "Failed sending outgoing message of size " << sendBuf_.size()
145 << " to processor " << toProcNo_
146 << Foam::abort(FatalError);
152 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
154 Foam::Ostream& Foam::UOPstream::write(const token&)
156 notImplemented("Ostream& UOPstream::write(const token&)");
162 Foam::Ostream& Foam::UOPstream::write(const char c)
173 Foam::Ostream& Foam::UOPstream::write(const char* str)
175 word nonWhiteChars(string::validate<word>(str));
177 if (nonWhiteChars.size() == 1)
179 return write(nonWhiteChars.c_str()[1]);
181 else if (nonWhiteChars.size())
183 return write(nonWhiteChars);
192 Foam::Ostream& Foam::UOPstream::write(const word& str)
194 write(char(token::WORD));
196 size_t len = str.size();
198 writeToBuffer(str.c_str(), len + 1, 1);
204 Foam::Ostream& Foam::UOPstream::write(const string& str)
206 write(char(token::STRING));
208 size_t len = str.size();
210 writeToBuffer(str.c_str(), len + 1, 1);
216 Foam::Ostream& Foam::UOPstream::writeQuoted
218 const std::string& str,
224 write(char(token::STRING));
228 write(char(token::WORD));
231 size_t len = str.size();
233 writeToBuffer(str.c_str(), len + 1, 1);
239 Foam::Ostream& Foam::UOPstream::write(const label val)
241 write(char(token::LABEL));
247 Foam::Ostream& Foam::UOPstream::write(const floatScalar val)
249 write(char(token::FLOAT_SCALAR));
255 Foam::Ostream& Foam::UOPstream::write(const doubleScalar val)
257 write(char(token::DOUBLE_SCALAR));
263 Foam::Ostream& Foam::UOPstream::write(const char* data, std::streamsize count)
265 if (format() != BINARY)
267 FatalErrorIn("Ostream::write(const char*, std::streamsize)")
268 << "stream format not binary"
269 << Foam::abort(FatalError);
272 writeToBuffer(data, count, 8);
278 void Foam::UOPstream::print(Ostream& os) const
280 os << "Writing from processor " << toProcNo_
281 << " to processor " << myProcNo() << Foam::endl;
285 // ************************************************************************* //