Program Listing for File idata.hpp¶
↰ Return to documentation for file (falcon/idata.hpp)
// ---------------------------------------------------------------------
// This file is part of falcon-core.
//
// Copyright (C) 2015, 2016, 2017 Neuro-Electronics Research Flanders
//
// Falcon-server is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Falcon-server is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with falcon-core. If not, see <http://www.gnu.org/licenses/>.
// ---------------------------------------------------------------------
#pragma once
#include <cassert>
#include <chrono>
#include <deque>
#include <limits>
#include <memory>
#include <string>
#include "ringbuffer.hpp"
#include "logging/log.hpp"
#include "utilities/time.hpp"
#include "utilities/string.hpp"
#include "serialization.hpp"
#include "yaml-cpp/yaml.h"
#include "datatype_generated.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/flexbuffers.h"
class BaseData {};
class BaseType {
public:
static const bool ispure() { return true; }
using Data = BaseData;
};
template<
typename DATA, typename BASETYPE, bool pure = false,
typename CAPS = typename DATA::Capabilities,
typename PARAMS = typename DATA::Parameters
>
class DefineType {
public:
static const std::string datatype() { return DATA::static_datatype(); }
static const std::string dataname() { return DATA::static_dataname(); }
static const bool ispure() { return pure && BASETYPE::ispure();}
using Data = DATA;
using Parameters = PARAMS;
using Capabilities = CAPS;
using Base = BASETYPE;
};
template <typename T, typename BASETYPE>
class IData : public BASETYPE::Data {
public:
using Base=typename BASETYPE::Data;
using Base::Base;
public:
virtual std::string datatype() const { return T::static_datatype(); }
virtual std::string dataname() const { return T::static_dataname(); }
};
namespace nsAnyType {
struct Parameters {};
class Data : public IData<Data, BaseType> {
public:
Data() : hardware_timestamp_(0), serial_number_(0) {}
virtual ~Data() {}
static const std::string static_datatype() { return "any"; }
static const std::string static_dataname() { return "data"; }
virtual void ClearData() {}
uint64_t serial_number() const;
void set_serial_number(uint64_t n);
void set_source_timestamp();
void set_source_timestamp(TimePoint t);
TimePoint source_timestamp() const;
template <typename DURATION = std::chrono::microseconds>
DURATION time_passed() const {
return std::chrono::duration_cast<DURATION>(Clock::now() -
source_timestamp_);
}
template <typename DURATION = std::chrono::microseconds>
DURATION time_since(TimePoint reference) const {
return std::chrono::duration_cast<DURATION>(Clock::now() - reference);
}
uint64_t hardware_timestamp() const;
void set_hardware_timestamp(uint64_t t);
void CloneTimestamps(const Data &data);
virtual void SerializeBinary(std::ostream &stream,
Serialization::Format format) const;
virtual void SerializeYAML(YAML::Node &node,
Serialization::Format format) const;
virtual void YAMLDescription(YAML::Node &node,
Serialization::Format format) const;
virtual void SerializeFlatBuffer(flexbuffers::Builder& flex_builder);
protected:
TimePoint source_timestamp_;
uint64_t hardware_timestamp_; // e.g. from Neuralynx
uint64_t serial_number_;
};
class Capabilities {
public:
void Validate(const Data & prototype) const {}
};
} // namespace nsAnyType
using AnyType = DefineType<
nsAnyType::Data, BaseType, true,
nsAnyType::Capabilities, nsAnyType::Parameters
>;