home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 02 Useful Techniques / 08 Zarozinski / src / ffllapi / FFLLBase.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-05  |  7.6 KB  |  235 lines

  1. //
  2. // File:        FFLLBase.h    
  3. //
  4. // Purpose:        This file contains the common classes/structs/etc for FFLL
  5. //
  6. // Copyright ⌐ 2000-2001 Louder Than A Bomb! Software
  7. //
  8. // This file is part of the FFLL (Free Fuzzy Logic Library) project (http://ffll.sourceforge.net)
  9. // It is released under the BSD license, see http://ffll.sourceforge.net/license.txt for the full text.
  10. //
  11.  
  12.  
  13. // The debugger can't handle symbols more than 255 characters long.
  14. // STL often creates symbols longer than that.
  15. // When symbols are longer than 255 characters, the warning is disabled.
  16. #pragma warning(disable:4786)
  17. // disable "did not inline" warnings 
  18. #pragma warning(disable:4710) 
  19.  
  20. #ifndef _FFLLBASE_H
  21. #define _FFLLBASE_H
  22.  
  23. // define the numbers that identify warning and error messages 
  24. // each class of message has a base offsets.  We subtract
  25. // this value from the value to get the index into the approprite array
  26.  
  27. // array of text
  28. #define LITERAL_BASE    0
  29. #define STR_NEW            LITERAL_BASE + 0
  30. #define STR_VARIABLE    LITERAL_BASE + 1
  31. #define STR_SET            LITERAL_BASE + 2
  32. #define STR_OUTPUT        LITERAL_BASE + 3
  33. #define STR_COPY_OF        LITERAL_BASE + 4
  34.  
  35.  
  36. #define ERROR_BASE        2000
  37. #define ERR_VAR_NON_UNIQUE_ID        ERROR_BASE + 0
  38. #define ERR_SET_NON_UNIQUE_ID        ERROR_BASE + 1
  39. #define ERR_SAME_LEFT_RIGHT_VALS    ERROR_BASE + 2
  40. #define ERR_INVALID_FILE_FORMAT        ERROR_BASE + 3
  41. #define ERR_CANT_DEL_OUTPUT_VAR        ERROR_BASE + 4
  42. #define ERR_EOF_READING_VARS        ERROR_BASE + 5
  43. #define ERR_EOF_READING_RULES        ERROR_BASE + 6
  44. #define ERR_EOF_READING_SETS        ERROR_BASE + 7
  45. #define ERR_OUT_VAR_EXISTS            ERROR_BASE + 8
  46. #define ERR_ALLOC_MEM                ERROR_BASE + 9
  47. #define ERR_INVALID_DEFUZZ_MTHD        ERROR_BASE + 10
  48. #define ERR_INVALID_COMP_MTHD        ERROR_BASE + 11
  49. #define ERR_INVALID_INFERENCE_MTHD    ERROR_BASE + 12
  50. #define ERR_OPENING_FILE            ERROR_BASE + 13
  51. #define ERR_VAR_MIN_VALUE            ERROR_BASE + 14
  52. #define ERR_VAR_MAX_VALUE            ERROR_BASE + 15
  53.  
  54.  
  55. #define WARNING_BASE        4000
  56.  
  57.  
  58. // these are used to export FFLL classes.  The standard API
  59. // is exported via a .DEF file because that is the most flexible
  60. // way and we can avoid name mangling
  61.  
  62. #ifdef _STATIC_LIB
  63. #    define FFLL_API 
  64. #else
  65. #    ifdef FFLL_EXPORTS
  66. #        define FFLL_API __declspec(dllexport)
  67. #    else
  68. #        define FFLL_API __declspec(dllimport)
  69. #    endif
  70. #endif // not _STATIC_LIB
  71.  
  72.  
  73. // if FFLL is in a DLL we do not want to have inline functions.  They would
  74. // require the program using the library to be re linked.  Without inlines
  75. // you can just drop in the new dll and everything will work (assuming you
  76. // only changed the function's implementation and not it's declaration)
  77. // Note that we avoid inlining virtual functions cuz "itÆs best not to use 
  78. // inline virtual functions, since theyÆre almost never expanded anyway." 
  79. // according to http://msdn.microsoft.com/msdnmag/issues/0600/c/c0600.asp
  80. //
  81. // NOTE: That inlining for FFLL doesn't work when using Microsoft MSVC
  82. // because we define the functions in the .cpp file and MSVC does not like that
  83. #ifdef USE_INLINE
  84. #    define FFLL_INLINE inline
  85. #else
  86. #    define FFLL_INLINE 
  87. #endif 
  88.  
  89.  
  90. #include <string>
  91. #include <assert.h>
  92. #include <float.h> // needed for FLT_MIN
  93.  
  94.  
  95. FFLL_API char* convert_to_ascii(const wchar_t* wstr, char replace_space = -1 );
  96. FFLL_API wchar_t* convert_to_wide_char(const char* astr);
  97.  
  98. namespace FFLLDatatypes 
  99. {
  100. const int OUTPUT_IDX = -1; // constant for output variable
  101.  
  102. // We're using unsigned char for the rules array cuz it's the smallest we can (easily) implement right now
  103. // we could shrink the memory footprint in 1/2 if we used bytes to store the info.  This would limit
  104. // us to 16 output sets... probably a reasonable amount but may be to limited.
  105. typedef unsigned char  RuleArrayType;
  106.  
  107. // using float for the variable's left/right values can cause a loss of precision so we'll typedef
  108. // a datatype...
  109. typedef double RealType;
  110.  
  111. const RuleArrayType NO_RULE = 255;    // 255 is the MAX that an unsigned char can hold - so this means we're
  112.                                     // limited to 255 set in the output variable... a limitation I believe
  113.                                     // we can live with
  114.  
  115. // Typedef for the number of elements in the values[] array
  116. typedef unsigned short ValuesArrCountType;
  117.  
  118. // The DOM (Degree of Membership) is a value between 0 and MAX_DOM.  we could
  119. // have used a float and represened (as most FL systems do) the value from 0 to 1
  120. // but that would just be a waste of memory.  Using an unsigned char gives us
  121. // up to 256 values in only 8 bits.  Not too bad!  A Float would cost us (at least)
  122. // 4 bytes!
  123. typedef unsigned char DOMType;
  124.      
  125. // create classes for the 'x' and 'y' node points,
  126. // this allows us to overload the '=' operator and EASILY
  127. // perform sanity checks.
  128.  
  129. // NOTE: The assignment operator has some additional restrictions. 
  130. // It can be overloaded only as a nonstatic member function, not 
  131. // as a friend function. It is the only operator that cannot be 
  132. // inherited; a derived class cannot use a base class's assignment operator. 
  133.  
  134. class FFLL_API NodeValue
  135. {
  136.     public:
  137.         operator int();
  138.          int& operator=(const int& _value);
  139.         int& operator+=(const int& _value);
  140.         int& operator-=(const int& _value);
  141.         void operator<<(const int& _value);
  142.  
  143.     private:
  144.          virtual void validate() = 0;
  145.  
  146.     protected:
  147.              int value;
  148.  
  149. }; // end class NodeValue
  150.  
  151. class FFLL_API XNodeValue : public NodeValue
  152. {
  153.     public:
  154.          int& operator=(const int& _value); // can't be inherited
  155.  
  156.     private:
  157.         void validate();
  158.  
  159. }; // end class XNodeValue
  160.  
  161. class FFLL_API YNodeValue : public NodeValue
  162. {
  163.     public:
  164.          int& operator=(const int& _value); // can't be inherited
  165.  
  166.     private:
  167.         void validate();
  168.  
  169. }; // end class YNodeValue
  170.  
  171. class FFLL_API NodePoint 
  172. {
  173.     public:
  174.         XNodeValue x;
  175.         YNodeValue y;
  176.     
  177. }; // end class NodePoint
  178.  
  179.  
  180. // define our own point struct to use rather than rely on
  181. // something like POINT that requires windef.h
  182. typedef struct _point_struct
  183. {
  184.     int x;
  185.     int y;
  186. } _point;
  187.  
  188. }; // end namespace FFLLDatatypes
  189.  
  190. using namespace FFLLDatatypes ;
  191.  
  192. // Class:  FFLLBase
  193. //
  194. // This is the base class for all FFLL classes. It stores the parent of the object
  195. // so we can get the parent to go up the model hierarchy and holds a string that contains
  196. // an error or warning message.
  197. // Note:  Since each object has it's own msg_text string, to report an error you need to
  198. // propigate it up the classes.  For example if an error occurs in a set the calling
  199. // function should detect the error and set the msg_text for the set's variable, then the caller
  200. // for the variable object should set it for the model object - until it's shown to the user.
  201. //
  202.  
  203. class FFLL_API FFLLBase
  204. {
  205.  
  206.     public:
  207.  
  208.         FFLLBase();    // This has NO body. This (DEFAULT constructor) should not be called so we're explicitly disallowing it
  209.         FFLLBase(void* _parent);
  210.  
  211.          const wchar_t* get_msg_text() const;
  212.  
  213.         void set_msg_text(int msg_id) const;
  214.         void set_msg_text(const std::wstring _text) const;
  215.         void set_msg_text(const wchar_t* _text = NULL) const;
  216.  
  217.     protected:
  218.  
  219.         void* get_parent() const;
  220.         const wchar_t* load_string(int str_id) const;
  221.  
  222.     private:
  223.  
  224.         // vars are 'mutable' so we can write an error in a func that's declared as 'const'.
  225.         // This enables us to be able to write out errors when appropriate and still declare
  226.         // funcs as 'const' who wouldn't modify anything in the object if no error occured
  227.         mutable bool            error_read;    // indicates if the current error has been read or not.  If it's been
  228.                                             // read, we can assume it's been handled.
  229.         mutable std::wstring    msg_text;    // error or warning message
  230.         void*                    parent;        // points to the parent. 
  231.  
  232. }; // end class FFLLBase
  233.  
  234. #endif // _FFLLBASE_H
  235.