NO-OP: Re-indent websockets code

"Always use Tabstops for block-indent (the code must be
formatted correctly with "[TAB] = N spaces" for any value of N).
Use space only for alignment." - https://ardour.org/styleguide.html
This commit is contained in:
Robin Gareus 2020-02-23 16:03:59 +01:00
parent 224be91211
commit 5e3480ba8f
No known key found for this signature in database
GPG key ID: A090BCE02CF57F04
23 changed files with 1260 additions and 1217 deletions

View file

@ -105,7 +105,7 @@ ArdourWebsockets::do_request (ArdourWebsocketsUIRequest* req)
int
ArdourWebsockets::start ()
{
// startup the event loop thread
/* startup the event loop thread */
BaseUI::run ();
for (std::vector<SurfaceComponent*>::iterator it = _components.begin ();
@ -122,7 +122,8 @@ ArdourWebsockets::start ()
}
int
ArdourWebsockets::stop () {
ArdourWebsockets::stop ()
{
for (std::vector<SurfaceComponent*>::iterator it = _components.begin ();
it != _components.end (); ++it) {
(*it)->stop ();

View file

@ -33,17 +33,17 @@
#include "control_protocol/control_protocol.h"
#include "component.h"
#include "strips.h"
#include "dispatcher.h"
#include "feedback.h"
#include "globals.h"
#include "server.h"
#include "feedback.h"
#include "dispatcher.h"
#include "strips.h"
#define SURFACE_NAME "WebSockets Server (Experimental)"
#define SURFACE_ID "uri://ardour.org/surfaces/ardour_websockets:0"
namespace ArdourSurface {
namespace ArdourSurface
{
struct ArdourWebsocketsUIRequest : public BaseUI::BaseRequestObject {
public:
ArdourWebsocketsUIRequest () {}
@ -54,7 +54,6 @@ class ArdourWebsockets : public ARDOUR::ControlProtocol,
public AbstractUI<ArdourWebsocketsUIRequest>
{
public:
ArdourWebsockets (ARDOUR::Session&);
virtual ~ArdourWebsockets ();
@ -62,25 +61,38 @@ class ArdourWebsockets : public ARDOUR::ControlProtocol,
int set_active (bool);
ARDOUR::Session& ardour_session () { return *session; }
ArdourStrips& strips_component () { return _strips; }
ArdourGlobals& globals_component () { return _globals; }
WebsocketsServer& server_component () { return _server; }
WebsocketsDispatcher& dispatcher_component () { return _dispatcher; }
ARDOUR::Session& ardour_session ()
{
return *session;
}
ArdourStrips& strips_component ()
{
return _strips;
}
ArdourGlobals& globals_component ()
{
return _globals;
}
WebsocketsServer& server_component ()
{
return _server;
}
WebsocketsDispatcher& dispatcher_component ()
{
return _dispatcher;
}
// ControlProtocol
/* ControlProtocol */
void stripable_selection_changed () {}
protected:
// BaseUI
/* BaseUI */
void thread_init ();
// AbstractUI
/* AbstractUI */
void do_request (ArdourWebsocketsUIRequest*);
private:
ArdourStrips _strips;
ArdourGlobals _globals;
ArdourFeedback _feedback;
@ -90,9 +102,8 @@ class ArdourWebsockets : public ARDOUR::ControlProtocol,
int start ();
int stop ();
};
} // namespace
} // namespace ArdourSurface
#endif // ardour_websockets_h

View file

@ -22,8 +22,8 @@
#include <boost/unordered_set.hpp>
#include <list>
#include "state.h"
#include "message.h"
#include "state.h"
typedef struct lws* Client;
typedef std::list<NodeStateMessage> ClientOutputBuffer;
@ -31,28 +31,32 @@ typedef std::list<NodeStateMessage> ClientOutputBuffer;
class ClientContext
{
public:
ClientContext (Client wsi) : _wsi(wsi) {};
ClientContext (Client wsi)
: _wsi (wsi){};
virtual ~ClientContext (){};
Client wsi () const { return _wsi; }
Client wsi () const
{
return _wsi;
}
bool has_state (const NodeState&);
void update_state (const NodeState&);
ClientOutputBuffer& output_buf () { return _output_buf; }
ClientOutputBuffer& output_buf ()
{
return _output_buf;
}
std::string debug_str ();
private:
Client _wsi;
typedef boost::unordered_set<NodeState> ClientState;
ClientState _state;
ClientOutputBuffer _output_buf;
};
#endif // client_context_h

View file

@ -24,7 +24,8 @@
#include "ardour/session.h"
#include "pbd/event_loop.h"
namespace ArdourSurface {
namespace ArdourSurface
{
class ArdourWebsockets;
}
@ -36,13 +37,19 @@ class WebsocketsDispatcher;
class SurfaceComponent
{
public:
SurfaceComponent (ArdourSurface::ArdourWebsockets& surface) : _surface (surface) {};
SurfaceComponent (ArdourSurface::ArdourWebsockets& surface)
: _surface (surface){};
virtual ~SurfaceComponent (){};
virtual int start () { return 0; }
virtual int stop () { return 0; }
virtual int start ()
{
return 0;
}
virtual int stop ()
{
return 0;
}
PBD::EventLoop* event_loop () const;
Glib::RefPtr<Glib::MainLoop> main_loop () const;
@ -53,9 +60,7 @@ class SurfaceComponent
WebsocketsDispatcher& dispatcher () const;
protected:
ArdourSurface::ArdourWebsockets& _surface;
};
#endif // surface_component_h

View file

@ -20,8 +20,8 @@
#include "ardour/plugin_insert.h"
#include "dispatcher.h"
#include "ardour_websockets.h"
#include "dispatcher.h"
#include "state.h"
using namespace ARDOUR;
@ -35,8 +35,7 @@ WebsocketsDispatcher::_node_to_method = boost::assign::map_list_of
NODE_METHOD_PAIR (strip_pan)
NODE_METHOD_PAIR (strip_mute)
NODE_METHOD_PAIR (strip_plugin_enable)
NODE_METHOD_PAIR(strip_plugin_param_value)
;
NODE_METHOD_PAIR (strip_plugin_param_value);
void
WebsocketsDispatcher::dispatch (Client client, const NodeStateMessage& msg)

View file

@ -21,23 +21,21 @@
#include <boost/unordered_map.hpp>
#include "component.h"
#include "client.h"
#include "component.h"
#include "message.h"
class WebsocketsDispatcher : public SurfaceComponent
{
public:
WebsocketsDispatcher (ArdourSurface::ArdourWebsockets& surface) : SurfaceComponent (surface) {};
WebsocketsDispatcher (ArdourSurface::ArdourWebsockets& surface)
: SurfaceComponent (surface){};
virtual ~WebsocketsDispatcher (){};
void dispatch (Client, const NodeStateMessage&);
void update_all_nodes (Client);
private:
typedef void (WebsocketsDispatcher::*DispatcherMethod) (Client, const NodeStateMessage&);
typedef boost::unordered_map<std::string, DispatcherMethod> NodeMethodMap;
@ -55,7 +53,6 @@ class WebsocketsDispatcher : public SurfaceComponent
void update (Client, std::string, uint32_t, uint32_t, TypedValue);
void update (Client, std::string, uint32_t, uint32_t, uint32_t, TypedValue);
void update (Client, std::string, const AddressVector&, const ValueVector&);
};
#endif // websockets_dispatcher_h

View file

@ -16,46 +16,51 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "ardour/meter.h"
#include "ardour/plugin_insert.h"
#include "ardour/session.h"
#include "ardour/tempo.h"
#include "ardour/plugin_insert.h"
#include "ardour/meter.h"
#include "feedback.h"
#include "strips.h"
#include "globals.h"
#include "state.h"
#include "server.h"
#include "state.h"
#include "strips.h"
using namespace ARDOUR;
struct TempoObserver {
void operator() (ArdourFeedback* p) {
void operator() (ArdourFeedback* p)
{
p->update_all (Node::tempo, p->globals ().tempo ());
}
};
struct StripGainObserver {
void operator() (ArdourFeedback* p, uint32_t strip_n) {
void operator() (ArdourFeedback* p, uint32_t strip_n)
{
// fires multiple times (4x as of ardour 6.0)
p->update_all (Node::strip_gain, strip_n, p->strips ().strip_gain (strip_n));
}
};
struct StripPanObserver {
void operator() (ArdourFeedback* p, uint32_t strip_n) {
void operator() (ArdourFeedback* p, uint32_t strip_n)
{
p->update_all (Node::strip_pan, strip_n, p->strips ().strip_pan (strip_n));
}
};
struct StripMuteObserver {
void operator() (ArdourFeedback* p, uint32_t strip_n) {
void operator() (ArdourFeedback* p, uint32_t strip_n)
{
p->update_all (Node::strip_mute, strip_n, p->strips ().strip_mute (strip_n));
}
};
struct PluginBypassObserver {
void operator() (ArdourFeedback* p, uint32_t strip_n, uint32_t plugin_n) {
void operator() (ArdourFeedback* p, uint32_t strip_n, uint32_t plugin_n)
{
p->update_all (Node::strip_plugin_enable, strip_n, plugin_n,
p->strips ().strip_plugin_enabled (strip_n, plugin_n));
}
@ -63,7 +68,8 @@ struct PluginBypassObserver {
struct PluginParamValueObserver {
void operator() (ArdourFeedback* p, uint32_t strip_n, uint32_t plugin_n,
uint32_t param_n, boost::shared_ptr<AutomationControl> control) {
uint32_t param_n, boost::shared_ptr<AutomationControl> control)
{
p->update_all (Node::strip_plugin_param_value, strip_n, plugin_n, param_n,
ArdourStrips::plugin_param_value (control));
}
@ -215,6 +221,7 @@ ArdourFeedback::observe_strip_plugin_param_values (uint32_t strip_n,
control->Changed.connect (_signal_connections, MISSING_INVALIDATOR,
boost::bind<void> (PluginParamValueObserver (), this, strip_n, plugin_n, param_n,
control), event_loop ());
control),
event_loop ());
}
}

View file

@ -19,8 +19,8 @@
#ifndef ardour_feedback_h
#define ardour_feedback_h
#include <glibmm/main.h>
#include <boost/shared_ptr.hpp>
#include <glibmm/main.h>
#include "component.h"
#include "typed_value.h"
@ -28,8 +28,8 @@
class ArdourFeedback : public SurfaceComponent
{
public:
ArdourFeedback (ArdourSurface::ArdourWebsockets& surface) : SurfaceComponent (surface) {};
ArdourFeedback (ArdourSurface::ArdourWebsockets& surface)
: SurfaceComponent (surface){};
virtual ~ArdourFeedback (){};
int start ();
@ -41,7 +41,6 @@ class ArdourFeedback : public SurfaceComponent
void update_all (std::string, uint32_t, uint32_t, uint32_t, TypedValue) const;
private:
Glib::Threads::Mutex _client_state_lock;
PBD::ScopedConnectionList _signal_connections;
sigc::connection _periodic_connection;
@ -53,7 +52,6 @@ class ArdourFeedback : public SurfaceComponent
void observe_strip_plugins (uint32_t, boost::shared_ptr<ARDOUR::Stripable>);
void observe_strip_plugin_param_values (uint32_t, uint32_t,
boost::shared_ptr<ARDOUR::PluginInsert>);
};
#endif // ardour_feedback_h

View file

@ -24,13 +24,12 @@
class ArdourGlobals : public SurfaceComponent
{
public:
ArdourGlobals (ArdourSurface::ArdourWebsockets& surface) : SurfaceComponent (surface) {};
ArdourGlobals (ArdourSurface::ArdourWebsockets& surface)
: SurfaceComponent (surface){};
virtual ~ArdourGlobals (){};
double tempo () const;
void set_tempo (double);
};
#endif // ardour_globals_h

View file

@ -68,6 +68,8 @@ static ControlProtocolDescriptor ardour_websockets_descriptor = {
/*request_buffer_factory */ ardour_websockets_request_buffer_factory
};
extern "C" ARDOURSURFACE_API ControlProtocolDescriptor* protocol_descriptor () {
extern "C" ARDOURSURFACE_API ControlProtocolDescriptor*
protocol_descriptor ()
{
return &ardour_websockets_descriptor;
}

View file

@ -20,10 +20,10 @@
#include <iostream>
#endif
#include <sstream>
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <sstream>
#include "message.h"
@ -65,7 +65,6 @@ NodeStateMessage::NodeStateMessage (void *buf, size_t len)
pt::ptree val = root.get_child ("val", pt::ptree ());
for (pt::ptree::iterator it = val.begin (); it != val.end (); ++it) {
std::string val = it->second.data ();
try {

View file

@ -24,22 +24,28 @@
class NodeStateMessage
{
public:
NodeStateMessage (const NodeState& state);
NodeStateMessage (void*, size_t);
size_t serialize (void*, size_t) const;
bool is_valid () const { return _valid; }
bool is_write () const { return _write; }
const NodeState& state () const { return _state; }
bool is_valid () const
{
return _valid;
}
bool is_write () const
{
return _write;
}
const NodeState& state () const
{
return _state;
}
private:
bool _valid;
bool _write;
NodeState _state;
};
#endif // websockets_message_h

View file

@ -20,8 +20,8 @@
#include <iostream>
#endif
#include "server.h"
#include "dispatcher.h"
#include "server.h"
/* backport from libwebsockets 3.0,
* allow to compile on GNU/Linux with libwebsockets 2.x
@ -44,7 +44,7 @@ WebsocketsServer::WebsocketsServer (ArdourSurface::ArdourWebsockets& surface)
: SurfaceComponent (surface)
, _lws_context (0)
{
// keep references to all config for libwebsockets 2
/* keep references to all config for libwebsockets 2 */
lws_protocols proto;
memset (&proto, 0, sizeof (lws_protocols));
proto.name = "lws-ardour";
@ -77,17 +77,19 @@ WebsocketsServer::start ()
return -1;
}
// add_poll_fd() should have been called once during lws_create_context()
// if _fd_ctx is empty then LWS_CALLBACK_ADD_POLL_FD was not called
// this means libwesockets was not compiled with LWS_WITH_EXTERNAL_POLL
// - macos homebrew libwebsockets: disabled (3.2.2 as of Feb 2020)
// - linux ubuntu libwebsockets-dev: enabled (2.0.3 as of Feb 2020) but
// #if defined(LWS_WITH_EXTERNAL_POLL) check is not reliable -- constant
// missing from /usr/include/lws_config.h
/* add_poll_fd() should have been called once during lws_create_context()
* if _fd_ctx is empty then LWS_CALLBACK_ADD_POLL_FD was not called
* this means libwesockets was not compiled with LWS_WITH_EXTERNAL_POLL
* - macos homebrew libwebsockets: disabled (3.2.2 as of Feb 2020)
* - linux ubuntu libwebsockets-dev: enabled (2.0.3 as of Feb 2020) but
* #if defined(LWS_WITH_EXTERNAL_POLL) check is not reliable -- constant
* missing from /usr/include/lws_config.h
*/
if (_fd_ctx.empty ()) {
PBD::error << "ArdourWebsockets: check your libwebsockets was compiled"
" with LWS_WITH_EXTERNAL_POLL enabled" << endmsg;
" with LWS_WITH_EXTERNAL_POLL enabled"
<< endmsg;
return -1;
}
@ -124,7 +126,7 @@ WebsocketsServer::update_client (Client wsi, const NodeState& state, bool force)
}
if (force || !it->second.has_state (state)) {
// write to client only if state was updated
/* write to client only if state was updated */
it->second.update_state (state);
it->second.output_buf ().push_back (NodeStateMessage (state));
lws_callback_on_writable (wsi);
@ -142,7 +144,7 @@ WebsocketsServer::update_all_clients (const NodeState& state, bool force)
void
WebsocketsServer::add_poll_fd (struct lws_pollargs* pa)
{
// fd can be SOCKET or int depending platform
/* fd can be SOCKET or int depending platform */
lws_sockfd_type fd = pa->fd;
#ifdef PLATFORM_WINDOWS
@ -179,13 +181,14 @@ WebsocketsServer::mod_poll_fd (struct lws_pollargs *pa)
it->second.lws_pfd.events = pa->events;
if (pa->events & LWS_POLLOUT) {
// libwebsockets wants to write but cannot find a way to update
// an existing glib::iosource event flags using glibmm,
// create another iosource and set to IO_OUT, it will be destroyed
// after clearing POLLOUT (see 'else' body below)
/* libwebsockets wants to write but cannot find a way to update
* an existing glib::iosource event flags using glibmm,
* create another iosource and set to IO_OUT, it will be destroyed
* after clearing POLLOUT (see 'else' body below)
*/
if (it->second.wg_iosrc) {
// already polling for write
/* already polling for write */
return;
}
@ -251,7 +254,7 @@ WebsocketsServer::recv_client (Client wsi, void *buf, size_t len)
return;
}
// avoid echo
/* avoid echo */
it->second.update_state (msg.state ());
dispatcher ().dispatch (wsi, msg);
@ -270,7 +273,7 @@ WebsocketsServer::write_client (Client wsi)
return;
}
// one lws_write() call per LWS_CALLBACK_SERVER_WRITEABLE callback
/* one lws_write() call per LWS_CALLBACK_SERVER_WRITEABLE callback */
NodeStateMessage msg = pending.front ();
pending.pop_front ();
@ -295,7 +298,7 @@ WebsocketsServer::write_client (Client wsi)
bool
WebsocketsServer::io_handler (Glib::IOCondition ioc, lws_sockfd_type fd)
{
// IO_IN=1, IO_PRI=2, IO_ERR=8, IO_HUP=16
/* IO_IN=1, IO_PRI=2, IO_ERR=8, IO_HUP=16 */
//printf ("io_handler ioc = %d\n", ioc);
LwsPollFdGlibSourceMap::iterator it = _fd_ctx.find (fd);

View file

@ -19,9 +19,9 @@
#ifndef websockets_server_h
#define websockets_server_h
#include <boost/unordered_map.hpp>
#include <glibmm.h>
#include <libwebsockets.h>
#include <boost/unordered_map.hpp>
#if LWS_LIBRARY_VERSION_MAJOR < 3
// <libwebsockets.h> includes <uv.h> which in turn includes
@ -34,10 +34,10 @@
// version 3 in order to keep things simpler for the end user
#endif
#include "component.h"
#include "client.h"
#include "state.h"
#include "component.h"
#include "message.h"
#include "state.h"
#define WEBSOCKET_LISTEN_PORT 9000
@ -51,7 +51,6 @@ struct LwsPollFdGlibSource {
class WebsocketsServer : public SurfaceComponent
{
public:
WebsocketsServer (ArdourSurface::ArdourWebsockets&);
virtual ~WebsocketsServer (){};
@ -62,7 +61,6 @@ class WebsocketsServer : public SurfaceComponent
void update_all_clients (const NodeState&, bool);
private:
struct lws_protocols _lws_proto[2];
struct lws_context_creation_info _lws_info;
struct lws_context* _lws_context;
@ -90,7 +88,6 @@ class WebsocketsServer : public SurfaceComponent
int ioc_to_events (Glib::IOCondition);
static int lws_callback (struct lws*, enum lws_callback_reasons, void*, void*, size_t);
};
#endif // websockets_server_h

View file

@ -16,20 +16,24 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <sstream>
#include <boost/unordered_set.hpp>
#include <sstream>
#include "state.h"
NodeState::NodeState () {}
NodeState::NodeState (std::string node)
: _node (node) { }
: _node (node)
{
}
NodeState::NodeState (std::string node, AddressVector addr, ValueVector val)
: _node (node)
, _addr (addr)
, _val (val) { }
, _val (val)
{
}
std::string
NodeState::debug_str () const
@ -38,7 +42,8 @@ NodeState::debug_str () const
s << "node = " << _node;
if (!_addr.empty ()) {
s << std::endl << " addr = ";
s << std::endl
<< " addr = ";
for (AddressVector::const_iterator it = _addr.begin (); it != _addr.end (); ++it) {
s << *it << ";";
@ -46,7 +51,8 @@ NodeState::debug_str () const
}
for (ValueVector::const_iterator it = _val.begin (); it != _val.end (); ++it) {
s << std::endl << " val " << it->debug_str ();
s << std::endl
<< " val " << it->debug_str ();
}
return s.str ();
@ -107,7 +113,8 @@ NodeState::operator== (const NodeState& other) const
return node_addr_hash () == other.node_addr_hash ();
}
std::size_t hash_value (const NodeState &state)
std::size_t
hash_value (const NodeState& state)
{
return state.node_addr_hash ();
}

View file

@ -19,17 +19,18 @@
#ifndef node_state_h
#define node_state_h
#include <stdint.h>
#include <climits>
#include <cmath>
#include <cstring>
#include <stdint.h>
#include <vector>
#include "typed_value.h"
#define ADDR_NONE UINT_MAX
namespace Node {
namespace Node
{
const std::string tempo = "tempo";
const std::string strip_desc = "strip_desc";
const std::string strip_meter = "strip_meter";
@ -40,22 +41,24 @@ namespace Node {
const std::string strip_plugin_enable = "strip_plugin_enable";
const std::string strip_plugin_param_desc = "strip_plugin_param_desc";
const std::string strip_plugin_param_value = "strip_plugin_param_value";
}
} // namespace Node
typedef std::vector<uint32_t> AddressVector;
typedef std::vector<TypedValue> ValueVector;
class NodeState {
class NodeState
{
public:
NodeState ();
NodeState (std::string);
NodeState (std::string, AddressVector, ValueVector = ValueVector ());
std::string debug_str () const;
std::string node () const { return _node; }
std::string node () const
{
return _node;
}
int n_addr () const;
uint32_t nth_addr (int) const;
@ -70,13 +73,12 @@ class NodeState {
bool operator== (const NodeState& other) const;
private:
std::string _node;
AddressVector _addr;
ValueVector _val;
};
std::size_t hash_value (const NodeState&);
std::size_t
hash_value (const NodeState&);
#endif // node_state_h

View file

@ -16,9 +16,9 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "ardour/session.h"
#include "ardour/plugin_insert.h"
#include "ardour/dB.h"
#include "ardour/plugin_insert.h"
#include "ardour/session.h"
#include "pbd/controllable.h"
#include "strips.h"
@ -28,7 +28,7 @@ using namespace ARDOUR;
int
ArdourStrips::start ()
{
// take an indexed snapshot of current strips
/* take an indexed snapshot of current strips */
StripableList strips;
session ().get_stripables (strips, PresentationInfo::AllStripables);
@ -85,7 +85,7 @@ ArdourStrips::set_strip_gain (uint32_t strip_n, double db)
double
ArdourStrips::strip_pan (uint32_t strip_n) const
{
// scale from [0.0 ; 1.0] to [-1.0 ; 1.0]
/* scale from [0.0 ; 1.0] to [-1.0 ; 1.0] */
return 2.0 * nth_strip (strip_n)->pan_azimuth_control ()->get_value () - 1.0;
}

View file

@ -25,8 +25,8 @@
class ArdourStrips : public SurfaceComponent
{
public:
ArdourStrips (ArdourSurface::ArdourWebsockets& surface) : SurfaceComponent (surface) {};
ArdourStrips (ArdourSurface::ArdourWebsockets& surface)
: SurfaceComponent (surface){};
virtual ~ArdourStrips (){};
int start ();
@ -61,10 +61,8 @@ class ArdourStrips : public SurfaceComponent
static TypedValue plugin_param_value (boost::shared_ptr<ARDOUR::AutomationControl>);
private:
typedef std::vector<boost::shared_ptr<ARDOUR::Stripable> > StripableVector;
StripableVector _strips;
};
#endif // ardour_strips_h

View file

@ -16,10 +16,10 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <boost/lexical_cast.hpp>
#include <cmath>
#include <limits>
#include <string>
#include <boost/lexical_cast.hpp>
#include "typed_value.h"
@ -29,35 +29,44 @@ TypedValue::TypedValue ()
: _type (Empty)
, _b (false)
, _i (0)
, _d (0) { }
, _d (0)
{
}
TypedValue::TypedValue (bool value)
: _type (Bool)
, _b (value)
, _i (0)
, _d (0) { }
, _d (0)
{
}
TypedValue::TypedValue (int value)
: _type (Int)
, _b (false)
, _i (value)
, _d (0) { }
, _d (0)
{
}
TypedValue::TypedValue (double value)
: _type (Double)
, _b (false)
, _i (0)
, _d (value) { }
, _d (value)
{
}
TypedValue::TypedValue (std::string value)
: _type (String)
, _b (false)
, _i (0)
, _d (0)
, _s (value) { }
, _s (value)
{
}
TypedValue::operator
bool () const
TypedValue::operator bool () const
{
switch (_type) {
case Bool:
@ -73,8 +82,7 @@ bool () const
}
}
TypedValue::operator
int () const
TypedValue::operator int () const
{
switch (_type) {
case Int:
@ -94,8 +102,7 @@ int () const
}
}
TypedValue::operator
double () const
TypedValue::operator double () const
{
switch (_type) {
case Double:
@ -115,8 +122,7 @@ double () const
}
}
TypedValue::operator
std::string () const
TypedValue::operator std::string () const
{
switch (_type) {
case String:
@ -136,9 +142,10 @@ bool
TypedValue::operator== (const TypedValue& other) const
{
if (_type != other._type) {
// make an exception when comparing doubles and ints
// for example browser json implementations will send
// 1 instead of 1.0 removing any type hint
/* make an exception when comparing doubles and ints
* for example browser json implementations will send
* 1 instead of 1.0 removing any type hint
*/
if ((_type == Int) && (other._type == Double)) {
return fabs (static_cast<double> (_i) - other._d) < DBL_TOLERANCE;
} else if ((_type == Double) && (other._type == Int)) {
@ -155,9 +162,7 @@ TypedValue::operator== (const TypedValue& other) const
return _i == other._i;
case Double: {
double inf = std::numeric_limits<double>::infinity ();
return ((_d == inf) && (other._d == inf))
|| ((_d == -inf) && (other._d == -inf))
|| (fabs (_d - other._d) < DBL_TOLERANCE);
return ((_d == inf) && (other._d == inf)) || ((_d == -inf) && (other._d == -inf)) || (fabs (_d - other._d) < DBL_TOLERANCE);
}
case String:
return _s == other._s;

View file

@ -24,7 +24,6 @@
class TypedValue
{
public:
enum Type {
Empty,
Bool,
@ -39,8 +38,14 @@ class TypedValue
TypedValue (double);
TypedValue (std::string);
bool empty () const { return _type == Empty; };
Type type () const { return _type; };
bool empty () const
{
return _type == Empty;
};
Type type () const
{
return _type;
};
operator bool () const;
operator int () const;
@ -53,13 +58,11 @@ class TypedValue
std::string debug_str () const;
private:
Type _type;
bool _b;
int _i;
double _d;
std::string _s;
};
#endif // typed_value_h