JoshDreamland

serializer_start

May 2nd, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. /**
  2. * @file serializer.h
  3. * @brief Provides classes and functions for printing data to arbitrary formats.
  4. *
  5. * The point of these classes is to allow resources to work with
  6. * format-independent data structures which are used in read-write
  7. * operations by actual format-aware file manipulation code.
  8. *
  9. * @section License
  10. *
  11. * Copyright (C) 2013 Josh Ventura
  12. * This file is a part of the Natural GM IDE.
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. **/
  27.  
  28. #ifndef _SERIALIZER__H
  29. #define _SERIALIZER__H
  30.  
  31. #include <map>
  32. #include <string>
  33. #include <vector>
  34.  
  35. namespace Serializer
  36. {
  37. using std::map;
  38. using std::string;
  39. using std::vector;
  40.  
  41. struct SerialData;
  42.  
  43. struct SerialObject;
  44. struct SerialImage;
  45. struct SerialAnimation;
  46. struct SerialBlob;
  47.  
  48. struct Serializer;
  49.  
  50. /**
  51. A namespace containing all SerialData UUIDs. As a convention, these are in the format "[plugin]-[brief]",
  52. where "[plugin]" is the name of the plugin that created the ID, and
  53. */
  54. namespace SerialUUID {
  55. const string object("ngm-object");
  56. const string image("ngm-image");
  57. const string animation("ngm-animation");
  58. const string blob("ngm-blob");
  59. }
  60.  
  61. /** Class representing some kind of serialized data. */
  62. struct SerialData {
  63. /** @return The UUID of this object. */
  64. virtual const string getUUID();
  65. /** @return An instance of this cast to a SerialObject, or NULL if this is not an object. */
  66. virtual SerialObject *asObject();
  67. /** @return An instance of this cast to a SerialImage, or NULL if this is not an object. */
  68. virtual SerialImage *asImage();
  69. /** @return An instance of this cast to a SerialImage, or NULL if this is not an object. */
  70. virtual SerialImage *asAnimation();
  71. /** @return An instance of this cast to a SerialBlob, or NULL if this object doesn. */
  72. virtual SerialImage *asBlob();
  73.  
  74. /** Return true if this is an instance of a child of SerialData that is not known here. False otherwise. */
  75. virtual bool isType(string type);
  76.  
  77. /** @return Returns whether anything in this object to be written to disk has changed. */
  78. virtual bool hasChanges();
  79. /** @return Returns whether the data in this object can be saved to disk independent of the rest of the file, right now. */
  80. virtual bool canCommit();
  81. /** Commit to disk any changes that have been made to this object, if they can be committed.
  82. @return True if the data was committed to the disk, false otherwise. */
  83. virtual bool commit(Serializer* s);
  84.  
  85. virtual ~SerialData();
  86. };
  87.  
  88. /** An object in the data which may have a value and/or children */
  89. struct SerialObject: SerialData {
  90. map<string, string> attributes;
  91. map<string, SerialData*> properties;
  92.  
  93. /** Returns this. */
  94. virtual SerialObject *asObject();
  95. };
  96.  
  97. /** An image in the serial data. */
  98. struct SerialImage: SerialData {
  99. QImage *getImage();
  100. bool putImage(QImage img);
  101.  
  102. /** Returns this. */
  103. virtual SerialImage *asImage();
  104. };
  105.  
  106. /** An animation in the serial data. */
  107. struct SerialAnimation: SerialData {
  108. vector<SerialImage*> images;
  109.  
  110. /** Returns this. */
  111. virtual SerialImage *asAnimation();
  112. };
  113.  
  114. /** Any other kinds of binary data included in the serial data. */
  115. struct SerialBlob: SerialData {
  116. /** @return An instance of this cast to a SerialBlob, or NULL if this is not a binary blob. */
  117. virtual SerialImage *asBlob();
  118. /** @return Returns the data in this binary blob, as a raw buffer of characters. */
  119. virtual char *getData();
  120.  
  121. SerialBlob();
  122. virtual ~SerialBlob();
  123. };
  124.  
  125. /**
  126. A node containing information for reading types of nodes from various formats.
  127. Useful when the extent of a format is unknown, as with EGM. Plugins can add to the
  128. format reader by creating schemes for their resource data.
  129. */
  130. struct SchemaNode {
  131. /// Get the name of this schema node, such as "Sprite Scheme", for debugging purposes.
  132. virtual string name();
  133. /** Interprets the given SerialData object into an appropriate subclass, depending on schema.
  134. Each SchemaNode child will handle this differently, as their name implies; see the \n Schema
  135. namespace for stock schemes.
  136.  
  137. @param data The data to be reinterpreted according to this scheme.
  138. @return Returns a new pointer to a SerialData object of a potentially different kind.
  139. */
  140. virtual SerialData *interpret(SerialData *data);
  141. /** Individual scheme handlers will need to implement this class, and may keep pointers around. */
  142. virtual ~SchemaNode();
  143. };
  144.  
  145. /** A collection of useful, pre-defined scheme handlers to make your life easier. */
  146. namespace Schema
  147. {
  148. /** Schema object expecting a SerialObject whose value is an image filename. */
  149. struct ImageFile: SchemaNode {
  150. string name();
  151. SerialData *interpret(SerialData *data);
  152. /** Read in an image file as a SerialImage object. */
  153. static SerialImage *imageFromFile(string filename);
  154. };
  155.  
  156. /** Schema object expecting a SerialObject containing more SerialObjects whose values are an image filename. */
  157. struct AnimationImageFile: SchemaNode {
  158. string name();
  159. SerialData *interpret(SerialData *data);
  160. /** Read in an image file as a SerialImage object. */
  161. static SerialAnimation *imageFromFile(string filename);
  162. };
  163. }
  164.  
  165. /** The actual class that handles serializing to a format. This is the abstract version; you'll
  166. need to use a specific format's serializer to actually read any files.
  167. */
  168. struct Serializer {
  169. /** Get the number of supported file extensions for this serializer to load */
  170. int extensionCount();
  171. /** Get the supported file extension with a given index (or get the first by default).
  172. @param eind The zero-based extension index. */
  173. const string getExtension(int eind = 0);
  174. /** Get the
  175. */
  176. SerialData *loadFile(string fname);
  177. };
  178. }
  179.  
  180. #endif
  181.  
  182.  
  183. //#include "serializer.h"
Advertisement
Add Comment
Please, Sign In to add comment