arezey

config.h

Mar 3rd, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.45 KB | None | 0 0
  1. #ifndef __OPTIONS_H__
  2. #define __OPTIONS_H__
  3.  
  4. #include "common.h"
  5. #include "str.h"
  6. #include "color.h"
  7.  
  8. // =============================================================================
  9. // Determine configuration file. Use APPNAME if given.
  10. #ifdef APPNAME
  11.  #define CONFIGFILE APPNAME ".ini"
  12. #else // APPNAME
  13.  #define APPNAME "(unnamed application)"
  14.  #define CONFIGFILE "config.ini"
  15. #endif // APPNAME
  16.  
  17. // -------------------------------
  18. #define CFGSECTNAME(X) CFGSECT_##X
  19.  
  20. #define MAX_INI_LINE 512
  21. #define NUM_CONFIG (sizeof config::pointers / sizeof *config::pointers)
  22.  
  23. // =============================================================================
  24. enum configsection_e {
  25. #define CFG(...)
  26. #define SECT(A,B) CFGSECTNAME (A),
  27.  #include "cfgdef.h"
  28. #undef CFG
  29. #undef SECT
  30.     NUM_ConfigSections,
  31.     NO_CONFIG_SECTION = -1
  32. };
  33.  
  34. // =============================================================================
  35. enum configtype_e {
  36.     CONFIG_none,
  37.     CONFIG_int,
  38.     CONFIG_str,
  39.     CONFIG_float,
  40.     CONFIG_bool,
  41.     CONFIG_color,
  42. };
  43.  
  44. // =========================================================
  45. class config {
  46. public:
  47.     configsection_e sect;
  48.     const char* description, *name, *fullname, *typestring, *defaultstring;
  49.    
  50.     virtual configtype_e getType () {
  51.         return CONFIG_none;
  52.     }
  53.    
  54.     virtual void resetValue () {}
  55.    
  56.     // ------------------------------------------
  57.     static bool load ();
  58.     static bool save ();
  59.     static void reset ();
  60.     static config* pointers[];
  61.     static const char* sections[];
  62.     static const char* sectionNames[];
  63.     static str dirpath ();
  64.     static str filepath ();
  65. };
  66.  
  67. // =============================================================================
  68. #define DEFINE_UNARY_OPERATOR(T, OP) \
  69.     T operator OP () { \
  70.         return (OP value); \
  71.     } \
  72.  
  73. #define DEFINE_BINARY_OPERATOR(T, OP) \
  74.     T operator OP (const T other) { \
  75.         return (value OP other); \
  76.     } \
  77.  
  78. #define DEFINE_ASSIGN_OPERATOR(T, OP) \
  79.     T& operator OP (const T other) { \
  80.         return (value OP other); \
  81.     } \
  82.  
  83. #define DEFINE_COMPARE_OPERATOR(T, OP) \
  84.     bool operator OP (const T other) { \
  85.         return (value OP other); \
  86.     } \
  87.  
  88. #define DEFINE_CAST_OPERATOR(T) \
  89.     operator R () { \
  90.         return (R) (value); \
  91.     } \
  92.  
  93. #define DEFINE_ALL_COMPARE_OPERATORS(T) \
  94.     DEFINE_COMPARE_OPERATOR (T, ==) \
  95.     DEFINE_COMPARE_OPERATOR (T, !=) \
  96.     DEFINE_COMPARE_OPERATOR (T, >) \
  97.     DEFINE_COMPARE_OPERATOR (T, <) \
  98.     DEFINE_COMPARE_OPERATOR (T, >=) \
  99.     DEFINE_COMPARE_OPERATOR (T, <=) \
  100.  
  101. #define CONFIGTYPE(T) \
  102. class T##config : public config
  103.  
  104. #define IMPLEMENT_CONFIG(T) \
  105. public: \
  106.     T value, defval; \
  107.     \
  108.     T##config (const configsection_e _sect, const char* _description, \
  109.         T _defval, const char* _name, const char* _fullname, const char* _typestring, \
  110.         const char* _defaultstring) \
  111.     { \
  112.         sect = _sect; \
  113.         description = _description; \
  114.         value = defval = _defval; \
  115.         name = _name; \
  116.         fullname = _fullname; \
  117.         typestring = _typestring; \
  118.         defaultstring = _defaultstring; \
  119.     } \
  120.     operator T () { \
  121.         return value; \
  122.     } \
  123.     configtype_e getType () { \
  124.         return CONFIG_##T; \
  125.     } \
  126.     void resetValue () { \
  127.         value = defval; \
  128.     } \
  129.     \
  130.  
  131. // =============================================================================
  132. CONFIGTYPE (int) {
  133.     IMPLEMENT_CONFIG (int)
  134.    
  135.     // Int-specific operators
  136.     DEFINE_ALL_COMPARE_OPERATORS (int)
  137.     DEFINE_BINARY_OPERATOR (int, -)
  138.     DEFINE_BINARY_OPERATOR (int, *)
  139.     DEFINE_UNARY_OPERATOR (int, !)
  140.     DEFINE_ASSIGN_OPERATOR (int, =)
  141.     DEFINE_ASSIGN_OPERATOR (int, +=)
  142.     DEFINE_ASSIGN_OPERATOR (int, -=)
  143.     DEFINE_ASSIGN_OPERATOR (int, *=)
  144.     DEFINE_BINARY_OPERATOR (int, +)
  145.     DEFINE_BINARY_OPERATOR (int, /)
  146.     DEFINE_BINARY_OPERATOR (int, ^)
  147.     DEFINE_BINARY_OPERATOR (int, %)
  148.     DEFINE_BINARY_OPERATOR (int, >>)
  149.     DEFINE_BINARY_OPERATOR (int, <<)
  150.     DEFINE_ASSIGN_OPERATOR (int, >>=)
  151.     DEFINE_ASSIGN_OPERATOR (int, <<=)
  152.     DEFINE_UNARY_OPERATOR (int, ~)
  153.     DEFINE_UNARY_OPERATOR (int, -)
  154. };
  155.  
  156. // =============================================================================
  157. CONFIGTYPE (str) {
  158.     IMPLEMENT_CONFIG (str)
  159.    
  160.     DEFINE_ALL_COMPARE_OPERATORS (str)
  161.     DEFINE_BINARY_OPERATOR (str, -)
  162.     DEFINE_BINARY_OPERATOR (str, *)
  163.     DEFINE_UNARY_OPERATOR (str, !)
  164.     DEFINE_ASSIGN_OPERATOR (str, =)
  165.     DEFINE_ASSIGN_OPERATOR (str, +=)
  166.     DEFINE_ASSIGN_OPERATOR (str, -=)
  167.     DEFINE_ASSIGN_OPERATOR (str, *=)
  168.    
  169.     operator QString () {
  170.         return QString (value.chars());
  171.     }
  172. };
  173.  
  174. // =============================================================================
  175. CONFIGTYPE (float) {
  176.     IMPLEMENT_CONFIG (float)
  177.    
  178.     DEFINE_ALL_COMPARE_OPERATORS (float)
  179.     DEFINE_BINARY_OPERATOR (float, +)
  180.     DEFINE_BINARY_OPERATOR (float, -)
  181.     DEFINE_BINARY_OPERATOR (float, *)
  182.     DEFINE_UNARY_OPERATOR (float, !)
  183.     DEFINE_ASSIGN_OPERATOR (float, =)
  184.     DEFINE_ASSIGN_OPERATOR (float, +=)
  185.     DEFINE_ASSIGN_OPERATOR (float, -=)
  186.     DEFINE_ASSIGN_OPERATOR (float, *=)
  187. };
  188.  
  189. // =============================================================================
  190. CONFIGTYPE (bool) {
  191.     IMPLEMENT_CONFIG (bool)
  192.     DEFINE_ALL_COMPARE_OPERATORS (bool)
  193. };
  194.  
  195. // =============================================================================
  196. CONFIGTYPE (color) {
  197.     IMPLEMENT_CONFIG (color)
  198.     // DEFINE_COMPARE_OPERATOR (color, ==)
  199.     // DEFINE_COMPARE_OPERATOR (color, !=)
  200. };
  201.  
  202. // =============================================================================
  203. // Extern the configurations now
  204. #define CFG(TYPE, SECT, NAME, DESCR, DEFAULT) extern TYPE##config SECT##_##NAME;
  205. #define SECT(...)
  206.  #include "cfgdef.h"
  207. #undef CFG
  208. #undef SECT
  209.  
  210. #endif // __OPTIONS_H__
Advertisement
Add Comment
Please, Sign In to add comment