From 6e499e2cc5d2e1566145ac3719c9be9ca67d1bea Mon Sep 17 00:00:00 2001 From: Luciano Iam Date: Thu, 20 Feb 2020 23:16:02 +0100 Subject: [PATCH] Make code adhere to C++98 (WIP) --- libs/surfaces/websockets/client.h | 4 +- libs/surfaces/websockets/dispatcher.cc | 4 +- libs/surfaces/websockets/dispatcher.h | 7 ++-- libs/surfaces/websockets/feedback.cc | 3 +- libs/surfaces/websockets/feedback.h | 2 +- libs/surfaces/websockets/message.cc | 6 +-- libs/surfaces/websockets/server.cc | 3 +- libs/surfaces/websockets/server.h | 6 +-- libs/surfaces/websockets/state.cc | 50 ++++++++++++------------- libs/surfaces/websockets/state.h | 38 +++++-------------- libs/surfaces/websockets/strips.h | 2 +- libs/surfaces/websockets/typed_value.cc | 44 +++++++++++++++++++--- libs/surfaces/websockets/typed_value.h | 16 ++++---- 13 files changed, 96 insertions(+), 89 deletions(-) diff --git a/libs/surfaces/websockets/client.h b/libs/surfaces/websockets/client.h index 1f75163338..bf90fb5ad0 100644 --- a/libs/surfaces/websockets/client.h +++ b/libs/surfaces/websockets/client.h @@ -19,7 +19,7 @@ #ifndef client_context_h #define client_context_h -#include +#include #include #include "state.h" @@ -48,7 +48,7 @@ class ClientContext Client _wsi; - typedef std::unordered_set ClientState; + typedef boost::unordered_set ClientState; ClientState _state; ClientOutputBuffer _output_buf; diff --git a/libs/surfaces/websockets/dispatcher.cc b/libs/surfaces/websockets/dispatcher.cc index dbb627a027..88cd2024e4 100644 --- a/libs/surfaces/websockets/dispatcher.cc +++ b/libs/surfaces/websockets/dispatcher.cc @@ -187,8 +187,8 @@ WebsocketsDispatcher::strip_plugin_param_value_handler (Client client, const Nod } void -WebsocketsDispatcher::update (Client client, std::string node, - std::initializer_list addr, std::initializer_list val) +WebsocketsDispatcher::update (Client client, std::string node, std::vector addr, + std::vector val) { server ().update_client (client, { node, addr, val }, true); } diff --git a/libs/surfaces/websockets/dispatcher.h b/libs/surfaces/websockets/dispatcher.h index 07739ee218..a6b60d9221 100644 --- a/libs/surfaces/websockets/dispatcher.h +++ b/libs/surfaces/websockets/dispatcher.h @@ -19,7 +19,7 @@ #ifndef websockets_dispatcher_h #define websockets_dispatcher_h -#include +#include #include "component.h" #include "client.h" @@ -39,7 +39,7 @@ class WebsocketsDispatcher : public SurfaceComponent private: typedef void (WebsocketsDispatcher::*DispatcherMethod) (Client, const NodeStateMessage&); - typedef std::unordered_map NodeMethodMap; + typedef boost::unordered_map NodeMethodMap; static NodeMethodMap _node_to_method; @@ -50,8 +50,7 @@ class WebsocketsDispatcher : public SurfaceComponent void strip_plugin_enable_handler (Client, const NodeStateMessage&); void strip_plugin_param_value_handler (Client, const NodeStateMessage&); - void update (Client, std::string, std::initializer_list, - std::initializer_list); + void update (Client, std::string, std::vector, std::vector); }; diff --git a/libs/surfaces/websockets/feedback.cc b/libs/surfaces/websockets/feedback.cc index 7cb52a76ef..de7fad9c7e 100644 --- a/libs/surfaces/websockets/feedback.cc +++ b/libs/surfaces/websockets/feedback.cc @@ -164,8 +164,7 @@ ArdourFeedback::observe_strip_plugin_param_values (uint32_t strip_n, } void -ArdourFeedback::update_all (std::string node, std::initializer_list addr, - TypedValue val) const +ArdourFeedback::update_all (std::string node, std::vector addr, TypedValue val) const { server ().update_all_clients ({ node, addr, { val }}, false); } diff --git a/libs/surfaces/websockets/feedback.h b/libs/surfaces/websockets/feedback.h index 8a8c6d33d4..c4490ca09e 100644 --- a/libs/surfaces/websockets/feedback.h +++ b/libs/surfaces/websockets/feedback.h @@ -49,7 +49,7 @@ class ArdourFeedback : public SurfaceComponent void observe_strip_plugin_param_values (uint32_t, uint32_t, boost::shared_ptr); - void update_all (std::string, std::initializer_list, TypedValue) const; + void update_all (std::string, std::vector, TypedValue) const; }; diff --git a/libs/surfaces/websockets/message.cc b/libs/surfaces/websockets/message.cc index 941e01f539..b82291d245 100644 --- a/libs/surfaces/websockets/message.cc +++ b/libs/surfaces/websockets/message.cc @@ -47,13 +47,13 @@ NodeStateMessage::NodeStateMessage (void *buf, size_t len) , _write (false) { try { - std::string s { static_cast(buf), len }; + std::string s (static_cast(buf), len); - std::istringstream is { s }; + std::istringstream is (s); pt::ptree root; pt::read_json (is, root); - _state = NodeState { root.get ("node") }; + _state = NodeState (root.get ("node")); pt::ptree addr = root.get_child ("addr", pt::ptree ()); diff --git a/libs/surfaces/websockets/server.cc b/libs/surfaces/websockets/server.cc index e3f8fe7b7e..178aeca011 100644 --- a/libs/surfaces/websockets/server.cc +++ b/libs/surfaces/websockets/server.cc @@ -25,8 +25,7 @@ using namespace Glib; -WebsocketsServer::WebsocketsServer - (ArdourSurface::ArdourWebsockets& surface) +WebsocketsServer::WebsocketsServer (ArdourSurface::ArdourWebsockets& surface) : SurfaceComponent (surface) , _lws_context (0) { diff --git a/libs/surfaces/websockets/server.h b/libs/surfaces/websockets/server.h index afb136f442..462b4887f3 100644 --- a/libs/surfaces/websockets/server.h +++ b/libs/surfaces/websockets/server.h @@ -19,7 +19,7 @@ #ifndef websockets_server_h #define websockets_server_h -#include +#include #include #if LWS_LIBRARY_VERSION_MAJOR < 3 @@ -68,10 +68,10 @@ class WebsocketsServer : public SurfaceComponent Glib::RefPtr _channel; - typedef std::unordered_map LwsPollFdGlibSourceMap; + typedef boost::unordered_map LwsPollFdGlibSourceMap; LwsPollFdGlibSourceMap _fd_ctx; - typedef std::unordered_map ClientContextMap; + typedef boost::unordered_map ClientContextMap; ClientContextMap _client_ctx; void add_poll_fd (struct lws_pollargs*); diff --git a/libs/surfaces/websockets/state.cc b/libs/surfaces/websockets/state.cc index ed31103863..63e76b4afb 100644 --- a/libs/surfaces/websockets/state.cc +++ b/libs/surfaces/websockets/state.cc @@ -17,28 +17,20 @@ */ #include +#include #include "state.h" -NodeState::NodeState () -{ - update_node_addr_hash (); -} +NodeState::NodeState () { } NodeState::NodeState (std::string node) - : _node (node) -{ - update_node_addr_hash (); -} + : _node (node) { } -NodeState::NodeState (std::string node, std::initializer_list addr, - std::initializer_list val) +NodeState::NodeState (std::string node, std::vector addr, + std::vector val) : _node (node) , _addr (addr) - , _val (val) -{ - update_node_addr_hash (); -} + , _val (val) { } std::string NodeState::debug_str () const @@ -57,8 +49,6 @@ NodeState::debug_str () const for (std::vector::const_iterator it = _val.begin (); it != _val.end (); ++it) { s << std::endl << " val " << it->debug_str (); } - - s << std::endl << " hash = " << _node_addr_hash; return s.str (); } @@ -79,7 +69,6 @@ void NodeState::add_addr (uint32_t addr) { _addr.push_back (addr); - update_node_addr_hash (); } int @@ -104,15 +93,22 @@ NodeState::add_val (TypedValue val) _val.push_back (val); } -void -NodeState::update_node_addr_hash () +std::size_t +NodeState::node_addr_hash () const { - std::stringstream ss; - ss << _node; - - for (std::vector::iterator it = _addr.begin (); it != _addr.end (); ++it) { - ss << "_" << *it; - } - - _node_addr_hash = ss.str (); + std::size_t seed = 0; + boost::hash_combine (seed, _node); + boost::hash_combine (seed, _addr); + return seed; +} + +bool +NodeState::operator== (const NodeState& other) const +{ + return node_addr_hash () == other.node_addr_hash (); +} + +std::size_t hash_value (const NodeState &state) +{ + return state.node_addr_hash (); } diff --git a/libs/surfaces/websockets/state.h b/libs/surfaces/websockets/state.h index 814eb3ccd4..f9e0f8d19e 100644 --- a/libs/surfaces/websockets/state.h +++ b/libs/surfaces/websockets/state.h @@ -19,9 +19,10 @@ #ifndef node_state_h #define node_state_h -#include +#include #include #include +#include #include "typed_value.h" @@ -36,7 +37,7 @@ 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"; -}; +} class NodeState { @@ -44,8 +45,8 @@ class NodeState { NodeState (); NodeState (std::string); - NodeState (std::string, std::initializer_list, - std::initializer_list = {}); + NodeState (std::string, std::vector, + std::vector = std::vector()); std::string debug_str () const; @@ -59,37 +60,18 @@ class NodeState { TypedValue nth_val (int) const; void add_val (TypedValue); + std::size_t node_addr_hash () const; + + bool operator== (const NodeState& other) const; + private: std::string _node; std::vector _addr; std::vector _val; - std::string _node_addr_hash; - - void update_node_addr_hash (); - - friend struct std::hash; - friend struct std::equal_to; }; -namespace std { - template <> - struct hash { - size_t operator () (const NodeState &state) const { - // std::hash produces a hash of the value of the - // pointer (the memory address), it does not examine the contents - // of any character array. - return std::hash()(state._node_addr_hash); - } - }; - - template<> - struct equal_to { - bool operator() (const NodeState& lhs, const NodeState& rhs) const { - return lhs._node_addr_hash == rhs._node_addr_hash; - } - }; -} +std::size_t hash_value (const NodeState&); #endif // node_state_h diff --git a/libs/surfaces/websockets/strips.h b/libs/surfaces/websockets/strips.h index fd740c1bad..264ce85eb2 100644 --- a/libs/surfaces/websockets/strips.h +++ b/libs/surfaces/websockets/strips.h @@ -62,7 +62,7 @@ class ArdourStrips : public SurfaceComponent private: - typedef std::vector> StripableVector; + typedef std::vector > StripableVector; StripableVector _strips; }; diff --git a/libs/surfaces/websockets/typed_value.cc b/libs/surfaces/websockets/typed_value.cc index 808142a36f..3c44c66d4e 100644 --- a/libs/surfaces/websockets/typed_value.cc +++ b/libs/surfaces/websockets/typed_value.cc @@ -19,11 +19,43 @@ #include #include #include +#include #include "typed_value.h" #define DBL_TOLERANCE 0.001 +TypedValue::TypedValue () + : _type (Empty) + , _b (false) + , _i (0) + , _d (0) { } + +TypedValue::TypedValue (bool value) + : _type (Bool) + , _b (value) + , _i (0) + , _d (0) { } + +TypedValue::TypedValue (int value) + : _type (Int) + , _b (false) + , _i (value) + , _d (0) { } + +TypedValue::TypedValue (double value) + : _type (Double) + , _b (false) + , _i (0) + , _d (value) { } + +TypedValue::TypedValue (std::string value) + : _type (String) + , _b (false) + , _i (0) + , _d (0) + , _s (value) { } + TypedValue::operator bool () const { @@ -53,8 +85,8 @@ int () const return static_cast(_d); case String: try { - return std::stoi (_s); - } catch (const std::exception&) { + return boost::lexical_cast (_s); + } catch (const boost::bad_lexical_cast&) { return 0; } default: @@ -74,8 +106,8 @@ double () const return static_cast(_i); case String: try { - return std::stod (_s); - } catch (const std::exception&) { + return boost::lexical_cast (_s); + } catch (const boost::bad_lexical_cast&) { return 0; } default: @@ -92,9 +124,9 @@ std::string () const case Bool: return _b ? "true" : "false"; case Int: - return std::to_string (_i); + return boost::lexical_cast (_i); case Double: - return std::to_string (_d); + return boost::lexical_cast (_d); default: return ""; } diff --git a/libs/surfaces/websockets/typed_value.h b/libs/surfaces/websockets/typed_value.h index 0f4680d693..a30ef3e940 100644 --- a/libs/surfaces/websockets/typed_value.h +++ b/libs/surfaces/websockets/typed_value.h @@ -33,11 +33,11 @@ class TypedValue String }; - TypedValue (): _type (Empty) { } - TypedValue (bool value): _type { Bool }, _b (value) { } - TypedValue (int value): _type { Int }, _i (value) { } - TypedValue (double value): _type { Double }, _d (value) { } - TypedValue (std::string value): _type { String }, _s (value) { } + TypedValue (); + TypedValue (bool); + TypedValue (int); + TypedValue (double); + TypedValue (std::string); bool empty () const { return _type == Empty; }; Type type () const { return _type; }; @@ -55,9 +55,9 @@ class TypedValue private: Type _type; - bool _b = false; - int _i = 0; - double _d = 0.0; + bool _b; + int _i; + double _d; std::string _s; };