/*
   HARK Library
   Copyright (C) 2021 Honda Research Institute Japan Inc.
 */

#include "hark-lib/datastream.h"
#include "hark-lib/error.h"

namespace hark
{
    DataSource::~DataSource()
    {
    }

    std::string
    DataSource::name() const
    {
        return this->_name;
    }

    void
    DataSource::connect_to(DataSink *sink)
    {
#if defined(VERBOSE_DEBUG)
        {
            std::stringstream ss;
            ss << "DataSource::connect_to: from=" << this->parent()->name() << "/" << this->name() << ", to=" << (sink->parent() ? sink->parent()->name() : "(unknown)") << "/" << sink->name() << std::endl;
            std::cout << ss.str();
        }
#endif /* #if defined(VERBOSE_DEBUG) */
        std::vector<DataSink *>::iterator it = std::find(this->_down_streams.begin(), this->_down_streams.end(), sink);
        if(it != this->_down_streams.end()){
            std::stringstream ss;
            ss << "DataSource " << this->name() << " is already connected to DataSink " << (*it)->name() << ".";
            throw RuntimeError(ss.str());
        }else{
            this->_down_streams.push_back(sink);
        }
    }
}

/* end of file */
