Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- You know how hard it is to get back to structural design while you're just trying to make things work?
- Always getting your ideas hammered down by the subconscious telling you to keep data in mind?
- Consider your problems solved with the following prototype library (must be adjusted to fit your templated containers).
- The main idea is to keep one data variable, like this: "PVar Data;", and keep all of the other variables in it.
- Just like you'd do (even if you don't know it) in a scripting language.
- The retrieval and storage are slightly more complicated, though. But feel free to expand and simplify on that.
- This should solve all data problems at certain performance costs which can be easily removed once the full design is created!
- */
- #pragma once
- #include "SGUtils.h"
- /*
- Prototyping library
- usage:
- PVar Data;
- --
- int item = Data.Get( "item" ).GetInt();
- Data.Get( "item" ).SetInt( item );
- */
- enum PVarType
- {
- PVT_Null = 0,
- PVT_Int,
- PVT_Real,
- PVT_Vec2,
- PVT_String,
- PVT_Dict,
- PVT_Array,
- };
- struct PVarObj;
- struct PVar
- {
- typedef TArray< PVar > PVArray;
- typedef TMap< TString, PVar > PVDict;
- struct Obj
- {
- Obj( PVarType type ) : Type( type ), Int( 0 ), Real( 0 ), Vec2( 0.0f ){}
- PVarType Type;
- INT32 Int;
- FLOAT Real;
- mVec2 Vec2;
- TString String;
- PVDict Dict;
- PVArray Array;
- void Unload()
- {
- String.Clear();
- Dict.Clear();
- Array.Clear();
- }
- };
- TRefPtr< Obj > Data;
- PVar(){}
- PVar( Obj* obj ) : Data( obj ){}
- // helpers
- static PVar MakeNull(){ return PVar( new Obj( PVT_Null ) ); }
- void Init( PVarType type )
- {
- if( !Data )
- Data = new Obj( type );
- }
- void ChangeType( PVarType type )
- {
- Init( type );
- Data->Unload();
- Data->Type = type;
- }
- // base types
- INT32 GetInt(){ return Data() ? Data->Int : 0; }
- FLOAT GetReal(){ return Data() ? Data->Real : 0; }
- mVec2 GetVec2(){ return Data() ? Data->Vec2 : 0.0f; }
- TString GetString(){ return Data() ? Data->String : TString(); }
- void SetInt( INT32 val ){ ChangeType( PVT_Int ); Data->Int = val; }
- void SetReal( FLOAT val ){ ChangeType( PVT_Real ); Data->Real = val; }
- void SetVec2( mVec2 val ){ ChangeType( PVT_Vec2 ); Data->Vec2 = val; }
- void SetStr( const TString& val ){ ChangeType( PVT_String ); Data->String = val; }
- // dict
- PVar& Get( const TString& item )
- {
- Init( PVT_Dict );
- if( Data->Type != PVT_Dict )
- return *this;
- return Data->Dict[ item ];
- }
- void Unset( const TString& item )
- {
- Init( PVT_Dict );
- if( Data->Type == PVT_Dict )
- Data->Dict.Erase( item );
- }
- // array
- PVArray& Array()
- {
- Init( PVT_Array );
- if( Data->Type != PVT_Array )
- ChangeType( PVT_Array );
- return Data->Array;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement