|
| 1 | +// |
| 2 | +// This file is part of ArduinoCloudThing |
| 3 | +// |
| 4 | +// Copyright 2019 ARDUINO SA (http://www.arduino.cc/) |
| 5 | +// |
| 6 | +// This software is released under the GNU General Public License version 3, |
| 7 | +// which covers the main part of ArduinoCloudThing. |
| 8 | +// The terms of this license can be found at: |
| 9 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 10 | +// |
| 11 | +// You can be released from the requirements of the above licenses by purchasing |
| 12 | +// a commercial license. Buying such a license is mandatory if you want to modify or |
| 13 | +// otherwise use the software for commercial activities involving the Arduino |
| 14 | +// software without disclosing the source code of your own applications. To purchase |
| 15 | +// a commercial license, send an email to [email protected]. |
| 16 | +// |
| 17 | + |
| 18 | +#ifndef CLOUDUINT_H_ |
| 19 | +#define CLOUDUINT_H_ |
| 20 | + |
| 21 | +/****************************************************************************** |
| 22 | + INCLUDE |
| 23 | + ******************************************************************************/ |
| 24 | + |
| 25 | +#include <Arduino.h> |
| 26 | +#include "../Property.h" |
| 27 | + |
| 28 | +/****************************************************************************** |
| 29 | + CLASS DECLARATION |
| 30 | + ******************************************************************************/ |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +class CloudUnsignedInt : public Property { |
| 35 | + private: |
| 36 | + unsigned int _value, |
| 37 | + _cloud_value; |
| 38 | + public: |
| 39 | + CloudUnsignedInt() { |
| 40 | + CloudUnsignedInt(0); |
| 41 | + } |
| 42 | + CloudUnsignedInt(unsigned int v) : _value(v), _cloud_value(v) {} |
| 43 | + operator unsigned int() const { |
| 44 | + return _value; |
| 45 | + } |
| 46 | + virtual bool isDifferentFromCloud() { |
| 47 | + return _value != _cloud_value && (abs(_value - _cloud_value) >= Property::_min_delta_property); |
| 48 | + } |
| 49 | + virtual void fromCloudToLocal() { |
| 50 | + _value = _cloud_value; |
| 51 | + } |
| 52 | + virtual void fromLocalToCloud() { |
| 53 | + _cloud_value = _value; |
| 54 | + } |
| 55 | + virtual CborError appendAttributesToCloud() { |
| 56 | + return appendAttribute(_value); |
| 57 | + } |
| 58 | + virtual void setAttributesFromCloud() { |
| 59 | + setAttribute(_cloud_value); |
| 60 | + } |
| 61 | + //modifiers |
| 62 | + CloudUnsignedInt& operator=(unsigned int v) { |
| 63 | + _value = v; |
| 64 | + updateLocalTimestamp(); |
| 65 | + return *this; |
| 66 | + } |
| 67 | + CloudUnsignedInt& operator=(CloudUnsignedInt v) { |
| 68 | + return operator=((unsigned int)v); |
| 69 | + } |
| 70 | + CloudUnsignedInt& operator+=(unsigned int v) { |
| 71 | + return operator=(_value += v); |
| 72 | + } |
| 73 | + CloudUnsignedInt& operator-=(unsigned int v) { |
| 74 | + return operator=(_value -= v); |
| 75 | + } |
| 76 | + CloudUnsignedInt& operator*=(unsigned int v) { |
| 77 | + return operator=(_value *= v); |
| 78 | + } |
| 79 | + CloudUnsignedInt& operator/=(unsigned int v) { |
| 80 | + return operator=(_value /= v); |
| 81 | + } |
| 82 | + CloudUnsignedInt& operator%=(unsigned int v) { |
| 83 | + return operator=(_value %= v); |
| 84 | + } |
| 85 | + CloudUnsignedInt& operator++() { |
| 86 | + return operator=(++_value); |
| 87 | + } |
| 88 | + CloudUnsignedInt& operator--() { |
| 89 | + return operator=(--_value); |
| 90 | + } |
| 91 | + CloudUnsignedInt operator++(int) { |
| 92 | + operator=(_value + 1); |
| 93 | + return CloudUnsignedInt(_value); |
| 94 | + } |
| 95 | + CloudUnsignedInt operator--(int) { |
| 96 | + operator=(_value - 1); |
| 97 | + return CloudUnsignedInt(_value); |
| 98 | + } |
| 99 | + CloudUnsignedInt& operator&=(unsigned int v) { |
| 100 | + return operator=(_value &= v); |
| 101 | + } |
| 102 | + CloudUnsignedInt& operator|=(unsigned int v) { |
| 103 | + return operator=(_value |= v); |
| 104 | + } |
| 105 | + CloudUnsignedInt& operator^=(unsigned int v) { |
| 106 | + return operator=(_value ^= v); |
| 107 | + } |
| 108 | + CloudUnsignedInt& operator<<=(unsigned int v) { |
| 109 | + return operator=(_value <<= v); |
| 110 | + } |
| 111 | + CloudUnsignedInt& operator>>=(unsigned int v) { |
| 112 | + return operator=(_value >>= v); |
| 113 | + } |
| 114 | + //accessors |
| 115 | + CloudUnsignedInt operator+() const { |
| 116 | + return CloudUnsignedInt(+_value); |
| 117 | + } |
| 118 | + CloudUnsignedInt operator-() const { |
| 119 | + return CloudUnsignedInt(-_value); |
| 120 | + } |
| 121 | + CloudUnsignedInt operator!() const { |
| 122 | + return CloudUnsignedInt(!_value); |
| 123 | + } |
| 124 | + CloudUnsignedInt operator~() const { |
| 125 | + return CloudUnsignedInt(~_value); |
| 126 | + } |
| 127 | + //friends |
| 128 | + friend CloudUnsignedInt operator+(CloudUnsignedInt iw, CloudUnsignedInt v) { |
| 129 | + return iw += v; |
| 130 | + } |
| 131 | + friend CloudUnsignedInt operator+(CloudUnsignedInt iw, unsigned int v) { |
| 132 | + return iw += v; |
| 133 | + } |
| 134 | + friend CloudUnsignedInt operator+(unsigned int v, CloudUnsignedInt iw) { |
| 135 | + return CloudUnsignedInt(v) += iw; |
| 136 | + } |
| 137 | + friend CloudUnsignedInt operator-(CloudUnsignedInt iw, CloudUnsignedInt v) { |
| 138 | + return iw -= v; |
| 139 | + } |
| 140 | + friend CloudUnsignedInt operator-(CloudUnsignedInt iw, unsigned int v) { |
| 141 | + return iw -= v; |
| 142 | + } |
| 143 | + friend CloudUnsignedInt operator-(unsigned int v, CloudUnsignedInt iw) { |
| 144 | + return CloudUnsignedInt(v) -= iw; |
| 145 | + } |
| 146 | + friend CloudUnsignedInt operator*(CloudUnsignedInt iw, CloudUnsignedInt v) { |
| 147 | + return iw *= v; |
| 148 | + } |
| 149 | + friend CloudUnsignedInt operator*(CloudUnsignedInt iw, unsigned int v) { |
| 150 | + return iw *= v; |
| 151 | + } |
| 152 | + friend CloudUnsignedInt operator*(unsigned int v, CloudUnsignedInt iw) { |
| 153 | + return CloudUnsignedInt(v) *= iw; |
| 154 | + } |
| 155 | + friend CloudUnsignedInt operator/(CloudUnsignedInt iw, CloudUnsignedInt v) { |
| 156 | + return iw /= v; |
| 157 | + } |
| 158 | + friend CloudUnsignedInt operator/(CloudUnsignedInt iw, unsigned int v) { |
| 159 | + return iw /= v; |
| 160 | + } |
| 161 | + friend CloudUnsignedInt operator/(unsigned int v, CloudUnsignedInt iw) { |
| 162 | + return CloudUnsignedInt(v) /= iw; |
| 163 | + } |
| 164 | + friend CloudUnsignedInt operator%(CloudUnsignedInt iw, CloudUnsignedInt v) { |
| 165 | + return iw %= v; |
| 166 | + } |
| 167 | + friend CloudUnsignedInt operator%(CloudUnsignedInt iw, unsigned int v) { |
| 168 | + return iw %= v; |
| 169 | + } |
| 170 | + friend CloudUnsignedInt operator%(unsigned int v, CloudUnsignedInt iw) { |
| 171 | + return CloudUnsignedInt(v) %= iw; |
| 172 | + } |
| 173 | + friend CloudUnsignedInt operator&(CloudUnsignedInt iw, CloudUnsignedInt v) { |
| 174 | + return iw &= v; |
| 175 | + } |
| 176 | + friend CloudUnsignedInt operator&(CloudUnsignedInt iw, unsigned int v) { |
| 177 | + return iw &= v; |
| 178 | + } |
| 179 | + friend CloudUnsignedInt operator&(unsigned int v, CloudUnsignedInt iw) { |
| 180 | + return CloudUnsignedInt(v) &= iw; |
| 181 | + } |
| 182 | + friend CloudUnsignedInt operator|(CloudUnsignedInt iw, CloudUnsignedInt v) { |
| 183 | + return iw |= v; |
| 184 | + } |
| 185 | + friend CloudUnsignedInt operator|(CloudUnsignedInt iw, unsigned int v) { |
| 186 | + return iw |= v; |
| 187 | + } |
| 188 | + friend CloudUnsignedInt operator|(unsigned int v, CloudUnsignedInt iw) { |
| 189 | + return CloudUnsignedInt(v) |= iw; |
| 190 | + } |
| 191 | + friend CloudUnsignedInt operator^(CloudUnsignedInt iw, CloudUnsignedInt v) { |
| 192 | + return iw ^= v; |
| 193 | + } |
| 194 | + friend CloudUnsignedInt operator^(CloudUnsignedInt iw, unsigned int v) { |
| 195 | + return iw ^= v; |
| 196 | + } |
| 197 | + friend CloudUnsignedInt operator^(unsigned int v, CloudUnsignedInt iw) { |
| 198 | + return CloudUnsignedInt(v) ^= iw; |
| 199 | + } |
| 200 | + friend CloudUnsignedInt operator<<(CloudUnsignedInt iw, CloudUnsignedInt v) { |
| 201 | + return iw <<= v; |
| 202 | + } |
| 203 | + friend CloudUnsignedInt operator<<(CloudUnsignedInt iw, unsigned int v) { |
| 204 | + return iw <<= v; |
| 205 | + } |
| 206 | + friend CloudUnsignedInt operator<<(unsigned int v, CloudUnsignedInt iw) { |
| 207 | + return CloudUnsignedInt(v) <<= iw; |
| 208 | + } |
| 209 | + friend CloudUnsignedInt operator>>(CloudUnsignedInt iw, CloudUnsignedInt v) { |
| 210 | + return iw >>= v; |
| 211 | + } |
| 212 | + friend CloudUnsignedInt operator>>(CloudUnsignedInt iw, unsigned int v) { |
| 213 | + return iw >>= v; |
| 214 | + } |
| 215 | + friend CloudUnsignedInt operator>>(unsigned int v, CloudUnsignedInt iw) { |
| 216 | + return CloudUnsignedInt(v) >>= iw; |
| 217 | + } |
| 218 | + |
| 219 | +}; |
| 220 | + |
| 221 | + |
| 222 | +#endif /* CLOUDUINT_H_ */ |
0 commit comments