CefValueHolder.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. namespace Xilium.CefGlue.Common.Shared.Serialization
  3. {
  4. internal class CefValueHolder : CefListWrapper
  5. {
  6. private bool _isReadOnly;
  7. private Action<ICefListValue, int> _setValue;
  8. public CefValueHolder() : base(null, 0)
  9. {
  10. }
  11. public CefValueHolder(CefListValue cefList, int index, bool isReadOnly = false) : base(cefList, index)
  12. {
  13. _isReadOnly = isReadOnly;
  14. }
  15. public override void SetNull()
  16. {
  17. _setValue = (container, index) => container.SetNull(index);
  18. HandleValueSet();
  19. }
  20. public override void SetBool(bool value)
  21. {
  22. _setValue = (container, index) => container.SetBool(index, value);
  23. HandleValueSet();
  24. }
  25. public override void SetInt(int value)
  26. {
  27. _setValue = (container, index) => container.SetInt(index, value);
  28. HandleValueSet();
  29. }
  30. public override void SetDouble(double value)
  31. {
  32. _setValue = (container, index) => container.SetDouble(index, value);
  33. HandleValueSet();
  34. }
  35. public override void SetString(string value)
  36. {
  37. _setValue = (container, index) => container.SetString(index, value);
  38. HandleValueSet();
  39. }
  40. public override void SetBinary(ICefBinaryValue value)
  41. {
  42. _setValue = (container, index) => container.SetBinary(index, value as CefBinaryValue);
  43. HandleValueSet();
  44. }
  45. public override void SetList(ICefListValue value)
  46. {
  47. _setValue = (container, index) => container.SetList(index, value as CefListValue);
  48. HandleValueSet();
  49. }
  50. public override void SetDictionary(ICefDictionaryValue value)
  51. {
  52. _setValue = (container, index) => container.SetDictionary(index, value as CefDictionaryValue);
  53. HandleValueSet();
  54. }
  55. private void HandleValueSet()
  56. {
  57. if (_isReadOnly)
  58. {
  59. throw new InvalidOperationException("This value holder is read only");
  60. }
  61. if (_container != null)
  62. {
  63. _setValue.Invoke(_container, _index);
  64. }
  65. }
  66. public override bool GetBool()
  67. {
  68. return _container.GetBool(_index);
  69. }
  70. public override int GetInt()
  71. {
  72. return _container.GetInt(_index);
  73. }
  74. public override double GetDouble()
  75. {
  76. return _container.GetDouble(_index);
  77. }
  78. public override string GetString()
  79. {
  80. return _container.GetString(_index);
  81. }
  82. public override ICefBinaryValue GetBinary()
  83. {
  84. return _container.GetBinary(_index);
  85. }
  86. public override ICefListValue GetList()
  87. {
  88. return _container.GetList(_index);
  89. }
  90. public override ICefDictionaryValue GetDictionary()
  91. {
  92. return _container.GetDictionary(_index);
  93. }
  94. public override CefValueType GetValueType()
  95. {
  96. return _container.GetValueType(_index);
  97. }
  98. public void CopyTo(CefListValue cefList, int index)
  99. {
  100. _setValue?.Invoke(cefList, index);
  101. }
  102. }
  103. }