• Immutable Page
  • Info
  • Attachments

Class Definition

/**
 * A configuration option with boolean, int, float, String, Color, Key, Button,
 * Edge, Bell, or List.
 */
class CompOption {
        /**
         * Option data types
         */
    public:
        typedef enum {
            TypeBool,
            TypeInt,
            TypeFloat,
            TypeString,
            TypeColor,
            TypeAction,
            TypeKey,
            TypeButton,
            TypeEdge,
            TypeBell,
            TypeMatch,
            TypeList,
            /* internal use only */
            TypeUnset
        } Type;
        
        /**
         * A value of an Option
         */
        class Value {
            public:
                typedef std::vector<Value> Vector;

            public:
                Value ();
                Value (const Value &);
                Value (const bool b);
                Value (const int i);
                Value (const float f);
                Value (const unsigned short *color);
                Value (const CompString& s);
                Value (const char *s);
                Value (const CompMatch& m);
                Value (const CompAction& a);
                Value (Type type, const Vector& l);
                ~Value ();

                Type type () const;

                void set (const bool b);
                void set (const int i);
                void set (const float f);
                void set (const unsigned short *color);
                void set (const CompString& s);
                void set (const char *s);
                void set (const CompMatch& m);
                void set (const CompAction& a);
                void set (Type type, const Vector& l);

                bool               b ();
                int                i ();
                float              f ();
                unsigned short*    c ();
                CompString         s ();
                CompMatch &        match ();
                CompAction &       action ();
                Type               listType ();
                Vector &           list ();

                bool operator== (const Value& val);
                bool operator!= (const Value& val);
                Value & operator= (const Value &val);

                operator bool ();
                operator int ();
                operator float ();
                operator unsigned short * ();
                operator CompString ();
                operator CompMatch & ();
                operator CompAction & ();
                operator CompAction * ();
                operator Type ();
                operator Vector & ();

            private:
                PrivateValue *priv;
        };

        typedef std::vector<CompOption> Vector;

    public:
        CompOption ();
        CompOption (const CompOption &);
        CompOption (CompString name, Type type);
        ~CompOption ();

        void setName (CompString name, Type type);

        CompString name ();

        Type type ();
        Value & value ();
        Restriction & rest ();

        bool set (Value &val);
        bool isAction ();

        CompOption & operator= (const CompOption &option);

    public:
        static CompOption * findOption (Vector &options, CompString name,
                                        unsigned int *index = NULL);

        static bool
        getBoolOptionNamed (const Vector& options,
                            const CompString& name,
                            bool defaultValue = false);

        static int
        getIntOptionNamed (const Vector& options,
                           const CompString& name,
                           int defaultValue = 0);

        static float
        getFloatOptionNamed (const Vector& options,
                             const CompString& name,
                             const float& defaultValue = 0.0);

        static CompString
        getStringOptionNamed (const Vector& options,
                              const CompString& name,
                              const CompString& defaultValue = "");

        static unsigned short *
        getColorOptionNamed (const Vector& options,
                             const CompString& name,
                             unsigned short *defaultValue);

        static CompMatch
        getMatchOptionNamed (const Vector& options,
                             const CompString& name,
                             const CompMatch& defaultValue);

        static CompString typeToString (Type type);

        static bool stringToColor (CompString     color,
                                   unsigned short *rgba);

        static CompString colorToString (unsigned short *rgba);


        static bool setOption (CompOption  &o, Value &value);
};

Usage

A CompOption is an all-encompassing class which represents some value or changeable option. Usually this class is there for transferring lists of values of different types, for example in CompAction callbacks.

Setting an Option

Once you have a reference to an option, you can set it just by changing it's value. This can be done with the operator= by just doing option.value ().set (value) where value is another CompOption::Value. You can also change the name of the option, through setName and also it's type setType. The value that is read from the option, depends on it's type, so if you change the type to something else, then you must also set the option to a value of that type. Trying to read a value of another type (eg bool, from int) from an option of a different type will result in the returning of garbage and a warning.

Finding Options

Often plugins transfer information through vectors of options, and you might be looking to get the value of a particular option name of a particular type. This is what the CompOption::getTypeOptionNamed () static functions are for. They take a list of options, and then search that list for an option of that name and type, and return the option value, or the default value, which is the last item in the list.

Development/zero-nine/CoreClasses/CompOption (last edited 2010-08-23 08:53:14 by 203-173-33-106)