Program Listing for File EntityWithSources.hpp

Return to documentation for file (include/nix/base/EntityWithSources.hpp)

// Copyright (c) 2013, German Neuroinformatics Node (G-Node)
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted under the terms of the BSD License. See
// LICENSE file in the root of the Project.

#ifndef NIX_ENTITY_WITH_SOURCES_H
#define NIX_ENTITY_WITH_SOURCES_H

#include <nix/base/EntityWithMetadata.hpp>
#include <nix/Source.hpp>

namespace nix {
namespace base {


template <typename T>
class EntityWithSources : public base::EntityWithMetadata<T> {

public:

    EntityWithSources()
        : EntityWithMetadata<T>()
    {
    }

    EntityWithSources(const std::shared_ptr<T> &p_impl)
        : EntityWithMetadata<T>(p_impl)
    {
    }

    EntityWithSources(std::shared_ptr<T> &&ptr)
        : EntityWithMetadata<T>(std::move(ptr))
    {
    }

    ndsize_t sourceCount() const {
        return EntityWithMetadata<T>::backend()->sourceCount();
    }

    bool hasSource(const std::string &id) const {
        return EntityWithMetadata<T>::backend()->hasSource(id);
    }

    bool hasSource(const Source &source) const {
        std::string id;
        try {
            id = source.id();
        } catch (...) {
            return false;
        }
        return EntityWithMetadata<T>::backend()->hasSource(id);
    }

    Source getSource(const std::string &id) const {
        return EntityWithMetadata<T>::backend()->getSource(id);
    }

    Source getSource(const size_t index) const {
        return EntityWithMetadata<T>::backend()->getSource(index);
    }

    std::vector<Source> sources(util::Filter<Source>::type filter = util::AcceptAll<Source>()) const
    {
        auto f = [this] (size_t i) { return getSource(i); };
        return nix::base::ImplContainer<T>::template getEntities<nix::Source, decltype(f)>(f,sourceCount(),filter);
    }

    virtual void sources(const std::vector<Source> &sources) {
        EntityWithMetadata<T>::backend()->sources(sources);
    }

    void addSource(const std::string &id) {
        EntityWithMetadata<T>::backend()->addSource(id);
    }


    void addSource(const Source &source) {
        EntityWithMetadata<T>::backend()->addSource(source.id());
    }

    bool removeSource(const std::string &id) {
        return EntityWithMetadata<T>::backend()->removeSource(id);
    }

    bool removeSource(const Source &source) {
        return EntityWithMetadata<T>::backend()->removeSource(source.id());
    }

    virtual ~EntityWithSources() {}

};

} // namespace base
} // namespace nix


#endif // NIX_ENTITY_WITH_SOURCES_H