Provide C++ API. Move all classes to "nt" namespace.

The C API is now just simple wrappers around the C++ API.
This commit is contained in:
Peter Johnson
2015-07-17 07:21:07 -07:00
committed by Peter Johnson
parent 56f1481c24
commit fcbd2751ba
38 changed files with 880 additions and 439 deletions

View File

@@ -13,13 +13,17 @@
#include <string>
#include <vector>
#include "ntcore.h"
#include "llvm/ArrayRef.h"
#include "llvm/StringRef.h"
namespace ntimpl {
#include "ntcore_c.h"
namespace nt {
using llvm::ArrayRef;
using llvm::StringRef;
/** NetworkTables Entry Value */
class Value {
struct private_init {};
@@ -30,6 +34,7 @@ class Value {
NT_Type type() const { return m_val.type; }
const NT_Value& value() const { return m_val; }
unsigned long long last_change() const { return m_val.last_change; }
/*
* Type-Safe Getters
@@ -42,29 +47,29 @@ class Value {
assert(m_val.type == NT_DOUBLE);
return m_val.data.v_double;
}
llvm::StringRef GetString() const {
StringRef GetString() const {
assert(m_val.type == NT_STRING);
return m_string;
}
llvm::StringRef GetRaw() const {
StringRef GetRaw() const {
assert(m_val.type == NT_RAW);
return m_string;
}
llvm::StringRef GetRpc() const {
StringRef GetRpc() const {
assert(m_val.type == NT_RPC);
return m_string;
}
llvm::ArrayRef<int> GetBooleanArray() const {
ArrayRef<int> GetBooleanArray() const {
assert(m_val.type == NT_BOOLEAN_ARRAY);
return llvm::ArrayRef<int>(m_val.data.arr_boolean.arr,
m_val.data.arr_boolean.size);
return ArrayRef<int>(m_val.data.arr_boolean.arr,
m_val.data.arr_boolean.size);
}
llvm::ArrayRef<double> GetDoubleArray() const {
ArrayRef<double> GetDoubleArray() const {
assert(m_val.type == NT_DOUBLE_ARRAY);
return llvm::ArrayRef<double>(m_val.data.arr_double.arr,
m_val.data.arr_double.size);
return ArrayRef<double>(m_val.data.arr_double.arr,
m_val.data.arr_double.size);
}
llvm::ArrayRef<std::string> GetStringArray() const {
ArrayRef<std::string> GetStringArray() const {
assert(m_val.type == NT_STRING_ARRAY);
return m_string_array;
}
@@ -79,7 +84,7 @@ class Value {
val->m_val.data.v_double = value;
return val;
}
static std::shared_ptr<Value> MakeString(llvm::StringRef value) {
static std::shared_ptr<Value> MakeString(StringRef value) {
auto val = std::make_shared<Value>(NT_STRING, private_init());
val->m_string = value;
val->m_val.data.v_string.str = const_cast<char*>(val->m_string.c_str());
@@ -93,7 +98,7 @@ class Value {
val->m_val.data.v_string.len = val->m_string.size();
return val;
}
static std::shared_ptr<Value> MakeRaw(llvm::StringRef value) {
static std::shared_ptr<Value> MakeRaw(StringRef value) {
auto val = std::make_shared<Value>(NT_RAW, private_init());
val->m_string = value;
val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
@@ -107,7 +112,7 @@ class Value {
val->m_val.data.v_raw.len = val->m_string.size();
return val;
}
static std::shared_ptr<Value> MakeRpc(llvm::StringRef value) {
static std::shared_ptr<Value> MakeRpc(StringRef value) {
auto val = std::make_shared<Value>(NT_RPC, private_init());
val->m_string = value;
val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
@@ -122,10 +127,9 @@ class Value {
return val;
}
static std::shared_ptr<Value> MakeBooleanArray(llvm::ArrayRef<int> value);
static std::shared_ptr<Value> MakeDoubleArray(llvm::ArrayRef<double> value);
static std::shared_ptr<Value> MakeStringArray(
llvm::ArrayRef<std::string> value);
static std::shared_ptr<Value> MakeBooleanArray(ArrayRef<int> value);
static std::shared_ptr<Value> MakeDoubleArray(ArrayRef<double> value);
static std::shared_ptr<Value> MakeStringArray(ArrayRef<std::string> value);
// Note: This function moves the values out of the vector.
static std::shared_ptr<Value> MakeStringArray(
@@ -141,18 +145,11 @@ class Value {
std::vector<std::string> m_string_array;
};
void ConvertToC(const Value& in, NT_Value* out);
void ConvertToC(llvm::StringRef in, NT_String* out);
std::shared_ptr<Value> ConvertFromC(const NT_Value& value);
inline llvm::StringRef ConvertFromC(const NT_String& str) {
return llvm::StringRef(str.str, str.len);
}
bool operator==(const Value& lhs, const Value& rhs);
inline bool operator!=(const Value& lhs, const Value& rhs) {
return !(lhs == rhs);
}
} // namespace ntimpl
} // namespace nt
#endif // NT_VALUE_H_

View File

@@ -8,308 +8,12 @@
#ifndef NTCORE_H_
#define NTCORE_H_
#include <stddef.h>
/* C API */
#include "ntcore_c.h"
#ifdef __cplusplus
extern "C" {
#endif
/* C++ API */
#include "ntcore_cpp.h"
#endif /* __cplusplus */
/** Default network tables port number */
#define NT_DEFAULT_PORT 1735
/** NetworkTables data types. */
enum NT_Type {
NT_UNASSIGNED = 0,
NT_BOOLEAN = 0x01,
NT_DOUBLE = 0x02,
NT_STRING = 0x04,
NT_RAW = 0x08,
NT_BOOLEAN_ARRAY = 0x10,
NT_DOUBLE_ARRAY = 0x20,
NT_STRING_ARRAY = 0x40,
NT_RPC = 0x80
};
/** NetworkTables entry flags. */
enum NT_EntryFlags {
NT_PERSISTENT = 0x01
};
/*
* Structures
*/
/** A NetworkTables string. */
struct NT_String {
/** String contents (UTF-8).
* The string is NOT required to be zero-terminated.
* When returned by the library, this is zero-terminated and allocated with
* malloc().
*/
char *str;
/** Length of the string in bytes. If the string happens to be zero
* terminated, this does not include the zero-termination.
*/
size_t len;
};
/** NetworkTables Entry Value. Note this is a typed union. */
struct NT_Value {
enum NT_Type type;
unsigned long long last_change;
union {
int v_boolean;
double v_double;
struct NT_String v_string;
struct NT_String v_raw;
struct {
int *arr;
size_t size;
} arr_boolean;
struct {
double *arr;
size_t size;
} arr_double;
struct {
struct NT_String *arr;
size_t size;
} arr_string;
} data;
};
/** NetworkTables Entry Information */
struct NT_EntryInfo {
/** Entry name */
struct NT_String name;
/** Entry type */
enum NT_Type type;
/** Entry flags */
unsigned int flags;
/** Timestamp of last change to entry (type or value). */
unsigned long long last_change;
};
/** NetworkTables Connection Information */
struct NT_ConnectionInfo {
struct NT_String remote_id;
const char *remote_name;
unsigned int remote_port;
unsigned long long last_update;
unsigned int protocol_version;
};
/** NetworkTables RPC Parameter Definition */
struct NT_RpcParamDef {
struct NT_String name;
struct NT_Value def_value;
};
/** NetworkTables RPC Result Definition */
struct NT_RpcResultDef {
struct NT_String name;
enum NT_Type type;
};
/** NetworkTables RPC Definition */
struct NT_RpcDefinition {
unsigned int version;
struct NT_String name;
size_t num_params;
NT_RpcParamDef *params;
size_t num_results;
NT_RpcResultDef *results;
};
/*
* Table Functions
*/
/** Get Entry Value.
* Returns copy of current entry value.
* Note that one of the type options is "unassigned".
*
* @param name entry name (UTF-8 string)
* @param name_len length of name in bytes
* @param value storage for returned entry value
*
* It is the caller's responsibility to free value once it's no longer
* needed (the utility function NT_DisposeValue() is useful for this
* purpose).
*/
void NT_GetEntryValue(const char *name, size_t name_len,
struct NT_Value *value);
/** Set Entry Value.
* Sets new entry value. If type of new value differs from the type of the
* currently stored entry, returns error and does not update value.
*
* @param name entry name (UTF-8 string)
* @param name_len length of name in bytes
* @param value new entry value
* @return 0 on error (type mismatch), 1 on success
*/
int NT_SetEntryValue(const char *name, size_t name_len,
const struct NT_Value *value);
/** Set Entry Type and Value.
* Sets new entry value. If type of new value differs from the type of the
* currently stored entry, the currently stored entry type is overridden
* (generally this will generate an Entry Assignment message).
*
* This is NOT the preferred method to update a value; generally
* NT_SetEntryValue() should be used instead, with appropriate error handling.
*
* @param name entry name (UTF-8 string)
* @param name_len length of name in bytes
* @param value new entry value
*/
void NT_SetEntryTypeValue(const char *name, size_t name_len,
const struct NT_Value *value);
/** Set Entry Flags.
*/
void NT_SetEntryFlags(const char *name, size_t name_len, unsigned int flags);
/** Get Entry Flags.
*/
unsigned int NT_GetEntryFlags(const char *name, size_t name_len);
/** Delete Entry.
* Deletes an entry. This is a new feature in version 3.0 of the protocol,
* so this may not have an effect if any other node in the network is not
* version 3.0 or newer.
*
* Note: NT_GetConnections() can be used to determine the protocol version
* of direct remote connection(s), but this is not sufficient to determine
* if all nodes in the network are version 3.0 or newer.
*
* @param name entry name (UTF-8 string)
* @param name_len length of name in bytes
*/
void NT_DeleteEntry(const char *name, size_t name_len);
/** Delete All Entries.
* Deletes ALL table entries. This is a new feature in version 3.0 of the
* so this may not have an effect if any other node in the network is not
* version 3.0 or newer.
*
* Note: NT_GetConnections() can be used to determine the protocol version
* of direct remote connection(s), but this is not sufficient to determine
* if all nodes in the network are version 3.0 or newer.
*/
void NT_DeleteAllEntries(void);
/** Get Entry Information.
* Returns an array of entry information (name, entry type,
* and timestamp of last change to type/value). The results are optionally
* filtered by string prefix and entry type to only return a subset of all
* entries.
*
* @param prefix entry name required prefix; only entries whose name
* starts with this string are returned
* @param prefix_len length of prefix in bytes
* @param types bitmask of NT_Type values; 0 is treated specially
* as a "don't care"
* @param count output parameter; set to length of returned array
* @return Array of entry information.
*/
struct NT_EntryInfo *NT_GetEntryInfo(const char *prefix, size_t prefix_len,
unsigned int types, size_t *count);
/** Flush Entries.
* Forces an immediate flush of all local entry changes to network.
* Normally this is done on a regularly scheduled interval (see
* NT_SetUpdateRate()).
*
* Note: flushes are rate limited to avoid excessive network traffic. If
* the time between calls is too short, the flush will occur after the minimum
* time elapses (rather than immediately).
*/
void NT_Flush(void);
/*
* Callback Creation Functions
*/
typedef void (*NT_EntryListenerCallback)(
unsigned int uid, void *data, const char *name, size_t name_len,
struct NT_Value *value);
typedef void (*NT_ConnectionListenerCallback)(
unsigned int uid, void *data, int connected,
const struct NT_ConnectionInfo *conn);
unsigned int NT_AddEntryListener(const char *prefix, size_t prefix_len,
void *data, NT_EntryListenerCallback callback);
void NT_RemoveEntryListener(unsigned int entry_listener_uid);
unsigned int NT_AddConnectionListener(void *data,
NT_ConnectionListenerCallback callback);
void NT_RemoveConnectionListener(unsigned int conn_listener_uid);
/*
* Remote Procedure Call Functions
*/
typedef NT_Value *(*NT_RpcCallback)(unsigned int uid, void *data,
const char *name, size_t name_len,
NT_Value *params, size_t params_len,
size_t *results_len);
unsigned int NT_CreateRpc(const char *name, size_t name_len,
const NT_RpcDefinition *def, void *data,
NT_RpcCallback callback);
void NT_DeleteRpc(unsigned int rpc_uid);
unsigned int NT_CallRpc(const char *name, size_t name_len,
const NT_Value *params, size_t params_len);
NT_Value *NT_GetRpcResult(unsigned int result_uid, size_t *results_len);
/*
* Client/Server Functions
*/
void NT_SetNetworkIdentity(const char *name, size_t name_len);
void NT_StartServer(const char *persist_filename, const char *listen_address,
unsigned int port);
void NT_StopServer(void);
void NT_StartClient(const char *server_name, unsigned int port);
void NT_StopClient(void);
void NT_SetUpdateRate(double interval);
struct NT_ConnectionInfo *NT_GetConnections(size_t *count);
/*
* Persistent Functions
*/
/* return error string, or NULL if successful */
const char *NT_SavePersistent(const char *filename);
const char *NT_LoadPersistent(const char *filename,
void (*warn)(size_t line, const char *msg));
/*
* Utility Functions
*/
/* frees value memory */
void NT_DisposeValue(struct NT_Value *value);
/* sets type to unassigned and clears rest of struct */
void NT_InitValue(struct NT_Value *value);
/* frees string memory */
void NT_DisposeString(struct NT_String *str);
/* sets length to zero and pointer to null */
void NT_InitString(struct NT_String *str);
void NT_DisposeConnectionInfoArray(struct NT_ConnectionInfo *arr, size_t count);
/* timestamp */
unsigned long long NT_Now(void);
#ifdef __cplusplus
}
#endif
#endif /* NTCORE_H_ */
#endif /* NTCORE_H_ */

315
include/ntcore_c.h Normal file
View File

@@ -0,0 +1,315 @@
/*----------------------------------------------------------------------------*/
/* 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 NTCORE_C_H_
#define NTCORE_C_H_
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/** Default network tables port number */
#define NT_DEFAULT_PORT 1735
/** NetworkTables data types. */
enum NT_Type {
NT_UNASSIGNED = 0,
NT_BOOLEAN = 0x01,
NT_DOUBLE = 0x02,
NT_STRING = 0x04,
NT_RAW = 0x08,
NT_BOOLEAN_ARRAY = 0x10,
NT_DOUBLE_ARRAY = 0x20,
NT_STRING_ARRAY = 0x40,
NT_RPC = 0x80
};
/** NetworkTables entry flags. */
enum NT_EntryFlags {
NT_PERSISTENT = 0x01
};
/*
* Structures
*/
/** A NetworkTables string. */
struct NT_String {
/** String contents (UTF-8).
* The string is NOT required to be zero-terminated.
* When returned by the library, this is zero-terminated and allocated with
* malloc().
*/
char *str;
/** Length of the string in bytes. If the string happens to be zero
* terminated, this does not include the zero-termination.
*/
size_t len;
};
/** NetworkTables Entry Value. Note this is a typed union. */
struct NT_Value {
enum NT_Type type;
unsigned long long last_change;
union {
int v_boolean;
double v_double;
struct NT_String v_string;
struct NT_String v_raw;
struct {
int *arr;
size_t size;
} arr_boolean;
struct {
double *arr;
size_t size;
} arr_double;
struct {
struct NT_String *arr;
size_t size;
} arr_string;
} data;
};
/** NetworkTables Entry Information */
struct NT_EntryInfo {
/** Entry name */
struct NT_String name;
/** Entry type */
enum NT_Type type;
/** Entry flags */
unsigned int flags;
/** Timestamp of last change to entry (type or value). */
unsigned long long last_change;
};
/** NetworkTables Connection Information */
struct NT_ConnectionInfo {
struct NT_String remote_id;
const char *remote_name;
unsigned int remote_port;
unsigned long long last_update;
unsigned int protocol_version;
};
/** NetworkTables RPC Parameter Definition */
struct NT_RpcParamDef {
struct NT_String name;
struct NT_Value def_value;
};
/** NetworkTables RPC Result Definition */
struct NT_RpcResultDef {
struct NT_String name;
enum NT_Type type;
};
/** NetworkTables RPC Definition */
struct NT_RpcDefinition {
unsigned int version;
struct NT_String name;
size_t num_params;
NT_RpcParamDef *params;
size_t num_results;
NT_RpcResultDef *results;
};
/*
* Table Functions
*/
/** Get Entry Value.
* Returns copy of current entry value.
* Note that one of the type options is "unassigned".
*
* @param name entry name (UTF-8 string)
* @param name_len length of name in bytes
* @param value storage for returned entry value
*
* It is the caller's responsibility to free value once it's no longer
* needed (the utility function NT_DisposeValue() is useful for this
* purpose).
*/
void NT_GetEntryValue(const char *name, size_t name_len,
struct NT_Value *value);
/** Set Entry Value.
* Sets new entry value. If type of new value differs from the type of the
* currently stored entry, returns error and does not update value.
*
* @param name entry name (UTF-8 string)
* @param name_len length of name in bytes
* @param value new entry value
* @return 0 on error (type mismatch), 1 on success
*/
int NT_SetEntryValue(const char *name, size_t name_len,
const struct NT_Value *value);
/** Set Entry Type and Value.
* Sets new entry value. If type of new value differs from the type of the
* currently stored entry, the currently stored entry type is overridden
* (generally this will generate an Entry Assignment message).
*
* This is NOT the preferred method to update a value; generally
* NT_SetEntryValue() should be used instead, with appropriate error handling.
*
* @param name entry name (UTF-8 string)
* @param name_len length of name in bytes
* @param value new entry value
*/
void NT_SetEntryTypeValue(const char *name, size_t name_len,
const struct NT_Value *value);
/** Set Entry Flags.
*/
void NT_SetEntryFlags(const char *name, size_t name_len, unsigned int flags);
/** Get Entry Flags.
*/
unsigned int NT_GetEntryFlags(const char *name, size_t name_len);
/** Delete Entry.
* Deletes an entry. This is a new feature in version 3.0 of the protocol,
* so this may not have an effect if any other node in the network is not
* version 3.0 or newer.
*
* Note: NT_GetConnections() can be used to determine the protocol version
* of direct remote connection(s), but this is not sufficient to determine
* if all nodes in the network are version 3.0 or newer.
*
* @param name entry name (UTF-8 string)
* @param name_len length of name in bytes
*/
void NT_DeleteEntry(const char *name, size_t name_len);
/** Delete All Entries.
* Deletes ALL table entries. This is a new feature in version 3.0 of the
* so this may not have an effect if any other node in the network is not
* version 3.0 or newer.
*
* Note: NT_GetConnections() can be used to determine the protocol version
* of direct remote connection(s), but this is not sufficient to determine
* if all nodes in the network are version 3.0 or newer.
*/
void NT_DeleteAllEntries(void);
/** Get Entry Information.
* Returns an array of entry information (name, entry type,
* and timestamp of last change to type/value). The results are optionally
* filtered by string prefix and entry type to only return a subset of all
* entries.
*
* @param prefix entry name required prefix; only entries whose name
* starts with this string are returned
* @param prefix_len length of prefix in bytes
* @param types bitmask of NT_Type values; 0 is treated specially
* as a "don't care"
* @param count output parameter; set to length of returned array
* @return Array of entry information.
*/
struct NT_EntryInfo *NT_GetEntryInfo(const char *prefix, size_t prefix_len,
unsigned int types, size_t *count);
/** Flush Entries.
* Forces an immediate flush of all local entry changes to network.
* Normally this is done on a regularly scheduled interval (see
* NT_SetUpdateRate()).
*
* Note: flushes are rate limited to avoid excessive network traffic. If
* the time between calls is too short, the flush will occur after the minimum
* time elapses (rather than immediately).
*/
void NT_Flush(void);
/*
* Callback Creation Functions
*/
typedef void (*NT_EntryListenerCallback)(
unsigned int uid, void *data, const char *name, size_t name_len,
struct NT_Value *value);
typedef void (*NT_ConnectionListenerCallback)(
unsigned int uid, void *data, int connected,
const struct NT_ConnectionInfo *conn);
unsigned int NT_AddEntryListener(const char *prefix, size_t prefix_len,
void *data, NT_EntryListenerCallback callback);
void NT_RemoveEntryListener(unsigned int entry_listener_uid);
unsigned int NT_AddConnectionListener(void *data,
NT_ConnectionListenerCallback callback);
void NT_RemoveConnectionListener(unsigned int conn_listener_uid);
/*
* Remote Procedure Call Functions
*/
typedef NT_Value *(*NT_RpcCallback)(unsigned int uid, void *data,
const char *name, size_t name_len,
NT_Value *params, size_t params_len,
size_t *results_len);
unsigned int NT_CreateRpc(const char *name, size_t name_len,
const NT_RpcDefinition *def, void *data,
NT_RpcCallback callback);
void NT_DeleteRpc(unsigned int rpc_uid);
unsigned int NT_CallRpc(const char *name, size_t name_len,
const NT_Value *params, size_t params_len);
NT_Value *NT_GetRpcResult(unsigned int result_uid, size_t *results_len);
/*
* Client/Server Functions
*/
void NT_SetNetworkIdentity(const char *name, size_t name_len);
void NT_StartServer(const char *persist_filename, const char *listen_address,
unsigned int port);
void NT_StopServer(void);
void NT_StartClient(const char *server_name, unsigned int port);
void NT_StopClient(void);
void NT_SetUpdateRate(double interval);
struct NT_ConnectionInfo *NT_GetConnections(size_t *count);
/*
* Persistent Functions
*/
/* return error string, or NULL if successful */
const char *NT_SavePersistent(const char *filename);
const char *NT_LoadPersistent(const char *filename,
void (*warn)(size_t line, const char *msg));
/*
* Utility Functions
*/
/* frees value memory */
void NT_DisposeValue(struct NT_Value *value);
/* sets type to unassigned and clears rest of struct */
void NT_InitValue(struct NT_Value *value);
/* frees string memory */
void NT_DisposeString(struct NT_String *str);
/* sets length to zero and pointer to null */
void NT_InitString(struct NT_String *str);
void NT_DisposeConnectionInfoArray(struct NT_ConnectionInfo *arr, size_t count);
/* timestamp */
unsigned long long NT_Now(void);
#ifdef __cplusplus
}
#endif
#endif /* NTCORE_C_H_ */

228
include/ntcore_cpp.h Normal file
View File

@@ -0,0 +1,228 @@
/*----------------------------------------------------------------------------*/
/* 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 NTCORE_CPP_H_
#define NTCORE_CPP_H_
#include <cassert>
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "llvm/ArrayRef.h"
#include "llvm/StringRef.h"
#include "nt_Value.h"
namespace nt {
using llvm::ArrayRef;
using llvm::StringRef;
/** NetworkTables Entry Information */
struct EntryInfo {
/** Entry name */
std::string name;
/** Entry type */
NT_Type type;
/** Entry flags */
unsigned int flags;
/** Timestamp of last change to entry (type or value). */
unsigned long long last_change;
};
/** NetworkTables Connection Information */
struct ConnectionInfo {
std::string remote_id;
std::string remote_name;
unsigned int remote_port;
unsigned long long last_update;
unsigned int protocol_version;
};
/** NetworkTables RPC Parameter Definition */
struct RpcParamDef {
std::string name;
std::shared_ptr<Value> def_value;
};
/** NetworkTables RPC Result Definition */
struct RpcResultDef {
std::string name;
NT_Type type;
};
/** NetworkTables RPC Definition */
struct RpcDefinition {
unsigned int version;
std::string name;
std::vector<RpcParamDef> params;
std::vector<RpcResultDef> results;
};
/*
* Table Functions
*/
/** Get Entry Value.
* Returns copy of current entry value.
* Note that one of the type options is "unassigned".
*
* @param name entry name (UTF-8 string)
* @return entry value
*
* It is the caller's responsibility to free value once it's no longer
* needed (the utility function NT_DisposeValue() is useful for this
* purpose).
*/
std::shared_ptr<Value> GetEntryValue(StringRef name);
/** Set Entry Value.
* Sets new entry value. If type of new value differs from the type of the
* currently stored entry, returns error and does not update value.
*
* @param name entry name (UTF-8 string)
* @param value new entry value
* @return False on error (type mismatch), True on success
*/
bool SetEntryValue(StringRef name, std::shared_ptr<Value> value);
/** Set Entry Type and Value.
* Sets new entry value. If type of new value differs from the type of the
* currently stored entry, the currently stored entry type is overridden
* (generally this will generate an Entry Assignment message).
*
* This is NOT the preferred method to update a value; generally
* SetEntryValue() should be used instead, with appropriate error handling.
*
* @param name entry name (UTF-8 string)
* @param value new entry value
*/
void SetEntryTypeValue(StringRef name, std::shared_ptr<Value> value);
/** Set Entry Flags.
*/
void SetEntryFlags(StringRef name, unsigned int flags);
/** Get Entry Flags.
*/
unsigned int GetEntryFlags(StringRef name);
/** Delete Entry.
* Deletes an entry. This is a new feature in version 3.0 of the protocol,
* so this may not have an effect if any other node in the network is not
* version 3.0 or newer.
*
* Note: GetConnections() can be used to determine the protocol version
* of direct remote connection(s), but this is not sufficient to determine
* if all nodes in the network are version 3.0 or newer.
*
* @param name entry name (UTF-8 string)
*/
void DeleteEntry(StringRef name);
/** Delete All Entries.
* Deletes ALL table entries. This is a new feature in version 3.0 of the
* so this may not have an effect if any other node in the network is not
* version 3.0 or newer.
*
* Note: GetConnections() can be used to determine the protocol version
* of direct remote connection(s), but this is not sufficient to determine
* if all nodes in the network are version 3.0 or newer.
*/
void DeleteAllEntries();
/** Get Entry Information.
* Returns an array of entry information (name, entry type,
* and timestamp of last change to type/value). The results are optionally
* filtered by string prefix and entry type to only return a subset of all
* entries.
*
* @param prefix entry name required prefix; only entries whose name
* starts with this string are returned
* @param types bitmask of NT_Type values; 0 is treated specially
* as a "don't care"
* @return Array of entry information.
*/
std::vector<EntryInfo> GetEntryInfo(StringRef prefix, unsigned int types);
/** Flush Entries.
* Forces an immediate flush of all local entry changes to network.
* Normally this is done on a regularly scheduled interval (see
* NT_SetUpdateRate()).
*
* Note: flushes are rate limited to avoid excessive network traffic. If
* the time between calls is too short, the flush will occur after the minimum
* time elapses (rather than immediately).
*/
void Flush();
/*
* Callback Creation Functions
*/
typedef std::function<void(unsigned int uid, StringRef name,
std::shared_ptr<Value> value)> EntryListenerCallback;
typedef std::function<void(unsigned int uid, int connected,
const ConnectionInfo& conn)>
ConnectionListenerCallback;
unsigned int AddEntryListener(StringRef prefix, EntryListenerCallback callback);
void RemoveEntryListener(unsigned int entry_listener_uid);
unsigned int AddConnectionListener(ConnectionListenerCallback callback);
void RemoveConnectionListener(unsigned int conn_listener_uid);
/*
* Remote Procedure Call Functions
*/
typedef std::function<std::vector<std::shared_ptr<Value>>(
unsigned int uid, StringRef name, ArrayRef<std::shared_ptr<Value>> params)>
RpcCallback;
unsigned int CreateRpc(StringRef name, const RpcDefinition& def,
RpcCallback callback);
void DeleteRpc(unsigned int rpc_uid);
unsigned int CallRpc(StringRef name, ArrayRef<std::shared_ptr<Value>> params);
std::vector<std::shared_ptr<Value>> GetRpcResult(unsigned int result_uid);
/*
* Client/Server Functions
*/
void SetNetworkIdentity(llvm::StringRef name);
void StartServer(const char* persist_filename, const char* listen_address,
unsigned int port);
void StopServer();
void StartClient(const char* server_name, unsigned int port);
void StopClient();
void SetUpdateRate(double interval);
std::vector<ConnectionInfo> GetConnections();
/*
* Persistent Functions
*/
/* return error string, or nullptr if successful */
const char* SavePersistent(const char* filename);
const char* LoadPersistent(
const char* filename,
std::function<void(size_t line, const char *msg)> warn);
/*
* Utility Functions
*/
/* timestamp */
unsigned long long Now();
} // namespace nt
#endif /* NTCORE_CPP_H_ */

View File

@@ -64,7 +64,7 @@
#include "Base64.h"
namespace ntimpl {
namespace nt {
// 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 ntimpl
} // namespace nt

View File

@@ -13,11 +13,11 @@
#include "llvm/StringRef.h"
namespace ntimpl {
namespace nt {
std::size_t Base64Decode(llvm::StringRef encoded, std::string* plain);
void Base64Encode(llvm::StringRef plain, std::string* encoded);
} // namespace ntimpl
} // namespace nt
#endif // NT_BASE64_H_

View File

@@ -10,7 +10,7 @@
#include "tcpsockets/TCPAcceptor.h"
#include "tcpsockets/TCPConnector.h"
using namespace ntimpl;
using namespace nt;
std::unique_ptr<Dispatcher> Dispatcher::m_instance;

View File

@@ -18,7 +18,7 @@
#include "NetworkConnection.h"
namespace ntimpl {
namespace nt {
class Dispatcher {
public:
@@ -64,6 +64,6 @@ class Dispatcher {
static std::unique_ptr<Dispatcher> m_instance;
};
} // namespace ntimpl
} // namespace nt
#endif // NT_DISPATCHER_H_

View File

@@ -12,7 +12,7 @@
static constexpr unsigned long kClearAllMagic = 0xD06CB27Aul;
using namespace ntimpl;
using namespace nt;
std::shared_ptr<Message> Message::Read(WireDecoder& decoder,
GetEntryTypeFunc get_entry_type) {

View File

@@ -11,9 +11,9 @@
#include <memory>
#include <string>
#include "Value.h"
#include "nt_Value.h"
namespace ntimpl {
namespace nt {
class WireDecoder;
class WireEncoder;
@@ -107,6 +107,6 @@ class Message {
unsigned int m_seq_num_uid;
};
} // namespace ntimpl
} // namespace nt
#endif // NT_MESSAGE_H_

View File

@@ -12,7 +12,7 @@
#include "WireDecoder.h"
#include "WireEncoder.h"
using namespace ntimpl;
using namespace nt;
NetworkConnection::NetworkConnection(std::unique_ptr<TCPStream> stream,
Message::GetEntryTypeFunc get_entry_type)

View File

@@ -17,7 +17,7 @@
class TCPStream;
namespace ntimpl {
namespace nt {
class NetworkConnection {
public:
@@ -56,6 +56,6 @@ class NetworkConnection {
std::atomic_uint m_proto_rev;
};
} // namespace ntimpl
} // namespace nt
#endif // NT_NETWORKCONNECTION_H_

View File

@@ -7,7 +7,7 @@
#include "SequenceNumber.h"
namespace ntimpl {
namespace nt {
bool operator<(const SequenceNumber& lhs, const SequenceNumber& rhs) {
if (lhs.m_value < rhs.m_value)
@@ -27,4 +27,4 @@ bool operator>(const SequenceNumber& lhs, const SequenceNumber& rhs) {
return false;
}
} // namespace ntimpl
} // namespace nt

View File

@@ -8,7 +8,7 @@
#ifndef NT_SEQNUM_H_
#define NT_SEQNUM_H_
namespace ntimpl {
namespace nt {
/* A sequence number per RFC 1982 */
class SequenceNumber {
@@ -57,6 +57,6 @@ inline bool operator!=(const SequenceNumber& lhs, const SequenceNumber& rhs) {
return lhs.m_value != rhs.m_value;
}
} // namespace ntimpl
} // namespace nt
#endif // NT_SEQNUM_H_

View File

@@ -13,7 +13,7 @@
#include "llvm/StringExtras.h"
#include "Base64.h"
using namespace ntimpl;
using namespace nt;
std::unique_ptr<Storage> Storage::m_instance;
@@ -233,8 +233,9 @@ static void UnescapeString(llvm::StringRef source, std::string* dest) {
}
}
bool Storage::LoadPersistent(std::istream& is,
void (*warn)(std::size_t line, const char* msg)) {
bool Storage::LoadPersistent(
std::istream& is,
std::function<void(std::size_t line, const char* msg)> warn) {
std::string line_str;
std::size_t line_num = 1;

View File

@@ -9,15 +9,14 @@
#define NT_STORAGE_H_
#include <cstddef>
#include <functional>
#include <iosfwd>
#include <memory>
#include "ntcore.h"
#include "Value.h"
#include "llvm/StringMap.h"
#include "nt_Value.h"
namespace ntimpl {
namespace nt {
class StorageEntry {
public:
@@ -53,8 +52,9 @@ class Storage {
const EntriesMap& entries() const { return m_entries; }
void SavePersistent(std::ostream& os) const;
bool LoadPersistent(std::istream& is,
void (*warn)(std::size_t line, const char* msg));
bool LoadPersistent(
std::istream& is,
std::function<void(std::size_t line, const char* msg)> warn);
private:
Storage();
@@ -66,6 +66,6 @@ class Storage {
static std::unique_ptr<Storage> m_instance;
};
} // namespace ntimpl
} // namespace nt
#endif // NT_STORAGE_H_

View File

@@ -5,9 +5,10 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#include "Value.h"
#include "nt_Value.h"
#include "Value_internal.h"
using namespace ntimpl;
using namespace nt;
Value::Value() { m_val.type = NT_UNASSIGNED; }
@@ -74,7 +75,7 @@ std::shared_ptr<Value> Value::MakeStringArray(
return val;
}
void ntimpl::ConvertToC(const Value& in, NT_Value* out) {
void nt::ConvertToC(const Value& in, NT_Value* out) {
NT_DisposeValue(out);
switch (in.type()) {
case NT_UNASSIGNED:
@@ -131,7 +132,7 @@ void ntimpl::ConvertToC(const Value& in, NT_Value* out) {
out->type = in.type();
}
void ntimpl::ConvertToC(llvm::StringRef in, NT_String* out) {
void nt::ConvertToC(llvm::StringRef in, NT_String* out) {
NT_DisposeString(out);
out->len = in.size();
out->str = static_cast<char*>(std::malloc(in.size()+1));
@@ -139,7 +140,7 @@ void ntimpl::ConvertToC(llvm::StringRef in, NT_String* out) {
out->str[in.size()] = '\0';
}
std::shared_ptr<Value> ntimpl::ConvertFromC(const NT_Value& value) {
std::shared_ptr<Value> nt::ConvertFromC(const NT_Value& value) {
switch (value.type) {
case NT_UNASSIGNED:
return nullptr;
@@ -172,7 +173,7 @@ std::shared_ptr<Value> ntimpl::ConvertFromC(const NT_Value& value) {
}
}
bool ntimpl::operator==(const Value& lhs, const Value& rhs) {
bool nt::operator==(const Value& lhs, const Value& rhs) {
if (lhs.type() != rhs.type()) return false;
switch (lhs.type()) {
case NT_UNASSIGNED:

30
src/Value_internal.h Normal file
View File

@@ -0,0 +1,30 @@
/*----------------------------------------------------------------------------*/
/* 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 NT_VALUE_INTERNAL_H_
#define NT_VALUE_INTERNAL_H_
#include <memory>
#include <string>
#include "llvm/StringRef.h"
#include "ntcore_c.h"
namespace nt {
class Value;
void ConvertToC(const Value& in, NT_Value* out);
std::shared_ptr<Value> ConvertFromC(const NT_Value& value);
void ConvertToC(llvm::StringRef in, NT_String* out);
inline llvm::StringRef ConvertFromC(const NT_String& str) {
return llvm::StringRef(str.str, str.len);
}
} // namespace nt
#endif // NT_VALUE_INTERNAL_H_

View File

@@ -14,7 +14,7 @@
#include "leb128.h"
using namespace ntimpl;
using namespace nt;
static double ReadDouble(const char*& buf) {
// Fast but non-portable!

View File

@@ -10,12 +10,11 @@
#include <cstddef>
#include "ntcore.h"
#include "nt_Value.h"
#include "leb128.h"
#include "raw_istream.h"
#include "Value.h"
namespace ntimpl {
namespace nt {
/* Decodes network data into native representation.
* This class is designed to read from a raw_istream, which provides a blocking
@@ -98,7 +97,7 @@ class WireDecoder {
/* Reads an ULEB128-encoded unsigned integer. */
bool ReadUleb128(unsigned long* val) {
return ntimpl::ReadUleb128(m_is, val);
return nt::ReadUleb128(m_is, val);
}
bool ReadType(NT_Type* type);
@@ -129,6 +128,6 @@ class WireDecoder {
std::size_t m_allocated;
};
} // namespace ntimpl
} // namespace nt
#endif // NT_WIREDECODER_H_

View File

@@ -14,7 +14,7 @@
#include "leb128.h"
using namespace ntimpl;
using namespace nt;
WireEncoder::WireEncoder(unsigned int proto_rev) {
// Start with a 1024-byte buffer. Use malloc instead of new so we can
@@ -59,7 +59,7 @@ void WireEncoder::ReserveSlow(std::size_t len) {
void WireEncoder::WriteUleb128(unsigned long val) {
Reserve(SizeUleb128(val));
m_cur += ntimpl::WriteUleb128(m_cur, val);
m_cur += nt::WriteUleb128(m_cur, val);
}
void WireEncoder::WriteType(NT_Type type) {

View File

@@ -12,9 +12,9 @@
#include <cstddef>
#include "llvm/StringRef.h"
#include "Value.h"
#include "nt_Value.h"
namespace ntimpl {
namespace nt {
/* Encodes native data for network transmission.
* This class maintains an internal memory buffer for written data so that
@@ -130,6 +130,6 @@ class WireEncoder {
char* m_end;
};
} // namespace ntimpl
} // namespace nt
#endif // NT_WIREENCODER_H_

View File

@@ -9,7 +9,7 @@
#include "raw_istream.h"
namespace ntimpl {
namespace nt {
/**
* Get size of unsigned LEB128 data
@@ -117,4 +117,4 @@ bool ReadUleb128(raw_istream& is, unsigned long* ret) {
return true;
}
} // namespace ntimpl
} // namespace nt

View File

@@ -10,7 +10,7 @@
#include <cstddef>
namespace ntimpl {
namespace nt {
class raw_istream;
@@ -19,6 +19,6 @@ std::size_t WriteUleb128(char* addr, unsigned long val);
std::size_t ReadUleb128(const char* addr, unsigned long* ret);
bool ReadUleb128(raw_istream& is, unsigned long* ret);
} // namespace ntimpl
} // namespace nt
#endif // NT_LEB128_H_

View File

@@ -11,35 +11,45 @@
#include <cstdlib>
#include <fstream>
#include "Dispatcher.h"
#include "Storage.h"
#include "Value_internal.h"
using namespace ntimpl;
using namespace nt;
/*
* Table Functions
*/
void NT_GetEntryValue(const char *name, unsigned int name_len,
struct NT_Value *value) {}
struct NT_Value *value) {
NT_InitValue(value);
auto v = nt::GetEntryValue(StringRef(name, name_len));
if (!v) return;
ConvertToC(*v, value);
}
int NT_SetEntryValue(const char *name, unsigned int name_len,
const struct NT_Value *value) {
return 0;
return nt::SetEntryValue(StringRef(name, name_len), ConvertFromC(*value));
}
void NT_SetEntryTypeValue(const char *name, unsigned int name_len,
const struct NT_Value *value) {}
void NT_SetEntryFlags(const char *name, size_t name_len, unsigned int flags) {}
unsigned int NT_GetEntryFlags(const char *name, size_t name_len) {
return 0;
const struct NT_Value *value) {
nt::SetEntryTypeValue(StringRef(name, name_len), ConvertFromC(*value));
}
void NT_DeleteEntry(const char *name, unsigned int name_len) {}
void NT_SetEntryFlags(const char *name, size_t name_len, unsigned int flags) {
nt::SetEntryFlags(StringRef(name, name_len), flags);
}
void NT_DeleteAllEntries(void) {}
unsigned int NT_GetEntryFlags(const char *name, size_t name_len) {
return nt::GetEntryFlags(StringRef(name, name_len));
}
void NT_DeleteEntry(const char *name, unsigned int name_len) {
nt::DeleteEntry(StringRef(name, name_len));
}
void NT_DeleteAllEntries(void) { nt::DeleteAllEntries(); }
struct NT_EntryInfo *NT_GetEntryInfo(const char *prefix,
unsigned int prefix_len, int types,
@@ -47,7 +57,7 @@ struct NT_EntryInfo *NT_GetEntryInfo(const char *prefix,
return nullptr;
}
void NT_Flush(void) {}
void NT_Flush(void) { nt::Flush(); }
/*
* Callback Creation Functions
@@ -58,12 +68,16 @@ unsigned int NT_AddEntryListener(const char *prefix, size_t prefix_len,
NT_EntryListenerCallback callback) {
return 0;
}
void NT_RemoveEntryListener(unsigned int entry_listener_uid) {}
void NT_RemoveEntryListener(unsigned int entry_listener_uid) {
nt::RemoveEntryListener(entry_listener_uid);
}
unsigned int NT_AddConnectionListener(void *data,
NT_ConnectionListenerCallback callback) {
return 0;
}
void NT_RemoveConnectionListener(unsigned int conn_listener_uid) {}
void NT_RemoveConnectionListener(unsigned int conn_listener_uid) {
nt::RemoveConnectionListener(conn_listener_uid);
}
/*
* Remote Procedure Call Functions
@@ -72,44 +86,56 @@ void NT_RemoveConnectionListener(unsigned int conn_listener_uid) {}
unsigned int NT_CreateRpc(const char *name, size_t name_len,
const NT_RpcDefinition *def, void *data,
NT_RpcCallback callback);
void NT_DeleteRpc(unsigned int rpc_uid);
void NT_DeleteRpc(unsigned int rpc_uid) {
nt::DeleteRpc(rpc_uid);
}
unsigned int NT_CallRpc(const char *name, size_t name_len,
const NT_Value *params, size_t params_len);
NT_Value *NT_GetRpcResult(unsigned int result_uid, size_t *results_len);
const NT_Value *params, size_t params_len) {
std::vector<std::shared_ptr<Value>> params_v;
params_v.reserve(params_len);
for (size_t i=0; i<params_len; ++i)
params_v.push_back(ConvertFromC(params[i]));
return nt::CallRpc(StringRef(name, name_len), params_v);
}
NT_Value *NT_GetRpcResult(unsigned int result_uid, size_t *results_len) {
auto results_v = nt::GetRpcResult(result_uid);
*results_len = results_v.size();
if (results_v.size() == 0) return nullptr;
NT_Value *results =
static_cast<NT_Value *>(std::malloc(results_v.size() * sizeof(NT_Value)));
for (size_t i=0; i<results_v.size(); ++i)
ConvertToC(*results_v[i], &results[i]);
return results;
}
/*
* Client/Server Functions
*/
void NT_SetNetworkIdentity(const char *name, size_t name_len) {
Dispatcher& dispatcher = Dispatcher::GetInstance();
dispatcher.SetIdentity(llvm::StringRef(name, name_len));
nt::SetNetworkIdentity(StringRef(name, name_len));
}
void NT_StartServer(const char *persist_filename, const char *listen_address,
unsigned int port) {
Dispatcher& dispatcher = Dispatcher::GetInstance();
dispatcher.StartServer(listen_address, port);
nt::StartServer(persist_filename, listen_address, port);
}
void NT_StopServer(void) {
Dispatcher& dispatcher = Dispatcher::GetInstance();
dispatcher.Stop();
}
void NT_StopServer(void) { nt::StopServer(); }
void NT_StartClient(const char *server_name, unsigned int port) {
Dispatcher& dispatcher = Dispatcher::GetInstance();
dispatcher.StartClient(server_name, port);
nt::StartClient(server_name, port);
}
void NT_StopClient(void) {
Dispatcher& dispatcher = Dispatcher::GetInstance();
dispatcher.Stop();
nt::StopClient();
}
void NT_SetUpdateRate(double interval) {
Dispatcher& dispatcher = Dispatcher::GetInstance();
dispatcher.SetUpdateRate(interval);
nt::SetUpdateRate(interval);
}
struct NT_ConnectionInfo *NT_GetConnections(size_t *count) {
@@ -121,20 +147,12 @@ struct NT_ConnectionInfo *NT_GetConnections(size_t *count) {
*/
const char *NT_SavePersistent(const char *filename) {
const Storage& storage = Storage::GetInstance();
std::ofstream os(filename);
if (!os) return "could not open file";
storage.SavePersistent(os);
return nullptr;
return nt::SavePersistent(filename);
}
const char *NT_LoadPersistent(const char *filename,
void (*warn)(size_t line, const char *msg)) {
Storage& storage = Storage::GetInstance();
std::ifstream is(filename);
if (!is) return "could not open file";
if (!storage.LoadPersistent(is, warn)) return "error reading file";
return nullptr;
return nt::LoadPersistent(filename, warn);
}
/*

147
src/ntcore_cpp.cpp Normal file
View File

@@ -0,0 +1,147 @@
/*----------------------------------------------------------------------------*/
/* 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. */
/*----------------------------------------------------------------------------*/
#include "ntcore.h"
#include <cassert>
#include <cstdlib>
#include <fstream>
#include "Dispatcher.h"
#include "Storage.h"
namespace nt {
/*
* Table Functions
*/
std::shared_ptr<Value> GetEntryValue(StringRef name) {
return nullptr;
}
bool SetEntryValue(StringRef name, std::shared_ptr<Value> value) {
return false;
}
void SetEntryTypeValue(StringRef name, std::shared_ptr<Value> value) {}
void SetEntryFlags(StringRef name, unsigned int flags) {}
unsigned int GetEntryFlags(StringRef name) {
return 0;
}
void DeleteEntry(StringRef name) {}
void DeleteAllEntries() {}
std::vector<EntryInfo> GetEntryInfo(StringRef prefix, unsigned int types) {
return std::vector<EntryInfo>();
}
void Flush() {}
/*
* Callback Creation Functions
*/
unsigned int AddEntryListener(StringRef prefix,
EntryListenerCallback callback) {
return 0;
}
void RemoveEntryListener(unsigned int entry_listener_uid) {}
unsigned int AddConnectionListener(ConnectionListenerCallback callback) {
return 0;
}
void RemoveConnectionListener(unsigned int conn_listener_uid) {}
/*
* Remote Procedure Call Functions
*/
unsigned int CreateRpc(StringRef name, const RpcDefinition& def,
RpcCallback callback) {
return 0;
}
void DeleteRpc(unsigned int rpc_uid) {}
unsigned int CallRpc(StringRef name,
ArrayRef<std::shared_ptr<Value>> params) {
return 0;
}
std::vector<std::shared_ptr<Value>> GetRpcResult(unsigned int result_uid) {
return std::vector<std::shared_ptr<Value>>();
}
/*
* Client/Server Functions
*/
void SetNetworkIdentity(StringRef name) {
Dispatcher& dispatcher = Dispatcher::GetInstance();
dispatcher.SetIdentity(name);
}
void StartServer(const char *persist_filename, const char *listen_address,
unsigned int port) {
Dispatcher& dispatcher = Dispatcher::GetInstance();
dispatcher.StartServer(listen_address, port);
}
void StopServer() {
Dispatcher& dispatcher = Dispatcher::GetInstance();
dispatcher.Stop();
}
void StartClient(const char *server_name, unsigned int port) {
Dispatcher& dispatcher = Dispatcher::GetInstance();
dispatcher.StartClient(server_name, port);
}
void StopClient() {
Dispatcher& dispatcher = Dispatcher::GetInstance();
dispatcher.Stop();
}
void SetUpdateRate(double interval) {
Dispatcher& dispatcher = Dispatcher::GetInstance();
dispatcher.SetUpdateRate(interval);
}
std::vector<ConnectionInfo> GetConnections() {
return std::vector<ConnectionInfo>();
}
/*
* Persistent Functions
*/
const char* SavePersistent(const char* filename) {
const Storage& storage = Storage::GetInstance();
std::ofstream os(filename);
if (!os) return "could not open file";
storage.SavePersistent(os);
return nullptr;
}
const char* LoadPersistent(
const char* filename,
std::function<void(size_t line, const char* msg)> warn) {
Storage& storage = Storage::GetInstance();
std::ifstream is(filename);
if (!is) return "could not open file";
if (!storage.LoadPersistent(is, warn)) return "error reading file";
return nullptr;
}
} // namespace nt

View File

@@ -9,7 +9,7 @@
#include <cstring>
using namespace ntimpl;
using namespace nt;
void raw_istream::anchor() {}

View File

@@ -10,7 +10,7 @@
#include <cstddef>
namespace ntimpl {
namespace nt {
class raw_istream {
void anchor();
@@ -37,6 +37,6 @@ class raw_mem_istream : public raw_istream {
std::size_t m_left;
};
} // namespace ntimpl
} // namespace nt
#endif // NT_RAW_ISTREAM_H_

View File

@@ -7,7 +7,7 @@
#include "raw_socket_istream.h"
using namespace ntimpl;
using namespace nt;
raw_socket_istream::~raw_socket_istream() {}

View File

@@ -12,7 +12,7 @@
#include "tcpsockets/TCPStream.h"
namespace ntimpl {
namespace nt {
class raw_socket_istream : public raw_istream {
public:
@@ -27,6 +27,6 @@ class raw_socket_istream : public raw_istream {
int m_timeout;
};
} // namespace ntimpl
} // namespace nt
#endif // NT_RAW_SOCKET_ISTREAM_H_

View File

@@ -9,7 +9,7 @@
#include "gtest/gtest.h"
namespace ntimpl {
namespace nt {
struct Base64TestParam {
int plain_len;
@@ -73,4 +73,4 @@ static Base64TestParam standard[] = {
INSTANTIATE_TEST_CASE_P(Base64Standard, Base64Test,
::testing::ValuesIn(standard));
} // namespace ntimpl
} // namespace nt

View File

@@ -5,11 +5,12 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#include "Value.h"
#include "nt_Value.h"
#include "Value_internal.h"
#include "gtest/gtest.h"
namespace ntimpl {
namespace nt {
class ValueTest : public ::testing::Test {};
@@ -361,4 +362,4 @@ TEST_F(ValueTest, StringArrayComparison) {
ASSERT_NE(*v1, *v2);
}
} // namespace ntimpl
} // namespace nt

View File

@@ -15,7 +15,7 @@
#include "llvm/StringRef.h"
namespace ntimpl {
namespace nt {
class WireDecoderTest : public ::testing::Test {
protected:
@@ -599,4 +599,4 @@ TEST_F(WireDecoderTest, ReadString3) {
ASSERT_EQ(nullptr, d.error());
}
} // namespace ntimpl
} // namespace nt

View File

@@ -17,7 +17,7 @@
#define BUFSIZE 1024
namespace ntimpl {
namespace nt {
class WireEncoderTest : public ::testing::Test {
protected:
@@ -496,4 +496,4 @@ TEST_F(WireEncoderTest, WriteString3) {
EXPECT_EQ('x', e.data()[65539]);
}
} // namespace ntimpl
} // namespace nt

View File

@@ -24,7 +24,7 @@
#include "raw_istream.h"
namespace ntimpl {
namespace nt {
TEST(LEB128Test, WriteUleb128) {
#define EXPECT_ULEB128_EQ(EXPECTED, VALUE, PAD) \
@@ -109,4 +109,4 @@ TEST(LEB128Test, SizeUleb128) {
EXPECT_EQ(5u, SizeUleb128(UINT32_MAX));
}
} // namespace ntimpl
} // namespace nt