Make many more utility classes/headers public. (#76)

Moving these headers from src to include enables other libraries to use the
functionality provided.

* tcpsockets
* atomic_static
* raw_istream
* timestamp
* SafeThread
* Base64
* LEB128
* ConcurrentQueue

The classes have been moved into the wpi namespace as they're generic.
This commit is contained in:
Peter Johnson
2016-07-27 00:39:38 -07:00
committed by GitHub
parent d66f65e376
commit a73166a665
47 changed files with 361 additions and 259 deletions

View File

@@ -5,19 +5,19 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#ifndef NT_BASE64_H_
#define NT_BASE64_H_
#ifndef WPIUTIL_SUPPORT_BASE64_H_
#define WPIUTIL_SUPPORT_BASE64_H_
#include <cstddef>
#include <string>
#include "llvm/StringRef.h"
namespace nt {
namespace wpi {
std::size_t Base64Decode(llvm::StringRef encoded, std::string* plain);
void Base64Encode(llvm::StringRef plain, std::string* encoded);
} // namespace nt
} // namespace wpi
#endif // NT_BASE64_H_
#endif // WPIUTIL_SUPPORT_BASE64_H_

View File

@@ -4,14 +4,16 @@
// - see < http://opensource.org/licenses/BSD-2-Clause>
//
#ifndef NT_SUPPORT_CONCURRENT_QUEUE_H_
#define NT_SUPPORT_CONCURRENT_QUEUE_H_
#ifndef WPIUTIL_SUPPORT_CONCURRENT_QUEUE_H_
#define WPIUTIL_SUPPORT_CONCURRENT_QUEUE_H_
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
namespace wpi {
template <typename T>
class ConcurrentQueue {
public:
@@ -76,4 +78,6 @@ class ConcurrentQueue {
std::condition_variable cond_;
};
#endif // NT_SUPPORT_CONCURRENT_QUEUE_H_
} // namespace wpi
#endif // WPIUTIL_SUPPORT_CONCURRENT_QUEUE_H_

82
include/support/Logger.h Normal file
View File

@@ -0,0 +1,82 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2015. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#ifndef WPIUTIL_SUPPORT_LOGGER_H_
#define WPIUTIL_SUPPORT_LOGGER_H_
#include <functional>
#include <sstream>
#include <string>
namespace wpi {
enum LogLevel {
WPI_LOG_CRITICAL = 50,
WPI_LOG_ERROR = 40,
WPI_LOG_WARNING = 30,
WPI_LOG_INFO = 20,
WPI_LOG_DEBUG = 10,
WPI_LOG_DEBUG1 = 9,
WPI_LOG_DEBUG2 = 8,
WPI_LOG_DEBUG3 = 7,
WPI_LOG_DEBUG4 = 6
};
class Logger {
public:
typedef std::function<void(unsigned int level, const char* file,
unsigned int line, const char* msg)> LogFunc;
void SetLogger(LogFunc func) { m_func = func; }
void set_min_level(unsigned int level) { m_min_level = level; }
unsigned int min_level() const { return m_min_level; }
void Log(unsigned int level, const char* file, unsigned int line,
const char* msg) {
if (!m_func || level < m_min_level) return;
m_func(level, file, line, msg);
}
bool HasLogger() const { return m_func != nullptr; }
private:
LogFunc m_func;
unsigned int m_min_level = 20;
};
#define WPI_LOG(logger_inst, level, x) \
do { \
::wpi::Logger& WPI_logger_ = logger_inst; \
if (WPI_logger_.min_level() <= level && WPI_logger_.HasLogger()) { \
std::ostringstream oss; \
oss << x; \
WPI_logger_.Log(level, __FILE__, __LINE__, oss.str().c_str()); \
} \
} while (0)
#define WPI_ERROR(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_ERROR, x)
#define WPI_WARNING(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_WARNING, x)
#define WPI_INFO(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_INFO, x)
#ifdef NDEBUG
#define WPI_DEBUG(inst, x) do {} while (0)
#define WPI_DEBUG1(inst, x) do {} while (0)
#define WPI_DEBUG2(inst, x) do {} while (0)
#define WPI_DEBUG3(inst, x) do {} while (0)
#define WPI_DEBUG4(inst, x) do {} while (0)
#else
#define WPI_DEBUG(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG, x)
#define WPI_DEBUG1(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG1, x)
#define WPI_DEBUG2(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG2, x)
#define WPI_DEBUG3(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG3, x)
#define WPI_DEBUG4(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG4, x)
#endif
} // namespace wpi
#endif // WPIUTIL_SUPPORT_LOGGER_H_

View File

@@ -5,15 +5,15 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#ifndef NT_SAFETHREAD_H_
#define NT_SAFETHREAD_H_
#ifndef WPIUTIL_SUPPORT_SAFETHREAD_H_
#define WPIUTIL_SUPPORT_SAFETHREAD_H_
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
namespace nt {
namespace wpi {
// Base class for SafeThreadOwner threads.
class SafeThread {
@@ -88,6 +88,6 @@ class SafeThreadOwner : public detail::SafeThreadOwnerBase {
Proxy GetThread() { return Proxy(detail::SafeThreadOwnerBase::GetThread()); }
};
} // namespace nt
} // namespace wpi
#endif // NT_SAFETHREAD_H_
#endif // WPIUTIL_SUPPORT_SAFETHREAD_H_

View File

@@ -5,8 +5,8 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#ifndef NT_ATOMIC_STATIC_H_
#define NT_ATOMIC_STATIC_H_
#ifndef WPIUTIL_SUPPORT_ATOMIC_STATIC_H_
#define WPIUTIL_SUPPORT_ATOMIC_STATIC_H_
#if !defined(_MSC_VER) || (_MSC_VER >= 1900)
@@ -46,4 +46,4 @@
#endif
#endif // NT_ATOMIC_STATIC_H_
#endif // WPIUTIL_SUPPORT_ATOMIC_STATIC_H_

View File

@@ -5,14 +5,14 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#ifndef NT_LEB128_H_
#define NT_LEB128_H_
#ifndef WPIUTIL_SUPPORT_LEB128_H_
#define WPIUTIL_SUPPORT_LEB128_H_
#include <cstddef>
#include "llvm/SmallVector.h"
namespace nt {
namespace wpi {
class raw_istream;
@@ -21,6 +21,6 @@ std::size_t WriteUleb128(llvm::SmallVectorImpl<char>& dest, unsigned long val);
std::size_t ReadUleb128(const char* addr, unsigned long* ret);
bool ReadUleb128(raw_istream& is, unsigned long* ret);
} // namespace nt
} // namespace wpi
#endif // NT_LEB128_H_
#endif // WPIUTIL_SUPPORT_LEB128_H_

View File

@@ -5,12 +5,12 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#ifndef NT_RAW_ISTREAM_H_
#define NT_RAW_ISTREAM_H_
#ifndef WPIUTIL_SUPPORT_RAW_ISTREAM_H_
#define WPIUTIL_SUPPORT_RAW_ISTREAM_H_
#include <cstddef>
namespace nt {
namespace wpi {
class raw_istream {
public:
@@ -35,6 +35,6 @@ class raw_mem_istream : public raw_istream {
std::size_t m_left;
};
} // namespace nt
} // namespace wpi
#endif // NT_RAW_ISTREAM_H_
#endif // WPIUTIL_SUPPORT_RAW_ISTREAM_H_

View File

@@ -5,14 +5,13 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#ifndef NT_RAW_SOCKET_ISTREAM_H_
#define NT_RAW_SOCKET_ISTREAM_H_
#include "raw_istream.h"
#ifndef WPIUTIL_SUPPORT_RAW_SOCKET_ISTREAM_H_
#define WPIUTIL_SUPPORT_RAW_SOCKET_ISTREAM_H_
#include "support/raw_istream.h"
#include "tcpsockets/NetworkStream.h"
namespace nt {
namespace wpi {
class raw_socket_istream : public raw_istream {
public:
@@ -27,6 +26,6 @@ class raw_socket_istream : public raw_istream {
int m_timeout;
};
} // namespace nt
} // namespace wpi
#endif // NT_RAW_SOCKET_ISTREAM_H_
#endif // WPIUTIL_SUPPORT_RAW_SOCKET_ISTREAM_H_

View File

@@ -4,25 +4,25 @@
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#ifndef NT_SUPPORT_TIMESTAMP_H_
#define NT_SUPPORT_TIMESTAMP_H_
#ifndef WPIUTIL_SUPPORT_TIMESTAMP_H_
#define WPIUTIL_SUPPORT_TIMESTAMP_H_
#ifdef __cplusplus
extern "C" {
#endif
unsigned long long NT_Now(void);
unsigned long long WPI_Now(void);
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
namespace nt {
namespace wpi {
unsigned long long Now();
} // namespace nt
} // namespace wpi
#endif
#endif // NT_SUPPORT_TIMESTAMP_H_
#endif // WPIUTIL_SUPPORT_TIMESTAMP_H_

View File

@@ -5,10 +5,12 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#ifndef TCPSOCKETS_NETWORKACCEPTOR_H_
#define TCPSOCKETS_NETWORKACCEPTOR_H_
#ifndef WPIUTIL_TCPSOCKETS_NETWORKACCEPTOR_H_
#define WPIUTIL_TCPSOCKETS_NETWORKACCEPTOR_H_
#include "NetworkStream.h"
#include "tcpsockets/NetworkStream.h"
namespace wpi {
class NetworkAcceptor {
public:
@@ -23,4 +25,6 @@ class NetworkAcceptor {
NetworkAcceptor& operator=(const NetworkAcceptor&) = delete;
};
#endif // TCPSOCKETS_NETWORKACCEPTOR_H_
} // namespace wpi
#endif // WPIUTIL_TCPSOCKETS_NETWORKACCEPTOR_H_

View File

@@ -5,13 +5,15 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#ifndef TCPSOCKETS_NETWORKSTREAM_H_
#define TCPSOCKETS_NETWORKSTREAM_H_
#ifndef WPIUTIL_TCPSOCKETS_NETWORKSTREAM_H_
#define WPIUTIL_TCPSOCKETS_NETWORKSTREAM_H_
#include <cstddef>
#include "llvm/StringRef.h"
namespace wpi {
class NetworkStream {
public:
NetworkStream() = default;
@@ -36,4 +38,6 @@ class NetworkStream {
NetworkStream& operator=(const NetworkStream&) = delete;
};
#endif // TCPSOCKETS_NETWORKSTREAM_H_
} // namespace wpi
#endif // WPIUTIL_TCPSOCKETS_NETWORKSTREAM_H_

View File

@@ -5,8 +5,8 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#ifndef TCPSOCKETS_SOCKETERROR_H_
#define TCPSOCKETS_SOCKETERROR_H_
#ifndef WPIUTIL_TCPSOCKETS_SOCKETERROR_H_
#define WPIUTIL_TCPSOCKETS_SOCKETERROR_H_
#include <string>
@@ -16,7 +16,7 @@
#include <errno.h>
#endif
namespace tcpsockets {
namespace wpi {
static inline int SocketErrno() {
#ifdef _WIN32
@@ -32,6 +32,6 @@ static inline std::string SocketStrerror() {
return SocketStrerror(SocketErrno());
}
} // namespace tcpsockets
} // namespace wpi
#endif // TCPSOCKETS_SOCKETERROR_H_
#endif // WPIUTIL_TCPSOCKETS_SOCKETERROR_H_

View File

@@ -21,15 +21,19 @@
limitations under the License.
*/
#ifndef TCPSOCKETS_TCPACCEPTOR_H_
#define TCPSOCKETS_TCPACCEPTOR_H_
#ifndef WPIUTIL_TCPSOCKETS_TCPACCEPTOR_H_
#define WPIUTIL_TCPSOCKETS_TCPACCEPTOR_H_
#include <atomic>
#include <memory>
#include <string>
#include "NetworkAcceptor.h"
#include "TCPStream.h"
#include "tcpsockets/NetworkAcceptor.h"
#include "tcpsockets/TCPStream.h"
namespace wpi {
class Logger;
class TCPAcceptor : public NetworkAcceptor {
int m_lsd;
@@ -37,9 +41,10 @@ class TCPAcceptor : public NetworkAcceptor {
std::string m_address;
bool m_listening;
std::atomic_bool m_shutdown;
Logger& m_logger;
public:
TCPAcceptor(int port, const char* address);
TCPAcceptor(int port, const char* address, Logger& logger);
~TCPAcceptor();
int start() override;
@@ -47,4 +52,6 @@ class TCPAcceptor : public NetworkAcceptor {
std::unique_ptr<NetworkStream> accept() override;
};
#endif
} // namespace wpi
#endif // WPIUTIL_TCPSOCKETS_TCPACCEPTOR_H_

View File

@@ -21,17 +21,24 @@
limitations under the License
*/
#ifndef TCPSOCKETS_TCPCONNECTOR_H_
#define TCPSOCKETS_TCPCONNECTOR_H_
#ifndef WPIUTIL_TCPSOCKETS_TCPCONNECTOR_H_
#define WPIUTIL_TCPSOCKETS_TCPCONNECTOR_H_
#include <memory>
#include "NetworkStream.h"
#include "tcpsockets/NetworkStream.h"
namespace wpi {
class Logger;
class TCPConnector {
public:
static std::unique_ptr<NetworkStream> connect(const char* server, int port,
Logger& logger,
int timeout = 0);
};
#endif
} // namespace wpi
#endif // WPIUTIL_TCPSOCKETS_TCPCONNECTOR_H_

View File

@@ -21,8 +21,8 @@
limitations under the License.
*/
#ifndef TCPSOCKETS_TCPSTREAM_H_
#define TCPSOCKETS_TCPSTREAM_H_
#ifndef WPIUTIL_TCPSOCKETS_TCPSTREAM_H_
#define WPIUTIL_TCPSOCKETS_TCPSTREAM_H_
#include <cstddef>
#include <string>
@@ -33,7 +33,11 @@
#include <sys/socket.h>
#endif
#include "NetworkStream.h"
#include "tcpsockets/NetworkStream.h"
struct sockaddr_in;
namespace wpi {
class TCPStream : public NetworkStream {
int m_sd;
@@ -60,8 +64,10 @@ class TCPStream : public NetworkStream {
private:
bool WaitForReadEvent(int timeout);
TCPStream(int sd, struct sockaddr_in* address);
TCPStream(int sd, sockaddr_in* address);
TCPStream() = delete;
};
} // namespace wpi
#endif

View File

@@ -9,8 +9,8 @@
#include "edu_wpi_first_wpilibj_networktables_NetworkTablesJNI.h"
#include "ntcore.h"
#include "atomic_static.h"
#include "SafeThread.h"
#include "support/atomic_static.h"
#include "support/SafeThread.h"
#include "llvm/ConvertUTF.h"
#include "llvm/SmallString.h"
#include "llvm/SmallVector.h"
@@ -1427,7 +1427,7 @@ JNIEXPORT jlong JNICALL Java_edu_wpi_first_wpilibj_networktables_NetworkTablesJN
// Instead, this class attaches just once. When a hardware notification
// occurs, a condition variable wakes up this thread and this thread actually
// makes the call into Java.
class LoggerThreadJNI : public nt::SafeThread {
class LoggerThreadJNI : public wpi::SafeThread {
public:
void Main();
@@ -1445,7 +1445,7 @@ class LoggerThreadJNI : public nt::SafeThread {
jmethodID m_mid;
};
class LoggerJNI : public nt::SafeThreadOwner<LoggerThreadJNI> {
class LoggerJNI : public wpi::SafeThreadOwner<LoggerThreadJNI> {
public:
static LoggerJNI& GetInstance() {
ATOMIC_STATIC(LoggerJNI, instance);

View File

@@ -10,26 +10,28 @@
#include <algorithm>
#include <iterator>
#include "Log.h"
#include "tcpsockets/TCPAcceptor.h"
#include "tcpsockets/TCPConnector.h"
#include "Log.h"
using namespace nt;
ATOMIC_STATIC_INIT(Dispatcher)
void Dispatcher::StartServer(StringRef persist_filename,
void Dispatcher::StartServer(llvm::StringRef persist_filename,
const char* listen_address, unsigned int port) {
DispatcherBase::StartServer(persist_filename,
std::unique_ptr<NetworkAcceptor>(new TCPAcceptor(
static_cast<int>(port), listen_address)));
DispatcherBase::StartServer(
persist_filename,
std::unique_ptr<wpi::NetworkAcceptor>(new wpi::TCPAcceptor(
static_cast<int>(port), listen_address, Logger::GetInstance())));
}
void Dispatcher::StartClient(const char* server_name, unsigned int port) {
std::string server_name_copy(server_name);
DispatcherBase::StartClient([=]() -> std::unique_ptr<NetworkStream> {
return TCPConnector::connect(server_name_copy.c_str(),
static_cast<int>(port), 1);
DispatcherBase::StartClient([=]() -> std::unique_ptr<wpi::NetworkStream> {
return wpi::TCPConnector::connect(server_name_copy.c_str(),
static_cast<int>(port),
Logger::GetInstance(), 1);
});
}
@@ -39,9 +41,10 @@ void Dispatcher::StartClient(
for (const auto& server : servers) {
std::string server_name(server.first);
unsigned int port = server.second;
connectors.emplace_back([=]() -> std::unique_ptr<NetworkStream> {
return TCPConnector::connect(server_name.c_str(),
static_cast<int>(port), 1);
connectors.emplace_back([=]() -> std::unique_ptr<wpi::NetworkStream> {
return wpi::TCPConnector::connect(server_name.c_str(),
static_cast<int>(port),
Logger::GetInstance(), 1);
});
}
DispatcherBase::StartClient(std::move(connectors));
@@ -61,8 +64,9 @@ DispatcherBase::~DispatcherBase() {
Stop();
}
void DispatcherBase::StartServer(StringRef persist_filename,
std::unique_ptr<NetworkAcceptor> acceptor) {
void DispatcherBase::StartServer(
StringRef persist_filename,
std::unique_ptr<wpi::NetworkAcceptor> acceptor) {
{
std::lock_guard<std::mutex> lock(m_user_mutex);
if (m_active) return;

View File

@@ -19,25 +19,27 @@
#include "llvm/StringRef.h"
#include "atomic_static.h"
#include "support/atomic_static.h"
#include "NetworkConnection.h"
#include "Notifier.h"
#include "Storage.h"
namespace wpi {
class NetworkAcceptor;
class NetworkStream;
}
namespace nt {
class DispatcherBase {
friend class DispatcherTest;
public:
typedef std::function<std::unique_ptr<NetworkStream>()> Connector;
typedef std::function<std::unique_ptr<wpi::NetworkStream>()> Connector;
virtual ~DispatcherBase();
void StartServer(StringRef persist_filename,
std::unique_ptr<NetworkAcceptor> acceptor);
void StartServer(llvm::StringRef persist_filename,
std::unique_ptr<wpi::NetworkAcceptor> acceptor);
void StartClient(Connector connector);
void StartClient(std::vector<Connector>&& connectors);
void Stop();
@@ -81,7 +83,7 @@ class DispatcherBase {
std::thread m_dispatch_thread;
std::thread m_clientserver_thread;
std::unique_ptr<NetworkAcceptor> m_server_acceptor;
std::unique_ptr<wpi::NetworkAcceptor> m_server_acceptor;
std::vector<Connector> m_client_connectors;
// Mutex for user-accessible items

View File

@@ -61,6 +61,6 @@ static void def_log_func(unsigned int level, const char* file,
#endif
}
Logger::Logger() : m_func(def_log_func) {}
Logger::Logger() { SetLogger(def_log_func); }
Logger::~Logger() {}

View File

@@ -12,12 +12,12 @@
#include <sstream>
#include <string>
#include "atomic_static.h"
#include "ntcore_c.h"
#include "support/atomic_static.h"
#include "support/Logger.h"
namespace nt {
class Logger {
class Logger : public wpi::Logger {
public:
static Logger& GetInstance() {
ATOMIC_STATIC(Logger, instance);
@@ -25,59 +25,24 @@ class Logger {
}
~Logger();
typedef std::function<void(unsigned int level, const char* file,
unsigned int line, const char* msg)> LogFunc;
void SetLogger(LogFunc func) { m_func = func; }
void set_min_level(unsigned int level) { m_min_level = level; }
unsigned int min_level() const { return m_min_level; }
void Log(unsigned int level, const char* file, unsigned int line,
const char* msg) {
if (!m_func || level < m_min_level) return;
m_func(level, file, line, msg);
}
bool HasLogger() const { return m_func != nullptr; }
private:
Logger();
LogFunc m_func;
unsigned int m_min_level = 20;
ATOMIC_STATIC_DECL(Logger)
};
#define LOG(level, x) \
do { \
nt::Logger& logger = nt::Logger::GetInstance(); \
if (logger.min_level() <= level && logger.HasLogger()) { \
std::ostringstream oss; \
oss << x; \
logger.Log(level, __FILE__, __LINE__, oss.str().c_str()); \
} \
} while (0)
#define LOG(level, x) WPI_LOG(nt::Logger::GetInstance(), level, x)
#undef ERROR
#define ERROR(x) LOG(NT_LOG_ERROR, x)
#define WARNING(x) LOG(NT_LOG_WARNING, x)
#define INFO(x) LOG(NT_LOG_INFO, x)
#define ERROR(x) WPI_ERROR(nt::Logger::GetInstance(), x)
#define WARNING(x) WPI_WARNING(nt::Logger::GetInstance(), x)
#define INFO(x) WPI_INFO(nt::Logger::GetInstance(), x)
#ifdef NDEBUG
#define DEBUG(x) do {} while (0)
#define DEBUG1(x) do {} while (0)
#define DEBUG2(x) do {} while (0)
#define DEBUG3(x) do {} while (0)
#define DEBUG4(x) do {} while (0)
#else
#define DEBUG(x) LOG(NT_LOG_DEBUG, x)
#define DEBUG1(x) LOG(NT_LOG_DEBUG1, x)
#define DEBUG2(x) LOG(NT_LOG_DEBUG2, x)
#define DEBUG3(x) LOG(NT_LOG_DEBUG3, x)
#define DEBUG4(x) LOG(NT_LOG_DEBUG4, x)
#endif
#define DEBUG(x) WPI_DEBUG(nt::Logger::GetInstance(), x)
#define DEBUG1(x) WPI_DEBUG1(nt::Logger::GetInstance(), x)
#define DEBUG2(x) WPI_DEBUG2(nt::Logger::GetInstance(), x)
#define DEBUG3(x) WPI_DEBUG3(nt::Logger::GetInstance(), x)
#define DEBUG4(x) WPI_DEBUG4(nt::Logger::GetInstance(), x)
} // namespace nt

View File

@@ -7,11 +7,11 @@
#include "NetworkConnection.h"
#include "support/raw_socket_istream.h"
#include "support/timestamp.h"
#include "tcpsockets/NetworkStream.h"
#include "Log.h"
#include "Notifier.h"
#include "raw_socket_istream.h"
#include "WireDecoder.h"
#include "WireEncoder.h"
@@ -19,7 +19,7 @@ using namespace nt;
std::atomic_uint NetworkConnection::s_uid;
NetworkConnection::NetworkConnection(std::unique_ptr<NetworkStream> stream,
NetworkConnection::NetworkConnection(std::unique_ptr<wpi::NetworkStream> stream,
Notifier& notifier,
HandshakeFunc handshake,
Message::GetEntryTypeFunc get_entry_type)
@@ -106,7 +106,7 @@ void NetworkConnection::set_remote_id(StringRef remote_id) {
}
void NetworkConnection::ReadThreadMain() {
raw_socket_istream is(*m_stream);
wpi::raw_socket_istream is(*m_stream);
WireDecoder decoder(is, m_proto_rev);
m_state = static_cast<int>(kHandshake);
@@ -179,7 +179,7 @@ void NetworkConnection::WriteThreadMain() {
msg->Write(encoder);
}
}
NetworkStream::Error err;
wpi::NetworkStream::Error err;
if (!m_stream) break;
if (encoder.size() == 0) continue;
if (m_stream->send(encoder.data(), encoder.size(), &err) == 0) break;

View File

@@ -17,7 +17,9 @@
#include "Message.h"
#include "ntcore_cpp.h"
namespace wpi {
class NetworkStream;
}
namespace nt {
@@ -35,9 +37,9 @@ class NetworkConnection {
typedef std::function<void(std::shared_ptr<Message> msg,
NetworkConnection* conn)> ProcessIncomingFunc;
typedef std::vector<std::shared_ptr<Message>> Outgoing;
typedef ConcurrentQueue<Outgoing> OutgoingQueue;
typedef wpi::ConcurrentQueue<Outgoing> OutgoingQueue;
NetworkConnection(std::unique_ptr<NetworkStream> stream,
NetworkConnection(std::unique_ptr<wpi::NetworkStream> stream,
Notifier& notifier,
HandshakeFunc handshake,
Message::GetEntryTypeFunc get_entry_type);
@@ -54,7 +56,7 @@ class NetworkConnection {
ConnectionInfo info() const;
bool active() const { return m_active; }
NetworkStream& stream() { return *m_stream; }
wpi::NetworkStream& stream() { return *m_stream; }
void QueueOutgoing(std::shared_ptr<Message> msg);
void PostOutgoing(bool keep_alive);
@@ -82,7 +84,7 @@ class NetworkConnection {
static std::atomic_uint s_uid;
unsigned int m_uid;
std::unique_ptr<NetworkStream> m_stream;
std::unique_ptr<wpi::NetworkStream> m_stream;
Notifier& m_notifier;
OutgoingQueue m_outgoing;
HandshakeFunc m_handshake;

View File

@@ -60,7 +60,7 @@ class UidVector {
} // anonymous namespace
class Notifier::Thread : public SafeThread {
class Notifier::Thread : public wpi::SafeThread {
public:
Thread(std::function<void()> on_start, std::function<void()> on_exit)
: m_on_start(on_start), m_on_exit(on_exit) {}
@@ -83,7 +83,7 @@ class Notifier::Thread : public SafeThread {
UidVector<ConnectionListenerCallback> m_conn_listeners;
struct EntryNotification {
EntryNotification(StringRef name_, std::shared_ptr<Value> value_,
EntryNotification(llvm::StringRef name_, std::shared_ptr<Value> value_,
unsigned int flags_, EntryListenerCallback only_)
: name(name_),
value(value_),

View File

@@ -10,9 +10,9 @@
#include <functional>
#include "atomic_static.h"
#include "support/atomic_static.h"
#include "support/SafeThread.h"
#include "ntcore_cpp.h"
#include "SafeThread.h"
namespace nt {
@@ -34,7 +34,7 @@ class Notifier {
void SetOnStart(std::function<void()> on_start) { m_on_start = on_start; }
void SetOnExit(std::function<void()> on_exit) { m_on_exit = on_exit; }
unsigned int AddEntryListener(StringRef prefix,
unsigned int AddEntryListener(llvm::StringRef prefix,
EntryListenerCallback callback,
unsigned int flags);
void RemoveEntryListener(unsigned int entry_listener_uid);
@@ -52,7 +52,7 @@ class Notifier {
Notifier();
class Thread;
SafeThreadOwner<Thread> m_owner;
wpi::SafeThreadOwner<Thread> m_owner;
std::atomic_bool m_local_notifiers;

View File

@@ -15,7 +15,7 @@ using namespace nt;
ATOMIC_STATIC_INIT(RpcServer)
class RpcServer::Thread : public SafeThread {
class RpcServer::Thread : public wpi::SafeThread {
public:
Thread(std::function<void()> on_start, std::function<void()> on_exit)
: m_on_start(on_start), m_on_exit(on_exit) {}

View File

@@ -15,10 +15,10 @@
#include <utility>
#include "llvm/DenseMap.h"
#include "atomic_static.h"
#include "support/atomic_static.h"
#include "support/SafeThread.h"
#include "Message.h"
#include "ntcore_cpp.h"
#include "SafeThread.h"
namespace nt {
@@ -51,7 +51,7 @@ class RpcServer {
RpcServer();
class Thread;
SafeThreadOwner<Thread> m_owner;
wpi::SafeThreadOwner<Thread> m_owner;
struct RpcCall {
RpcCall(StringRef name_, std::shared_ptr<Message> msg_, RpcCallback func_,

View File

@@ -12,7 +12,7 @@
#include <tuple>
#include "llvm/StringExtras.h"
#include "Base64.h"
#include "support/Base64.h"
#include "Log.h"
#include "NetworkConnection.h"
@@ -853,7 +853,7 @@ static void SavePersistentImpl(
WriteString(os, v->GetString());
break;
case NT_RAW:
Base64Encode(v->GetRaw(), &base64_encoded);
wpi::Base64Encode(v->GetRaw(), &base64_encoded);
os << base64_encoded;
break;
case NT_BOOLEAN_ARRAY: {
@@ -1139,7 +1139,7 @@ bool Storage::LoadPersistent(
break;
}
case NT_RAW:
Base64Decode(line, &str);
wpi::Base64Decode(line, &str);
value = Value::MakeRaw(std::move(str));
break;
case NT_BOOLEAN_ARRAY: {

View File

@@ -19,7 +19,7 @@
#include "llvm/DenseMap.h"
#include "llvm/StringMap.h"
#include "atomic_static.h"
#include "support/atomic_static.h"
#include "Message.h"
#include "Notifier.h"
#include "ntcore_cpp.h"

View File

@@ -13,12 +13,12 @@ using namespace nt;
Value::Value() {
m_val.type = NT_UNASSIGNED;
m_val.last_change = Now();
m_val.last_change = wpi::Now();
}
Value::Value(NT_Type type, const private_init&) {
m_val.type = type;
m_val.last_change = Now();
m_val.last_change = wpi::Now();
if (m_val.type == NT_BOOLEAN_ARRAY)
m_val.data.arr_boolean.arr = nullptr;
else if (m_val.type == NT_DOUBLE_ARRAY)

View File

@@ -13,7 +13,7 @@
#include <cstring>
#include "llvm/MathExtras.h"
#include "leb128.h"
#include "support/leb128.h"
using namespace nt;
@@ -45,7 +45,8 @@ static double ReadDouble(const char*& buf) {
return llvm::BitsToDouble(val);
}
WireDecoder::WireDecoder(raw_istream& is, unsigned int proto_rev) : m_is(is) {
WireDecoder::WireDecoder(wpi::raw_istream& is, unsigned int proto_rev)
: m_is(is) {
// Start with a 1K temporary buffer. Use malloc instead of new so we can
// realloc.
m_allocated = 1024;

View File

@@ -11,9 +11,9 @@
#include <cstddef>
#include "nt_Value.h"
#include "leb128.h"
#include "support/leb128.h"
#include "support/raw_istream.h"
//#include "Log.h"
#include "raw_istream.h"
namespace nt {
@@ -26,7 +26,7 @@ namespace nt {
*/
class WireDecoder {
public:
explicit WireDecoder(raw_istream& is, unsigned int proto_rev);
explicit WireDecoder(wpi::raw_istream& is, unsigned int proto_rev);
~WireDecoder();
void set_proto_rev(unsigned int proto_rev) { m_proto_rev = proto_rev; }
@@ -113,7 +113,7 @@ class WireDecoder {
/* Reads an ULEB128-encoded unsigned integer. */
bool ReadUleb128(unsigned long* val) {
return nt::ReadUleb128(m_is, val);
return wpi::ReadUleb128(m_is, val);
}
bool ReadType(NT_Type* type);
@@ -135,7 +135,7 @@ class WireDecoder {
void Realloc(std::size_t len);
/* input stream */
raw_istream& m_is;
wpi::raw_istream& m_is;
/* temporary buffer */
char* m_buf;

View File

@@ -13,7 +13,7 @@
#include <cstring>
#include "llvm/MathExtras.h"
#include "leb128.h"
#include "support/leb128.h"
using namespace nt;
@@ -38,7 +38,7 @@ void WireEncoder::WriteDouble(double val) {
}
void WireEncoder::WriteUleb128(unsigned long val) {
nt::WriteUleb128(m_data, val);
wpi::WriteUleb128(m_data, val);
}
void WireEncoder::WriteType(NT_Type type) {
@@ -191,7 +191,7 @@ std::size_t WireEncoder::GetStringSize(llvm::StringRef str) const {
if (len > 0xffff) len = 0xffff; // Limited to 64K length; truncate
return 2 + len;
}
return SizeUleb128(str.size()) + str.size();
return wpi::SizeUleb128(str.size()) + str.size();
}
void WireEncoder::WriteString(llvm::StringRef str) {

View File

@@ -10,6 +10,7 @@
#include <cassert>
#include <cstdlib>
#include "support/timestamp.h"
#include "Value_internal.h"
using namespace nt;
@@ -405,6 +406,10 @@ const char *NT_LoadPersistent(const char *filename,
* Utility Functions
*/
unsigned long long NT_Now() {
return wpi::Now();
}
void NT_SetLogger(NT_LogFunc func, unsigned int min_level) {
nt::SetLogger(func, min_level);
}

View File

@@ -11,8 +11,9 @@
#include <cstdio>
#include <cstdlib>
#include "Dispatcher.h"
#include "support/timestamp.h"
#include "Log.h"
#include "Dispatcher.h"
#include "Notifier.h"
#include "RpcServer.h"
#include "Storage.h"
@@ -168,7 +169,7 @@ std::string PackRpcDefinition(const RpcDefinition& def) {
}
bool UnpackRpcDefinition(StringRef packed, RpcDefinition* def) {
raw_mem_istream is(packed.data(), packed.size());
wpi::raw_mem_istream is(packed.data(), packed.size());
WireDecoder dec(is, 0x0300);
if (!dec.Read8(&def->version)) return false;
if (!dec.ReadString(&def->name)) return false;
@@ -211,7 +212,7 @@ std::string PackRpcValues(ArrayRef<std::shared_ptr<Value>> values) {
std::vector<std::shared_ptr<Value>> UnpackRpcValues(StringRef packed,
ArrayRef<NT_Type> types) {
raw_mem_istream is(packed.data(), packed.size());
wpi::raw_mem_istream is(packed.data(), packed.size());
WireDecoder dec(is, 0x0300);
std::vector<std::shared_ptr<Value>> vec;
for (auto type : types) {
@@ -281,6 +282,10 @@ const char* LoadPersistent(
return Storage::GetInstance().LoadPersistent(filename, warn);
}
unsigned long long Now() {
return wpi::Now();
}
void SetLogger(LogFunc func, unsigned int min_level) {
Logger& logger = Logger::GetInstance();
logger.SetLogger(func);

View File

@@ -62,9 +62,9 @@
*
*/
#include "Base64.h"
#include "support/Base64.h"
namespace nt {
namespace wpi {
// aaaack but it's fast and const should make it shared text page.
static const unsigned char pr2six[256] =
@@ -149,4 +149,4 @@ void Base64Encode(llvm::StringRef plain, std::string* encoded) {
}
}
} // namespace nt
} // namespace wpi

View File

@@ -5,9 +5,9 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#include "SafeThread.h"
#include "support/SafeThread.h"
using namespace nt;
using namespace wpi;
void detail::SafeThreadOwnerBase::Start(SafeThread* thr) {
SafeThread* curthr = nullptr;

View File

@@ -5,11 +5,11 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#include "leb128.h"
#include "support/leb128.h"
#include "raw_istream.h"
#include "support/raw_istream.h"
namespace nt {
namespace wpi {
/**
* Get size of unsigned LEB128 data
@@ -116,4 +116,4 @@ bool ReadUleb128(raw_istream& is, unsigned long* ret) {
return true;
}
} // namespace nt
} // namespace wpi

View File

@@ -5,11 +5,11 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#include "raw_istream.h"
#include "support/raw_istream.h"
#include <cstring>
using namespace nt;
using namespace wpi;
bool raw_mem_istream::read(void* data, std::size_t len) {
if (len > m_left) return false;

View File

@@ -5,9 +5,9 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#include "raw_socket_istream.h"
#include "support/raw_socket_istream.h"
using namespace nt;
using namespace wpi;
bool raw_socket_istream::read(void* data, std::size_t len) {
char* cdata = static_cast<char*>(data);

View File

@@ -4,7 +4,7 @@
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "timestamp.h"
#include "support/timestamp.h"
#ifdef _WIN32
#include <cassert>
@@ -70,7 +70,7 @@ static const unsigned long long offset_val = timestamp();
static const unsigned long long frequency_val = update_frequency();
#endif
unsigned long long nt::Now() {
unsigned long long wpi::Now() {
#ifdef _WIN32
assert(offset_val > 0u);
assert(frequency_val > 0u);
@@ -84,6 +84,6 @@ unsigned long long nt::Now() {
#endif
}
unsigned long long NT_Now() {
return nt::Now();
unsigned long long WPI_Now() {
return wpi::Now();
}

View File

@@ -5,7 +5,7 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#include "SocketError.h"
#include "tcpsockets/SocketError.h"
#ifdef _WIN32
#include <windows.h>
@@ -13,7 +13,7 @@
#include <string.h>
#endif
namespace tcpsockets {
namespace wpi {
std::string SocketStrerror(int code) {
#ifdef _WIN32
@@ -28,4 +28,4 @@ std::string SocketStrerror(int code) {
#endif
}
} // namespace tcpsockets
} // namespace wpi

View File

@@ -21,7 +21,7 @@
limitations under the License.
*/
#include "TCPAcceptor.h"
#include "tcpsockets/TCPAcceptor.h"
#include <cstdio>
#include <cstring>
@@ -36,16 +36,17 @@
#endif
#include "llvm/SmallString.h"
#include "../Log.h"
#include "SocketError.h"
#include "support/Logger.h"
#include "tcpsockets/SocketError.h"
using namespace tcpsockets;
using namespace wpi;
TCPAcceptor::TCPAcceptor(int port, const char* address)
TCPAcceptor::TCPAcceptor(int port, const char* address, Logger& logger)
: m_lsd(0),
m_port(port),
m_address(address),
m_listening(false) {
m_listening(false),
m_logger(logger) {
m_shutdown = false;
#ifdef _WIN32
WSAData wsaData;
@@ -73,7 +74,7 @@ int TCPAcceptor::start() {
m_lsd = socket(PF_INET, SOCK_STREAM, 0);
if (m_lsd < 0) {
ERROR("could not create socket");
WPI_ERROR(m_logger, "could not create socket");
return -1;
}
struct sockaddr_in address;
@@ -99,13 +100,13 @@ int TCPAcceptor::start() {
int result = bind(m_lsd, (struct sockaddr*)&address, sizeof(address));
if (result != 0) {
ERROR("bind() failed: " << SocketStrerror());
WPI_ERROR(m_logger, "bind() failed: " << SocketStrerror());
return result;
}
result = listen(m_lsd, 5);
if (result != 0) {
ERROR("listen() failed: " << SocketStrerror());
WPI_ERROR(m_logger, "listen() failed: " << SocketStrerror());
return result;
}
m_listening = true;
@@ -172,7 +173,8 @@ std::unique_ptr<NetworkStream> TCPAcceptor::accept() {
std::memset(&address, 0, sizeof(address));
int sd = ::accept(m_lsd, (struct sockaddr*)&address, &len);
if (sd < 0) {
if (!m_shutdown) ERROR("accept() failed: " << SocketStrerror());
if (!m_shutdown)
WPI_ERROR(m_logger, "accept() failed: " << SocketStrerror());
return nullptr;
}
if (m_shutdown) {

View File

@@ -21,7 +21,7 @@
limitations under the License
*/
#include "TCPConnector.h"
#include "tcpsockets/TCPConnector.h"
#include <errno.h>
#include <fcntl.h>
@@ -38,13 +38,13 @@
#include <unistd.h>
#endif
#include "TCPStream.h"
#include "tcpsockets/TCPStream.h"
#include "llvm/SmallString.h"
#include "../Log.h"
#include "SocketError.h"
#include "support/Logger.h"
#include "tcpsockets/SocketError.h"
using namespace tcpsockets;
using namespace wpi;
static int ResolveHostName(const char* hostname, struct in_addr* addr) {
struct addrinfo hints;
@@ -68,7 +68,8 @@ static int ResolveHostName(const char* hostname, struct in_addr* addr) {
}
std::unique_ptr<NetworkStream> TCPConnector::connect(const char* server,
int port, int timeout) {
int port, Logger& logger,
int timeout) {
#ifdef _WIN32
struct WSAHelper {
WSAHelper() {
@@ -90,7 +91,7 @@ std::unique_ptr<NetworkStream> TCPConnector::connect(const char* server,
addr_copy.push_back('\0');
int size = sizeof(address);
if (WSAStringToAddress(addr_copy.data(), PF_INET, nullptr, (struct sockaddr*)&address, &size) != 0) {
ERROR("could not resolve " << server << " address");
WPI_ERROR(logger, "could not resolve " << server << " address");
return nullptr;
}
#else
@@ -102,11 +103,11 @@ std::unique_ptr<NetworkStream> TCPConnector::connect(const char* server,
if (timeout == 0) {
int sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd < 0) {
ERROR("could not create socket");
WPI_ERROR(logger, "could not create socket");
return nullptr;
}
if (::connect(sd, (struct sockaddr*)&address, sizeof(address)) != 0) {
ERROR("connect() to " << server << " port " << port << " failed: " << SocketStrerror());
WPI_ERROR(logger, "connect() to " << server << " port " << port << " failed: " << SocketStrerror());
#ifdef _WIN32
closesocket(sd);
#else
@@ -122,7 +123,7 @@ std::unique_ptr<NetworkStream> TCPConnector::connect(const char* server,
socklen_t len;
int result = -1, valopt, sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd < 0) {
ERROR("could not create socket");
WPI_ERROR(logger, "could not create socket");
return nullptr;
}
@@ -154,15 +155,15 @@ std::unique_ptr<NetworkStream> TCPConnector::connect(const char* server,
len = sizeof(int);
getsockopt(sd, SOL_SOCKET, SO_ERROR, (char*)(&valopt), &len);
if (valopt) {
ERROR("select() to " << server << " port " << port << " error " << valopt << " - " << SocketStrerror(valopt));
WPI_ERROR(logger, "select() to " << server << " port " << port << " error " << valopt << " - " << SocketStrerror(valopt));
}
// connection established
else
result = 0;
} else
INFO("connect() to " << server << " port " << port << " timed out");
WPI_INFO(logger, "connect() to " << server << " port " << port << " timed out");
} else
ERROR("connect() to " << server << " port " << port << " error " << SocketErrno() << " - " << SocketStrerror());
WPI_ERROR(logger, "connect() to " << server << " port " << port << " error " << SocketErrno() << " - " << SocketStrerror());
}
// Return socket to blocking mode

View File

@@ -21,7 +21,7 @@
limitations under the License.
*/
#include "TCPStream.h"
#include "tcpsockets/TCPStream.h"
#ifdef _WIN32
#include <WinSock2.h>
@@ -31,13 +31,15 @@
#include <unistd.h>
#endif
TCPStream::TCPStream(int sd, struct sockaddr_in* address) : m_sd(sd) {
using namespace wpi;
TCPStream::TCPStream(int sd, sockaddr_in* address) : m_sd(sd) {
char ip[50];
#ifdef _WIN32
unsigned long size = sizeof(ip) - 1;
WSAAddressToString((struct sockaddr*)address, sizeof sockaddr_in, nullptr, ip, &size);
WSAAddressToString((sockaddr*)address, sizeof sockaddr_in, nullptr, ip, &size);
#else
inet_ntop(PF_INET, (struct in_addr*)&(address->sin_addr.s_addr), ip,
inet_ntop(PF_INET, (in_addr*)&(address->sin_addr.s_addr), ip,
sizeof(ip) - 1);
#endif
m_peerIP = ip;

View File

@@ -5,11 +5,11 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#include "Base64.h"
#include "support/Base64.h"
#include "gtest/gtest.h"
namespace nt {
namespace wpi {
struct Base64TestParam {
int plain_len;
@@ -73,4 +73,4 @@ static Base64TestParam standard[] = {
INSTANTIATE_TEST_CASE_P(Base64Standard, Base64Test,
::testing::ValuesIn(standard));
} // namespace nt
} // namespace wpi

View File

@@ -63,21 +63,21 @@ class WireDecoderTest : public ::testing::Test {
};
TEST_F(WireDecoderTest, Construct) {
raw_mem_istream is("", 0);
wpi::raw_mem_istream is("", 0);
WireDecoder d(is, 0x0300u);
EXPECT_EQ(nullptr, d.error());
EXPECT_EQ(0x0300u, d.proto_rev());
}
TEST_F(WireDecoderTest, SetProtoRev) {
raw_mem_istream is("", 0);
wpi::raw_mem_istream is("", 0);
WireDecoder d(is, 0x0300u);
d.set_proto_rev(0x0200u);
EXPECT_EQ(0x0200u, d.proto_rev());
}
TEST_F(WireDecoderTest, Read8) {
raw_mem_istream is("\x05\x01\x00", 3);
wpi::raw_mem_istream is("\x05\x01\x00", 3);
WireDecoder d(is, 0x0300u);
unsigned int val;
ASSERT_TRUE(d.Read8(&val));
@@ -91,7 +91,7 @@ TEST_F(WireDecoderTest, Read8) {
}
TEST_F(WireDecoderTest, Read16) {
raw_mem_istream is("\x00\x05\x00\x01\x45\x67\x00\x00", 8);
wpi::raw_mem_istream is("\x00\x05\x00\x01\x45\x67\x00\x00", 8);
WireDecoder d(is, 0x0300u);
unsigned int val;
ASSERT_TRUE(d.Read16(&val));
@@ -107,7 +107,7 @@ TEST_F(WireDecoderTest, Read16) {
}
TEST_F(WireDecoderTest, Read32) {
raw_mem_istream is(
wpi::raw_mem_istream is(
"\x00\x00\x00\x05\x00\x00\x00\x01\x00\x00\xab\xcd"
"\x12\x34\x56\x78\x00\x00\x00\x00",
20);
@@ -130,7 +130,7 @@ TEST_F(WireDecoderTest, Read32) {
TEST_F(WireDecoderTest, ReadDouble) {
// values except min and max from
// http://www.binaryconvert.com/result_double.html
raw_mem_istream is(
wpi::raw_mem_istream is(
"\x00\x00\x00\x00\x00\x00\x00\x00"
"\x41\x0c\x13\x80\x00\x00\x00\x00"
"\x7f\xf0\x00\x00\x00\x00\x00\x00"
@@ -154,7 +154,7 @@ TEST_F(WireDecoderTest, ReadDouble) {
}
TEST_F(WireDecoderTest, ReadUleb128) {
raw_mem_istream is("\x00\x7f\x80\x01\x80", 5);
wpi::raw_mem_istream is("\x00\x7f\x80\x01\x80", 5);
WireDecoder d(is, 0x0300u);
unsigned long val;
ASSERT_TRUE(d.ReadUleb128(&val));
@@ -168,7 +168,7 @@ TEST_F(WireDecoderTest, ReadUleb128) {
}
TEST_F(WireDecoderTest, ReadType) {
raw_mem_istream is("\x00\x01\x02\x03\x10\x11\x12\x20", 8);
wpi::raw_mem_istream is("\x00\x01\x02\x03\x10\x11\x12\x20", 8);
WireDecoder d(is, 0x0300u);
NT_Type val;
ASSERT_TRUE(d.ReadType(&val));
@@ -192,7 +192,7 @@ TEST_F(WireDecoderTest, ReadType) {
}
TEST_F(WireDecoderTest, ReadTypeError) {
raw_mem_istream is("\x30", 1);
wpi::raw_mem_istream is("\x30", 1);
WireDecoder d(is, 0x0200u);
NT_Type val;
ASSERT_FALSE(d.ReadType(&val));
@@ -201,7 +201,7 @@ TEST_F(WireDecoderTest, ReadTypeError) {
}
TEST_F(WireDecoderTest, Reset) {
raw_mem_istream is("\x30", 1);
wpi::raw_mem_istream is("\x30", 1);
WireDecoder d(is, 0x0200u);
NT_Type val;
ASSERT_FALSE(d.ReadType(&val));
@@ -212,7 +212,7 @@ TEST_F(WireDecoderTest, Reset) {
}
TEST_F(WireDecoderTest, ReadBooleanValue2) {
raw_mem_istream is("\x01\x00", 2);
wpi::raw_mem_istream is("\x01\x00", 2);
WireDecoder d(is, 0x0200u);
auto val = d.ReadValue(NT_BOOLEAN);
ASSERT_TRUE(bool(val));
@@ -228,7 +228,7 @@ TEST_F(WireDecoderTest, ReadBooleanValue2) {
}
TEST_F(WireDecoderTest, ReadDoubleValue2) {
raw_mem_istream is(
wpi::raw_mem_istream is(
"\x3f\xf0\x00\x00\x00\x00\x00\x00"
"\x3f\xf0\x00\x00\x00\x00\x00\x00",
16);
@@ -246,7 +246,7 @@ TEST_F(WireDecoderTest, ReadDoubleValue2) {
}
TEST_F(WireDecoderTest, ReadStringValue2) {
raw_mem_istream is("\x00\x05hello\x00\x03" "bye\x55", 13);
wpi::raw_mem_istream is("\x00\x05hello\x00\x03" "bye\x55", 13);
WireDecoder d(is, 0x0200u);
auto val = d.ReadValue(NT_STRING);
ASSERT_TRUE(bool(val));
@@ -266,7 +266,7 @@ TEST_F(WireDecoderTest, ReadStringValue2) {
}
TEST_F(WireDecoderTest, ReadBooleanArrayValue2) {
raw_mem_istream is("\x03\x00\x01\x00\x02\x01\x00\xff", 8);
wpi::raw_mem_istream is("\x03\x00\x01\x00\x02\x01\x00\xff", 8);
WireDecoder d(is, 0x0200u);
auto val = d.ReadValue(NT_BOOLEAN_ARRAY);
ASSERT_TRUE(bool(val));
@@ -285,7 +285,7 @@ TEST_F(WireDecoderTest, ReadBooleanArrayBigValue2) {
std::string s;
s.push_back('\xff');
s.append(255, '\x00');
raw_mem_istream is(s.data(), s.size());
wpi::raw_mem_istream is(s.data(), s.size());
WireDecoder d(is, 0x0200u);
auto val = d.ReadValue(NT_BOOLEAN_ARRAY);
ASSERT_TRUE(bool(val));
@@ -296,7 +296,7 @@ TEST_F(WireDecoderTest, ReadBooleanArrayBigValue2) {
}
TEST_F(WireDecoderTest, ReadDoubleArrayValue2) {
raw_mem_istream is(
wpi::raw_mem_istream is(
"\x02\x3f\xe0\x00\x00\x00\x00\x00\x00"
"\x3f\xd0\x00\x00\x00\x00\x00\x00\x55",
18);
@@ -317,7 +317,7 @@ TEST_F(WireDecoderTest, ReadDoubleArrayBigValue2) {
std::string s;
s.push_back('\xff');
s.append(255*8, '\x00');
raw_mem_istream is(s.data(), s.size());
wpi::raw_mem_istream is(s.data(), s.size());
WireDecoder d(is, 0x0200u);
auto val = d.ReadValue(NT_DOUBLE_ARRAY);
ASSERT_TRUE(bool(val));
@@ -328,7 +328,7 @@ TEST_F(WireDecoderTest, ReadDoubleArrayBigValue2) {
}
TEST_F(WireDecoderTest, ReadStringArrayValue2) {
raw_mem_istream is("\x02\x00\x05hello\x00\x07goodbye\x55", 18);
wpi::raw_mem_istream is("\x02\x00\x05hello\x00\x07goodbye\x55", 18);
WireDecoder d(is, 0x0200u);
auto val = d.ReadValue(NT_STRING_ARRAY);
ASSERT_TRUE(bool(val));
@@ -347,7 +347,7 @@ TEST_F(WireDecoderTest, ReadStringArrayBigValue2) {
s.push_back('\xff');
for (int i=0; i<255; ++i)
s.append("\x00\x01h", 3);
raw_mem_istream is(s.data(), s.size());
wpi::raw_mem_istream is(s.data(), s.size());
WireDecoder d(is, 0x0200u);
auto val = d.ReadValue(NT_STRING_ARRAY);
ASSERT_TRUE(bool(val));
@@ -358,7 +358,7 @@ TEST_F(WireDecoderTest, ReadStringArrayBigValue2) {
}
TEST_F(WireDecoderTest, ReadValueError2) {
raw_mem_istream is("", 0);
wpi::raw_mem_istream is("", 0);
WireDecoder d(is, 0x0200u);
ASSERT_FALSE(d.ReadValue(NT_UNASSIGNED)); // unassigned
ASSERT_NE(nullptr, d.error());
@@ -373,7 +373,7 @@ TEST_F(WireDecoderTest, ReadValueError2) {
}
TEST_F(WireDecoderTest, ReadBooleanValue3) {
raw_mem_istream is("\x01\x00", 2);
wpi::raw_mem_istream is("\x01\x00", 2);
WireDecoder d(is, 0x0300u);
auto val = d.ReadValue(NT_BOOLEAN);
ASSERT_TRUE(bool(val));
@@ -389,7 +389,7 @@ TEST_F(WireDecoderTest, ReadBooleanValue3) {
}
TEST_F(WireDecoderTest, ReadDoubleValue3) {
raw_mem_istream is(
wpi::raw_mem_istream is(
"\x3f\xf0\x00\x00\x00\x00\x00\x00"
"\x3f\xf0\x00\x00\x00\x00\x00\x00",
16);
@@ -407,7 +407,7 @@ TEST_F(WireDecoderTest, ReadDoubleValue3) {
}
TEST_F(WireDecoderTest, ReadStringValue3) {
raw_mem_istream is("\x05hello\x03" "bye\x55", 11);
wpi::raw_mem_istream is("\x05hello\x03" "bye\x55", 11);
WireDecoder d(is, 0x0300u);
auto val = d.ReadValue(NT_STRING);
ASSERT_TRUE(bool(val));
@@ -427,7 +427,7 @@ TEST_F(WireDecoderTest, ReadStringValue3) {
}
TEST_F(WireDecoderTest, ReadRawValue3) {
raw_mem_istream is("\x05hello\x03" "bye\x55", 11);
wpi::raw_mem_istream is("\x05hello\x03" "bye\x55", 11);
WireDecoder d(is, 0x0300u);
auto val = d.ReadValue(NT_RAW);
ASSERT_TRUE(bool(val));
@@ -447,7 +447,7 @@ TEST_F(WireDecoderTest, ReadRawValue3) {
}
TEST_F(WireDecoderTest, ReadBooleanArrayValue3) {
raw_mem_istream is("\x03\x00\x01\x00\x02\x01\x00\xff", 8);
wpi::raw_mem_istream is("\x03\x00\x01\x00\x02\x01\x00\xff", 8);
WireDecoder d(is, 0x0300u);
auto val = d.ReadValue(NT_BOOLEAN_ARRAY);
ASSERT_TRUE(bool(val));
@@ -466,7 +466,7 @@ TEST_F(WireDecoderTest, ReadBooleanArrayBigValue3) {
std::string s;
s.push_back('\xff');
s.append(255, '\x00');
raw_mem_istream is(s.data(), s.size());
wpi::raw_mem_istream is(s.data(), s.size());
WireDecoder d(is, 0x0300u);
auto val = d.ReadValue(NT_BOOLEAN_ARRAY);
ASSERT_TRUE(bool(val));
@@ -477,7 +477,7 @@ TEST_F(WireDecoderTest, ReadBooleanArrayBigValue3) {
}
TEST_F(WireDecoderTest, ReadDoubleArrayValue3) {
raw_mem_istream is(
wpi::raw_mem_istream is(
"\x02\x3f\xe0\x00\x00\x00\x00\x00\x00"
"\x3f\xd0\x00\x00\x00\x00\x00\x00\x55",
18);
@@ -498,7 +498,7 @@ TEST_F(WireDecoderTest, ReadDoubleArrayBigValue3) {
std::string s;
s.push_back('\xff');
s.append(255*8, '\x00');
raw_mem_istream is(s.data(), s.size());
wpi::raw_mem_istream is(s.data(), s.size());
WireDecoder d(is, 0x0300u);
auto val = d.ReadValue(NT_DOUBLE_ARRAY);
ASSERT_TRUE(bool(val));
@@ -509,7 +509,7 @@ TEST_F(WireDecoderTest, ReadDoubleArrayBigValue3) {
}
TEST_F(WireDecoderTest, ReadStringArrayValue3) {
raw_mem_istream is("\x02\x05hello\x07goodbye\x55", 16);
wpi::raw_mem_istream is("\x02\x05hello\x07goodbye\x55", 16);
WireDecoder d(is, 0x0300u);
auto val = d.ReadValue(NT_STRING_ARRAY);
ASSERT_TRUE(bool(val));
@@ -528,7 +528,7 @@ TEST_F(WireDecoderTest, ReadStringArrayBigValue3) {
s.push_back('\xff');
for (int i=0; i<255; ++i)
s.append("\x01h", 2);
raw_mem_istream is(s.data(), s.size());
wpi::raw_mem_istream is(s.data(), s.size());
WireDecoder d(is, 0x0300u);
auto val = d.ReadValue(NT_STRING_ARRAY);
ASSERT_TRUE(bool(val));
@@ -539,7 +539,7 @@ TEST_F(WireDecoderTest, ReadStringArrayBigValue3) {
}
TEST_F(WireDecoderTest, ReadValueError3) {
raw_mem_istream is("", 0);
wpi::raw_mem_istream is("", 0);
WireDecoder d(is, 0x0200u);
ASSERT_FALSE(d.ReadValue(NT_UNASSIGNED)); // unassigned
ASSERT_NE(nullptr, d.error());
@@ -554,7 +554,7 @@ TEST_F(WireDecoderTest, ReadString2) {
s.append("\xff\xff", 2);
s += s_big2;
s.push_back('\x55');
raw_mem_istream is(s.data(), s.size());
wpi::raw_mem_istream is(s.data(), s.size());
WireDecoder d(is, 0x0200u);
std::string outs;
ASSERT_TRUE(d.ReadString(&outs));
@@ -581,7 +581,7 @@ TEST_F(WireDecoderTest, ReadString3) {
s.append("\x81\x80\x04", 3);
s += s_big3;
s.push_back('\x55');
raw_mem_istream is(s.data(), s.size());
wpi::raw_mem_istream is(s.data(), s.size());
WireDecoder d(is, 0x0300u);
std::string outs;
ASSERT_TRUE(d.ReadString(&outs));

View File

@@ -14,7 +14,7 @@
//
//===----------------------------------------------------------------------===//
#include "leb128.h"
#include "support/leb128.h"
#include "gtest/gtest.h"
@@ -24,9 +24,9 @@
#include "llvm/SmallString.h"
#include "llvm/StringRef.h"
#include "raw_istream.h"
#include "support/raw_istream.h"
namespace nt {
namespace wpi {
TEST(LEB128Test, WriteUleb128) {
#define EXPECT_ULEB128_EQ(EXPECTED, VALUE, PAD) \
@@ -111,4 +111,4 @@ TEST(LEB128Test, SizeUleb128) {
EXPECT_EQ(5u, SizeUleb128(UINT32_MAX));
}
} // namespace nt
} // namespace wpi