IotWebConfTParameterBuilder.h 5.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * IotWebConfTParameter.h -- IotWebConf is an ESP8266/ESP32
 *   non blocking WiFi/AP web configuration library for Arduino.
 *   https://github.com/prampec/IotWebConf
 *
 * Copyright (C) 2021 Balazs Kelemen <prampec+arduino@gmail.com>
 *                    rovo89
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

#ifndef IotWebConfTParameterBuilder_h
#define IotWebConfTParameterBuilder_h

Eric Duminil's avatar
Eric Duminil committed
16
#include "IotWebConfTParameter.h"
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

namespace iotwebconf
{

template <typename ParamType> class Builder;

template <typename ParamType>
class AbstractBuilder
{
public:
  AbstractBuilder(const char* id) : _id(id) { };
  virtual ParamType build() const
  {
    ParamType instance = std::move(
      ParamType(this->_id, this->_label, this->_defaultValue));
    this->apply(&instance);
    return instance;
  }

  Builder<ParamType>& label(const char* label)
    { this->_label = label; return static_cast<Builder<ParamType>&>(*this); }
  Builder<ParamType>& defaultValue(typename ParamType::DefaultValueType defaultValue)
    { this->_defaultValue = defaultValue; return static_cast<Builder<ParamType>&>(*this); }

protected:
  virtual ParamType* apply(ParamType* instance) const
  {
    return instance;
  }
  const char* _label;
  const char* _id;
  typename ParamType::DefaultValueType _defaultValue;
};

template <typename ParamType>
class Builder : public AbstractBuilder<ParamType>
{
public:
  Builder(const char* id) : AbstractBuilder<ParamType>(id) { };
};

///////////////////////////////////////////////////////////////////////////

template <typename ValueType, typename ParamType>
class PrimitiveBuilder :
  public AbstractBuilder<ParamType>
{
public:
  PrimitiveBuilder<ValueType, ParamType>(const char* id) :
    AbstractBuilder<ParamType>(id) { };
  Builder<ParamType>& min(ValueType min) { this->_minDefined = true; this->_min = min; return static_cast<Builder<ParamType>&>(*this); }
  Builder<ParamType>& max(ValueType max) { this->_maxDefined = true; this->_max = max; return static_cast<Builder<ParamType>&>(*this); }
  Builder<ParamType>& step(ValueType step) { this->_step = step; return static_cast<Builder<ParamType>&>(*this); }
  Builder<ParamType>& placeholder(const char* placeholder) { this->_placeholder = placeholder; return static_cast<Builder<ParamType>&>(*this); }

protected:
  virtual ParamType* apply(
     ParamType* instance) const override
  {
    if (this->_minDefined)
    {
      instance->setMin(this->_min);
    }
    if (this->_maxDefined)
    {
      instance->setMax(this->_max);
    }
    instance->setStep(this->_step);
    instance->setPlaceholder(this->_placeholder);
    return instance;
  }

  bool _minDefined = false;
  bool _maxDefined = false;
  ValueType _min;
  ValueType _max;
  ValueType _step = 0;
  const char* _placeholder = nullptr;
};

template <typename ValueType, int base>
class Builder<IntTParameter<ValueType, base>> :
  public PrimitiveBuilder<ValueType, IntTParameter<ValueType, base>>
{
public:
  Builder<IntTParameter<ValueType, base>>(const char* id) :
    PrimitiveBuilder<ValueType, IntTParameter<ValueType, base>>(id) { };
};

template <typename ValueType, int base>
class Builder<UIntTParameter<ValueType, base>> :
  public PrimitiveBuilder<ValueType, UIntTParameter<ValueType, base>>
{
public:
  Builder<UIntTParameter<ValueType, base>>(const char* id) :
    PrimitiveBuilder<ValueType, UIntTParameter<ValueType, base>>(id) { };
};

template <>
class Builder<FloatTParameter> :
  public PrimitiveBuilder<float, FloatTParameter>
{
public:
  Builder<FloatTParameter>(const char* id) :
    PrimitiveBuilder<float, FloatTParameter>(id) { };
};


template <size_t len>
class Builder<SelectTParameter<len>> :
  public AbstractBuilder<SelectTParameter<len>>
{
public:
  Builder<SelectTParameter<len>>(const char* id) :
    AbstractBuilder<SelectTParameter<len>>(id) { };

  virtual SelectTParameter<len> build() const override
  {
    return SelectTParameter<len>(
      this->_id, this->_label, this->_defaultValue,
      this->_optionValues, this->_optionNames,
      this->_optionCount, this->_nameLength);
  }

  Builder<SelectTParameter<len>>& optionValues(const char* optionValues)
    { this->_optionValues = optionValues; return *this; }
  Builder<SelectTParameter<len>>& optionNames(const char* optionNames)
    { this->_optionNames = optionNames; return *this; }
  Builder<SelectTParameter<len>>& optionCount(size_t optionCount)
    { this->_optionCount = optionCount; return *this; }
  Builder<SelectTParameter<len>>& nameLength(size_t nameLength)
    { this->_nameLength = nameLength; return *this; }

protected:
  virtual SelectTParameter<len>* apply(
     SelectTParameter<len>* instance) const override
  {
    instance->setOptionValues(this->_optionValues);
    instance->setOptionNames(this->_optionNames);
    instance->setOptionCount(this->_optionCount);
    instance->setNameLength(this->_nameLength);
    return instance;
  }

private:
  const char* _optionValues;
  const char* _optionNames;
  size_t _optionCount;
  size_t _nameLength;
};

} // End namespace

#endif