Advertisement
snake5

A Prototyping Library

Dec 19th, 2012
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1.  
  2. /*
  3.     You know how hard it is to get back to structural design while you're just trying to make things work?
  4.     Always getting your ideas hammered down by the subconscious telling you to keep data in mind?
  5.     Consider your problems solved with the following prototype library (must be adjusted to fit your templated containers).
  6.     The main idea is to keep one data variable, like this: "PVar Data;", and keep all of the other variables in it.
  7.     Just like you'd do (even if you don't know it) in a scripting language.
  8.     The retrieval and storage are slightly more complicated, though. But feel free to expand and simplify on that.
  9.     This should solve all data problems at certain performance costs which can be easily removed once the full design is created!
  10. */
  11.  
  12. #pragma once
  13.  
  14. #include "SGUtils.h"
  15.  
  16.  
  17. /*
  18.     Prototyping library
  19. usage:
  20.     PVar Data;
  21.     --
  22.     int item = Data.Get( "item" ).GetInt();
  23.     Data.Get( "item" ).SetInt( item );
  24. */
  25.  
  26. enum PVarType
  27. {
  28.     PVT_Null = 0,
  29.     PVT_Int,
  30.     PVT_Real,
  31.     PVT_Vec2,
  32.     PVT_String,
  33.     PVT_Dict,
  34.     PVT_Array,
  35. };
  36.  
  37. struct PVarObj;
  38. struct PVar
  39. {
  40.     typedef TArray< PVar > PVArray;
  41.     typedef TMap< TString, PVar > PVDict;
  42.  
  43.     struct Obj
  44.     {
  45.         Obj( PVarType type ) : Type( type ), Int( 0 ), Real( 0 ), Vec2( 0.0f ){}
  46.  
  47.         PVarType Type;
  48.         INT32 Int;
  49.         FLOAT Real;
  50.         mVec2 Vec2;
  51.         TString String;
  52.         PVDict Dict;
  53.         PVArray Array;
  54.        
  55.         void Unload()
  56.         {
  57.             String.Clear();
  58.             Dict.Clear();
  59.             Array.Clear();
  60.         }
  61.     };
  62.  
  63.     TRefPtr< Obj > Data;
  64.  
  65.     PVar(){}
  66.     PVar( Obj* obj ) : Data( obj ){}
  67.  
  68.     // helpers
  69.     static PVar MakeNull(){ return PVar( new Obj( PVT_Null ) ); }
  70.     void Init( PVarType type )
  71.     {
  72.         if( !Data )
  73.             Data = new Obj( type );
  74.     }
  75.     void ChangeType( PVarType type )
  76.     {
  77.         Init( type );
  78.         Data->Unload();
  79.         Data->Type = type;
  80.     }
  81.  
  82.     // base types
  83.     INT32 GetInt(){ return Data() ? Data->Int : 0; }
  84.     FLOAT GetReal(){ return Data() ? Data->Real : 0; }
  85.     mVec2 GetVec2(){ return Data() ? Data->Vec2 : 0.0f; }
  86.     TString GetString(){ return Data() ? Data->String : TString(); }
  87.     void SetInt( INT32 val ){ ChangeType( PVT_Int ); Data->Int = val; }
  88.     void SetReal( FLOAT val ){ ChangeType( PVT_Real ); Data->Real = val; }
  89.     void SetVec2( mVec2 val ){ ChangeType( PVT_Vec2 ); Data->Vec2 = val; }
  90.     void SetStr( const TString& val ){ ChangeType( PVT_String ); Data->String = val; }
  91.  
  92.     // dict
  93.     PVar& Get( const TString& item )
  94.     {
  95.         Init( PVT_Dict );
  96.         if( Data->Type != PVT_Dict )
  97.             return *this;
  98.         return Data->Dict[ item ];
  99.     }
  100.     void Unset( const TString& item )
  101.     {
  102.         Init( PVT_Dict );
  103.         if( Data->Type == PVT_Dict )
  104.             Data->Dict.Erase( item );
  105.     }
  106.  
  107.     // array
  108.     PVArray& Array()
  109.     {
  110.         Init( PVT_Array );
  111.         if( Data->Type != PVT_Array )
  112.             ChangeType( PVT_Array );
  113.         return Data->Array;
  114.     }
  115. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement