In src/misc/Preferences.h, [cf21683b40] and [e9eb21c01e] (dated 2022-10-25 12:39:55 -0400 and 12:59:04 -0400 respectively), Ted Felix added:

    // Was thinking about doing a template, but there are finicky little
    // differences between the types (QVariant conversions).  Bool is the
    // most popular, so I'm trying this one first.  Might take a shot at
    // a template later.
    class PreferenceBool
    {
    public:
        PreferenceBool(QString group, QString key, bool defaultValue) :
            m_group(group),
            m_key(key),
            m_defaultValue(defaultValue)
        {
        }
    ...

Commit [f36420](https://sourceforge.net/u/thanks4opensrc/rosegarden/ci/f364202e180e068537a1726ddcba3afdffbadf42/), dated 2022-06-30 12:52:15 -0700 and submitted as [Merge Request #76](https://sourceforge.net/p/rosegarden/git/merge-requests/76/) implemented:

    extern std::map<std::pair<const char*, const char*>, QVariant>
                preferences;

    template <typename T> T setPreference(const char *group,
                                          const char *preference,
                                          T value)
    {
        std::pair<const char*, const char*> grpPref{group, preference};
        auto iter = preferences.find(grpPref);

        if (iter == preferences.end() || iter->second != value) {
            QSettings settings;
            settings.beginGroup(group);
            settings.setValue(preference, value);
            preferences[grpPref] = value;
        }
        return value;
    }

    template <typename T> T getPreference(const char *group,
                                          const char *preference,
                                          T value)
    {
        QVariant qv = value;
        std::pair<const char*, const char*> grpPref{group, preference};
        auto iter = preferences.find(grpPref);

        if (iter == preferences.end()) {
            QSettings settings;
            settings.beginGroup(group);
            qv = settings.value(preference, value);
            preferences[grpPref] = qv;
        }
        else
            qv = iter->second;
        return qv.value<T>();
    }

This was documented in the commit message / merge request, and has been in use (at least for P.O.D. types including and beyond bool) in my testing and subsequent `thanks4opensrc-devel` branch commits without any noticeable problems ever since. I suppose there's nothing wrong with "reinventing the wheel" -- even if it's a slightly flatter and less round one -- but it seems like extra work and a waste of collaborative open-source development effort.

If on the other hand my implementation was in fact reviewed and rejected due to some obvious (or subtle) flaw, I'd appreciate being told what it is. Thanks in advance for any such help. If there are problems that arise instantiating the templates using more complex Qt datatypes, they would likely be no different than attempting to persist the data using explicit code and could be ameliorated via template specialization or merely documenting the .h file with "Don't use these methods with Qt QSomethingOrOther objects".


_______________________________________________
Rosegarden-devel mailing list
Rosegarden-devel@lists.sourceforge.net - use the link below to unsubscribe
https://lists.sourceforge.net/lists/listinfo/rosegarden-devel

Reply via email to