ParameterOutput 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGDB_PARAMETEROUTPUT
  14. #define OSGDB_PARAMETEROUTPUT 1
  15. #include <osgDB/Output>
  16. namespace osgDB {
  17. class ParameterOutput
  18. {
  19. public:
  20. ParameterOutput(Output& fw):
  21. _fw(fw),
  22. _numItemsPerLine(fw.getNumIndicesPerLine()),
  23. _column(0) {}
  24. ParameterOutput(Output& fw,int numItemsPerLine):
  25. _fw(fw),
  26. _numItemsPerLine(numItemsPerLine),
  27. _column(0) {}
  28. void begin()
  29. {
  30. _fw.indent() << "{"<<std::endl;
  31. _fw.moveIn();
  32. }
  33. void newLine()
  34. {
  35. if (_column!=0) _fw << std::endl;
  36. _column = 0;
  37. }
  38. void end()
  39. {
  40. if (_column!=0) _fw << std::endl;
  41. _fw.moveOut();
  42. _fw.indent() << "}"<<std::endl;
  43. _column = 0;
  44. }
  45. template<class T>
  46. void write(const T& t)
  47. {
  48. if (_column==0) _fw.indent();
  49. _fw << t;
  50. ++_column;
  51. if (_column==_numItemsPerLine)
  52. {
  53. _fw << std::endl;
  54. _column = 0;
  55. }
  56. else
  57. {
  58. _fw << " ";
  59. }
  60. }
  61. template<class Iterator>
  62. void write(Iterator first, Iterator last)
  63. {
  64. for(Iterator itr=first;
  65. itr!=last;
  66. ++itr)
  67. {
  68. write(*itr);
  69. }
  70. }
  71. template<class Iterator>
  72. void writeAsInts(Iterator first, Iterator last)
  73. {
  74. for(Iterator itr=first;
  75. itr!=last;
  76. ++itr)
  77. {
  78. write((int)*itr);
  79. }
  80. }
  81. protected:
  82. ParameterOutput& operator = (const ParameterOutput&) { return *this; }
  83. Output& _fw;
  84. int _numItemsPerLine;
  85. int _column;
  86. };
  87. template<class Iterator>
  88. void writeArray(Output& fw, Iterator first, Iterator last,int noItemsPerLine=0)
  89. {
  90. if (noItemsPerLine==0) noItemsPerLine=fw.getNumIndicesPerLine();
  91. fw.indent() << "{"<<std::endl;
  92. fw.moveIn();
  93. int column=0;
  94. for(Iterator itr=first;
  95. itr!=last;
  96. ++itr)
  97. {
  98. if (column==0) fw.indent();
  99. fw << *itr;
  100. ++column;
  101. if (column==noItemsPerLine)
  102. {
  103. fw << std::endl;
  104. column = 0;
  105. }
  106. else
  107. {
  108. fw << " ";
  109. }
  110. }
  111. if (column!=0) fw << std::endl;
  112. fw.moveOut();
  113. fw.indent()<<"}"<<std::endl;
  114. }
  115. template<class Iterator>
  116. void writeArrayAsInts(Output& fw, Iterator first, Iterator last,int noItemsPerLine=0)
  117. {
  118. if (noItemsPerLine==0) noItemsPerLine=fw.getNumIndicesPerLine();
  119. fw.indent() << "{"<<std::endl;
  120. fw.moveIn();
  121. int column=0;
  122. for(Iterator itr=first;
  123. itr!=last;
  124. ++itr)
  125. {
  126. if (column==0) fw.indent();
  127. fw << (int)*itr;
  128. ++column;
  129. if (column==noItemsPerLine)
  130. {
  131. fw << std::endl;
  132. column = 0;
  133. }
  134. else
  135. {
  136. fw << " ";
  137. }
  138. }
  139. if (column!=0) fw << std::endl;
  140. fw.moveOut();
  141. fw.indent()<<"}"<<std::endl;
  142. }
  143. }
  144. #endif // __SG_OUTPUT_H