source: XIOS/trunk/src/filter/store_filter.cpp @ 638

Last change on this file since 638 was 638, checked in by rlacroix, 6 years ago

Add a source filter and two terminal filters.

File size: 2.1 KB
Line 
1#include "store_filter.hpp"
2#include "context.hpp"
3#include "grid.hpp"
4#include "timer.hpp"
5
6namespace xios
7{
8  CStoreFilter::CStoreFilter(CContext* context, CGrid* grid)
9    : CInputPin(1)
10    , context(context)
11    , grid(grid)
12  {
13    if (!context)
14      ERROR("CStoreFilter::CStoreFilter(CContext* context, CGrid* grid)",
15            "Impossible to construct a store filter without providing a context.");
16    if (!context)
17      ERROR("CStoreFilter::CStoreFilter(CContext* context, CGrid* grid)",
18            "Impossible to construct a store filter without providing a grid.");
19  }
20
21  CConstDataPacketPtr CStoreFilter::getPacket(Time timestamp)
22  {
23    CTimer timer("CStoreFilter::getPacket");
24    CConstDataPacketPtr packet;
25    const double timeout = 10; // 10 seconds timeout
26
27    do
28    {
29      timer.resume();
30
31      std::map<Time, CDataPacketPtr>::const_iterator it = packets.find(timestamp);
32      if (it != packets.end())
33        packet = it->second;
34      else // if the packet is not available yet, check if it can be received
35        context->checkBuffersAndListen();
36
37      timer.suspend();
38    } while (!packet && timer.getCumulatedTime() < timeout);
39
40    if (!packet)
41      ERROR("CConstDataPacketPtr CStoreFilter::getPacket(Time timestamp) const",
42            << "Impossible to get the packet with timestamp = " << timestamp);
43
44    return packet;
45  }
46
47  template <int N>
48  CDataPacket::StatusCode CStoreFilter::getData(Time timestamp, CArray<double, N>& data)
49  {
50    CConstDataPacketPtr packet = getPacket(timestamp);
51
52    if (packet->status == CDataPacket::NO_ERROR)
53      grid->outputField(packet->data, data);
54
55    return packet->status;
56  }
57
58  template CDataPacket::StatusCode CStoreFilter::getData<1>(Time timestamp, CArray<double, 1>& data);
59  template CDataPacket::StatusCode CStoreFilter::getData<2>(Time timestamp, CArray<double, 2>& data);
60  template CDataPacket::StatusCode CStoreFilter::getData<3>(Time timestamp, CArray<double, 3>& data);
61
62  void CStoreFilter::onInputReady(std::vector<CDataPacketPtr> data)
63  {
64    packets.insert(std::make_pair(data[0]->timestamp, data[0]));
65  }
66} // namespace xios
Note: See TracBrowser for help on using the repository browser.