CCDataVisitor.h 3.3 KB

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