diff --git a/revision.txt b/revision.txt index e522732..425151f 100644 --- a/revision.txt +++ b/revision.txt @@ -1 +1 @@ -38 +40 diff --git a/src/FlowException.h b/src/FlowException.h index e7dde5c..ed867f2 100644 --- a/src/FlowException.h +++ b/src/FlowException.h @@ -38,15 +38,15 @@ namespace Flows /** * Base class for all Flow exceptions */ -class FlowException +class FlowException : public std::exception { public: - FlowException(std::string message) { _message = message; } - virtual ~FlowException() {} + explicit FlowException(const std::string& message) { _message = message.c_str(); } + virtual ~FlowException() = default; - const std::string what() const { return _message; } + const char* what() const noexcept override { return _message; } protected: - std::string _message; + const char* _message = nullptr; }; } #endif diff --git a/src/INode.cpp b/src/INode.cpp index a3b3f26..66083ea 100644 --- a/src/INode.cpp +++ b/src/INode.cpp @@ -63,23 +63,28 @@ INode::~INode() _getConfigParameter = std::function(); } -void INode::setLog(std::function value) +void INode::setLog(std::function value) { _log.swap(value); if(_out) _out->setLog(&_log); } -void INode::log(int32_t logLevel, std::string message) +void INode::log(int32_t logLevel, const std::string& message) { if(_log) _log(_id, logLevel, message); } -void INode::subscribePeer(uint64_t peerId, int32_t channel, std::string variable) +void INode::frontendEventLog(const std::string& message) +{ + if(_frontendEventLog) _frontendEventLog(_id, message); +} + +void INode::subscribePeer(uint64_t peerId, int32_t channel, const std::string& variable) { if(_subscribePeer) _subscribePeer(_id, peerId, channel, variable); } -void INode::unsubscribePeer(uint64_t peerId, int32_t channel, std::string variable) +void INode::unsubscribePeer(uint64_t peerId, int32_t channel, const std::string& variable) { if(_unsubscribePeer) _unsubscribePeer(_id, peerId, channel, variable); } @@ -106,22 +111,22 @@ void INode::unsubscribeGlobal() void INode::output(uint32_t index, PVariable message, bool synchronous) { - if(_output) _output(_id, index, message, synchronous); + if(_output) _output(_id, index, std::move(message), synchronous); } -PVariable INode::invoke(std::string methodName, PArray parameters) +PVariable INode::invoke(const std::string& methodName, PArray parameters) { - if(_invoke) return _invoke(methodName, parameters); + if(_invoke) return _invoke(methodName, std::move(parameters)); return Variable::createError(-32500, "No callback method set."); } -PVariable INode::invokeNodeMethod(std::string nodeId, std::string methodName, PArray parameters, bool wait) +PVariable INode::invokeNodeMethod(const std::string& nodeId, const std::string& methodName, PArray parameters, bool wait) { - if(_invokeNodeMethod) return _invokeNodeMethod(nodeId, methodName, parameters, wait); + if(_invokeNodeMethod) return _invokeNodeMethod(nodeId, methodName, std::move(parameters), wait); return Variable::createError(-32500, "No callback method set."); } -PVariable INode::invokeLocal(std::string methodName, PArray parameters) +PVariable INode::invokeLocal(const std::string& methodName, PArray parameters) { std::map>::iterator localMethodIterator = _localRpcMethods.find(methodName); if(localMethodIterator == _localRpcMethods.end()) @@ -133,61 +138,61 @@ PVariable INode::invokeLocal(std::string methodName, PArray parameters) return localMethodIterator->second(parameters); } -void INode::nodeEvent(std::string topic, PVariable value) +void INode::nodeEvent(const std::string& topic, PVariable value) { - if(_nodeEvent) _nodeEvent(_id, topic, value); + if(_nodeEvent) _nodeEvent(_id, topic, std::move(value)); } -PVariable INode::getNodeData(std::string key) +PVariable INode::getNodeData(const std::string& key) { if(_getNodeData) return _getNodeData(_id, key); return Variable::createError(-32500, "No callback method set."); } -void INode::setNodeData(std::string key, PVariable value) +void INode::setNodeData(const std::string& key, PVariable value) { - if(_setNodeData) _setNodeData(_id, key, value); + if(_setNodeData) _setNodeData(_id, key, std::move(value)); } -PVariable INode::getFlowData(std::string key) +PVariable INode::getFlowData(const std::string& key) { if(_getFlowData) return _getFlowData(_flowId, key); return Variable::createError(-32500, "No callback method set."); } -void INode::setFlowData(std::string key, PVariable value) +void INode::setFlowData(const std::string& key, PVariable value) { - if(_setFlowData) _setFlowData(_flowId, key, value); + if(_setFlowData) _setFlowData(_flowId, key, std::move(value)); } -PVariable INode::getGlobalData(std::string key) +PVariable INode::getGlobalData(const std::string& key) { if(_getGlobalData) return _getGlobalData(key); return Variable::createError(-32500, "No callback method set."); } -void INode::setGlobalData(std::string key, PVariable value) +void INode::setGlobalData(const std::string& key, PVariable value) { - if(_setGlobalData) _setGlobalData(key, value); + if(_setGlobalData) _setGlobalData(key, std::move(value)); } void INode::setInternalMessage(PVariable message) { - if(_setInternalMessage) _setInternalMessage(_id, message); + if(_setInternalMessage) _setInternalMessage(_id, std::move(message)); } -PVariable INode::getConfigParameter(std::string nodeId, std::string name) +PVariable INode::getConfigParameter(const std::string& nodeId, const std::string& name) { if(_getConfigParameter) return _getConfigParameter(nodeId, name); return Variable::createError(-32500, "No callback method set."); } -PVariable INode::getNodeVariable(std::string variable) +PVariable INode::getNodeVariable(const std::string& variable) { return Flows::PVariable(); } -void INode::setNodeVariable(std::string variable, PVariable value) +void INode::setNodeVariable(const std::string& variable, PVariable value) { } diff --git a/src/INode.h b/src/INode.h index 7c96e8f..f0cd9a8 100644 --- a/src/INode.h +++ b/src/INode.h @@ -49,13 +49,15 @@ class INode { public: INode(std::string path, std::string nodeNamespace, std::string type, const std::atomic_bool* frontendConnected); + INode(const INode&) = delete; + INode& operator=(const INode&) = delete; virtual ~INode(); std::string getNamespace() { return _namespace; } std::string getType() { return _type; } std::string getPath() { return _path; } std::string getFlowId() { return _flowId; } - void setFlowId(std::string value) { _flowId = value; } + void setFlowId(const std::string& value) { _flowId = value; } std::string getId() { return _id; } void setId(std::string value) { _id = value; if(_out) _out->setNodeId(value); } std::mutex& getInputMutex() { return _inputMutex; } @@ -101,12 +103,12 @@ class INode /** * Mustn't block. */ - virtual PVariable getNodeVariable(std::string variable); + virtual PVariable getNodeVariable(const std::string& variable); /** * Mustn't block. */ - virtual void setNodeVariable(std::string variable, PVariable value); + virtual void setNodeVariable(const std::string& variable, PVariable value); /** * Mustn't block. @@ -114,25 +116,25 @@ class INode virtual PVariable getConfigParameterIncoming(std::string name) { return std::make_shared(); } // {{{ Internal methods - void setLog(std::function value); - void setSubscribePeer(std::function value) { _subscribePeer.swap(value); } - void setUnsubscribePeer(std::function value) { _unsubscribePeer.swap(value); } - void setSubscribeFlow(std::function value) { _subscribeFlow.swap(value); } - void setUnsubscribeFlow(std::function value) { _unsubscribeFlow.swap(value); } - void setSubscribeGlobal(std::function value) { _subscribeGlobal.swap(value); } - void setUnsubscribeGlobal(std::function value) { _unsubscribeGlobal.swap(value); } - void setOutput(std::function value) { _output.swap(value); } - void setInvoke(std::function value) { _invoke.swap(value); } - void setInvokeNodeMethod(std::function value) { _invokeNodeMethod.swap(value); } - void setNodeEvent(std::function value) { _nodeEvent.swap(value); } - void setGetNodeData(std::function value) { _getNodeData.swap(value); } - void setSetNodeData(std::function value) { _setNodeData.swap(value); } - void setGetFlowData(std::function value) { _getFlowData.swap(value); } - void setSetFlowData(std::function value) { _setFlowData.swap(value); } - void setGetGlobalData(std::function value) { _getGlobalData.swap(value); } - void setSetGlobalData(std::function value) { _setGlobalData.swap(value); } - void setSetInternalMessage(std::function value) { _setInternalMessage.swap(value); } - void setGetConfigParameter(std::function value) { _getConfigParameter.swap(value); } + void setLog(std::function value); + void setSubscribePeer(std::function value) { _subscribePeer.swap(value); } + void setUnsubscribePeer(std::function value) { _unsubscribePeer.swap(value); } + void setSubscribeFlow(std::function value) { _subscribeFlow.swap(value); } + void setUnsubscribeFlow(std::function value) { _unsubscribeFlow.swap(value); } + void setSubscribeGlobal(std::function value) { _subscribeGlobal.swap(value); } + void setUnsubscribeGlobal(std::function value) { _unsubscribeGlobal.swap(value); } + void setOutput(std::function value) { _output.swap(value); } + void setInvoke(std::function value) { _invoke.swap(value); } + void setInvokeNodeMethod(std::function value) { _invokeNodeMethod.swap(value); } + void setNodeEvent(std::function value) { _nodeEvent.swap(value); } + void setGetNodeData(std::function value) { _getNodeData.swap(value); } + void setSetNodeData(std::function value) { _setNodeData.swap(value); } + void setGetFlowData(std::function value) { _getFlowData.swap(value); } + void setSetFlowData(std::function value) { _setFlowData.swap(value); } + void setGetGlobalData(std::function value) { _getGlobalData.swap(value); } + void setSetGlobalData(std::function value) { _setGlobalData.swap(value); } + void setSetInternalMessage(std::function value) { _setInternalMessage.swap(value); } + void setGetConfigParameter(std::function value) { _getConfigParameter.swap(value); } // }}} /** @@ -147,7 +149,7 @@ class INode /** * Executes local RPC method. Mustn't block. */ - virtual PVariable invokeLocal(std::string methodName, PArray parameters); + virtual PVariable invokeLocal(const std::string& methodName, PArray parameters); protected: std::shared_ptr _out; std::string _path; @@ -162,51 +164,50 @@ class INode */ std::map> _localRpcMethods; - void log(int32_t logLevel, std::string message); - void subscribePeer(uint64_t peerId, int32_t channel = -1, std::string variable = ""); - void unsubscribePeer(uint64_t peerId, int32_t channel = -1, std::string variable = ""); + void log(int32_t logLevel, const std::string& message); + void frontendEventLog(const std::string& message); + void subscribePeer(uint64_t peerId, int32_t channel = -1, const std::string& variable = ""); + void unsubscribePeer(uint64_t peerId, int32_t channel = -1, const std::string& variable = ""); void subscribeFlow(); void unsubscribeFlow(); void subscribeGlobal(); void unsubscribeGlobal(); void output(uint32_t outputIndex, PVariable message, bool synchronous = false); - PVariable invoke(std::string methodName, PArray parameters); - PVariable invokeNodeMethod(std::string nodeId, std::string methodName, PArray parameters, bool); - void nodeEvent(std::string topic, PVariable value); - PVariable getNodeData(std::string key); - void setNodeData(std::string key, PVariable value); - PVariable getFlowData(std::string key); - void setFlowData(std::string key, PVariable value); - PVariable getGlobalData(std::string key); - void setGlobalData(std::string key, PVariable value); + PVariable invoke(const std::string& methodName, PArray parameters); + PVariable invokeNodeMethod(const std::string& nodeId, const std::string& methodName, PArray parameters, bool); + void nodeEvent(const std::string& topic, PVariable value); + PVariable getNodeData(const std::string& key); + void setNodeData(const std::string& key, PVariable value); + PVariable getFlowData(const std::string& key); + void setFlowData(const std::string& key, PVariable value); + PVariable getGlobalData(const std::string& key); + void setGlobalData(const std::string& key, PVariable value); void setInternalMessage(PVariable message); - PVariable getConfigParameter(std::string nodeId, std::string name); + PVariable getConfigParameter(const std::string& nodeId, const std::string& name); private: std::atomic_bool _locked; std::atomic_int _referenceCounter; std::mutex _inputMutex; - std::function _log; - std::function _subscribePeer; - std::function _unsubscribePeer; - std::function _subscribeFlow; - std::function _unsubscribeFlow; - std::function _subscribeGlobal; - std::function _unsubscribeGlobal; - std::function _output; - std::function _invoke; - std::function _invokeNodeMethod; - std::function _nodeEvent; - std::function _getNodeData; - std::function _setNodeData; - std::function _getFlowData; - std::function _setFlowData; - std::function _getGlobalData; - std::function _setGlobalData; - std::function _setInternalMessage; - std::function _getConfigParameter; - - INode(const INode&) = delete; - INode& operator=(const INode&) = delete; + std::function _log; + std::function _frontendEventLog; + std::function _subscribePeer; + std::function _unsubscribePeer; + std::function _subscribeFlow; + std::function _unsubscribeFlow; + std::function _subscribeGlobal; + std::function _unsubscribeGlobal; + std::function _output; + std::function _invoke; + std::function _invokeNodeMethod; + std::function _nodeEvent; + std::function _getNodeData; + std::function _setNodeData; + std::function _getFlowData; + std::function _setFlowData; + std::function _getGlobalData; + std::function _setGlobalData; + std::function _setInternalMessage; + std::function _getConfigParameter; }; typedef std::shared_ptr PINode; diff --git a/src/Output.cpp b/src/Output.cpp index bcf9f82..501e084 100644 --- a/src/Output.cpp +++ b/src/Output.cpp @@ -33,7 +33,7 @@ namespace Flows { -Output::Output(std::string& nodeId, std::function* logMethod) +Output::Output(std::string& nodeId, std::function* logMethod) { _nodeId = nodeId; _log = logMethod; diff --git a/src/Output.h b/src/Output.h index edc73a6..33f1ea3 100644 --- a/src/Output.h +++ b/src/Output.h @@ -48,7 +48,7 @@ class Output /** * The main constructor. */ - Output(std::string& nodeId, std::function* logMethod); + Output(std::string& nodeId, std::function* logMethod); /** * The destructor. @@ -68,7 +68,7 @@ class Output * * @param value The new log function. */ - void setLog(std::function* value) { _log = value; } + void setLog(std::function* value) { _log = value; } /** * Prints an error message with filename, line number and function name. @@ -160,7 +160,7 @@ class Output */ private: std::string _nodeId; - std::function* _log = nullptr; + std::function* _log = nullptr; }; } #endif diff --git a/src/Variable.cpp b/src/Variable.cpp index 6db3e92..04efec6 100755 --- a/src/Variable.cpp +++ b/src/Variable.cpp @@ -353,7 +353,7 @@ bool Variable::operator!=(const Variable& rhs) return !(operator==(rhs)); } -Variable::operator Variable::bool_type() const +Variable::operator bool_type() const { bool result = false; if(type != VariableType::tBoolean)