ParameterList.h 4.74 KB
Newer Older
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
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

#ifndef PARAMETERLIST_H
#define PARAMETERLIST_H

#include <QMap>
28
#include <QVector>
29 30 31 32 33 34

#include "mavlink_types.h"
#include "QGCParamID.h"
#include "Parameter.h"
#include "OpalRT.h"

35 36
namespace OpalRT
{
37 38
    class ParameterList
    {
39 40
    public:

41 42
        class const_iterator
        {
43 44
            friend class ParameterList;

45
        public:
46 47 48 49 50 51 52 53 54 55 56 57 58
            inline const_iterator() {}
            const_iterator(const const_iterator& other);

            const_iterator& operator+=(int i) {index += i; return *this;}
            bool operator<(const const_iterator& other) {return (this->paramList == other.paramList)
                                                         &&(this->index<other.index);}
            bool operator==(const const_iterator& other) {return (this->paramList == other.paramList)
                                                          &&(this->index==other.index);}
            bool operator!=(const const_iterator& other) {return !((*this) == other);}
            const Parameter& operator*() const {return paramList[index];}
            const Parameter* operator->() const {return &paramList[index];}

            const_iterator& operator++() {++index; return *this;}
59
        private:
60 61 62
            const_iterator(QList<Parameter>);
            QList<Parameter> paramList;
            int index;
63
        };
64 65


66 67
        ParameterList();
        ~ParameterList();
68 69 70 71

        /** Count the number of parameters in the list.
          \return Total number of parameters
          */
72
        int count();
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

        /** Find p in the list and return its index.
          \note In order to use this index to look up p, the component is also needed.
          \return the index of p or -1 if p is not found
          \example
          \code
          int compid = OpalRT::CONTROLLER_ID;
          Parameter p("simulinkpath", "simulinkparamname", compid, QGCParamID("PID_GAIN"));
          ParameterList pList;
          if ((int index=pList.indexOf(p)) != -1)
             qDebug() << "PID_GAIN is at index " << index;
          \endcode          
          */

        int indexOf(const Parameter& p);
        bool contains(int compid, QGCParamID paramid) const {return (*params)[compid].contains(paramid);}

        /// Get a parameter from the list
        const Parameter getParameter(int compid, QGCParamID paramid) const {return (*params)[compid][paramid];}
        Parameter& getParameter(int compid, QGCParamID paramid) {return (*params)[compid][paramid];}
        const Parameter getParameter(int compid, int index) const {return *((*paramList)[compid][index]);}        

        /** Convenient syntax for calling OpalRT::Parameter::getParameter() */
        Parameter& operator()(int compid, QGCParamID paramid) {return getParameter(compid, paramid);}
        Parameter& operator()(uint8_t compid, QGCParamID paramid) {return getParameter(static_cast<int>(compid), paramid);}
98 99 100

        const_iterator begin() const;
        const_iterator end() const;
101

102
    protected:
103 104 105 106 107 108 109 110
        /** Store the parameters mapped by componentid, and paramid.
           \code
           // Look up a parameter
           int compid = 1;
           QGCParamID paramid("PID_GAIN");
           Parameter p = params[compid][paramid];
           \endcode
           */
111
        QMap<int, QMap<QGCParamID, Parameter> > *params;
112 113 114 115 116 117 118 119 120 121 122
        /**
          Store pointers to the parameters to allow fast lookup by index.
          This variable may be changed to const pointers to ensure all changes
          are made through the map container.
          */
        QList<QList<Parameter*> > *paramList;
        /**
          Get the list of available parameters from Opal-RT.
          \param[out] opalParams Map of parameter paths/names to ids which are valid in Opal-RT
          */
        void getParameterList(QMap<QString, unsigned short>* opalParams);
123 124 125
    };
}
#endif // PARAMETERLIST_H