CefValue.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Class that wraps other data value types. Complex types (binary, dictionary
  10. /// and list) will be referenced but not owned by this object. Can be used on any
  11. /// process and thread.
  12. /// </summary>
  13. public sealed unsafe partial class CefValue
  14. {
  15. /// <summary>
  16. /// Creates a new object.
  17. /// </summary>
  18. public static CefValue Create()
  19. {
  20. return CefValue.FromNative(cef_value_t.create());
  21. }
  22. /// <summary>
  23. /// Returns true if the underlying data is valid. This will always be true for
  24. /// simple types. For complex types (binary, dictionary and list) the
  25. /// underlying data may become invalid if owned by another object (e.g. list or
  26. /// dictionary) and that other object is then modified or destroyed. This value
  27. /// object can be re-used by calling Set*() even if the underlying data is
  28. /// invalid.
  29. /// </summary>
  30. public bool IsValid
  31. {
  32. get
  33. {
  34. return cef_value_t.is_valid(_self) != 0;
  35. }
  36. }
  37. /// <summary>
  38. /// Returns true if the underlying data is owned by another object.
  39. /// </summary>
  40. public bool IsOwned
  41. {
  42. get
  43. {
  44. return cef_value_t.is_owned(_self) != 0;
  45. }
  46. }
  47. /// <summary>
  48. /// Returns true if the underlying data is read-only. Some APIs may expose
  49. /// read-only objects.
  50. /// </summary>
  51. public bool IsReadOnly
  52. {
  53. get
  54. {
  55. return cef_value_t.is_read_only(_self) != 0;
  56. }
  57. }
  58. /// <summary>
  59. /// Returns true if this object and |that| object have the same underlying
  60. /// data. If true modifications to this object will also affect |that| object
  61. /// and vice-versa.
  62. /// </summary>
  63. public bool IsSame(CefValue that)
  64. {
  65. return cef_value_t.is_same(_self, that.ToNative()) != 0;
  66. }
  67. /// <summary>
  68. /// Returns true if this object and |that| object have an equivalent underlying
  69. /// value but are not necessarily the same object.
  70. /// </summary>
  71. public bool IsEqual(CefValue that)
  72. {
  73. return cef_value_t.is_equal(_self, that.ToNative()) != 0;
  74. }
  75. /// <summary>
  76. /// Returns a copy of this object. The underlying data will also be copied.
  77. /// </summary>
  78. public CefValue Copy()
  79. {
  80. return CefValue.FromNative(
  81. cef_value_t.copy(_self)
  82. );
  83. }
  84. /// <summary>
  85. /// Returns the underlying value type.
  86. /// </summary>
  87. public CefValueType GetValueType()
  88. {
  89. return cef_value_t.get_type(_self);
  90. }
  91. /// <summary>
  92. /// Returns the underlying value as type bool.
  93. /// </summary>
  94. public bool GetBool()
  95. {
  96. return cef_value_t.get_bool(_self) != 0;
  97. }
  98. /// <summary>
  99. /// Returns the underlying value as type int.
  100. /// </summary>
  101. public int GetInt()
  102. {
  103. return cef_value_t.get_int(_self);
  104. }
  105. /// <summary>
  106. /// Returns the underlying value as type double.
  107. /// </summary>
  108. public double GetDouble()
  109. {
  110. return cef_value_t.get_double(_self);
  111. }
  112. /// <summary>
  113. /// Returns the underlying value as type string.
  114. /// </summary>
  115. public string GetString()
  116. {
  117. var n_result = cef_value_t.get_string(_self);
  118. return cef_string_userfree.ToString(n_result);
  119. }
  120. /// <summary>
  121. /// Returns the underlying value as type binary. The returned reference may
  122. /// become invalid if the value is owned by another object or if ownership is
  123. /// transferred to another object in the future. To maintain a reference to
  124. /// the value after assigning ownership to a dictionary or list pass this
  125. /// object to the SetValue() method instead of passing the returned reference
  126. /// to SetBinary().
  127. /// </summary>
  128. public CefBinaryValue GetBinary()
  129. {
  130. return CefBinaryValue.FromNative(
  131. cef_value_t.get_binary(_self)
  132. );
  133. }
  134. /// <summary>
  135. /// Returns the underlying value as type dictionary. The returned reference may
  136. /// become invalid if the value is owned by another object or if ownership is
  137. /// transferred to another object in the future. To maintain a reference to
  138. /// the value after assigning ownership to a dictionary or list pass this
  139. /// object to the SetValue() method instead of passing the returned reference
  140. /// to SetDictionary().
  141. /// </summary>
  142. public CefDictionaryValue GetDictionary()
  143. {
  144. return CefDictionaryValue.FromNative(
  145. cef_value_t.get_dictionary(_self)
  146. );
  147. }
  148. /// <summary>
  149. /// Returns the underlying value as type list. The returned reference may
  150. /// become invalid if the value is owned by another object or if ownership is
  151. /// transferred to another object in the future. To maintain a reference to
  152. /// the value after assigning ownership to a dictionary or list pass this
  153. /// object to the SetValue() method instead of passing the returned reference
  154. /// to SetList().
  155. /// </summary>
  156. public CefListValue GetList()
  157. {
  158. return CefListValue.FromNative(
  159. cef_value_t.get_list(_self)
  160. );
  161. }
  162. /// <summary>
  163. /// Sets the underlying value as type null. Returns true if the value was set
  164. /// successfully.
  165. /// </summary>
  166. public bool SetNull()
  167. {
  168. return cef_value_t.set_null(_self) != 0;
  169. }
  170. /// <summary>
  171. /// Sets the underlying value as type bool. Returns true if the value was set
  172. /// successfully.
  173. /// </summary>
  174. public bool SetBool(bool value)
  175. {
  176. return cef_value_t.set_bool(_self, value ? 1 : 0) != 0;
  177. }
  178. /// <summary>
  179. /// Sets the underlying value as type int. Returns true if the value was set
  180. /// successfully.
  181. /// </summary>
  182. public bool SetInt(int value)
  183. {
  184. return cef_value_t.set_int(_self, value) != 0;
  185. }
  186. /// <summary>
  187. /// Sets the underlying value as type double. Returns true if the value was set
  188. /// successfully.
  189. /// </summary>
  190. public bool SetDouble(double value)
  191. {
  192. return cef_value_t.set_double(_self, value) != 0;
  193. }
  194. /// <summary>
  195. /// Sets the underlying value as type string. Returns true if the value was set
  196. /// successfully.
  197. /// </summary>
  198. public bool SetString(string value)
  199. {
  200. fixed (char* value_str = value)
  201. {
  202. var n_value = new cef_string_t(value_str, value != null ? value.Length : 0);
  203. return cef_value_t.set_string(_self, &n_value) != 0;
  204. }
  205. }
  206. /// <summary>
  207. /// Sets the underlying value as type binary. Returns true if the value was set
  208. /// successfully. This object keeps a reference to |value| and ownership of the
  209. /// underlying data remains unchanged.
  210. /// </summary>
  211. public bool SetBinary(CefBinaryValue value)
  212. {
  213. return cef_value_t.set_binary(_self, value.ToNative()) != 0;
  214. }
  215. /// <summary>
  216. /// Sets the underlying value as type dict. Returns true if the value was set
  217. /// successfully. This object keeps a reference to |value| and ownership of the
  218. /// underlying data remains unchanged.
  219. /// </summary>
  220. public bool SetDictionary(CefDictionaryValue value)
  221. {
  222. return cef_value_t.set_dictionary(_self, value.ToNative()) != 0;
  223. }
  224. /// <summary>
  225. /// Sets the underlying value as type list. Returns true if the value was set
  226. /// successfully. This object keeps a reference to |value| and ownership of the
  227. /// underlying data remains unchanged.
  228. /// </summary>
  229. public bool SetList(CefListValue value)
  230. {
  231. return cef_value_t.set_list(_self, value.ToNative()) != 0;
  232. }
  233. }
  234. }