summaryrefslogtreecommitdiff
path: root/fuzzylite/fl/fuzzylite.h
blob: 3cca94bcf4c1be3ef28801a4bc15a667ff169ef2 (plain)
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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
 fuzzylite (R), a fuzzy logic control library in C++.
 Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved.
 Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com>

 This file is part of fuzzylite.

 fuzzylite is free software: you can redistribute it and/or modify it under
 the terms of the FuzzyLite License included with the software.

 You should have received a copy of the FuzzyLite License along with
 fuzzylite. If not, see <http://www.fuzzylite.com/license/>.

 fuzzylite is a registered trademark of FuzzyLite Limited.
 */

#ifndef FL_FUZZYLITE_H
#define FL_FUZZYLITE_H

#include <algorithm>
#include <cmath>
#include <iostream>
#include <sstream>
#include <limits>
#include <memory>
#include <cstddef>

#ifndef FL_BUILD_PATH
#define FL_BUILD_PATH ""
#endif

#if defined(_WIN32) || defined(WIN32)
#define FL_WINDOWS
#endif

#if defined(unix) || defined(__unix__) || defined(__unix)
#define FL_UNIX
#endif

#ifdef __APPLE__
#define FL_APPLE
#ifndef FL_UNIX
#define FL_UNIX
#endif
#endif

#define FL__FILE__ std::string(__FILE__).substr(std::string(FL_BUILD_PATH).size())

#define FL_LOG_PREFIX FL__FILE__ << " (" << __LINE__ << "):"

#define FL_AT FL__FILE__, __LINE__, __FUNCTION__

#define FL_LOG(message) {if (fl::fuzzylite::isLogging()){std::cout << FL_LOG_PREFIX << message << std::endl;}}
#define FL_LOGP(message) {if (fl::fuzzylite::isLogging()){std::cout << message << std::endl;}}

#define FL_DEBUG_BEGIN if (fl::fuzzylite::isDebugging()){
#define FL_DEBUG_END }

#define FL_DBG(message) FL_DEBUG_BEGIN\
        std::cout << FL__FILE__ << "::" << __FUNCTION__ << "[" << __LINE__ << "]:" \
                << message << std::endl;\
        FL_DEBUG_END


#ifdef FL_WINDOWS
#include <ciso646> //alternative operator spellings:
//#define and &&
//#define or ||
//#define not !
//#define bitand &
//#define bitor |

//@todo: Address warning 4251 by exporting members?
//http://www.unknownroad.com/rtfm/VisualStudio/warningC4251.html
#ifdef _MSC_VER
#pragma warning (disable:4251)
#endif

//fuzzylite as a shared library is exported
//Applications linking with fuzzylite as a shared library need to import

//fuzzylite as a static library does not export or import
//Applications linking with fuzzylite as a static library do not import

#if defined(FL_EXPORT_LIBRARY)
#define FL_API __declspec(dllexport)
#elif defined(FL_IMPORT_LIBRARY)
#define FL_API __declspec(dllimport)
#else
#define FL_API
#endif

#else
#define FL_API
#endif

/**
  The fl namespace is the namespace where all the classes of the `fuzzylite`
  library are contained in.

  @author Juan Rada-Vilela, Ph.D.
  @since 4.0
 */
namespace fl {
    /**
      Represents floating-point values (typedef to float or double).
     */
#ifdef FL_USE_FLOAT
    typedef float scalar;
#else
    /**
      Represents floating-point values as doubles.
     */
    typedef double scalar;
#endif

#define FL_IUNUSED(x) (void) (x)

#ifdef __GNUC__
#define FL_IUNUSED_DECL __attribute__((unused))
#else
#define FL_IUNUSED_DECL
#endif

    /**
      Represents the Not-A-Number scalar value
     */
    const scalar nan FL_IUNUSED_DECL = std::numeric_limits<scalar>::quiet_NaN();
    /**
      Represents the infinity scalar value
     */
    const scalar inf FL_IUNUSED_DECL = std::numeric_limits<scalar>::infinity();

#ifdef FL_CPP98
    //C++98 defines

    //Pointers
    /**
      Represents the `C++11` or `C++98` null pointer depending on whether the
      compilation flag `-DFL_CPP98` is set
     */
    const long null = 0L;
#define FL_unique_ptr std::auto_ptr
#define FL_move_ptr(x) x

    //Identifiers
#define FL_IOVERRIDE
#define FL_IFINAL
#define FL_IDEFAULT
#define FL_IDELETE
#define FL_INOEXCEPT throw()
#define FL_ITHREAD_LOCAL

    //Constructors
#define FL_DEFAULT_COPY(Class)
#define FL_DEFAULT_MOVE(Class)
#define FL_DEFAULT_COPY_AND_MOVE(Class)

#define FL_DISABLE_COPY(Class) \
    Class(const Class &);\
    Class &operator=(const Class &);

#else
    //C++11 defines

    //Pointers
    /**
      Represents the `C++11` or `C++98` null pointer depending on whether the
      compilation flag `-DFL_CPP98` is set
     */
    const std::nullptr_t null = nullptr;
#define FL_unique_ptr std::unique_ptr
#define FL_move_ptr(x) std::move(x)

    //Identifiers
#define FL_IOVERRIDE override
#define FL_IFINAL final
#define FL_IDEFAULT = default
#define FL_IDELETE = delete
#define FL_INOEXCEPT noexcept
#define FL_ITHREAD_LOCAL /*thread_local (commented for performance)*/

    //Constructors
#define FL_DEFAULT_COPY(Class) \
    Class(const Class&) = default; \
    Class& operator=(const Class&) = default;
#define FL_DEFAULT_MOVE(Class) \
    Class(Class&&) = default; \
    Class& operator=(Class&&) = default;
#define FL_DEFAULT_COPY_AND_MOVE(Class) \
    Class(const Class&) = default; \
    Class& operator=(const Class&) = default;\
    Class(Class&&) = default; \
    Class& operator=(Class&&) = default;

#define FL_DISABLE_COPY(Class) \
    Class(const Class &) = delete;\
    Class &operator=(const Class &) = delete;

#endif

}


namespace fl {

    /**

      The fuzzylite class contains global settings and information about the
      library.

      @author Juan Rada-Vilela, Ph.D.
      @since 4.0

     */

    class FL_API fuzzylite {
        friend class Operation;
    private:
        static int _decimals;
        static scalar _macheps;
        static std::ios_base::fmtflags _scalarFormat;
        static bool _logging;
        static bool _debugging;
    public:
        /**
         Returns the name of the `fuzzylite` library
         @return the name of the `fuzzylite` library
         */
        static std::string name();
        /**
          Returns the version of the `fuzzylite` library
          @return the version of the `fuzzylite` library
         */
        static std::string version();
        /**
          Returns the name of the `fuzzylite` library including the version
          @return the name of the `fuzzylite` library including the version
         */
        static std::string library();

        /**
          Returns the license under which the `fuzzylite` library is released
          @return the license under which the `fuzzylite` library is released
         */
        static std::string license();

        /**
          Returns the name of the author of the `fuzzylite` library
          @return "Juan Rada-Vilela, Ph.D."
         */
        static std::string author();

        /**
          Returns the name of the company that owns the `fuzzylite` library
          @return "FuzzyLite Limited"
         */
        static std::string company();

        /**
          Returns the website of the `fuzzylite` library
          @return "http://www.fuzzylite.com/"
         */
        static std::string website();

        /**
         Returns the number of decimals utilized when formatting scalar values
         @return the number of decimals utilized when formatting scalar values
         (default is 3)
         */
        static int decimals();

        /**
          Sets the number of decimals utilized when formatting scalar values
          @param decimals is the number of decimals utilized when formatting
          scalar values
         */
        static void setDecimals(int decimals);

        /**
        Returns the minimum difference at which two floating-point values
        are considered equivalent
        @return the minimum difference at which two floating-point values
        are considered equivalent (default is 1e-6)
         */
        static scalar macheps();

        /**
          Sets the minimum difference at which two floating-point values are
          considered equivalent
          @param macheps is the minimum difference at which two floating-point
          values are considered equivalent (default is 1e-6)
         */
        static void setMachEps(scalar macheps);

        /**
          Sets the default format to be utilized for every fl::scalar passed to
          Op::str()
          @param scalarFormat is the format to be utilized for every fl::scalar
          passed to Op::str()
         */
        static void setScalarFormat(std::ios_base::fmtflags scalarFormat);

        /**
          Gets the default format to be utilized for every fl::scalar passed to
          Op::str()
          @return the format to be utilized for every fl::scalar passed to Op::str()
         */
        static std::ios_base::fmtflags scalarFormat();

        /**
          Returns whether the library is logging information via the `FL_LOG`
          macro
          @return whether the library is logging information via the `FL_LOG`
          macro
         */
        static bool isLogging();

        /**
          Sets whether the library is set to log information using the macro
          `FL_LOG`
          @param logging indicates whether the library is set to log
          information via the `FL_LOG` macro
         */
        static void setLogging(bool logging);

        /**
          Indicates whether the library is running in debug mode
          @return `true` if the library is running in debug mode, and `false`
          if it is running in release mode
         */
        static bool isDebugging();

        /**
          Sets whether the library is set to run in debug mode
          @param debugging indicates whether the library is set to run in debug mode
         */
        static void setDebugging(bool debugging);

        /**
          Returns the platform under which the `fuzzylite` library was built
          @return `Unix` or `Windows`
         */
        static std::string platform();

        /**
          Returns the name of the type of the floating-point variables
          @return `double` or `float`
         */
        static std::string floatingPoint();
    };
}


namespace fl {

    inline std::string fuzzylite::name() {
        return "fuzzylite";
    }

    inline std::string fuzzylite::library() {
        return name() + " " + version();
    }

    inline std::string fuzzylite::version() {
        return "6.0";
    }

    inline std::string fuzzylite::license() {
        return "FuzzyLite License";
    }

    inline std::string fuzzylite::author() {
        return "Juan Rada-Vilela, Ph.D.";
    }

    inline std::string fuzzylite::company() {
        return "FuzzyLite Limited";
    }

    inline std::string fuzzylite::website() {
        return "http://www.fuzzylite.com/";
    }

    inline void fuzzylite::setDebugging(bool debugging) {
        _debugging = debugging;
    }

    inline bool fuzzylite::isDebugging() {
        return _debugging;
    }

    inline void fuzzylite::setDecimals(int decimals) {
        _decimals = decimals;
    }

    inline int fuzzylite::decimals() {
        return _decimals;
    }

    inline void fuzzylite::setScalarFormat(std::ios_base::fmtflags scalarFormat) {
        _scalarFormat = scalarFormat;
    }

    inline std::ios_base::fmtflags fuzzylite::scalarFormat() {
        return _scalarFormat;
    }

    inline void fuzzylite::setMachEps(scalar macheps) {
        _macheps = macheps;
    }

    inline scalar fuzzylite::macheps() {
        return _macheps;
    }

    inline void fuzzylite::setLogging(bool logging) {
        _logging = logging;
    }

    inline bool fuzzylite::isLogging() {
        return _logging;
    }
}

#endif  /* FL_FUZZYLITE_H */