Program Listing for File NamedEntity.hpp

Return to documentation for file (include/nix/base/NamedEntity.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_NAMED_ENTITY_H
#define NIX_NAMED_ENTITY_H

#include <nix/base/INamedEntity.hpp>
#include <nix/base/Entity.hpp>

#include <memory>
#include <string>

namespace nix {
namespace base {

template<typename T>
class NamedEntity : public Entity<T> {

public:

    NamedEntity()
        : Entity<T>()
    {
    }

    NamedEntity(const std::shared_ptr<T> &p_impl)
        : Entity<T>(p_impl)
    {
    }

    NamedEntity(std::shared_ptr<T> &&ptr)
        : Entity<T>(std::move(ptr))
    {
    }

    void type(const std::string &type) {
        Entity<T>::backend()->type(type);
    }

    std::string type() const {
        return Entity<T>::backend()->type();
    }

    std::string name() const {
        return Entity<T>::backend()->name();
    }

    void definition(const std::string &definition) {
        Entity<T>::backend()->definition(definition);
    }

    boost::optional<std::string> definition() const {
        return Entity<T>::backend()->definition();
    }

    void definition(const none_t t)
    {
        Entity<T>::backend()->definition(t);
    }

    int compare(NamedEntity &other) const {
        return Entity<T>::backend()->compare(other.impl());
    }

    virtual ~NamedEntity() {}

};

template<typename Entity>
struct NIXAPI has_name {
#ifndef _WIN32
    typedef char have;
    typedef long nope;

        template<typename C> static have check(decltype(&C::name));
        template<typename C> static nope check(...);

    public:
        enum {
            value = sizeof(check<Entity>(0)) == sizeof(have)
        };

#else
    __if_exists(Entity::name) {
        static const bool value = true;
    }
    __if_not_exists(Entity::name) {
        static const bool value = false;
    }
#endif
};

template<typename Entity>
typename std::enable_if<has_name<Entity>::value,
               boost::optional<std::string>>::type
getEntityName(const Entity &e) {
    return boost::make_optional(e.name());
}

template<typename Entity>
typename std::enable_if<! has_name<Entity>::value,
                        boost::optional<std::string>>::type
getEntityName(const Entity &e) {
    return nix::none;
}

} // namespace base

template<typename Entity>
boost::optional<std::string> getEntityName(const Entity &e) {
    return base::getEntityName<Entity>(e);
}


} // namespace nix
#endif // NIX_NAMED_ENTITY_H