Jamoma API  0.6.0.a19
OscOutboundPacketStream.h
1 /*
2  oscpack -- Open Sound Control (OSC) packet manipulation library
3  http://www.rossbencina.com/code/oscpack
4 
5  Copyright (c) 2004-2013 Ross Bencina <rossb@audiomulch.com>
6 
7  Permission is hereby granted, free of charge, to any person obtaining
8  a copy of this software and associated documentation files
9  (the "Software"), to deal in the Software without restriction,
10  including without limitation the rights to use, copy, modify, merge,
11  publish, distribute, sublicense, and/or sell copies of the Software,
12  and to permit persons to whom the Software is furnished to do so,
13  subject to the following conditions:
14 
15  The above copyright notice and this permission notice shall be
16  included in all copies or substantial portions of the Software.
17 
18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
22  ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26 
27 /*
28  The text above constitutes the entire oscpack license; however,
29  the oscpack developer(s) also make the following non-binding requests:
30 
31  Any person wishing to distribute modifications to the Software is
32  requested to send the modifications to the original developer so that
33  they can be incorporated into the canonical version. It is also
34  requested that these non-binding requests be included whenever the
35  above license is reproduced.
36 */
37 #ifndef INCLUDED_OSCPACK_OSCOUTBOUNDPACKETSTREAM_H
38 #define INCLUDED_OSCPACK_OSCOUTBOUNDPACKETSTREAM_H
39 
40 #include <cstring> // size_t
41 
42 #include "OscTypes.h"
43 #include "OscException.h"
44 
45 
46 namespace osc{
47 
48 class OutOfBufferMemoryException : public Exception{
49 public:
50  OutOfBufferMemoryException( const char *w="out of buffer memory" )
51  : Exception( w ) {}
52 };
53 
54 class BundleNotInProgressException : public Exception{
55 public:
56  BundleNotInProgressException(
57  const char *w="call to EndBundle when bundle is not in progress" )
58  : Exception( w ) {}
59 };
60 
61 class MessageInProgressException : public Exception{
62 public:
63  MessageInProgressException(
64  const char *w="opening or closing bundle or message while message is in progress" )
65  : Exception( w ) {}
66 };
67 
68 class MessageNotInProgressException : public Exception{
69 public:
70  MessageNotInProgressException(
71  const char *w="call to EndMessage when message is not in progress" )
72  : Exception( w ) {}
73 };
74 
75 
76 class OutboundPacketStream{
77 public:
78  OutboundPacketStream( char *buffer, std::size_t capacity );
79  ~OutboundPacketStream();
80 
81  void Clear();
82 
83  std::size_t Capacity() const;
84 
85  // invariant: size() is valid even while building a message.
86  std::size_t Size() const;
87 
88  const char *Data() const;
89 
90  // indicates that all messages have been closed with a matching EndMessage
91  // and all bundles have been closed with a matching EndBundle
92  bool IsReady() const;
93 
94  bool IsMessageInProgress() const;
95  bool IsBundleInProgress() const;
96 
97  OutboundPacketStream& operator<<( const BundleInitiator& rhs );
98  OutboundPacketStream& operator<<( const BundleTerminator& rhs );
99 
100  OutboundPacketStream& operator<<( const BeginMessage& rhs );
101  OutboundPacketStream& operator<<( const MessageTerminator& rhs );
102 
103  OutboundPacketStream& operator<<( bool rhs );
104  OutboundPacketStream& operator<<( const NilType& rhs );
105  OutboundPacketStream& operator<<( const InfinitumType& rhs );
106  OutboundPacketStream& operator<<( int32 rhs );
107 
108 #if !(defined(__x86_64__) || defined(_M_X64))
109  OutboundPacketStream& operator<<( int rhs )
110  { *this << (int32)rhs; return *this; }
111 #endif
112 
113  OutboundPacketStream& operator<<( float rhs );
114  OutboundPacketStream& operator<<( char rhs );
115  OutboundPacketStream& operator<<( const RgbaColor& rhs );
116  OutboundPacketStream& operator<<( const MidiMessage& rhs );
117  OutboundPacketStream& operator<<( int64 rhs );
118  OutboundPacketStream& operator<<( const TimeTag& rhs );
119  OutboundPacketStream& operator<<( double rhs );
120  OutboundPacketStream& operator<<( const char* rhs );
121  OutboundPacketStream& operator<<( const Symbol& rhs );
122  OutboundPacketStream& operator<<( const Blob& rhs );
123 
124  OutboundPacketStream& operator<<( const ArrayInitiator& rhs );
125  OutboundPacketStream& operator<<( const ArrayTerminator& rhs );
126 
127 private:
128 
129  char *BeginElement( char *beginPtr );
130  void EndElement( char *endPtr );
131 
132  bool ElementSizeSlotRequired() const;
133  void CheckForAvailableBundleSpace();
134  void CheckForAvailableMessageSpace( const char *addressPattern );
135  void CheckForAvailableArgumentSpace( std::size_t argumentLength );
136 
137  char *data_;
138  char *end_;
139 
140  char *typeTagsCurrent_; // stored in reverse order
141  char *messageCursor_;
142  char *argumentCurrent_;
143 
144  // elementSizePtr_ has two special values: 0 indicates that a bundle
145  // isn't open, and elementSizePtr_==data_ indicates that a bundle is
146  // open but that it doesn't have a size slot (ie the outermost bundle)
147  uint32 *elementSizePtr_;
148 
149  bool messageIsInProgress_;
150 };
151 
152 } // namespace osc
153 
154 #endif /* INCLUDED_OSCPACK_OSCOUTBOUNDPACKETSTREAM_H */