Source Code for Me (s-c.me)

Allows you to paste souce code to blogs! Adapted for Twitter! Here is Search Form in case you missed your code.
Code:
Selected Language:
Show Linenumbers:
Short link for Twitter:
HTML:

HTML view:

Copy Source | Copy HTML
  1. struct BaseType
  2. {
  3.     bool IsStored;
  4.     enum BaseValueState {bvsStore, bvsSet, bvsGet, bvsErase};
  5.     BaseType():IsStored(false)
  6.     {
  7.     }
  8.     virtual ~BaseType()
  9.     {
  10.         if(IsStored)
  11.         {
  12.             int tempInt = int();
  13.             double tempDbl = double();
  14.             UnicodeString tempStr;
  15.             FieldStorage("", tempInt, bvsErase);
  16.             FieldStorage("", tempStr, bvsErase);
  17.             FieldStorage("", tempDbl, bvsErase);
  18.         }
  19.     }
  20.     virtual void StoreValue() =  0;
  21.     virtual void Free() =  0;
  22.  
  23.     template <class T>
  24.         void FieldStorage(const UnicodeString &Tag, T& Value, 
                                    BaseValueState State = bvsStore)
  25.         {
  26.             typedef std::map<void*, std::map<UnicodeString, T*> > FieldMap;
  27.             static FieldMap Values;
  28.             static T Empty = T();
  29.             if (!IsStored)
  30.                 IsStored = true;
  31.             switch(State)
  32.             {
  33.                 case bvsStore:
  34.                     Values[this][Tag] = &Value;
  35.                     break;
  36.                 case bvsSet:
  37.                     {
  38.                         T* Ptr = Values[this][Tag];
  39.                         if (Ptr)
  40.                             *Ptr = Value;
  41.                     }
  42.                     break;
  43.                 case bvsGet:
  44.                     {
  45.                         T* Ptr = Values[this][Tag];
  46.                         Value = (Ptr) ? *Ptr : Empty;
  47.                     }
  48.                     break;
  49.                 case bvsErase:
  50.                     Values.erase(this);
  51.                     break;
  52.             }
  53.         }
  54.  
  55.     template <typename T>
  56.         void GetField(const UnicodeString& Tag, T& Value)
  57.         {
  58.             FieldStorage(Tag, Value, bvsGet);
  59.         }
  60.  
  61.     template <typename T>
  62.         void SetField(const UnicodeString& Tag, T& Value)
  63.         {
  64.             FieldStorage(Tag, Value, bvsSet);
  65.         }
  66.  
  67.     template <typename T>
  68.         T GetField(const UnicodeString& Tag)
  69.         {
  70.             T Value;
  71.             FieldStorage(Tag, Value, bvsGet);
  72.             return Value;
  73.         }
  74. };




Based on Manoli.Net's CodeFormatter. Made by Topbot (c) 2008-2012