Quick.RTTI.Utils.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. { ***************************************************************************
  2. Copyright (c) 2016-2019 Kike Pérez
  3. Unit : Quick.RTTI.Utils
  4. Description : Files functions
  5. Author : Kike Pérez
  6. Version : 1.5
  7. Created : 09/03/2018
  8. Modified : 20/02/2019
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.RTTI.Utils;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. SysUtils,
  26. Rtti;
  27. type
  28. TRTTI = class
  29. private class var
  30. fCtx : TRttiContext;
  31. public
  32. //class function GetProperties();
  33. class function GetField(aInstance : TObject; const aFieldName : string) : TRttiField; overload;
  34. class function GetField(aTypeInfo : Pointer; const aFieldName : string) : TRttiField; overload;
  35. class function FieldExists(aTypeInfo : Pointer; const aFieldName : string) : Boolean;
  36. class function GetFieldValue(aInstance : TObject; const aFieldName : string) : TValue; overload;
  37. class function GetFieldValue(aTypeInfo : Pointer; const aFieldName: string) : TValue; overload;
  38. class function GetProperty(aInstance : TObject; const aPropertyName : string) : TRttiProperty; overload;
  39. class function GetProperty(aTypeInfo : Pointer; const aPropertyName : string) : TRttiProperty; overload;
  40. class function PropertyExists(aTypeInfo : Pointer; const aPropertyName : string) : Boolean;
  41. class function GetPropertyValue(aInstance : TObject; const aPropertyName : string) : TValue; overload;
  42. class function GetPropertyValue(aTypeInfo : Pointer; const aPropertyName : string) : TValue; overload;
  43. class function FindClass(const aClassName: string): TClass;
  44. end;
  45. ERTTIError = class(Exception);
  46. implementation
  47. { TRTTIUtils }
  48. class function TRTTI.FieldExists(aTypeInfo: Pointer; const aFieldName: string): Boolean;
  49. var
  50. rtype : TRttiType;
  51. begin
  52. rtype := fCtx.GetType(aTypeInfo);
  53. Result := rtype.GetField(aFieldName) <> nil;
  54. end;
  55. class function TRTTI.GetField(aInstance: TObject; const aFieldName: string): TRttiField;
  56. var
  57. rtype : TRttiType;
  58. begin
  59. rtype := fCtx.GetType(aInstance.ClassInfo);
  60. if rtype <> nil then
  61. begin
  62. Result := rtype.GetField(aFieldName);
  63. end;
  64. end;
  65. class function TRTTI.GetField(aTypeInfo: Pointer; const aFieldName: string): TRttiField;
  66. var
  67. rtype : TRttiType;
  68. begin
  69. rtype := fCtx.GetType(aTypeInfo);
  70. if rtype <> nil then
  71. begin
  72. Result := rtype.GetField(aFieldName);
  73. end;
  74. end;
  75. class function TRTTI.GetFieldValue(aInstance : TObject; const aFieldName: string): TValue;
  76. var
  77. rfield: TRttiField;
  78. begin
  79. rfield := GetField(aInstance,aFieldName);
  80. if rfield <> nil then Result := rfield.GetValue(aInstance);
  81. end;
  82. class function TRTTI.GetFieldValue(aTypeInfo : Pointer; const aFieldName: string): TValue;
  83. var
  84. rfield: TRttiField;
  85. begin
  86. rfield := GetField(aTypeInfo,aFieldName);
  87. if rfield <> nil then rfield.GetValue(aTypeInfo);
  88. end;
  89. class function TRTTI.GetProperty(aInstance: TObject; const aPropertyName: string): TRttiProperty;
  90. var
  91. rtype : TRttiType;
  92. begin
  93. rtype := fCtx.GetType(aInstance.ClassInfo);
  94. if rtype <> nil then Result := rtype.GetProperty(aPropertyName);
  95. end;
  96. class function TRTTI.GetProperty(aTypeInfo: Pointer; const aPropertyName: string): TRttiProperty;
  97. var
  98. rtype : TRttiType;
  99. begin
  100. rtype := fCtx.GetType(aTypeInfo);
  101. if rtype <> nil then Result := rtype.GetProperty(aPropertyName);
  102. end;
  103. class function TRTTI.GetPropertyValue(aInstance: TObject; const aPropertyName: string): TValue;
  104. var
  105. rprop : TRttiProperty;
  106. begin
  107. rprop := GetProperty(aInstance,aPropertyName);
  108. if rprop <> nil then Result := rprop.GetValue(aInstance);
  109. end;
  110. class function TRTTI.GetPropertyValue(aTypeInfo: Pointer; const aPropertyName: string): TValue;
  111. var
  112. rprop : TRttiProperty;
  113. begin
  114. rprop := GetProperty(aTypeInfo,aPropertyName);
  115. if rprop <> nil then Result := rprop.GetValue(aTypeInfo);
  116. end;
  117. class function TRTTI.PropertyExists(aTypeInfo: Pointer; const aPropertyName: string): Boolean;
  118. begin
  119. Result := fCtx.GetType(aTypeInfo).GetProperty(aPropertyName) <> nil;
  120. end;
  121. class function TRTTI.FindClass(const aClassName: string): TClass;
  122. var
  123. rType : TRttiType;
  124. rList : TArray<TRttiType>;
  125. begin
  126. Result := nil;
  127. rList := fCtx.GetTypes;
  128. for rType in rList do
  129. begin
  130. if (rType.IsInstance) and (aClassName.EndsWith(rType.Name)) then
  131. begin
  132. Result := rType.AsInstance.MetaClassType;
  133. Break;
  134. end;
  135. end;
  136. end;
  137. end.