From ceba58045049d771d7db726b5bcc63a92b519b2a Mon Sep 17 00:00:00 2001 From: Jason Watkins Date: Fri, 8 May 2015 15:18:24 -0700 Subject: [PATCH] Addressed issues found by Google code lint. Fixed: - Removed trailing whitespace. - Removed out of date TODO's. - Added #include to DataManager.cpp - Reordered includes to include C system headers before C++ system headers. - Added username to TODO's - Renamed include guards to use full project name as prefix. - Made methods and parameters const where possible. - Made UDPSocket constructor explicit. Won't Fix: - snprintf is introduced in C++11. - Trailing comments at the end of namespace & header guards - Did not re-inlude headers already included by the matching XPC header. - localtime is fine since X-Plane is not multithreaded at all on the plugin side. - Linter confuses #elif with parens as a function call. - namespace indentation --- xpcPlugin/DataManager.cpp | 4 ++-- xpcPlugin/DataManager.h | 4 ++-- xpcPlugin/Drawing.h | 4 ++-- xpcPlugin/Log.cpp | 5 +++-- xpcPlugin/Log.h | 4 ++-- xpcPlugin/Message.cpp | 16 ++++++++-------- xpcPlugin/Message.h | 18 +++++++++--------- xpcPlugin/MessageHandlers.cpp | 28 ++++++++++++++-------------- xpcPlugin/MessageHandlers.h | 30 +++++++++++++++--------------- xpcPlugin/UDPSocket.cpp | 8 ++++---- xpcPlugin/UDPSocket.h | 12 ++++++------ xpcPlugin/XPCPlugin.cpp | 19 ++++++++++--------- 12 files changed, 77 insertions(+), 75 deletions(-) diff --git a/xpcPlugin/DataManager.cpp b/xpcPlugin/DataManager.cpp index cb9ec3b..4a2bd97 100644 --- a/xpcPlugin/DataManager.cpp +++ b/xpcPlugin/DataManager.cpp @@ -20,6 +20,7 @@ #include "XPLMDataAccess.h" #include "XPLMGraphics.h" +#include #include #include #include @@ -516,7 +517,7 @@ namespace XPC drefSize = min(drefSize, size); XPLMSetDatavf(xdref, values, 0, drefSize); } - + void DataManager::Set(DREF dref, int values[], int size, char aircraft) { const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; @@ -738,7 +739,6 @@ namespace XPC Set(DREF_LocalY, local[1], aircraft); Set(DREF_LocalZ, local[2], aircraft); // If the sim is unpaused, this will override the above settings. - // TODO: Are these setable when paused? Are these necessary? Set(DREF_Latitude, (double)pos[0], aircraft); Set(DREF_Longitude, (double)pos[1], aircraft); Set(DREF_Elevation, (double)pos[2], aircraft); diff --git a/xpcPlugin/DataManager.h b/xpcPlugin/DataManager.h index 74010d2..1bb7ffb 100644 --- a/xpcPlugin/DataManager.h +++ b/xpcPlugin/DataManager.h @@ -1,7 +1,7 @@ //Copyright (c) 2013-2015 United States Government as represented by the Administrator of the //National Aeronautics and Space Administration. All Rights Reserved. -#ifndef XPC_DATAMANAGER_H -#define XPC_DATAMANAGER_H +#ifndef XPCPLUGIN_DATAMANAGER_H_ +#define XPCPLUGIN_DATAMANAGER_H_ #include diff --git a/xpcPlugin/Drawing.h b/xpcPlugin/Drawing.h index d24b2bf..ce1573a 100644 --- a/xpcPlugin/Drawing.h +++ b/xpcPlugin/Drawing.h @@ -1,7 +1,7 @@ //Copyright (c) 2013-2015 United States Government as represented by the Administrator of the //National Aeronautics and Space Administration. All Rights Reserved. -#ifndef XPC_DRAWING_H -#define XPC_DRAWING_H +#ifndef XPCPLUGIN_DRAWING_H_ +#define XPCPLUGIN_DRAWING_H_ #include diff --git a/xpcPlugin/Log.cpp b/xpcPlugin/Log.cpp index 6afc271..2266e99 100644 --- a/xpcPlugin/Log.cpp +++ b/xpcPlugin/Log.cpp @@ -4,10 +4,11 @@ #include "XPLMUtilities.h" -#include #include #include #include + +#include #include #include @@ -33,7 +34,7 @@ namespace XPC << std::setw(2) << tm->tm_min << ":" << std::setw(2) << tm->tm_sec << "." << std::setw(3) << ms.count() << "]"; - + std::fprintf(fd, ss.str().c_str()); } diff --git a/xpcPlugin/Log.h b/xpcPlugin/Log.h index b7413c1..6887eb9 100644 --- a/xpcPlugin/Log.h +++ b/xpcPlugin/Log.h @@ -1,7 +1,7 @@ //Copyright (c) 2013-2015 United States Government as represented by the Administrator of the //National Aeronautics and Space Administration. All Rights Reserved. -#ifndef XPC_LOG_H -#define XPC_LOG_H +#ifndef XPCPLUGIN_LOG_H_ +#define XPCPLUGIN_LOG_H_ #include // LOG_VERBOSITY determines the level of logging throughout the plugin. diff --git a/xpcPlugin/Message.cpp b/xpcPlugin/Message.cpp index 3221a8f..2f8f7e3 100644 --- a/xpcPlugin/Message.cpp +++ b/xpcPlugin/Message.cpp @@ -11,15 +11,15 @@ namespace XPC { Message::Message() {} - Message Message::ReadFrom(UDPSocket& sock) + Message Message::ReadFrom(const UDPSocket& sock) { Message m; int len = sock.Read(m.buffer, bufferSize, &m.source); m.size = len < 0 ? 0 : len; return m; } - - unsigned long Message::GetMagicNumber() + + unsigned long Message::GetMagicNumber() const { if (size < 4) { @@ -28,7 +28,7 @@ namespace XPC return *((unsigned long*)buffer); } - std::string Message::GetHead() + std::string Message::GetHead() const { if (size < 4) { @@ -37,7 +37,7 @@ namespace XPC return std::string((char*)buffer, 4); } - const unsigned char* Message::GetBuffer() + const unsigned char* Message::GetBuffer() const { if (size == 0) { @@ -46,17 +46,17 @@ namespace XPC return buffer; } - std::size_t Message::GetSize() + std::size_t Message::GetSize() const { return size; } - struct sockaddr Message::GetSource() + struct sockaddr Message::GetSource() const { return source; } - void Message::PrintToLog() + void Message::PrintToLog() const { #if LOG_VERBOSITY > 4 std::stringstream ss; diff --git a/xpcPlugin/Message.h b/xpcPlugin/Message.h index b44b41d..56352f6 100644 --- a/xpcPlugin/Message.h +++ b/xpcPlugin/Message.h @@ -1,7 +1,7 @@ //Copyright (c) 2013-2015 United States Government as represented by the Administrator of the //National Aeronautics and Space Administration. All Rights Reserved. -#ifndef XPC_MESSAGE_H -#define XPC_MESSAGE_H +#ifndef XPCPLUGIN_MESSAGE_H_ +#define XPCPLUGIN_MESSAGE_H_ #include "UDPSocket.h" @@ -24,25 +24,25 @@ namespace XPC /// \returns A message parsed from the data read from sock. If no /// data was read or an error occurs, returns a message /// with the size set to 0. - static Message ReadFrom(UDPSocket& sock); + static Message ReadFrom(const UDPSocket& sock); /// Gets the message header in binary form. - unsigned long GetMagicNumber(); + unsigned long GetMagicNumber() const; /// Gets the message header. - std::string GetHead(); + std::string GetHead() const; /// Gets the buffer underlying the message. - const unsigned char* GetBuffer(); + const unsigned char* GetBuffer() const; /// Gets the size of the message in bytes. - std::size_t GetSize(); + std::size_t GetSize() const; /// Gets the address this message was read from. - struct sockaddr GetSource(); + struct sockaddr GetSource() const; /// Prints the contents of the message to the XPC log. - void PrintToLog(); + void PrintToLog() const; private: Message(); diff --git a/xpcPlugin/MessageHandlers.cpp b/xpcPlugin/MessageHandlers.cpp index 37c7e71..281d59f 100644 --- a/xpcPlugin/MessageHandlers.cpp +++ b/xpcPlugin/MessageHandlers.cpp @@ -113,7 +113,7 @@ namespace XPC connection.id, connectionKey.c_str()); #endif } - + // Check if there is a handler for this message type. If so, execute // that handler. Otherwise, execute the unknown message handler. std::map::iterator iter = handlers.find(head); @@ -128,7 +128,7 @@ namespace XPC } } - void MessageHandlers::HandleConn(Message& msg) + void MessageHandlers::HandleConn(const Message& msg) { const unsigned char* buffer = msg.GetBuffer(); @@ -173,7 +173,7 @@ namespace XPC sock->SendTo(response, 6, &connection.addr); } - void MessageHandlers::HandleCtrl(Message& msg) + void MessageHandlers::HandleCtrl(const Message& msg) { // Update Log #if LOG_VERBOSITY > 2 @@ -252,7 +252,7 @@ namespace XPC } } - void MessageHandlers::HandleData(Message& msg) + void MessageHandlers::HandleData(const Message& msg) { // Parse data const unsigned char* buffer = msg.GetBuffer(); @@ -402,7 +402,7 @@ namespace XPC DREF dref = XPData[dataRef][j]; if (dref == DREF_None) { - // TODO: Send single line instead! + // TODO(jason): Send single line instead! HandleXPlaneData(msg); } else @@ -415,7 +415,7 @@ namespace XPC } } - void MessageHandlers::HandleDref(Message& msg) + void MessageHandlers::HandleDref(const Message& msg) { #if LOG_VERBOSITY >= 3 Log::FormatLine("[DREF] Request to set DREF value received (Conn %i)", connection.id); @@ -454,7 +454,7 @@ namespace XPC #endif } - void MessageHandlers::HandleGetD(Message& msg) + void MessageHandlers::HandleGetD(const Message& msg) { const unsigned char* buffer = msg.GetBuffer(); unsigned char drefCount = buffer[5]; @@ -504,7 +504,7 @@ namespace XPC sock->SendTo(response, cur, &connection.addr); } - void MessageHandlers::HandlePosi(Message& msg) + void MessageHandlers::HandlePosi(const Message& msg) { // Update log #if LOG_VERBOSITY > 0 @@ -548,7 +548,7 @@ namespace XPC } } - void MessageHandlers::HandleSimu(Message& msg) + void MessageHandlers::HandleSimu(const Message& msg) { // Update log #if LOG_VERBOSITY > 1 @@ -600,7 +600,7 @@ namespace XPC #endif } - void MessageHandlers::HandleText(Message& msg) + void MessageHandlers::HandleText(const Message& msg) { // Update Log #if LOG_VERBOSITY > 0 @@ -638,7 +638,7 @@ namespace XPC } } - void MessageHandlers::HandleView(Message& msg) + void MessageHandlers::HandleView(const Message& msg) { // Update Log #if LOG_VERBOSITY > 0 @@ -658,7 +658,7 @@ namespace XPC XPLMCommandKeyStroke(type); } - void MessageHandlers::HandleWypt(Message& msg) + void MessageHandlers::HandleWypt(const Message& msg) { // Update Log #if LOG_VERBOSITY > 0 @@ -702,7 +702,7 @@ namespace XPC } } - void MessageHandlers::HandleXPlaneData(Message& msg) + void MessageHandlers::HandleXPlaneData(const Message& msg) { #if LOG_VERBOSITY > 1 Log::WriteLine("[MSGH] Sending raw data to X-Plane"); @@ -714,7 +714,7 @@ namespace XPC sock->SendTo(msg.GetBuffer(), msg.GetSize(), (sockaddr*)&loopback); } - void MessageHandlers::HandleUnknown(Message& msg) + void MessageHandlers::HandleUnknown(const Message& msg) { // UPDATE LOG #if LOG_VERBOSITY > 0 diff --git a/xpcPlugin/MessageHandlers.h b/xpcPlugin/MessageHandlers.h index 123c39a..57ab2f6 100644 --- a/xpcPlugin/MessageHandlers.h +++ b/xpcPlugin/MessageHandlers.h @@ -1,7 +1,7 @@ //Copyright (c) 2013-2015 United States Government as represented by the Administrator of the //National Aeronautics and Space Administration. All Rights Reserved. -#ifndef XPC_MESSAGEHANDLERS_H -#define XPC_MESSAGEHANDLERS_H +#ifndef XPCPLUGIN_MESSAGEHANDLERS_H_ +#define XPCPLUGIN_MESSAGEHANDLERS_H_ #include "Message.h" #include @@ -10,7 +10,7 @@ namespace XPC { /// A function that handles a message. - typedef void(*MessageHandler)(Message&); + typedef void(*MessageHandler)(const Message&); /// Handles incommming messages and manages connections. /// @@ -37,18 +37,18 @@ namespace XPC static void SetSocket(UDPSocket* socket); private: - static void HandleConn(Message& msg); - static void HandleCtrl(Message& msg); - static void HandleData(Message& msg); - static void HandleDref(Message& msg); - static void HandleGetD(Message& msg); - static void HandlePosi(Message& msg); - static void HandleSimu(Message& msg); - static void HandleText(Message& msg); - static void HandleWypt(Message& msg); - static void HandleView(Message& msg); - static void HandleXPlaneData(Message& msg); - static void HandleUnknown(Message& msg); + static void HandleConn(const Message& msg); + static void HandleCtrl(const Message& msg); + static void HandleData(const Message& msg); + static void HandleDref(const Message& msg); + static void HandleGetD(const Message& msg); + static void HandlePosi(const Message& msg); + static void HandleSimu(const Message& msg); + static void HandleText(const Message& msg); + static void HandleWypt(const Message& msg); + static void HandleView(const Message& msg); + static void HandleXPlaneData(const Message& msg); + static void HandleUnknown(const Message& msg); typedef struct { diff --git a/xpcPlugin/UDPSocket.cpp b/xpcPlugin/UDPSocket.cpp index ed4710d..802a06d 100644 --- a/xpcPlugin/UDPSocket.cpp +++ b/xpcPlugin/UDPSocket.cpp @@ -18,7 +18,7 @@ namespace XPC localAddr.sin_family = AF_INET; localAddr.sin_addr.s_addr = INADDR_ANY; localAddr.sin_port = htons(recvPort); - + //Create and bind the socket #ifdef _WIN32 WSADATA wsa; @@ -92,7 +92,7 @@ namespace XPC #endif } - int UDPSocket::Read(unsigned char* dst, int maxLen, sockaddr* recvAddr) + int UDPSocket::Read(unsigned char* dst, int maxLen, sockaddr* recvAddr) const { socklen_t recvaddrlen = sizeof(*recvAddr); int status = 0; @@ -137,7 +137,7 @@ namespace XPC return status; } - void UDPSocket::SendTo(const unsigned char* buffer, std::size_t len, sockaddr* remote) + void UDPSocket::SendTo(const unsigned char* buffer, std::size_t len, sockaddr* remote) const { if (sendto(sock, (char*)buffer, (int)len, 0, remote, sizeof(*remote)) < 0) { @@ -152,7 +152,7 @@ namespace XPC #endif } } - + std::string UDPSocket::GetHost(sockaddr* sa) { char ip[INET6_ADDRSTRLEN + 6] = { 0 }; diff --git a/xpcPlugin/UDPSocket.h b/xpcPlugin/UDPSocket.h index 184f6e5..8f27eb2 100644 --- a/xpcPlugin/UDPSocket.h +++ b/xpcPlugin/UDPSocket.h @@ -1,14 +1,14 @@ //Copyright (c) 2013-2015 United States Government as represented by the Administrator of the //National Aeronautics and Space Administration. All Rights Reserved. -#ifndef XPC_SOCKET_H -#define XPC_SOCKET_H +#ifndef XPCPLUGIN_SOCKET_H_ +#define XPCPLUGIN_SOCKET_H_ #include #include #ifdef _WIN32 #include #include -#pragma comment(lib,"ws2_32.lib") //Winsock Library +#pragma comment(lib, "ws2_32.lib") //Winsock Library #elif (__APPLE__ || __linux) #include #include @@ -34,7 +34,7 @@ namespace XPC /// specified receive port. /// /// \param recvPort The port on which this instance will receive data. - UDPSocket(unsigned short recvPort); + explicit UDPSocket(unsigned short recvPort); /// Closes the underlying socket for this instance. ~UDPSocket(); @@ -48,14 +48,14 @@ namespace XPC /// of the remote host. /// \returns The number of bytes read, or a negative number if /// an error occurs. - int Read(unsigned char* buffer, int size, sockaddr* remoteAddr); + int Read(unsigned char* buffer, int size, sockaddr* remoteAddr) const; /// Sends data to the specified remote endpoint. /// /// \param data The data to be sent. /// \param len The number of bytes to send. /// \param remote The destination socket. - void SendTo(const unsigned char* buffer, std::size_t len, sockaddr* remote); + void SendTo(const unsigned char* buffer, std::size_t len, sockaddr* remote) const; /// Gets a string containing the IP address and port contained in the given sockaddr. /// diff --git a/xpcPlugin/XPCPlugin.cpp b/xpcPlugin/XPCPlugin.cpp index cd25d96..104e2d1 100755 --- a/xpcPlugin/XPCPlugin.cpp +++ b/xpcPlugin/XPCPlugin.cpp @@ -2,7 +2,7 @@ //National Aeronautics and Space Administration. All Rights Reserved. // //DISCLAIMERS -// No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY KIND, +// No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY KIND, // EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT // THE SUBJECT SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY @@ -75,7 +75,8 @@ XPC::UDPSocket* sock = NULL; -double start,lap; +double start; +double lap; static double timeConvert = 0.0; int benchmarkingSwitch = 0; // 1 = time for operations, 2 = time for op + cycle; int cyclesToClear = -1; // Clear message bus every n cycles. -1 == dont clear @@ -93,12 +94,12 @@ PLUGIN_API int XPluginStart(char* outName, char* outSig, char* outDesc) strcpy(outName, "X-Plane Connect [Version 1.0.1]"); strcpy(outSig, "NASA.XPlaneConnect"); strcpy(outDesc, "X Plane Communications Toolbox\nCopyright (c) 2013-2015 United States Government as represented by the Administrator of the National Aeronautics and Space Administration. All Rights Reserved."); - + #if (__APPLE__) if ( abs(timeConvert) <= 1e-9 ) // is about 0 { mach_timebase_info_data_t timeBase; - (void)mach_timebase_info( &timeBase ); + (void)mach_timebase_info(&timeBase); timeConvert = (double)timeBase.numer / (double)timeBase.denom / 1000000000.0; @@ -107,11 +108,11 @@ PLUGIN_API int XPluginStart(char* outName, char* outSig, char* outDesc) XPC::Log::Initialize("1.0.1"); XPC::Log::WriteLine("[EXEC] Plugin Start"); XPC::DataManager::Initialize(); - + float interval = -1; // Call every frame void* refcon = NULL; // Don't pass anything to the callback directly XPLMRegisterFlightLoopCallback(XPCFlightLoopCallback, interval, refcon); - + return 1; } @@ -151,7 +152,7 @@ PLUGIN_API int XPluginEnable(void) #if LOG_VERBOSITY > 0 XPC::Log::FormatLine("[EXEC] Debug Logging Enabled (Verbosity: %i)", LOG_VERBOSITY); #endif - + return 1; } @@ -196,8 +197,8 @@ float XPCFlightLoopCallback(float inElapsedSinceLastCall, { #if (__APPLE__) lap = (double)mach_absolute_time( ) * timeConvert; - diff_t = lap-start; - XPC::Log::FormatLine("[BENCH] Runtime %.6f",diff_t); + diff_t = lap - start; + XPC::Log::FormatLine("[BENCH] Runtime %.6f", diff_t); #endif } }