Program Listing for File shared_file.hpp

Return to documentation for file (/tmp/ws/src/ecl_core/ecl_devices/include/ecl/devices/shared_file.hpp)

/*****************************************************************************
** Ifdefs
*****************************************************************************/

#ifndef ECL_DEVICES_SHARED_FILE_HPP_
#define ECL_DEVICES_SHARED_FILE_HPP_

/*****************************************************************************
** Cross Platform Functionality
*****************************************************************************/

#include <ecl/config/ecl.hpp>

/*****************************************************************************
** Includes
*****************************************************************************/

#include <map>
#include <string>
#include <ecl/exceptions/standard_exception.hpp>
#include <ecl/threads/mutex.hpp>
#include "detail/character_buffer.hpp"
#include "ofile.hpp"
#include "traits.hpp"
#include "modes.hpp"
#include "macros.hpp"

/*****************************************************************************
** Namespaces
*****************************************************************************/

namespace ecl {

/*****************************************************************************
** Forward Definition
*****************************************************************************/

class SharedFile;

/*****************************************************************************
** Interfaces
*****************************************************************************/

namespace devices {

/*****************************************************************************
** Interface [SharedFileCommon]
*****************************************************************************/
class SharedFileCommon {
public:
    SharedFileCommon() : error_handler(NoError) {};
    SharedFileCommon(const std::string &name, ecl::WriteMode mode);
    virtual ~SharedFileCommon() {}

    friend class ecl::SharedFile;
    friend class SharedFileManager;

private:
    unsigned int count;
    ecl::Mutex mutex;
    OFile file;
    Error error_handler;
};

class SharedFileManager {
public:
    static SharedFileCommon* RegisterSharedFile(const std::string &name, ecl::WriteMode mode = New);
    static bool DeRegisterSharedFile(const std::string &name);
private:
    static ecl::Mutex mutex;
    static std::map<std::string,SharedFileCommon*> opened_files;
};

} // namespace devices

/*****************************************************************************
** Interface [SharedFile]
*****************************************************************************/
class ecl_devices_PUBLIC SharedFile {
public:
    /*********************
    ** C&D
    **********************/
    SharedFile() {};
    SharedFile(const std::string &name, WriteMode mode = New);
    virtual ~SharedFile();

    bool open(const std::string &name, WriteMode mode = New);

    /*********************
    ** Shared File Methods
    **********************/
    unsigned int count() { return shared_instance->count; }

    /*********************
    ** OutputDevice Methods
    **********************/
    bool open() { return shared_instance->file.open(); }
    long write(const char &c);
    long write(const char* s, unsigned long n);
    bool flush();

    const Error& error() const { return shared_instance->error_handler; }

private:
    devices::SharedFileCommon* shared_instance;
    devices::CharBuffer buffer;
};

/*****************************************************************************
** Traits [SharedFile]
*****************************************************************************/
template <>
class is_sink<SharedFile> : public True {};

} // namespace ecl

#endif /* ECL_DEVICES_SHARED_FILE_HPP_ */