Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
RawBytesWriter.cpp
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2016 The SKA organisation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #include "cheetah/psrdada/detail/RawBytesWriter.h"
26 #include "panda/Log.h"
27 #include <algorithm>
28 
29 namespace ska {
30 namespace cheetah {
31 namespace psrdada {
32 namespace detail {
33 
34 template <typename DataType, typename Iterator>
35 Iterator& RawBytesWriter::write(Iterator& begin, Iterator const& end)
36 {
37  std::size_t span = std::distance(begin, end);
38 
39  //Calculate the remaining number of elements that can be written
40  //in the remaining buffer size.
41  std::size_t remaining_elements = remaining_bytes() / sizeof(DataType);
42 
43  if (span > remaining_elements)
44  {
45  PANDA_LOG_DEBUG << "Writing "<< remaining_elements << " elements of size "
46  << sizeof(DataType) << " bytes";
47  std::copy(begin, begin + remaining_elements, (DataType*) _write_ptr);
48  _write_ptr += remaining_elements*sizeof(DataType);
49  begin += remaining_elements;
50  _full = true;
51  _bytes.used_bytes(_bytes.total_bytes());
52  }
53  else
54  {
55  PANDA_LOG_DEBUG << "Writing "<< span << " elements of size " << sizeof(DataType) << " bytes";
56  std::copy(begin, end, (DataType*)_write_ptr);
57  _write_ptr += span * sizeof(DataType);
58  begin += span;
59  _full = false;
60  _bytes.used_bytes(std::distance(_bytes.ptr(), _write_ptr));
61  }
62  return begin;
63 }
64 
65 } // namespace detail
66 } // namespace psrdada
67 } // namespace cheetah
68 } // namespace ska
char * ptr() const
Get a raw pointer to the start of the buffer.
Definition: RawBytes.cpp:59
std::size_t total_bytes() const
Get the total number of bytes in the buffer.
Definition: RawBytes.cpp:44
Some limits and constants for FLDO.
Definition: Brdz.h:35
std::size_t used_bytes() const
Get the number of currently used bytes in the buffer.
Definition: RawBytes.cpp:49