C ++と膝の反射

ここでは、構造内のフィールドに名前でアクセスできるようにすることが最近必要になりました-値の書き込み、読み取り。 そして、それが使用されていない場所のパフォーマンスは変わらないように。

起こったことは次のとおりです。

Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );



  1. Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );



  2. Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );



  3. Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );



  4. Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );



  5. Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );



  6. Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );



  7. Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );







完全な秘密は、「魔法の泡」にあります-TUserが継承されるクラスと、1つの関数-StoreValue()。

カットの下の詳細。



重要:このソリューションはクロスプラットフォームであり、STLとテンプレートを「認識する」C ++コンパイラのみが必要です。





最初は-同義語を入力します-そのため、実験の場合-変更が容易になりました。



Copy Source | Copy HTML



  1. typedef std :: string TWString;
  2. typedef __int64 bigint;
  3. const double DBL_NULL = 0 ;
  4. const bigint INT_NULL = -1 ;
  5. const TWString STR_NULL = TWString();




「グッズ」を提供する基本クラス

Copy Source | Copy HTML



  1. struct BaseType
  2. {
  3. bool IsStored;
  4. 列挙型 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. TWString tempStr = TWString ();
  15. FieldStorage( "" 、tempInt、bvsErase);
  16. FieldStorage( "" 、tempStr、bvsErase);
  17. FieldStorage( "" 、tempDbl、bvsErase);
  18. }
  19. }
  20. 仮想ボイド StoreValue()= 0 ;
  21. テンプレート< クラス T >
  22. void FieldStorage( const TWString &Tag、 T &Value、
  23. BaseValueState State = bvsStore)
  24. {
  25. typedef std :: map < void *、std :: map < TWStringT * >> FieldMap;
  26. 静的 FieldMap値。
  27. 静的 T Empty = T ();
  28. if (!IsStored)
  29. IsStored = true ;
  30. スイッチ (状態)
  31. {
  32. ケース bvsStore:
  33. 値[ this ] [Tag] =&Value;
  34. 休憩 ;
  35. ケース bvsSet:
  36. {
  37. T * Ptr =値[ this ] [タグ];
  38. if (Ptr)
  39. * Ptr =値;
  40. }
  41. 休憩 ;
  42. ケース bvsGet:
  43. {
  44. T * Ptr =値[ this ] [タグ];
  45. 値=(Ptr)? * Ptr:空;
  46. }
  47. 休憩 ;
  48. ケース bvsErase:
  49. Values.erase( this );
  50. 休憩 ;
  51. }
  52. }
  53. テンプレート<typename T >
  54. void GetField( const TWString &Tag、 T &Value)
  55. {
  56. FieldStorage(タグ、値、bvsGet);
  57. }
  58. テンプレート<typename T >
  59. void SetFieldconst TWString &Tag、 T &Value)
  60. {
  61. FieldStorage(タグ、値、bvsSet);
  62. }
  63. テンプレート<typename T >
  64. T GetField( const TWString &Tag)
  65. {
  66. T
  67. FieldStorage(タグ、値、bvsGet);
  68. 戻り値;
  69. }
  70. };




そして、ここで彼から継承します。

Copy Source | Copy HTML



  1. struct TUserパブリック BaseType
  2. {
  3. TUserint AId = INT_NULL、 const TWString&ALogin = STR_NULL、 const double ASalary = DBL_NULL)
  4. :ID(AId)、ログイン(ALogin)、給与(ASalary)
  5. {}
  6. bigint id;
  7. TWStringログイン。
  8. 二重給与。
  9. void StoreValue()
  10. {
  11. FieldStorage( "Id" 、Id);
  12. FieldStorage( "ログイン" 、ログイン);
  13. FieldStorage( "Salary" 、Salary);
  14. }
  15. };



All Articles