1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
 * MOAB, a Mesh-Oriented datABase, is a software component for creating,
 * storing and accessing finite element mesh data.
 *
 * Copyright 2004 Sandia Corporation.  Under the terms of Contract
 * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
 * retains certain rights in this software.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 */

#ifndef MOAB_READER_WRITER_SET_HPP
#define MOAB_READER_WRITER_SET_HPP

#include <list>
#include <string>
#include "moab/Types.hpp"

namespace moab
{

class ReaderIface;
class WriterIface;
class Core;

/**
 *\brief Maintain list of readers and writers.
 *\version 1.00
 *\date 2004-4-23
 *\author Jason Kraftcheck
 */
class ReaderWriterSet
{

  public:
    typedef ReaderIface* ( *reader_factory_t )( Interface* );
    typedef WriterIface* ( *writer_factory_t )( Interface* );

    ReaderWriterSet( Core* mdb );

    ~ReaderWriterSet();

    /**
     * Regiseter a reader and/or writer
     * Either factory function may be NULL, but not both.
     *
     *\param reader_fact  A factory method to create an instance of the reader
     *\param writer_fact  A factory method to create an instance of the reader
     *\param description  A short description of the file format.
     *\param extensions   A null-terminated list of file extensions
     *\param name         File format identifier string.
     */
    ErrorCode register_factory( reader_factory_t reader_fact,
                                writer_factory_t writer_fact,
                                const char* description,
                                const char* const* extensions,
                                const char* name );
    ErrorCode register_factory( reader_factory_t reader_fact,
                                writer_factory_t writer_fact,
                                const char* description,
                                const char* extension,
                                const char* name );

    /**
     * Create a reader object for the passed file name
     * according to the dot-extension of the file name.
     * Caller must delete the object when finished.
     * Returns null if no matching file extension.
     */
    ReaderIface* get_file_extension_reader( const std::string& filename ) const;

    /**
     * Create a writer object for the passed file name
     * according to the dot-extension of the file name.
     * Caller must delete the object when finished.
     * Returns null if no matching file extension.
     */
    WriterIface* get_file_extension_writer( const std::string& filename ) const;

    /**
     * Create a reader object for the passed file format type.
     * Caller is responsible for deletion of returned object.
     * Returns NULL if no match.
     */
    ReaderIface* get_file_reader( const char* format_name ) const;

    /**
     * Create a writer object for the passed file format type.
     * Caller is responsible for deletion of returned object.
     * Returns NULL if no match.
     */
    WriterIface* get_file_writer( const char* format_name ) const;

    /**
     * Get the file extension from a file name
     */
    static std::string extension_from_filename( const std::string& filename );

    class Handler
    {

        friend class ReaderWriterSet;

      public:
        Handler( reader_factory_t read_f,
                 writer_factory_t write_f,
                 const char* name,
                 const char* desc,
                 const char* const* ext,
                 int num_ext );

        inline const std::string& name() const
        {
            return mName;
        }
        inline const std::string& description() const
        {
            return mDescription;
        }
        inline void get_extensions( std::vector< std::string >& list_out ) const
        {
            list_out = mExtensions;
        }

        inline bool have_reader() const
        {
            return NULL != mReader;
        }
        inline bool have_writer() const
        {
            return NULL != mWriter;
        }

        inline ReaderIface* make_reader( Interface* iface ) const
        {
            return have_reader() ? mReader( iface ) : NULL;
        }

        inline WriterIface* make_writer( Interface* iface ) const
        {
            return have_writer() ? mWriter( iface ) : NULL;
        }

        bool reads_extension( const char* ext ) const;
        bool writes_extension( const char* ext ) const;

        bool operator==( const char* name ) const;

      private:
        reader_factory_t mReader;
        writer_factory_t mWriter;

        std::string mName, mDescription;
        std::vector< std::string > mExtensions;
    };

    typedef std::list< Handler >::const_iterator iterator;

    inline iterator begin() const
    {
        return handlerList.begin();
    }

    inline iterator end() const
    {
        return handlerList.end();
    }

    iterator handler_from_extension( const std::string& extension,
                                     bool with_reader = false,
                                     bool with_writer = false ) const;

    iterator handler_by_name( const char* name ) const;

  private:
    Core* mbCore;

    std::list< Handler > handlerList;
};

}  // namespace moab

#endif