0

I have an interface for file parser. This parser can be configured, then saved to the file. New parsers for new file types can be added using .dll plugins.

class FileParserInterface {
public:
    QString toString() const = 0;
    QString constructorName() const {return "FileParserInterface";}
    SomeDataStruct getData() = 0;
}

Therefore I need some way to save those instances by name (can be name of the constructor). I would then have some method to get instance by string, eg:

static std::shared_ptr<FileParserInterface> fileParserFromString(const QString& name, const QString& parameters);

I have no idea how to thread-safely implement this. I am using boost and Qt libraries. I prefer to use stdlib and Qt when possible.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • 1
    Sounds like a `Factory` pattern, so maybe refer to https://stackoverflow.com/questions/2576022/efficient-thread-safe-singleton-in-c? – Mine Jul 28 '16 at 08:29
  • @Mine The problem is I need to allow items to be added to the global map variable. So it must be writable but still thread-safe. The post you mentioned covers single (presumably read-only) class instance. – Tomáš Zato Jul 28 '16 at 08:37
  • @TomášZato you have to mutex out the factory just like you would have to do that with any other modifiable shared resource. – n. m. could be an AI Jul 28 '16 at 08:42
  • @n.m. I was hoping for some magic to do it for me. Mutexing stuff is tedious and produces ugly code, even in C++ – Tomáš Zato Jul 28 '16 at 08:46
  • @TomášZato you just encapsulate it in the methods of the factory. – n. m. could be an AI Jul 28 '16 at 09:34
  • Look at Qt. It has a lot of similar examples: item editors ([1](http://doc.qt.io/qt-5/qitemeditorfactory.html#createEditor) [2](http://doc.qt.io/qt-5/qitemeditorfactory.html#registerEditor)), SQL drivers plugins ([1](http://doc.qt.io/qt-5/qsqldatabase.html#registerSqlDriver) [2](http://doc.qt.io/qt-5/qsqldriverplugin.html) [3](http://doc.qt.io/qt-5/sql-driver.html#how-to-write-your-own-database-driver)), other plugins ([1](http://doc.qt.io/qt-5/plugins-howto.html)). – ilotXXI Jul 28 '16 at 11:05

0 Answers0