gfx/skia/trunk/include/utils/SkRTConf.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/include/utils/SkRTConf.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,195 @@
     1.4 +/*
     1.5 + * Copyright 2013 Google, Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +
    1.12 +#ifndef SkRTConf_DEFINED
    1.13 +#define SkRTConf_DEFINED
    1.14 +
    1.15 +#include "SkString.h"
    1.16 +#include "SkStream.h"
    1.17 +
    1.18 +#include "SkTDict.h"
    1.19 +#include "SkTArray.h"
    1.20 +
    1.21 +/** \class SkRTConfBase
    1.22 +    Non-templated base class for the runtime configs
    1.23 +*/
    1.24 +
    1.25 +class SkRTConfBase {
    1.26 +public:
    1.27 +    SkRTConfBase(const char *name) : fName(name) {}
    1.28 +    virtual ~SkRTConfBase() {}
    1.29 +    virtual const char *getName() const { return fName.c_str(); }
    1.30 +    virtual bool isDefault() const = 0;
    1.31 +    virtual void print(SkWStream *o) const = 0;
    1.32 +    virtual bool equals(const SkRTConfBase *conf) const = 0;
    1.33 +protected:
    1.34 +    SkString fName;
    1.35 +};
    1.36 +
    1.37 +/** \class SkRTConf
    1.38 +    A class to provide runtime configurability.
    1.39 +*/
    1.40 +template<typename T> class SkRTConf: public SkRTConfBase {
    1.41 +public:
    1.42 +    SkRTConf(const char *name, const T &defaultValue, const char *description);
    1.43 +    operator const T&() const { return fValue; }
    1.44 +    void print(SkWStream *o) const;
    1.45 +    bool equals(const SkRTConfBase *conf) const;
    1.46 +    bool isDefault() const { return fDefault == fValue; }
    1.47 +    void set(const T& value) { fValue = value; }
    1.48 +protected:
    1.49 +    void doPrint(char *s) const;
    1.50 +
    1.51 +    T        fValue;
    1.52 +    T        fDefault;
    1.53 +    SkString fDescription;
    1.54 +};
    1.55 +
    1.56 +#ifdef SK_DEVELOPER
    1.57 +#define SK_CONF_DECLARE(confType, varName, confName, defaultValue, description) static SkRTConf<confType> varName(confName, defaultValue, description)
    1.58 +#define SK_CONF_SET(confname, value) \
    1.59 +    skRTConfRegistry().set(confname, value, true)
    1.60 +/* SK_CONF_TRY_SET() is like SK_CONF_SET(), but doesn't complain if
    1.61 +   confname can't be found.  This is useful if the SK_CONF_DECLARE is
    1.62 +   inside a source file whose linkage is dependent on the system. */
    1.63 +#define SK_CONF_TRY_SET(confname, value) \
    1.64 +    skRTConfRegistry().set(confname, value, false)
    1.65 +#else
    1.66 +#define SK_CONF_DECLARE(confType, varName, confName, defaultValue, description) static confType varName = defaultValue
    1.67 +#define SK_CONF_SET(confname, value) (void) confname, (void) value
    1.68 +#define SK_CONF_TRY_SET(confname, value) (void) confname, (void) value
    1.69 +#endif
    1.70 +
    1.71 +/** \class SkRTConfRegistry
    1.72 +    A class that maintains a systemwide registry of all runtime configuration
    1.73 +    parameters.  Mainly used for printing them out and handling multiply-defined
    1.74 +    knobs.
    1.75 +*/
    1.76 +
    1.77 +class SkRTConfRegistry {
    1.78 +public:
    1.79 +    SkRTConfRegistry();
    1.80 +    ~SkRTConfRegistry();
    1.81 +    void printAll(const char *fname = NULL) const;
    1.82 +    bool hasNonDefault() const;
    1.83 +    void printNonDefault(const char *fname = NULL) const;
    1.84 +    const char *configFileLocation() const;
    1.85 +    void possiblyDumpFile() const;
    1.86 +    void validate() const;
    1.87 +    template <typename T> void set(const char *confname,
    1.88 +                                   T value,
    1.89 +                                   bool warnIfNotFound = true);
    1.90 +#ifdef SK_SUPPORT_UNITTEST
    1.91 +    static void UnitTest();
    1.92 +#endif
    1.93 +private:
    1.94 +    template<typename T> friend class SkRTConf;
    1.95 +
    1.96 +    void registerConf(SkRTConfBase *conf);
    1.97 +    template <typename T> bool parse(const char *name, T* value);
    1.98 +
    1.99 +    SkTDArray<SkString *> fConfigFileKeys, fConfigFileValues;
   1.100 +    typedef SkTDict< SkTDArray<SkRTConfBase *> * > ConfMap;
   1.101 +    ConfMap fConfs;
   1.102 +#ifdef SK_SUPPORT_UNITTEST
   1.103 +    SkRTConfRegistry(bool);
   1.104 +#endif
   1.105 +};
   1.106 +
   1.107 +// our singleton registry
   1.108 +
   1.109 +SkRTConfRegistry &skRTConfRegistry();
   1.110 +
   1.111 +template<typename T>
   1.112 +SkRTConf<T>::SkRTConf(const char *name, const T &defaultValue, const char *description)
   1.113 +    : SkRTConfBase(name)
   1.114 +    , fValue(defaultValue)
   1.115 +    , fDefault(defaultValue)
   1.116 +    , fDescription(description) {
   1.117 +
   1.118 +    T value;
   1.119 +    if (skRTConfRegistry().parse(fName.c_str(), &value)) {
   1.120 +        fValue = value;
   1.121 +    }
   1.122 +    skRTConfRegistry().registerConf(this);
   1.123 +}
   1.124 +
   1.125 +template<typename T>
   1.126 +void SkRTConf<T>::print(SkWStream *o) const {
   1.127 +    char outline[200]; // should be ok because we specify a max. width for everything here.
   1.128 +    char *outptr;
   1.129 +    if (strlen(getName()) >= 30) {
   1.130 +        o->writeText(getName());
   1.131 +        o->writeText(" ");
   1.132 +        outptr = &(outline[0]);
   1.133 +    } else {
   1.134 +        sprintf(outline, "%-30.30s", getName());
   1.135 +        outptr = &(outline[30]);
   1.136 +    }
   1.137 +
   1.138 +    doPrint(outptr);
   1.139 +    sprintf(outptr+30, " %.128s", fDescription.c_str());
   1.140 +    for (size_t i = strlen(outline); i --> 0 && ' ' == outline[i];) {
   1.141 +        outline[i] = '\0';
   1.142 +    }
   1.143 +    o->writeText(outline);
   1.144 +}
   1.145 +
   1.146 +template<typename T>
   1.147 +void SkRTConf<T>::doPrint(char *s) const {
   1.148 +    sprintf(s, "%-30.30s", "How do I print myself??");
   1.149 +}
   1.150 +
   1.151 +template<> inline void SkRTConf<bool>::doPrint(char *s) const {
   1.152 +    char tmp[30];
   1.153 +    sprintf(tmp, "%s # [%s]", fValue ? "true" : "false", fDefault ? "true" : "false");
   1.154 +    sprintf(s, "%-30.30s", tmp);
   1.155 +}
   1.156 +
   1.157 +template<> inline void SkRTConf<int>::doPrint(char *s) const {
   1.158 +    char tmp[30];
   1.159 +    sprintf(tmp, "%d # [%d]", fValue, fDefault);
   1.160 +    sprintf(s, "%-30.30s", tmp);
   1.161 +}
   1.162 +
   1.163 +template<> inline void SkRTConf<unsigned int>::doPrint(char *s) const {
   1.164 +    char tmp[30];
   1.165 +    sprintf(tmp, "%u # [%u]", fValue, fDefault);
   1.166 +    sprintf(s, "%-30.30s", tmp);
   1.167 +}
   1.168 +
   1.169 +template<> inline void SkRTConf<float>::doPrint(char *s) const {
   1.170 +    char tmp[30];
   1.171 +    sprintf(tmp, "%6.6f # [%6.6f]", fValue, fDefault);
   1.172 +    sprintf(s, "%-30.30s", tmp);
   1.173 +}
   1.174 +
   1.175 +template<> inline void SkRTConf<double>::doPrint(char *s) const {
   1.176 +    char tmp[30];
   1.177 +    sprintf(tmp, "%6.6f # [%6.6f]", fValue, fDefault);
   1.178 +    sprintf(s, "%-30.30s", tmp);
   1.179 +}
   1.180 +
   1.181 +template<> inline void SkRTConf<const char *>::doPrint(char *s) const {
   1.182 +    char tmp[30];
   1.183 +    sprintf(tmp, "%s # [%s]", fValue, fDefault);
   1.184 +    sprintf(s, "%-30.30s", tmp);
   1.185 +}
   1.186 +
   1.187 +template<typename T>
   1.188 +bool SkRTConf<T>::equals(const SkRTConfBase *conf) const {
   1.189 +    // static_cast here is okay because there's only one kind of child class.
   1.190 +    const SkRTConf<T> *child_pointer = static_cast<const SkRTConf<T> *>(conf);
   1.191 +    return child_pointer &&
   1.192 +           fName == child_pointer->fName &&
   1.193 +           fDescription == child_pointer->fDescription &&
   1.194 +           fValue == child_pointer->fValue &&
   1.195 +           fDefault == child_pointer->fDefault;
   1.196 +}
   1.197 +
   1.198 +#endif

mercurial