IWritableStream.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* ----------------------------------------------------------------- */
  2. /* The HMM-Based Singing Voice Synthesis System "Sinsy" */
  3. /* developed by Sinsy Working Group */
  4. /* http://sinsy.sourceforge.net/ */
  5. /* ----------------------------------------------------------------- */
  6. /* */
  7. /* Copyright (c) 2009-2015 Nagoya Institute of Technology */
  8. /* Department of Computer Science */
  9. /* */
  10. /* All rights reserved. */
  11. /* */
  12. /* Redistribution and use in source and binary forms, with or */
  13. /* without modification, are permitted provided that the following */
  14. /* conditions are met: */
  15. /* */
  16. /* - Redistributions of source code must retain the above copyright */
  17. /* notice, this list of conditions and the following disclaimer. */
  18. /* - Redistributions in binary form must reproduce the above */
  19. /* copyright notice, this list of conditions and the following */
  20. /* disclaimer in the documentation and/or other materials provided */
  21. /* with the distribution. */
  22. /* - Neither the name of the Sinsy working group nor the names of */
  23. /* its contributors may be used to endorse or promote products */
  24. /* derived from this software without specific prior written */
  25. /* permission. */
  26. /* */
  27. /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */
  28. /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  29. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  30. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  31. /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS */
  32. /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
  33. /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
  34. /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
  35. /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
  36. /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, */
  37. /* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY */
  38. /* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  39. /* POSSIBILITY OF SUCH DAMAGE. */
  40. /* ----------------------------------------------------------------- */
  41. #ifndef SINSY_I_WRITE_STREAM_H_
  42. #define SINSY_I_WRITE_STREAM_H_
  43. #include "util_types.h"
  44. #include "StreamException.h"
  45. namespace sinsy
  46. {
  47. class IWritableStream
  48. {
  49. public:
  50. //! destructor
  51. virtual ~IWritableStream() {}
  52. /*!
  53. write data to stream
  54. @param buffer buffer for data that you want to write
  55. @param byte byte you want to write
  56. @return write bytes
  57. */
  58. virtual size_t write(const void* buffer, size_t byte) throw (StreamException) = 0;
  59. };
  60. /*!
  61. write data to stream
  62. */
  63. template<class T>
  64. IWritableStream& toStream(IWritableStream& stream, const T& value)
  65. {
  66. size_t idx(0);
  67. const size_t size(sizeof(value));
  68. while (idx < size) {
  69. size_t result(stream.write(reinterpret_cast<const char*>(&value) + idx, size - idx));
  70. if (result <= 0) {
  71. throw StreamException("cannot write to IWritableStream");
  72. }
  73. idx += result;
  74. }
  75. return stream;
  76. }
  77. /*!
  78. write data to stream (char)
  79. */
  80. inline IWritableStream& operator<<(IWritableStream& stream, char buf) throw (StreamException)
  81. {
  82. return toStream(stream, buf);
  83. }
  84. /*!
  85. write data to stream (unsigned char)
  86. */
  87. inline IWritableStream& operator<<(IWritableStream& stream, unsigned char buf) throw (StreamException)
  88. {
  89. return toStream(stream, buf);
  90. }
  91. /*!
  92. write data to stream (INT16)
  93. */
  94. inline IWritableStream& operator<<(IWritableStream& stream, INT16 buf) throw (StreamException)
  95. {
  96. return toStream(stream, buf);
  97. }
  98. /*!
  99. write data to stream (UINT16)
  100. */
  101. inline IWritableStream& operator<<(IWritableStream& stream, UINT16 buf) throw (StreamException)
  102. {
  103. return toStream(stream, buf);
  104. }
  105. /*!
  106. write data to stream (INT32)
  107. */
  108. inline IWritableStream& operator<<(IWritableStream& stream, INT32 buf) throw (StreamException)
  109. {
  110. return toStream(stream, buf);
  111. }
  112. /*!
  113. write data to stream (UINT32)
  114. */
  115. inline IWritableStream& operator<<(IWritableStream& stream, UINT32 buf) throw (StreamException)
  116. {
  117. return toStream(stream, buf);
  118. }
  119. /*!
  120. write data to stream (INT64)
  121. */
  122. inline IWritableStream& operator<<(IWritableStream& stream, INT64 buf) throw (StreamException)
  123. {
  124. return toStream(stream, buf);
  125. }
  126. /*!
  127. write data to stream (UINT64)
  128. */
  129. inline IWritableStream& operator<<(IWritableStream& stream, UINT64 buf) throw (StreamException)
  130. {
  131. return toStream(stream, buf);
  132. }
  133. /*!
  134. write data to stream (float)
  135. */
  136. inline IWritableStream& operator<<(IWritableStream& stream, float buf) throw (StreamException)
  137. {
  138. return toStream(stream, buf);
  139. }
  140. /*!
  141. write data to stream (double)
  142. */
  143. inline IWritableStream& operator<<(IWritableStream& stream, double buf) throw (StreamException)
  144. {
  145. return toStream(stream, buf);
  146. }
  147. /*!
  148. write data to stream (long double)
  149. */
  150. inline IWritableStream& operator<<(IWritableStream& stream, long double buf) throw (StreamException)
  151. {
  152. return toStream(stream, buf);
  153. }
  154. };
  155. #endif // SINSY_I_WRITE_STREAM_H_