1 /**
2 * MOAB, a Mesh-Oriented datABase, is a software component for creating,
3 * storing and accessing finite element mesh data.
4 *
5 * Copyright 2004 Sandia Corporation. Under the terms of Contract
6 * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
7 * retains certain rights in this software.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 */
15
16 #ifndef MOAB_READER_WRITER_SET_HPP
17 #define MOAB_READER_WRITER_SET_HPP
18
19 #include <list>
20 #include <string>
21 #include "moab/Types.hpp"
22
23 namespace moab
24 {
25
26 class ReaderIface;
27 class WriterIface;
28 class Core;
29
30 /**
31 *\brief Maintain list of readers and writers.
32 *\version 1.00
33 *\date 2004-4-23
34 *\author Jason Kraftcheck
35 */
36 class ReaderWriterSet
37 {
38
39 public:
40 typedef ReaderIface* ( *reader_factory_t )( Interface* );
41 typedef WriterIface* ( *writer_factory_t )( Interface* );
42
43 ReaderWriterSet( Core* mdb );
44
45 ~ReaderWriterSet();
46
47 /**
48 * Regiseter a reader and/or writer
49 * Either factory function may be NULL, but not both.
50 *
51 *\param reader_fact A factory method to create an instance of the reader
52 *\param writer_fact A factory method to create an instance of the reader
53 *\param description A short description of the file format.
54 *\param extensions A null-terminated list of file extensions
55 *\param name File format identifier string.
56 */
57 ErrorCode register_factory( reader_factory_t reader_fact,
58 writer_factory_t writer_fact,
59 const char* description,
60 const char* const* extensions,
61 const char* name );
62 ErrorCode register_factory( reader_factory_t reader_fact,
63 writer_factory_t writer_fact,
64 const char* description,
65 const char* extension,
66 const char* name );
67
68 /**
69 * Create a reader object for the passed file name
70 * according to the dot-extension of the file name.
71 * Caller must delete the object when finished.
72 * Returns null if no matching file extension.
73 */
74 ReaderIface* get_file_extension_reader( const std::string& filename ) const;
75
76 /**
77 * Create a writer object for the passed file name
78 * according to the dot-extension of the file name.
79 * Caller must delete the object when finished.
80 * Returns null if no matching file extension.
81 */
82 WriterIface* get_file_extension_writer( const std::string& filename ) const;
83
84 /**
85 * Create a reader object for the passed file format type.
86 * Caller is responsible for deletion of returned object.
87 * Returns NULL if no match.
88 */
89 ReaderIface* get_file_reader( const char* format_name ) const;
90
91 /**
92 * Create a writer object for the passed file format type.
93 * Caller is responsible for deletion of returned object.
94 * Returns NULL if no match.
95 */
96 WriterIface* get_file_writer( const char* format_name ) const;
97
98 /**
99 * Get the file extension from a file name
100 */
101 static std::string extension_from_filename( const std::string& filename );
102
103 class Handler
104 {
105
106 friend class ReaderWriterSet;
107
108 public:
109 Handler( reader_factory_t read_f,
110 writer_factory_t write_f,
111 const char* name,
112 const char* desc,
113 const char* const* ext,
114 int num_ext );
115
116 inline const std::string& name() const
117 {
118 return mName;
119 }
120 inline const std::string& description() const
121 {
122 return mDescription;
123 }
124 inline void get_extensions( std::vector< std::string >& list_out ) const
125 {
126 list_out = mExtensions;
127 }
128
129 inline bool have_reader() const
130 {
131 return NULL != mReader;
132 }
133 inline bool have_writer() const
134 {
135 return NULL != mWriter;
136 }
137
138 inline ReaderIface* make_reader( Interface* iface ) const
139 {
140 return have_reader() ? mReader( iface ) : NULL;
141 }
142
143 inline WriterIface* make_writer( Interface* iface ) const
144 {
145 return have_writer() ? mWriter( iface ) : NULL;
146 }
147
148 bool reads_extension( const char* ext ) const;
149 bool writes_extension( const char* ext ) const;
150
151 bool operator==( const char* name ) const;
152
153 private:
154 reader_factory_t mReader;
155 writer_factory_t mWriter;
156
157 std::string mName, mDescription;
158 std::vector< std::string > mExtensions;
159 };
160
161 typedef std::list< Handler >::const_iterator iterator;
162
163 inline iterator begin() const
164 {
165 return handlerList.begin();
166 }
167
168 inline iterator end() const
169 {
170 return handlerList.end();
171 }
172
173 iterator handler_from_extension( const std::string& extension,
174 bool with_reader = false,
175 bool with_writer = false ) const;
176
177 iterator handler_by_name( const char* name ) const;
178
179 private:
180 Core* mbCore;
181
182 std::list< Handler > handlerList;
183 };
184
185 } // namespace moab
186
187 #endif