diff --git a/src/llvm/ArrayRef.h b/include/llvm/ArrayRef.h similarity index 100% rename from src/llvm/ArrayRef.h rename to include/llvm/ArrayRef.h diff --git a/src/llvm/None.h b/include/llvm/None.h similarity index 100% rename from src/llvm/None.h rename to include/llvm/None.h diff --git a/src/llvm/StringRef.h b/include/llvm/StringRef.h similarity index 100% rename from src/llvm/StringRef.h rename to include/llvm/StringRef.h diff --git a/src/Value.h b/include/nt_Value.h similarity index 76% rename from src/Value.h rename to include/nt_Value.h index e2625264d7..074bb8d538 100644 --- a/src/Value.h +++ b/include/nt_Value.h @@ -13,13 +13,17 @@ #include #include -#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 GetBooleanArray() const { + ArrayRef GetBooleanArray() const { assert(m_val.type == NT_BOOLEAN_ARRAY); - return llvm::ArrayRef(m_val.data.arr_boolean.arr, - m_val.data.arr_boolean.size); + return ArrayRef(m_val.data.arr_boolean.arr, + m_val.data.arr_boolean.size); } - llvm::ArrayRef GetDoubleArray() const { + ArrayRef GetDoubleArray() const { assert(m_val.type == NT_DOUBLE_ARRAY); - return llvm::ArrayRef(m_val.data.arr_double.arr, - m_val.data.arr_double.size); + return ArrayRef(m_val.data.arr_double.arr, + m_val.data.arr_double.size); } - llvm::ArrayRef GetStringArray() const { + ArrayRef 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 MakeString(llvm::StringRef value) { + static std::shared_ptr MakeString(StringRef value) { auto val = std::make_shared(NT_STRING, private_init()); val->m_string = value; val->m_val.data.v_string.str = const_cast(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 MakeRaw(llvm::StringRef value) { + static std::shared_ptr MakeRaw(StringRef value) { auto val = std::make_shared(NT_RAW, private_init()); val->m_string = value; val->m_val.data.v_raw.str = const_cast(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 MakeRpc(llvm::StringRef value) { + static std::shared_ptr MakeRpc(StringRef value) { auto val = std::make_shared(NT_RPC, private_init()); val->m_string = value; val->m_val.data.v_raw.str = const_cast(val->m_string.c_str()); @@ -122,10 +127,9 @@ class Value { return val; } - static std::shared_ptr MakeBooleanArray(llvm::ArrayRef value); - static std::shared_ptr MakeDoubleArray(llvm::ArrayRef value); - static std::shared_ptr MakeStringArray( - llvm::ArrayRef value); + static std::shared_ptr MakeBooleanArray(ArrayRef value); + static std::shared_ptr MakeDoubleArray(ArrayRef value); + static std::shared_ptr MakeStringArray(ArrayRef value); // Note: This function moves the values out of the vector. static std::shared_ptr MakeStringArray( @@ -141,18 +145,11 @@ class Value { std::vector m_string_array; }; -void ConvertToC(const Value& in, NT_Value* out); -void ConvertToC(llvm::StringRef in, NT_String* out); -std::shared_ptr 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_ diff --git a/include/ntcore.h b/include/ntcore.h index e86ac00a3e..b4f668b66b 100644 --- a/include/ntcore.h +++ b/include/ntcore.h @@ -8,308 +8,12 @@ #ifndef NTCORE_H_ #define NTCORE_H_ -#include +/* 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_ */ diff --git a/include/ntcore_c.h b/include/ntcore_c.h new file mode 100644 index 0000000000..9f7e755621 --- /dev/null +++ b/include/ntcore_c.h @@ -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 + +#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_ */ diff --git a/include/ntcore_cpp.h b/include/ntcore_cpp.h new file mode 100644 index 0000000000..f74b2f907a --- /dev/null +++ b/include/ntcore_cpp.h @@ -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 +#include +#include +#include +#include + +#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 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 params; + std::vector 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 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); + +/** 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); + +/** 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 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 value)> EntryListenerCallback; + +typedef std::function + 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>( + unsigned int uid, StringRef name, ArrayRef> params)> + RpcCallback; + +unsigned int CreateRpc(StringRef name, const RpcDefinition& def, + RpcCallback callback); +void DeleteRpc(unsigned int rpc_uid); +unsigned int CallRpc(StringRef name, ArrayRef> params); +std::vector> 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 GetConnections(); + +/* + * Persistent Functions + */ +/* return error string, or nullptr if successful */ +const char* SavePersistent(const char* filename); +const char* LoadPersistent( + const char* filename, + std::function warn); + +/* + * Utility Functions + */ + +/* timestamp */ +unsigned long long Now(); + +} // namespace nt + +#endif /* NTCORE_CPP_H_ */ diff --git a/src/Base64.cpp b/src/Base64.cpp index d3b9c0b982..17aa125e3f 100644 --- a/src/Base64.cpp +++ b/src/Base64.cpp @@ -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 diff --git a/src/Base64.h b/src/Base64.h index 963995162c..a86e699be2 100644 --- a/src/Base64.h +++ b/src/Base64.h @@ -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_ diff --git a/src/Dispatcher.cpp b/src/Dispatcher.cpp index c0ebe6abea..901f0f9da9 100644 --- a/src/Dispatcher.cpp +++ b/src/Dispatcher.cpp @@ -10,7 +10,7 @@ #include "tcpsockets/TCPAcceptor.h" #include "tcpsockets/TCPConnector.h" -using namespace ntimpl; +using namespace nt; std::unique_ptr Dispatcher::m_instance; diff --git a/src/Dispatcher.h b/src/Dispatcher.h index fdbf02d0db..bb2fe1f28e 100644 --- a/src/Dispatcher.h +++ b/src/Dispatcher.h @@ -18,7 +18,7 @@ #include "NetworkConnection.h" -namespace ntimpl { +namespace nt { class Dispatcher { public: @@ -64,6 +64,6 @@ class Dispatcher { static std::unique_ptr m_instance; }; -} // namespace ntimpl +} // namespace nt #endif // NT_DISPATCHER_H_ diff --git a/src/Message.cpp b/src/Message.cpp index 509ac9f6a7..9b27f61484 100644 --- a/src/Message.cpp +++ b/src/Message.cpp @@ -12,7 +12,7 @@ static constexpr unsigned long kClearAllMagic = 0xD06CB27Aul; -using namespace ntimpl; +using namespace nt; std::shared_ptr Message::Read(WireDecoder& decoder, GetEntryTypeFunc get_entry_type) { diff --git a/src/Message.h b/src/Message.h index a5d5655915..2c73842fb1 100644 --- a/src/Message.h +++ b/src/Message.h @@ -11,9 +11,9 @@ #include #include -#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_ diff --git a/src/NetworkConnection.cpp b/src/NetworkConnection.cpp index c985373484..e7d7515a6f 100644 --- a/src/NetworkConnection.cpp +++ b/src/NetworkConnection.cpp @@ -12,7 +12,7 @@ #include "WireDecoder.h" #include "WireEncoder.h" -using namespace ntimpl; +using namespace nt; NetworkConnection::NetworkConnection(std::unique_ptr stream, Message::GetEntryTypeFunc get_entry_type) diff --git a/src/NetworkConnection.h b/src/NetworkConnection.h index 3660fe11f1..977b25561e 100644 --- a/src/NetworkConnection.h +++ b/src/NetworkConnection.h @@ -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_ diff --git a/src/SequenceNumber.cpp b/src/SequenceNumber.cpp index 33c497d012..b22bfec89a 100644 --- a/src/SequenceNumber.cpp +++ b/src/SequenceNumber.cpp @@ -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 diff --git a/src/SequenceNumber.h b/src/SequenceNumber.h index 9469752393..2202fefc6d 100644 --- a/src/SequenceNumber.h +++ b/src/SequenceNumber.h @@ -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_ diff --git a/src/Storage.cpp b/src/Storage.cpp index 9f64fbf7a8..f54d747c87 100644 --- a/src/Storage.cpp +++ b/src/Storage.cpp @@ -13,7 +13,7 @@ #include "llvm/StringExtras.h" #include "Base64.h" -using namespace ntimpl; +using namespace nt; std::unique_ptr 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 warn) { std::string line_str; std::size_t line_num = 1; diff --git a/src/Storage.h b/src/Storage.h index 906c4b98c1..9ae436c912 100644 --- a/src/Storage.h +++ b/src/Storage.h @@ -9,15 +9,14 @@ #define NT_STORAGE_H_ #include +#include #include #include -#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 warn); private: Storage(); @@ -66,6 +66,6 @@ class Storage { static std::unique_ptr m_instance; }; -} // namespace ntimpl +} // namespace nt #endif // NT_STORAGE_H_ diff --git a/src/Value.cpp b/src/Value.cpp index e20be7a27e..6ebb8c86f1 100644 --- a/src/Value.cpp +++ b/src/Value.cpp @@ -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::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(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 ntimpl::ConvertFromC(const NT_Value& value) { +std::shared_ptr nt::ConvertFromC(const NT_Value& value) { switch (value.type) { case NT_UNASSIGNED: return nullptr; @@ -172,7 +173,7 @@ std::shared_ptr 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: diff --git a/src/Value_internal.h b/src/Value_internal.h new file mode 100644 index 0000000000..f09748c0ba --- /dev/null +++ b/src/Value_internal.h @@ -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 +#include + +#include "llvm/StringRef.h" +#include "ntcore_c.h" + +namespace nt { + +class Value; + +void ConvertToC(const Value& in, NT_Value* out); +std::shared_ptr 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_ diff --git a/src/WireDecoder.cpp b/src/WireDecoder.cpp index befc0bdf84..c4d4bfcf10 100644 --- a/src/WireDecoder.cpp +++ b/src/WireDecoder.cpp @@ -14,7 +14,7 @@ #include "leb128.h" -using namespace ntimpl; +using namespace nt; static double ReadDouble(const char*& buf) { // Fast but non-portable! diff --git a/src/WireDecoder.h b/src/WireDecoder.h index c88308412f..d09f2e911c 100644 --- a/src/WireDecoder.h +++ b/src/WireDecoder.h @@ -10,12 +10,11 @@ #include -#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_ diff --git a/src/WireEncoder.cpp b/src/WireEncoder.cpp index b8bc67d512..3066a6aaa7 100644 --- a/src/WireEncoder.cpp +++ b/src/WireEncoder.cpp @@ -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) { diff --git a/src/WireEncoder.h b/src/WireEncoder.h index b11b84a760..c74a87178d 100644 --- a/src/WireEncoder.h +++ b/src/WireEncoder.h @@ -12,9 +12,9 @@ #include #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_ diff --git a/src/leb128.cpp b/src/leb128.cpp index 5637bc8a66..d8f3408e9f 100644 --- a/src/leb128.cpp +++ b/src/leb128.cpp @@ -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 diff --git a/src/leb128.h b/src/leb128.h index dc211f1879..8a2863d551 100644 --- a/src/leb128.h +++ b/src/leb128.h @@ -10,7 +10,7 @@ #include -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_ diff --git a/src/ntcore.cpp b/src/ntcore_c.cpp similarity index 67% rename from src/ntcore.cpp rename to src/ntcore_c.cpp index 9cc84e45cb..d6514c7773 100644 --- a/src/ntcore.cpp +++ b/src/ntcore_c.cpp @@ -11,35 +11,45 @@ #include #include -#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> params_v; + params_v.reserve(params_len); + for (size_t i=0; i(std::malloc(results_v.size() * sizeof(NT_Value))); + for (size_t i=0; i +#include +#include + +#include "Dispatcher.h" +#include "Storage.h" + +namespace nt { + +/* + * Table Functions + */ + +std::shared_ptr GetEntryValue(StringRef name) { + return nullptr; +} + +bool SetEntryValue(StringRef name, std::shared_ptr value) { + return false; +} + +void SetEntryTypeValue(StringRef name, std::shared_ptr value) {} + +void SetEntryFlags(StringRef name, unsigned int flags) {} + +unsigned int GetEntryFlags(StringRef name) { + return 0; +} + +void DeleteEntry(StringRef name) {} + +void DeleteAllEntries() {} + +std::vector GetEntryInfo(StringRef prefix, unsigned int types) { + return std::vector(); +} + +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> params) { + return 0; +} + +std::vector> GetRpcResult(unsigned int result_uid) { + return std::vector>(); +} + +/* + * 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 GetConnections() { + return std::vector(); +} + +/* + * 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 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 diff --git a/src/raw_istream.cpp b/src/raw_istream.cpp index 9a841b99a1..c43ef0fb07 100644 --- a/src/raw_istream.cpp +++ b/src/raw_istream.cpp @@ -9,7 +9,7 @@ #include -using namespace ntimpl; +using namespace nt; void raw_istream::anchor() {} diff --git a/src/raw_istream.h b/src/raw_istream.h index 1af0bf7b81..3871d66693 100644 --- a/src/raw_istream.h +++ b/src/raw_istream.h @@ -10,7 +10,7 @@ #include -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_ diff --git a/src/raw_socket_istream.cpp b/src/raw_socket_istream.cpp index 81ca089951..421fefd79f 100644 --- a/src/raw_socket_istream.cpp +++ b/src/raw_socket_istream.cpp @@ -7,7 +7,7 @@ #include "raw_socket_istream.h" -using namespace ntimpl; +using namespace nt; raw_socket_istream::~raw_socket_istream() {} diff --git a/src/raw_socket_istream.h b/src/raw_socket_istream.h index ecc8287796..31b1a4116c 100644 --- a/src/raw_socket_istream.h +++ b/src/raw_socket_istream.h @@ -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_ diff --git a/test/unit/Base64Test.cpp b/test/unit/Base64Test.cpp index 029fc6d691..b83bb137fd 100644 --- a/test/unit/Base64Test.cpp +++ b/test/unit/Base64Test.cpp @@ -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 diff --git a/test/unit/ValueTest.cpp b/test/unit/ValueTest.cpp index b710751093..03ed61695d 100644 --- a/test/unit/ValueTest.cpp +++ b/test/unit/ValueTest.cpp @@ -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 diff --git a/test/unit/WireDecoderTest.cpp b/test/unit/WireDecoderTest.cpp index fd42a2c0b1..4a3ed6bb46 100644 --- a/test/unit/WireDecoderTest.cpp +++ b/test/unit/WireDecoderTest.cpp @@ -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 diff --git a/test/unit/WireEncoderTest.cpp b/test/unit/WireEncoderTest.cpp index dfe5bb8a83..604084cf00 100644 --- a/test/unit/WireEncoderTest.cpp +++ b/test/unit/WireEncoderTest.cpp @@ -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 diff --git a/test/unit/leb128Test.cpp b/test/unit/leb128Test.cpp index c82c5a2f82..80fe271701 100644 --- a/test/unit/leb128Test.cpp +++ b/test/unit/leb128Test.cpp @@ -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