BinaryPropertyList.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2009 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef BinaryPropertyList_h
  26. #define BinaryPropertyList_h
  27. #include <CoreFoundation/CoreFoundation.h>
  28. #include <wtf/Forward.h>
  29. #include <wtf/Vector.h>
  30. // Writes a limited subset of binary property lists.
  31. // Covers only what's needed for writing browser history as of this writing.
  32. class BinaryPropertyListObjectStream {
  33. public:
  34. // Call writeBooleanTrue to write the boolean true value.
  35. // A single shared object will be used in the serialized list.
  36. virtual void writeBooleanTrue() = 0;
  37. // Call writeInteger to write an integer value.
  38. // A single shared object will be used for each integer in the serialized list.
  39. virtual void writeInteger(int) = 0;
  40. // Call writeString to write a string value.
  41. // A single shared object will be used for each string in the serialized list.
  42. virtual void writeString(const String&) = 0;
  43. // Call writeUniqueString instead of writeString when it's unlikely the
  44. // string will be written twice in the same property list; this saves hash
  45. // table overhead for such strings. A separate object will be used for each
  46. // of these strings in the serialized list.
  47. virtual void writeUniqueString(const String&) = 0;
  48. virtual void writeUniqueString(const char*) = 0;
  49. // Call writeIntegerArray instead of writeArrayStart/writeArrayEnd for
  50. // arrays entirely composed of integers. A single shared object will be used
  51. // for each identical array in the serialized list. Warning: The integer
  52. // pointer must remain valid until the writeBinaryPropertyList function
  53. // returns, because these lists are put into a hash table without copying
  54. // them -- that's OK if the client already has a Vector<int>.
  55. virtual void writeIntegerArray(const int*, size_t) = 0;
  56. // After calling writeArrayStart, write array elements.
  57. // Then call writeArrayEnd, passing in the result from writeArrayStart.
  58. // A separate object will be used for each of these arrays in the serialized list.
  59. virtual size_t writeArrayStart() = 0;
  60. virtual void writeArrayEnd(size_t resultFromWriteArrayStart) = 0;
  61. // After calling writeDictionaryStart, write all keys, then all values.
  62. // Then call writeDictionaryEnd, passing in the result from writeDictionaryStart.
  63. // A separate object will be used for each dictionary in the serialized list.
  64. virtual size_t writeDictionaryStart() = 0;
  65. virtual void writeDictionaryEnd(size_t resultFromWriteDictionaryStart) = 0;
  66. protected:
  67. virtual ~BinaryPropertyListObjectStream() { }
  68. };
  69. class BinaryPropertyListWriter {
  70. public:
  71. // Calls writeObjects once to prepare for writing and determine how big a
  72. // buffer is required. Then calls buffer to get the appropriately-sized
  73. // buffer, then calls writeObjects a second time and writes the property list.
  74. void writePropertyList();
  75. protected:
  76. virtual ~BinaryPropertyListWriter() { }
  77. private:
  78. // Called by writePropertyList.
  79. // Must call the object stream functions for the objects to be written
  80. // into the property list.
  81. virtual void writeObjects(BinaryPropertyListObjectStream&) = 0;
  82. // Called by writePropertyList.
  83. // Returns the buffer that the writer should write into.
  84. virtual UInt8* buffer(size_t) = 0;
  85. friend class BinaryPropertyListPlan;
  86. friend class BinaryPropertyListSerializer;
  87. };
  88. #endif // BinaryPropertyList_h