Program Listing for File File.hpp

Return to documentation for file (include/nix/File.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_FILE_H
#define NIX_FILE_H

#include <nix/base/ImplContainer.hpp>
#include <nix/base/IFile.hpp>
#include <nix/Block.hpp>
#include <nix/Section.hpp>
#include <nix/Platform.hpp>
#include <nix/ObjectType.hpp>

#include <nix/valid/validate.hpp>

namespace nix {


class NIXAPI File : public base::ImplContainer<base::IFile> {

public:

    File() {}

    File(const File &other)
        : ImplContainer(other.impl())
    {
    }

    File(const std::shared_ptr<base::IFile> &p_impl)
        : ImplContainer(p_impl)
    {
    }

    File(std::shared_ptr<base::IFile> &&ptr)
        : ImplContainer(std::move(ptr))
    {
    }

    static File open(const std::string &name, FileMode mode=FileMode::ReadWrite,
                     const std::string &impl="hdf5", Compression compression=Compression::Auto,
                     OpenFlags flags=OpenFlags::None);

    bool flush();


    ndsize_t blockCount() const {
        return backend()->blockCount();
    }

    bool hasBlock(const std::string &name_or_id) const {
        return backend()->hasBlock(name_or_id);
    }

    bool hasBlock(const Block &block) const;

    Block getBlock(const std::string &name_or_id) const {
        return backend()->getBlock(name_or_id);
    }

    Block getBlock(ndsize_t index) const {
        if (index >= blockCount()) {
            throw nix::OutOfBounds("Index is out of bounds when calling File::getBlock(index)!");
        }
        return backend()->getBlock(index);
    }

    Block createBlock(const std::string &name, const std::string &type);

    bool deleteBlock(const std::string &name_or_id) {
        return backend()->deleteBlock(name_or_id);
    }

    bool deleteBlock(const Block &block);

    std::vector<Block> blocks(const util::Filter<Block>::type &filter) const;

    std::vector<Block> blocks() const
    {
        return blocks(util::AcceptAll<Block>());
    }

    //--------------------------------------------------
    // Methods concerning sections
    //--------------------------------------------------

    bool hasSection(const std::string &name_or_id) const {
        return backend()->hasSection(name_or_id);
    }

    bool hasSection(const Section &section) const;

    Section getSection(const std::string &name_or_id) const {
        return backend()->getSection(name_or_id);
    }

    Section getSection(ndsize_t index) const {
        if (index >= sectionCount()) {
            throw nix::OutOfBounds("Index is out of bounds when calling File::getSection(index)!");
        }
        return backend()->getSection(index);
    }

    ndsize_t sectionCount() const {
        return backend()->sectionCount();
    }


    std::vector<Section> sections(const util::Filter<Section>::type &filter) const;


    std::vector<Section> sections() const
    {
        return sections(util::AcceptAll<Section>());
    }


    std::vector<Section> findSections(const util::Filter<Section>::type &filter,
                                      size_t max_depth = std::numeric_limits<size_t>::max()) const;


    std::vector<Section> findSections(size_t max_depth = std::numeric_limits<size_t>::max()) const {
        return findSections(util::AcceptAll<Section>(), max_depth);
    }


    Section createSection(const std::string &name, const std::string &type);

    bool deleteSection(const std::string &name_or_id) {
        return backend()->deleteSection(name_or_id);
    }

    bool deleteSection(const Section &section);

    //--------------------------------------------------
    // Methods for file attribute access.
    //--------------------------------------------------

    std::vector<int> version() const {
        return backend()->version();
    }

    std::string format() const {
        return backend()->format();
    }

    std::string location() const {
        return backend()->location();
    }

    std::string id() const {
        return backend()->id();
    }

    void forceId() {
        backend()->forceId();
    }

    time_t createdAt() const {
        return backend()->createdAt();
    }

    time_t updatedAt() const {
        return backend()->updatedAt();
    }

    void setUpdatedAt() {
        backend()->setUpdatedAt();
    }

    void forceUpdatedAt() {
        backend()->forceUpdatedAt();
    }

    void setCreatedAt() {
        backend()->setCreatedAt();
    }

    void forceCreatedAt(time_t t) {
        backend()->forceCreatedAt(t);
    }

    //------------------------------------------------------
    // Operators and other functions
    //------------------------------------------------------

    void close();

    bool isOpen() const {
        return !isNone() && backend()->isOpen();
    }

    /*
     * @brief Returns the mode in which the file has been opened.
     *
     * @return the FileMode
     */
    FileMode fileMode() {
        return backend()->fileMode();
    }

    /*
    * @brief Returns the default choice for compressing datasets.
    * This choice can be made during file opening.
    *
    * @return true if dataset compression is selected as default.
    */
    Compression compression() const {
        return backend()->compression();
    }

    File &operator=(const none_t &t) {
        ImplContainer::operator=(t);
        return *this;
    }

    File &operator=(const File &other)  {
        ImplContainer::operator=(other);
        return *this;
    }
    //------------------------------------------------------
    // Validate
    //------------------------------------------------------

    valid::Result validate() const;

};

template<>
struct objectToType<nix::File> {
    static const bool isValid = true;
    static const ObjectType value = ObjectType::File;
    typedef nix::base::IFile backendType;
};


} // namespace nix

#endif