CCDataVisitor.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #ifndef __CCDATAVISITOR_H__
  21. #define __CCDATAVISITOR_H__
  22. /// @cond DO_NOT_SHOW
  23. #include "platform/CCPlatformMacros.h"
  24. #include <string>
  25. NS_CC_BEGIN
  26. class Ref;
  27. class __Bool;
  28. class __Integer;
  29. class __Float;
  30. class __Double;
  31. class __String;
  32. class __Array;
  33. class __Dictionary;
  34. class __Set;
  35. /**
  36. * Visitor that helps to perform action that depends on polymorphic object type
  37. *
  38. * Use cases:
  39. * - data serialization,
  40. * - pretty printing of Ref*
  41. * - safe value reading from Array, __Dictionary, Set
  42. *
  43. * Usage:
  44. * 1. subclass DataVisitor
  45. * 2. overload visit() methods for object that you need to handle
  46. * 3. handle other objects in visitObject()
  47. * 4. pass your visitor to Object::acceptVisitor()
  48. */
  49. class CC_DLL DataVisitor
  50. {
  51. public:
  52. /**
  53. * @js NA
  54. * @lua NA
  55. */
  56. virtual ~DataVisitor() {}
  57. /** default method, called from non-overloaded methods and for unrecognized objects */
  58. virtual void visitObject(const Ref *p) = 0;
  59. virtual void visit(const __Bool *p);
  60. virtual void visit(const __Integer *p);
  61. virtual void visit(const __Float *p);
  62. virtual void visit(const __Double *p);
  63. virtual void visit(const __String *p);
  64. virtual void visit(const __Array *p);
  65. virtual void visit(const __Dictionary *p);
  66. virtual void visit(const __Set *p);
  67. };
  68. class CC_DLL PrettyPrinter : public DataVisitor
  69. {
  70. public:
  71. PrettyPrinter(int indentLevel = 0);
  72. virtual void clear();
  73. virtual std::string getResult();
  74. virtual void visitObject(const Ref *p);
  75. virtual void visit(const __Bool * p);
  76. virtual void visit(const __Integer *p);
  77. virtual void visit(const __Float *p);
  78. virtual void visit(const __Double *p);
  79. virtual void visit(const __String *p);
  80. virtual void visit(const __Array *p);
  81. virtual void visit(const __Dictionary *p);
  82. virtual void visit(const __Set *p);
  83. private:
  84. void setIndentLevel(int indentLevel);
  85. int _indentLevel;
  86. std::string _indentStr;
  87. std::string _result;
  88. };
  89. /**
  90. * @endcond
  91. */
  92. NS_CC_END
  93. /// @endcond
  94. #endif // __CCDATAVISITOR_H__