3 Copyright (C) 2011 Grame
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
31 #include "smartpointer.h"
37 template <typename T
> class MsgParam
;
39 typedef SMARTP
<baseparam
> Sbaseparam
;
41 //--------------------------------------------------------------------------
43 \brief base class of a message parameters
45 class baseparam
: public smartable
48 virtual ~baseparam() {}
51 \brief utility for parameter type checking
53 template<typename X
> bool isType() const { return dynamic_cast<const MsgParam
<X
>*> (this) != 0; }
55 \brief utility for parameter convertion
56 \param errvalue the returned value when no conversion applies
57 \return the parameter value when the type matches
59 template<typename X
> X
value(X errvalue
) const
60 { const MsgParam
<X
>* o
= dynamic_cast<const MsgParam
<X
>*> (this); return o
? o
->getValue() : errvalue
; }
62 \brief utility for parameter comparison
64 template<typename X
> bool equal(const baseparam
& p
) const
66 const MsgParam
<X
>* a
= dynamic_cast<const MsgParam
<X
>*> (this);
67 const MsgParam
<X
>* b
= dynamic_cast<const MsgParam
<X
>*> (&p
);
68 return a
&& b
&& (a
->getValue() == b
->getValue());
71 \brief utility for parameter comparison
73 bool operator==(const baseparam
& p
) const
75 return equal
<float>(p
) || equal
<int>(p
) || equal
<std::string
>(p
);
77 bool operator!=(const baseparam
& p
) const
79 return !equal
<float>(p
) && !equal
<int>(p
) && !equal
<std::string
>(p
);
82 virtual SMARTP
<baseparam
> copy() const = 0;
85 //--------------------------------------------------------------------------
87 \brief template for a message parameter
89 template <typename T
> class MsgParam
: public baseparam
93 MsgParam(T val
) : fParam(val
) {}
94 virtual ~MsgParam() {}
96 T
getValue() const { return fParam
; }
98 virtual SMARTP
<baseparam
> copy() const { return new MsgParam
<T
>(fParam
); }
101 //--------------------------------------------------------------------------
103 \brief a message description
105 A message is composed of an address (actually an OSC address),
106 a message string that may be viewed as a method name
107 and a list of message parameters.
112 typedef SMARTP
<baseparam
> argPtr
; ///< a message argument ptr type
113 typedef std::vector
<argPtr
> argslist
; ///< args list type
116 unsigned long fSrcIP
; ///< the message source IP number
117 std::string fAddress
; ///< the message osc destination address
118 argslist fArguments
; ///< the message arguments
122 \brief an empty message constructor
126 \brief a message constructor
127 \param address the message destination address
129 Message(const std::string
& address
) : fAddress(address
) {}
131 \brief a message constructor
132 \param address the message destination address
133 \param args the message parameters
135 Message(const std::string
& address
, const argslist
& args
)
136 : fAddress(address
), fArguments(args
) {}
138 \brief a message constructor
141 Message(const Message
& msg
);
142 virtual ~Message() {} //{ freed++; std::cout << "running messages: " << (allocated - freed) << std::endl; }
145 \brief adds a parameter to the message
146 \param val the parameter
148 template <typename T
> void add(T val
) { fArguments
.push_back(new MsgParam
<T
>(val
)); }
150 \brief adds a float parameter to the message
151 \param val the parameter value
153 void add(float val
) { add
<float>(val
); }
155 \brief adds an int parameter to the message
156 \param val the parameter value
158 void add(int val
) { add
<int>(val
); }
160 \brief adds a string parameter to the message
161 \param val the parameter value
163 void add(const std::string
& val
) { add
<std::string
>(val
); }
166 \brief adds a parameter to the message
167 \param val the parameter
169 void add( argPtr val
) { fArguments
.push_back( val
); }
172 \brief sets the message address
173 \param addr the address
175 void setSrcIP(unsigned long addr
) { fSrcIP
= addr
; }
178 \brief sets the message address
179 \param addr the address
181 void setAddress(const std::string
& addr
) { fAddress
= addr
; }
183 \brief print the message
184 \param out the output stream
186 void print(std::ostream
& out
) const;
188 \brief send the message to OSC
189 \param out the OSC output stream
191 void print(OSCStream
& out
) const;
193 \brief print message arguments
194 \param out the OSC output stream
196 void printArgs(OSCStream
& out
) const;
198 /// \brief gives the message address
199 const std::string
& address() const { return fAddress
; }
200 /// \brief gives the message parameters list
201 const argslist
& params() const { return fArguments
; }
202 /// \brief gives the message parameters list
203 argslist
& params() { return fArguments
; }
204 /// \brief gives the message source IP
205 unsigned long src() const { return fSrcIP
; }
206 /// \brief gives the message parameters count
207 int size() const { return fArguments
.size(); }
209 bool operator == (const Message
& other
) const;
213 \brief gives a message float parameter
214 \param i the parameter index (0 <= i < size())
215 \param val on output: the parameter value when the parameter type matches
216 \return false when types don't match
218 bool param(int i
, float& val
) const { val
= params()[i
]->value
<float>(val
); return params()[i
]->isType
<float>(); }
220 \brief gives a message int parameter
221 \param i the parameter index (0 <= i < size())
222 \param val on output: the parameter value when the parameter type matches
223 \return false when types don't match
225 bool param(int i
, int& val
) const { val
= params()[i
]->value
<int>(val
); return params()[i
]->isType
<int>(); }
227 \brief gives a message int parameter
228 \param i the parameter index (0 <= i < size())
229 \param val on output: the parameter value when the parameter type matches
230 \return false when types don't match
232 bool param(int i
, unsigned int& val
) const { val
= params()[i
]->value
<int>(val
); return params()[i
]->isType
<int>(); }
234 \brief gives a message int parameter
235 \param i the parameter index (0 <= i < size())
236 \param val on output: the parameter value when the parameter type matches
237 \return false when types don't match
238 \note a boolean value is handled as integer
240 bool param(int i
, bool& val
) const { int ival
= 0; ival
= params()[i
]->value
<int>(ival
); val
= ival
!=0; return params()[i
]->isType
<int>(); }
242 \brief gives a message int parameter
243 \param i the parameter index (0 <= i < size())
244 \param val on output: the parameter value when the parameter type matches
245 \return false when types don't match
247 bool param(int i
, long int& val
) const { val
= long(params()[i
]->value
<int>(val
)); return params()[i
]->isType
<int>(); }
249 \brief gives a message string parameter
250 \param i the parameter index (0 <= i < size())
251 \param val on output: the parameter value when the parameter type matches
252 \return false when types don't match
254 bool param(int i
, std::string
& val
) const { val
= params()[i
]->value
<std::string
>(val
); return params()[i
]->isType
<std::string
>(); }