diff --git a/.clang-format b/.clang-format index 38804f78c4..ed38227927 100644 --- a/.clang-format +++ b/.clang-format @@ -1,65 +1,89 @@ --- Language: Cpp -# BasedOnStyle: Google +BasedOnStyle: Google AccessModifierOffset: -1 -AlignAfterOpenBracket: true +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false AlignEscapedNewlinesLeft: true AlignOperands: true AlignTrailingComments: true AllowAllParametersOfDeclarationOnNextLine: true AllowShortBlocksOnASingleLine: false AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All AllowShortIfStatementsOnASingleLine: true AllowShortLoopsOnASingleLine: true -AllowShortFunctionsOnASingleLine: All -AlwaysBreakAfterDefinitionReturnType: false -AlwaysBreakTemplateDeclarations: true +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: true +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach BreakBeforeTernaryOperators: true BreakConstructorInitializersBeforeComma: false -BinPackParameters: true -BinPackArguments: true ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' ConstructorInitializerAllOnOneLineOrOnePerLine: true ConstructorInitializerIndentWidth: 4 -DerivePointerAlignment: true +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false ExperimentalAutoDetectBinPacking: false +ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] +IncludeCategories: + - Regex: '^<.*\.h>' + Priority: 1 + - Regex: '^<.*' + Priority: 2 + - Regex: '.*' + Priority: 3 IndentCaseLabels: true +IndentWidth: 2 IndentWrappedFunctionNames: false -IndentFunctionDeclarationAfterType: false -MaxEmptyLinesToKeep: 1 KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 NamespaceIndentation: None ObjCBlockIndentWidth: 2 ObjCSpaceAfterProperty: false ObjCSpaceBeforeProtocolList: false PenaltyBreakBeforeFirstCallParameter: 1 PenaltyBreakComment: 300 -PenaltyBreakString: 1000 PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 PenaltyExcessCharacter: 1000000 PenaltyReturnTypeOnItsOwnLine: 200 PointerAlignment: Left +ReflowComments: true +SortIncludes: true +SpaceAfterCStyleCast: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false SpacesBeforeTrailingComments: 2 -Cpp11BracedListStyle: true -Standard: Auto -IndentWidth: 2 -TabWidth: 8 -UseTab: Never -BreakBeforeBraces: Attach +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false SpacesInParentheses: false SpacesInSquareBrackets: false -SpacesInAngles: false -SpaceInEmptyParentheses: false -SpacesInCStyleCastParentheses: false -SpaceAfterCStyleCast: false -SpacesInContainerLiterals: true -SpaceBeforeAssignmentOperators: true -ContinuationIndentWidth: 4 -CommentPragmas: '^ IWYU pragma:' -ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] -SpaceBeforeParens: ControlStatements -DisableFormat: false +Standard: Auto +TabWidth: 8 +UseTab: Never ... - diff --git a/include/networktables/NetworkTable.h b/include/networktables/NetworkTable.h index 5e317cd959..fa751c9e19 100644 --- a/include/networktables/NetworkTable.h +++ b/include/networktables/NetworkTable.h @@ -267,14 +267,15 @@ class NetworkTable : public ITable { * @return False if the table key already exists with a different type */ bool PutNumber(llvm::StringRef key, double value) override; - - /** + + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. * @returns False if the table key exists with a different type */ - virtual bool SetDefaultNumber(llvm::StringRef key, double defaultValue) override; + virtual bool SetDefaultNumber(llvm::StringRef key, + double defaultValue) override; /** * Gets the number associated with the given name. @@ -310,14 +311,14 @@ class NetworkTable : public ITable { * @return False if the table key already exists with a different type */ virtual bool PutString(llvm::StringRef key, llvm::StringRef value) override; - - /** + + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. * @returns False if the table key exists with a different type */ - virtual bool SetDefaultString(llvm::StringRef key, + virtual bool SetDefaultString(llvm::StringRef key, llvm::StringRef defaultValue) override; /** @@ -355,14 +356,15 @@ class NetworkTable : public ITable { * @return False if the table key already exists with a different type */ virtual bool PutBoolean(llvm::StringRef key, bool value) override; - - /** + + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. * @returns False if the table key exists with a different type */ - virtual bool SetDefaultBoolean(llvm::StringRef key, bool defaultValue) override; + virtual bool SetDefaultBoolean(llvm::StringRef key, + bool defaultValue) override; /** * Gets the boolean associated with the given name. @@ -404,14 +406,14 @@ class NetworkTable : public ITable { virtual bool PutBooleanArray(llvm::StringRef key, llvm::ArrayRef value) override; - /** + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. * @returns False if the table key exists with a different type - */ - virtual bool SetDefaultBooleanArray(llvm::StringRef key, - llvm::ArrayRef defaultValue) override; + */ + virtual bool SetDefaultBooleanArray( + llvm::StringRef key, llvm::ArrayRef defaultValue) override; /** * Returns the boolean array the key maps to. If the key does not exist or is @@ -440,14 +442,14 @@ class NetworkTable : public ITable { virtual bool PutNumberArray(llvm::StringRef key, llvm::ArrayRef value) override; - /** + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. * @returns False if the table key exists with a different type */ - virtual bool SetDefaultNumberArray(llvm::StringRef key, - llvm::ArrayRef defaultValue) override; + virtual bool SetDefaultNumberArray( + llvm::StringRef key, llvm::ArrayRef defaultValue) override; /** * Returns the number array the key maps to. If the key does not exist or is @@ -472,14 +474,14 @@ class NetworkTable : public ITable { virtual bool PutStringArray(llvm::StringRef key, llvm::ArrayRef value) override; - /** + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. * @returns False if the table key exists with a different type */ - virtual bool SetDefaultStringArray(llvm::StringRef key, - llvm::ArrayRef defaultValue) override; + virtual bool SetDefaultStringArray( + llvm::StringRef key, llvm::ArrayRef defaultValue) override; /** * Returns the string array the key maps to. If the key does not exist or is @@ -503,14 +505,14 @@ class NetworkTable : public ITable { * @return False if the table key already exists with a different type */ virtual bool PutRaw(llvm::StringRef key, llvm::StringRef value) override; - - /** + + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. * @returns False if the table key exists with a different type */ - virtual bool SetDefaultRaw(llvm::StringRef key, + virtual bool SetDefaultRaw(llvm::StringRef key, llvm::StringRef defaultValue) override; /** @@ -535,15 +537,15 @@ class NetworkTable : public ITable { * @return False if the table key already exists with a different type */ bool PutValue(llvm::StringRef key, std::shared_ptr value) override; - - /** + + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. * @returns False if the table key exists with a different type */ - virtual bool SetDefaultValue(llvm::StringRef key, - std::shared_ptr defaultValue) override; + virtual bool SetDefaultValue( + llvm::StringRef key, std::shared_ptr defaultValue) override; /** * Gets the value associated with a key as an object diff --git a/include/nt_Value.h b/include/nt_Value.h index a45e1802ff..a5c1304564 100644 --- a/include/nt_Value.h +++ b/include/nt_Value.h @@ -105,7 +105,8 @@ class Value { return val; } #ifdef _MSC_VER - template >> + template >> #else template ::value>::type> @@ -125,7 +126,8 @@ class Value { return val; } #ifdef _MSC_VER - template >> + template >> #else template ::value>::type> diff --git a/include/ntcore.h b/include/ntcore.h index b4f668b66b..43e74a73d3 100644 --- a/include/ntcore.h +++ b/include/ntcore.h @@ -14,6 +14,6 @@ #ifdef __cplusplus /* C++ API */ #include "ntcore_cpp.h" -#endif /* __cplusplus */ +#endif /* __cplusplus */ -#endif /* NTCORE_H_ */ +#endif /* NTCORE_H_ */ diff --git a/include/ntcore_c.h b/include/ntcore_c.h index ced4bcc69f..d51c31d575 100644 --- a/include/ntcore_c.h +++ b/include/ntcore_c.h @@ -31,9 +31,7 @@ enum NT_Type { }; /** NetworkTables entry flags. */ -enum NT_EntryFlags { - NT_PERSISTENT = 0x01 -}; +enum NT_EntryFlags { NT_PERSISTENT = 0x01 }; /** NetworkTables logging levels. */ enum NT_LogLevel { @@ -70,7 +68,7 @@ struct NT_String { * When returned by the library, this is zero-terminated and allocated with * malloc(). */ - char *str; + char* str; /** Length of the string in bytes. If the string happens to be zero * terminated, this does not include the zero-termination. @@ -88,15 +86,15 @@ struct NT_Value { struct NT_String v_string; struct NT_String v_raw; struct { - int *arr; + int* arr; size_t size; } arr_boolean; struct { - double *arr; + double* arr; size_t size; } arr_double; struct { - struct NT_String *arr; + struct NT_String* arr; size_t size; } arr_string; } data; @@ -143,9 +141,9 @@ struct NT_RpcDefinition { unsigned int version; struct NT_String name; size_t num_params; - NT_RpcParamDef *params; + NT_RpcParamDef* params; size_t num_results; - NT_RpcResultDef *results; + NT_RpcResultDef* results; }; /** NetworkTables RPC Call Data */ @@ -172,9 +170,9 @@ struct NT_RpcCallInfo { * 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); - +void NT_GetEntryValue(const char* name, size_t name_len, + struct NT_Value* value); + /** Set Default Entry Value. * Returns copy of current entry value if it exists. * Otherwise, sets passed in value, and returns set value. @@ -185,8 +183,8 @@ void NT_GetEntryValue(const char *name, size_t name_len, * @param default_value value to be set if name does not exist * @return 0 on error (value not set), 1 on success */ -int NT_SetDefaultEntryValue(const char *name, size_t name_len, - const struct NT_Value *default_value); +int NT_SetDefaultEntryValue(const char* name, size_t name_len, + const struct NT_Value* default_value); /** Set Entry Value. * Sets new entry value. If type of new value differs from the type of the @@ -197,8 +195,8 @@ int NT_SetDefaultEntryValue(const char *name, size_t name_len, * @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); +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 @@ -212,16 +210,16 @@ int NT_SetEntryValue(const char *name, size_t name_len, * @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); +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); +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); +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, @@ -235,7 +233,7 @@ unsigned int NT_GetEntryFlags(const char *name, size_t name_len); * @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); +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 @@ -262,8 +260,8 @@ void NT_DeleteAllEntries(void); * @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); +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. @@ -280,22 +278,23 @@ void NT_Flush(void); * Callback Creation Functions */ -void NT_SetListenerOnStart(void (*on_start)(void *data), void *data); -void NT_SetListenerOnExit(void (*on_exit)(void *data), void *data); +void NT_SetListenerOnStart(void (*on_start)(void* data), void* data); +void NT_SetListenerOnExit(void (*on_exit)(void* data), void* data); -typedef void (*NT_EntryListenerCallback)( - unsigned int uid, void *data, const char *name, size_t name_len, - const struct NT_Value *value, unsigned int flags); +typedef void (*NT_EntryListenerCallback)(unsigned int uid, void* data, + const char* name, size_t name_len, + const struct NT_Value* value, + unsigned int flags); typedef void (*NT_ConnectionListenerCallback)( - unsigned int uid, void *data, int connected, - const struct NT_ConnectionInfo *conn); + 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, +unsigned int NT_AddEntryListener(const char* prefix, size_t prefix_len, + void* data, NT_EntryListenerCallback callback, unsigned int flags); void NT_RemoveEntryListener(unsigned int entry_listener_uid); -unsigned int NT_AddConnectionListener(void *data, +unsigned int NT_AddConnectionListener(void* data, NT_ConnectionListenerCallback callback, int immediate_notify); void NT_RemoveConnectionListener(unsigned int conn_listener_uid); @@ -306,45 +305,45 @@ int NT_NotifierDestroyed(); * Remote Procedure Call Functions */ -void NT_SetRpcServerOnStart(void (*on_start)(void *data), void *data); -void NT_SetRpcServerOnExit(void (*on_exit)(void *data), void *data); +void NT_SetRpcServerOnStart(void (*on_start)(void* data), void* data); +void NT_SetRpcServerOnExit(void (*on_exit)(void* data), void* data); -typedef char *(*NT_RpcCallback)(void *data, const char *name, size_t name_len, - const char *params, size_t params_len, - size_t *results_len, +typedef char* (*NT_RpcCallback)(void* data, const char* name, size_t name_len, + const char* params, size_t params_len, + size_t* results_len, const struct NT_ConnectionInfo* conn_info); -void NT_CreateRpc(const char *name, size_t name_len, const char *def, - size_t def_len, void *data, NT_RpcCallback callback); -void NT_CreatePolledRpc(const char *name, size_t name_len, const char *def, +void NT_CreateRpc(const char* name, size_t name_len, const char* def, + size_t def_len, void* data, NT_RpcCallback callback); +void NT_CreatePolledRpc(const char* name, size_t name_len, const char* def, size_t def_len); int NT_PollRpc(int blocking, struct NT_RpcCallInfo* call_info); -int NT_PollRpcTimeout(int blocking, double time_out, +int NT_PollRpcTimeout(int blocking, double time_out, struct NT_RpcCallInfo* call_info); void NT_PostRpcResponse(unsigned int rpc_id, unsigned int call_uid, - const char *result, size_t result_len); + const char* result, size_t result_len); -unsigned int NT_CallRpc(const char *name, size_t name_len, const char *params, +unsigned int NT_CallRpc(const char* name, size_t name_len, const char* params, size_t params_len); -char *NT_GetRpcResult(int blocking, unsigned int call_uid, size_t *result_len); -char *NT_GetRpcResultTimeout(int blocking, unsigned int call_uid, - double time_out, size_t *result_len); +char* NT_GetRpcResult(int blocking, unsigned int call_uid, size_t* result_len); +char* NT_GetRpcResultTimeout(int blocking, unsigned int call_uid, + double time_out, size_t* result_len); void NT_CancelBlockingRpcResult(unsigned int call_uid); -char *NT_PackRpcDefinition(const struct NT_RpcDefinition *def, - size_t *packed_len); -int NT_UnpackRpcDefinition(const char *packed, size_t packed_len, - struct NT_RpcDefinition *def); -char *NT_PackRpcValues(const struct NT_Value **values, size_t values_len, - size_t *packed_len); -struct NT_Value **NT_UnpackRpcValues(const char *packed, size_t packed_len, - const NT_Type *types, size_t types_len); +char* NT_PackRpcDefinition(const struct NT_RpcDefinition* def, + size_t* packed_len); +int NT_UnpackRpcDefinition(const char* packed, size_t packed_len, + struct NT_RpcDefinition* def); +char* NT_PackRpcValues(const struct NT_Value** values, size_t values_len, + size_t* packed_len); +struct NT_Value** NT_UnpackRpcValues(const char* packed, size_t packed_len, + const NT_Type* types, size_t types_len); /* * Client/Server Functions */ -void NT_SetNetworkIdentity(const char *name, size_t name_len); +void NT_SetNetworkIdentity(const char* name, size_t name_len); /** Start Server * Starts a server using the specified filename, listening address, and port. @@ -355,7 +354,7 @@ void NT_SetNetworkIdentity(const char *name, size_t name_len); * address. (UTF-8 string, null terminated) * @param port port to communicate over. */ -void NT_StartServer(const char *persist_filename, const char *listen_address, +void NT_StartServer(const char* persist_filename, const char* listen_address, unsigned int port); /** Stop Server @@ -370,7 +369,7 @@ void NT_StopServer(void); * @param port port to communicate over * */ -void NT_StartClient(const char *server_name, unsigned int port); +void NT_StartClient(const char* server_name, unsigned int port); /** Starts Client * Starts a client using the specified (server, port) combinations. The @@ -382,8 +381,8 @@ void NT_StartClient(const char *server_name, unsigned int port); * @param ports array of ports to communicate over (one for each server) * */ -void NT_StartClientMulti(size_t count, const char **server_names, - const unsigned int *ports); +void NT_StartClientMulti(size_t count, const char** server_names, + const unsigned int* ports); /** Stop Client * Stops the client if it is running. @@ -417,34 +416,34 @@ void NT_SetUpdateRate(double interval); * It is the caller's responsibility to free the array. The * NT_DisposeConnectionInfoArray function is useful for this purpose. */ -struct NT_ConnectionInfo *NT_GetConnections(size_t *count); +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)); +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); +void NT_DisposeValue(struct NT_Value* value); /* sets type to unassigned and clears rest of struct */ -void NT_InitValue(struct NT_Value *value); +void NT_InitValue(struct NT_Value* value); /* frees string memory */ -void NT_DisposeString(struct NT_String *str); +void NT_DisposeString(struct NT_String* str); /* sets length to zero and pointer to null */ -void NT_InitString(struct NT_String *str); +void NT_InitString(struct NT_String* str); /* Gets the type for the specified key, or unassigned if non existent. */ -enum NT_Type NT_GetType(const char *name, size_t name_len); +enum NT_Type NT_GetType(const char* name, size_t name_len); /** Dispose Connection Info Array * Disposes a connection info array @@ -453,7 +452,7 @@ enum NT_Type NT_GetType(const char *name, size_t name_len); * @param count number of elements in the array * */ -void NT_DisposeConnectionInfoArray(struct NT_ConnectionInfo *arr, size_t count); +void NT_DisposeConnectionInfoArray(struct NT_ConnectionInfo* arr, size_t count); /** Dispose Entry Info Array * Disposes an entry info array @@ -462,7 +461,7 @@ void NT_DisposeConnectionInfoArray(struct NT_ConnectionInfo *arr, size_t count); * @param count number of elements in the array * */ -void NT_DisposeEntryInfoArray(struct NT_EntryInfo *arr, size_t count); +void NT_DisposeEntryInfoArray(struct NT_EntryInfo* arr, size_t count); /** Dispose Rpc Definition * Disposes a Rpc Definition structure @@ -470,7 +469,7 @@ void NT_DisposeEntryInfoArray(struct NT_EntryInfo *arr, size_t count); * @param def pointer to the struct to dispose * */ -void NT_DisposeRpcDefinition(struct NT_RpcDefinition *def); +void NT_DisposeRpcDefinition(struct NT_RpcDefinition* def); /** Dispose Rpc Call Info * Disposes a Rpc Call Info structure @@ -478,14 +477,14 @@ void NT_DisposeRpcDefinition(struct NT_RpcDefinition *def); * @param def pointer to the struct to dispose * */ -void NT_DisposeRpcCallInfo(struct NT_RpcCallInfo *call_info); +void NT_DisposeRpcCallInfo(struct NT_RpcCallInfo* call_info); /* timestamp */ unsigned long long NT_Now(void); /* logging */ -typedef void (*NT_LogFunc)(unsigned int level, const char *file, - unsigned int line, const char *msg); +typedef void (*NT_LogFunc)(unsigned int level, const char* file, + unsigned int line, const char* msg); void NT_SetLogger(NT_LogFunc func, unsigned int min_level); /* @@ -505,7 +504,7 @@ void NT_SetLogger(NT_LogFunc func, unsigned int min_level); * After use, the array should be freed using the NT_FreeCharArray() * function. */ -char *NT_AllocateCharArray(size_t size); +char* NT_AllocateCharArray(size_t size); /** Allocate Boolean Array * Allocates an array of booleans. @@ -518,7 +517,7 @@ char *NT_AllocateCharArray(size_t size); * After use, the array should be freed using the NT_FreeBooleanArray() * function. */ -int *NT_AllocateBooleanArray(size_t size); +int* NT_AllocateBooleanArray(size_t size); /** Allocate Double Array * Allocates an array of doubles. @@ -531,7 +530,7 @@ int *NT_AllocateBooleanArray(size_t size); * After use, the array should be freed using the NT_FreeDoubleArray() * function. */ -double *NT_AllocateDoubleArray(size_t size); +double* NT_AllocateDoubleArray(size_t size); /** Allocate NT_String Array * Allocates an array of NT_Strings. @@ -544,28 +543,28 @@ double *NT_AllocateDoubleArray(size_t size); * After use, the array should be freed using the NT_FreeStringArray() * function. */ -struct NT_String *NT_AllocateStringArray(size_t size); +struct NT_String* NT_AllocateStringArray(size_t size); /** Free Char Array * Frees an array of chars. * * @param v_boolean pointer to the char array to free */ -void NT_FreeCharArray(char *v_char); +void NT_FreeCharArray(char* v_char); /** Free Double Array * Frees an array of doubles. * * @param v_boolean pointer to the double array to free */ -void NT_FreeDoubleArray(double *v_double); +void NT_FreeDoubleArray(double* v_double); /** Free Boolean Array * Frees an array of booleans. * * @param v_boolean pointer to the boolean array to free */ -void NT_FreeBooleanArray(int *v_boolean); +void NT_FreeBooleanArray(int* v_boolean); /** Free String Array * Frees an array of NT_Strings. @@ -577,7 +576,7 @@ void NT_FreeBooleanArray(int *v_boolean); * freed before calling this. This function will free all the strings * individually. */ -void NT_FreeStringArray(struct NT_String *v_string, size_t arr_size); +void NT_FreeStringArray(struct NT_String* v_string, size_t arr_size); /** Get Value Type * Returns the type of an NT_Value struct. @@ -586,7 +585,7 @@ void NT_FreeStringArray(struct NT_String *v_string, size_t arr_size); * @param value The NT_Value struct to get the type from. * @return The type of the value, or unassigned if null. */ -enum NT_Type NT_GetValueType(const struct NT_Value *value); +enum NT_Type NT_GetValueType(const struct NT_Value* value); /** Get Value Boolean * Returns the boolean from the NT_Value. @@ -597,8 +596,8 @@ enum NT_Type NT_GetValueType(const struct NT_Value *value); * @param v_boolean returns the boolean assigned to the name * @return 1 if successful, or 0 if value is null or not a boolean */ -int NT_GetValueBoolean(const struct NT_Value *value, - unsigned long long *last_change, int *v_boolean); +int NT_GetValueBoolean(const struct NT_Value* value, + unsigned long long* last_change, int* v_boolean); /** Get Value Double * Returns the double from the NT_Value. @@ -609,8 +608,8 @@ int NT_GetValueBoolean(const struct NT_Value *value, * @param v_double returns the boolean assigned to the name * @return 1 if successful, or 0 if value is null or not a double */ -int NT_GetValueDouble(const struct NT_Value *value, - unsigned long long *last_change, double *v_double); +int NT_GetValueDouble(const struct NT_Value* value, + unsigned long long* last_change, double* v_double); /** Get Value String * Returns a copy of the string from the NT_Value. @@ -626,8 +625,8 @@ int NT_GetValueDouble(const struct NT_Value *value, * returned string is a copy of the string in the value, and must be freed * separately. */ -char *NT_GetValueString(const struct NT_Value *value, - unsigned long long *last_change, size_t *str_len); +char* NT_GetValueString(const struct NT_Value* value, + unsigned long long* last_change, size_t* str_len); /** Get Value Raw * Returns a copy of the raw value from the NT_Value. @@ -643,8 +642,8 @@ char *NT_GetValueString(const struct NT_Value *value, * returned string is a copy of the string in the value, and must be freed * separately. */ -char *NT_GetValueRaw(const struct NT_Value *value, - unsigned long long *last_change, size_t *raw_len); +char* NT_GetValueRaw(const struct NT_Value* value, + unsigned long long* last_change, size_t* raw_len); /** Get Value Boolean Array * Returns a copy of the boolean array from the NT_Value. @@ -660,8 +659,8 @@ char *NT_GetValueRaw(const struct NT_Value *value, * The returned array is a copy of the array in the value, and must be * freed separately. */ -int *NT_GetValueBooleanArray(const struct NT_Value *value, - unsigned long long *last_change, size_t *arr_size); +int* NT_GetValueBooleanArray(const struct NT_Value* value, + unsigned long long* last_change, size_t* arr_size); /** Get Value Double Array * Returns a copy of the double array from the NT_Value. @@ -677,9 +676,9 @@ int *NT_GetValueBooleanArray(const struct NT_Value *value, * The returned array is a copy of the array in the value, and must be * freed separately. */ -double *NT_GetValueDoubleArray(const struct NT_Value *value, - unsigned long long *last_change, - size_t *arr_size); +double* NT_GetValueDoubleArray(const struct NT_Value* value, + unsigned long long* last_change, + size_t* arr_size); /** Get Value String Array * Returns a copy of the NT_String array from the NT_Value. @@ -693,13 +692,13 @@ double *NT_GetValueDoubleArray(const struct NT_Value *value, * It is the caller's responsibility to free the array once its no longer * needed. The NT_FreeStringArray() function is useful for this purpose. * The returned array is a copy of the array in the value, and must be - * freed seperately. Note that the individual NT_Strings should not be freed, + * freed seperately. Note that the individual NT_Strings should not be freed, * but the entire array should be freed at once. The NT_FreeStringArray() * function will free all the NT_Strings. */ -NT_String *NT_GetValueStringArray(const struct NT_Value *value, - unsigned long long *last_change, - size_t *arr_size); +NT_String* NT_GetValueStringArray(const struct NT_Value* value, + unsigned long long* last_change, + size_t* arr_size); /** Get Entry Boolean * Returns the boolean currently assigned to the entry name. @@ -713,8 +712,8 @@ NT_String *NT_GetValueStringArray(const struct NT_Value *value, * @return 1 if successful, or 0 if value is unassigned or not a * boolean */ -int NT_GetEntryBoolean(const char *name, size_t name_len, - unsigned long long *last_change, int *v_boolean); +int NT_GetEntryBoolean(const char* name, size_t name_len, + unsigned long long* last_change, int* v_boolean); /** Get Entry Double * Returns the double currently assigned to the entry name. @@ -728,8 +727,8 @@ int NT_GetEntryBoolean(const char *name, size_t name_len, * @return 1 if successful, or 0 if value is unassigned or not a * double */ -int NT_GetEntryDouble(const char *name, size_t name_len, - unsigned long long *last_change, double *v_double); +int NT_GetEntryDouble(const char* name, size_t name_len, + unsigned long long* last_change, double* v_double); /** Get Entry String * Returns a copy of the string assigned to the entry name. @@ -745,8 +744,8 @@ int NT_GetEntryDouble(const char *name, size_t name_len, * It is the caller's responsibility to free the string once its no longer * needed. The NT_FreeCharArray() function is useful for this purpose. */ -char *NT_GetEntryString(const char *name, size_t name_len, - unsigned long long *last_change, size_t *str_len); +char* NT_GetEntryString(const char* name, size_t name_len, + unsigned long long* last_change, size_t* str_len); /** Get Entry Raw * Returns a copy of the raw value assigned to the entry name. @@ -762,8 +761,8 @@ char *NT_GetEntryString(const char *name, size_t name_len, * It is the caller's responsibility to free the raw value once its no longer * needed. The NT_FreeCharArray() function is useful for this purpose. */ -char *NT_GetEntryRaw(const char *name, size_t name_len, - unsigned long long *last_change, size_t *raw_len); +char* NT_GetEntryRaw(const char* name, size_t name_len, + unsigned long long* last_change, size_t* raw_len); /** Get Entry Boolean Array * Returns a copy of the boolean array assigned to the entry name. @@ -779,8 +778,8 @@ char *NT_GetEntryRaw(const char *name, size_t name_len, * It is the caller's responsibility to free the array once its no longer * needed. The NT_FreeBooleanArray() function is useful for this purpose. */ -int *NT_GetEntryBooleanArray(const char *name, size_t name_len, - unsigned long long *last_change, size_t *arr_size); +int* NT_GetEntryBooleanArray(const char* name, size_t name_len, + unsigned long long* last_change, size_t* arr_size); /** Get Entry Double Array * Returns a copy of the double array assigned to the entry name. @@ -796,13 +795,13 @@ int *NT_GetEntryBooleanArray(const char *name, size_t name_len, * It is the caller's responsibility to free the array once its no longer * needed. The NT_FreeDoubleArray() function is useful for this purpose. */ -double *NT_GetEntryDoubleArray(const char *name, size_t name_len, - unsigned long long *last_change, - size_t *arr_size); +double* NT_GetEntryDoubleArray(const char* name, size_t name_len, + unsigned long long* last_change, + size_t* arr_size); /** Get Entry String Array - * Returns a copy of the NT_String array assigned to the entry name. - * If the entry name is not currently assigned, or is assigned to a + * Returns a copy of the NT_String array assigned to the entry name. + * If the entry name is not currently assigned, or is assigned to a * different type, returns null. * * @param name entry name (UTF-8 string) @@ -817,9 +816,9 @@ double *NT_GetEntryDoubleArray(const char *name, size_t name_len, * should be freed at once. The NT_FreeStringArray() function will free all the * NT_Strings. */ -NT_String *NT_GetEntryStringArray(const char *name, size_t name_len, - unsigned long long *last_change, - size_t *arr_size); +NT_String* NT_GetEntryStringArray(const char* name, size_t name_len, + unsigned long long* last_change, + size_t* arr_size); /* Set Default Values */ @@ -833,7 +832,7 @@ NT_String *NT_GetEntryStringArray(const char *name, size_t name_len, * @param default_boolean value to be set if name does not exist * @return 0 on error (value not set), 1 on success */ -int NT_SetDefaultEntryBoolean(const char *name, size_t name_len, +int NT_SetDefaultEntryBoolean(const char* name, size_t name_len, int default_boolean); /** Set Default Entry Double. @@ -846,7 +845,7 @@ int NT_SetDefaultEntryBoolean(const char *name, size_t name_len, * @param default_double value to be set if name does not exist * @return 0 on error (value not set), 1 on success */ -int NT_SetDefaultEntryDouble(const char *name, size_t name_len, +int NT_SetDefaultEntryDouble(const char* name, size_t name_len, double default_double); /** Set Default Entry String. @@ -860,8 +859,8 @@ int NT_SetDefaultEntryDouble(const char *name, size_t name_len, * @param default_len length of value * @return 0 on error (value not set), 1 on success */ -int NT_SetDefaultEntryString(const char *name, size_t name_len, - const char *default_value, size_t default_len); +int NT_SetDefaultEntryString(const char* name, size_t name_len, + const char* default_value, size_t default_len); /** Set Default Entry Raw. * Sets the default for the specified key. @@ -874,8 +873,8 @@ int NT_SetDefaultEntryString(const char *name, size_t name_len, * @param default_len length of value array * @return 0 on error (value not set), 1 on success */ -int NT_SetDefaultEntryRaw(const char *name, size_t name_len, - const char *default_value, size_t default_len); +int NT_SetDefaultEntryRaw(const char* name, size_t name_len, + const char* default_value, size_t default_len); /** Set Default Entry Boolean Array. * Sets the default for the specified key. @@ -888,8 +887,8 @@ int NT_SetDefaultEntryRaw(const char *name, size_t name_len, * @param default_size size of value array * @return 0 on error (value not set), 1 on success */ -int NT_SetDefaultEntryBooleanArray(const char *name, size_t name_len, - const int *default_value, +int NT_SetDefaultEntryBooleanArray(const char* name, size_t name_len, + const int* default_value, size_t default_size); /** Set Default Entry Double Array. @@ -903,8 +902,8 @@ int NT_SetDefaultEntryBooleanArray(const char *name, size_t name_len, * @param default_size size of value array * @return 0 on error (value not set), 1 on success */ -int NT_SetDefaultEntryDoubleArray(const char *name, size_t name_len, - const double *default_value, +int NT_SetDefaultEntryDoubleArray(const char* name, size_t name_len, + const double* default_value, size_t default_size); /** Set Default Entry String Array. @@ -918,7 +917,7 @@ int NT_SetDefaultEntryDoubleArray(const char *name, size_t name_len, * @param default_size size of value array * @return 0 on error (value not set), 1 on success */ -int NT_SetDefaultEntryStringArray(const char *name, size_t name_len, +int NT_SetDefaultEntryStringArray(const char* name, size_t name_len, const struct NT_String* default_value, size_t default_size); @@ -934,7 +933,7 @@ int NT_SetDefaultEntryStringArray(const char *name, size_t name_len, * @param force 1 to force the entry to get overwritten, otherwise 0 * @return 0 on error (type mismatch), 1 on success */ -int NT_SetEntryBoolean(const char *name, size_t name_len, int v_boolean, +int NT_SetEntryBoolean(const char* name, size_t name_len, int v_boolean, int force); /** Set Entry Double @@ -947,7 +946,7 @@ int NT_SetEntryBoolean(const char *name, size_t name_len, int v_boolean, * @param force 1 to force the entry to get overwritten, otherwise 0 * @return 0 on error (type mismatch), 1 on success */ -int NT_SetEntryDouble(const char *name, size_t name_len, double v_double, +int NT_SetEntryDouble(const char* name, size_t name_len, double v_double, int force); /** Set Entry String @@ -961,7 +960,7 @@ int NT_SetEntryDouble(const char *name, size_t name_len, double v_double, * @param force 1 to force the entry to get overwritten, otherwise 0 * @return 0 on error (type mismatch), 1 on success */ -int NT_SetEntryString(const char *name, size_t name_len, const char *str, +int NT_SetEntryString(const char* name, size_t name_len, const char* str, size_t str_len, int force); /** Set Entry Raw @@ -975,7 +974,7 @@ int NT_SetEntryString(const char *name, size_t name_len, const char *str, * @param force 1 to force the entry to get overwritten, otherwise 0 * @return 0 on error (type mismatch), 1 on success */ -int NT_SetEntryRaw(const char *name, size_t name_len, const char *raw, +int NT_SetEntryRaw(const char* name, size_t name_len, const char* raw, size_t raw_len, int force); /** Set Entry Boolean Array @@ -989,7 +988,7 @@ int NT_SetEntryRaw(const char *name, size_t name_len, const char *raw, * @param force 1 to force the entry to get overwritten, otherwise 0 * @return 0 on error (type mismatch), 1 on success */ -int NT_SetEntryBooleanArray(const char *name, size_t name_len, const int *arr, +int NT_SetEntryBooleanArray(const char* name, size_t name_len, const int* arr, size_t size, int force); /** Set Entry Double Array @@ -1003,7 +1002,7 @@ int NT_SetEntryBooleanArray(const char *name, size_t name_len, const int *arr, * @param force 1 to force the entry to get overwritten, otherwise 0 * @return 0 on error (type mismatch), 1 on success */ -int NT_SetEntryDoubleArray(const char *name, size_t name_len, const double *arr, +int NT_SetEntryDoubleArray(const char* name, size_t name_len, const double* arr, size_t size, int force); /** Set Entry String Array @@ -1017,8 +1016,8 @@ int NT_SetEntryDoubleArray(const char *name, size_t name_len, const double *arr, * @param force 1 to force the entry to get overwritten, otherwise 0 * @return 0 on error (type mismatch), 1 on success */ -int NT_SetEntryStringArray(const char *name, size_t name_len, - const struct NT_String *arr, size_t size, int force); +int NT_SetEntryStringArray(const char* name, size_t name_len, + const struct NT_String* arr, size_t size, int force); #ifdef __cplusplus } diff --git a/include/ntcore_cpp.h b/include/ntcore_cpp.h index 6bdf4dfd84..7d7a69a565 100644 --- a/include/ntcore_cpp.h +++ b/include/ntcore_cpp.h @@ -100,7 +100,7 @@ std::shared_ptr GetEntryValue(StringRef name); * Returns copy of current entry value if it exists. * Otherwise, sets passed in value, and returns set value. * Note that one of the type options is "unassigned". - * + * * @param name entry name (UTF-8 string) * @param value value to be set if name does not exist * @return False on error (value not set), True on success @@ -195,8 +195,8 @@ void SetListenerOnStart(std::function on_start); void SetListenerOnExit(std::function on_exit); typedef std::function value, - unsigned int flags)> EntryListenerCallback; + std::shared_ptr value, unsigned int flags)> + EntryListenerCallback; typedef std::function @@ -214,7 +214,7 @@ bool NotifierDestroyed(); /* * Remote Procedure Call Functions */ - + #if defined(_MSC_VER) && _MSC_VER < 1900 const double kTimeout_Indefinite = -1; #else @@ -224,7 +224,7 @@ constexpr double kTimeout_Indefinite = -1; void SetRpcServerOnStart(std::function on_start); void SetRpcServerOnExit(std::function on_exit); -typedef std::function RpcCallback; @@ -238,12 +238,12 @@ void PostRpcResponse(unsigned int rpc_id, unsigned int call_uid, unsigned int CallRpc(StringRef name, StringRef params); bool GetRpcResult(bool blocking, unsigned int call_uid, std::string* result); -bool GetRpcResult(bool blocking, unsigned int call_uid, double time_out, +bool GetRpcResult(bool blocking, unsigned int call_uid, double time_out, std::string* result); void CancelBlockingRpcResult(unsigned int call_uid); std::string PackRpcDefinition(const RpcDefinition& def); -bool UnpackRpcDefinition(StringRef packed, RpcDefinition *def); +bool UnpackRpcDefinition(StringRef packed, RpcDefinition* def); std::string PackRpcValues(ArrayRef> values); std::vector> UnpackRpcValues(StringRef packed, ArrayRef types); @@ -280,9 +280,10 @@ unsigned long long Now(); /* logging */ typedef std::function LogFunc; + unsigned int line, const char* msg)> + LogFunc; void SetLogger(LogFunc func, unsigned int min_level); } // namespace nt -#endif /* NTCORE_CPP_H_ */ +#endif /* NTCORE_CPP_H_ */ diff --git a/include/ntcore_test.h b/include/ntcore_test.h index a3e5adea80..08e0fa6550 100644 --- a/include/ntcore_test.h +++ b/include/ntcore_test.h @@ -16,51 +16,44 @@ extern "C" { struct NT_String* NT_GetStringForTesting(const char* string, int* struct_size); // No need for free as one already exists in main library -struct NT_EntryInfo* NT_GetEntryInfoForTesting(const char* name, enum NT_Type type, - unsigned int flags, +struct NT_EntryInfo* NT_GetEntryInfoForTesting(const char* name, + enum NT_Type type, + unsigned int flags, unsigned long long last_change, int* struct_size); void NT_FreeEntryInfoForTesting(struct NT_EntryInfo* info); -struct NT_ConnectionInfo* NT_GetConnectionInfoForTesting(const char* remote_id, - const char* remote_ip, - unsigned int remote_port, - unsigned long long last_update, - unsigned int protocol_version, - int* struct_size); +struct NT_ConnectionInfo* NT_GetConnectionInfoForTesting( + const char* remote_id, const char* remote_ip, unsigned int remote_port, + unsigned long long last_update, unsigned int protocol_version, + int* struct_size); void NT_FreeConnectionInfoForTesting(struct NT_ConnectionInfo* info); -struct NT_Value* NT_GetValueBooleanForTesting( - unsigned long long last_change, int val, - int* struct_size); +struct NT_Value* NT_GetValueBooleanForTesting(unsigned long long last_change, + int val, int* struct_size); -struct NT_Value* NT_GetValueDoubleForTesting( - unsigned long long last_change, double val, - int* struct_size); +struct NT_Value* NT_GetValueDoubleForTesting(unsigned long long last_change, + double val, int* struct_size); -struct NT_Value* NT_GetValueStringForTesting( - unsigned long long last_change, - const char* str, int* struct_size); +struct NT_Value* NT_GetValueStringForTesting(unsigned long long last_change, + const char* str, int* struct_size); -struct NT_Value* NT_GetValueRawForTesting( - unsigned long long last_change, - const char* raw, int raw_len, int* struct_size); +struct NT_Value* NT_GetValueRawForTesting(unsigned long long last_change, + const char* raw, int raw_len, + int* struct_size); struct NT_Value* NT_GetValueBooleanArrayForTesting( - unsigned long long last_change, - const int* arr, size_t array_len, + unsigned long long last_change, const int* arr, size_t array_len, int* struct_size); struct NT_Value* NT_GetValueDoubleArrayForTesting( - unsigned long long last_change, - const double* arr, size_t array_len, + unsigned long long last_change, const double* arr, size_t array_len, int* struct_size); struct NT_Value* NT_GetValueStringArrayForTesting( - unsigned long long last_change, - const struct NT_String* arr, + unsigned long long last_change, const struct NT_String* arr, size_t array_len, int* struct_size); // No need for free as one already exists in the main library @@ -76,21 +69,15 @@ struct NT_RpcResultDef* NT_GetRpcResultsDefForTesting(const char* name, void NT_FreeRpcResultsDefForTesting(struct NT_RpcResultDef* def); -struct NT_RpcDefinition* NT_GetRpcDefinitionForTesting(unsigned int version, - const char* name, - size_t num_params, - const struct NT_RpcParamDef* params, - size_t num_results, - const struct NT_RpcResultDef* results, - int* struct_size); +struct NT_RpcDefinition* NT_GetRpcDefinitionForTesting( + unsigned int version, const char* name, size_t num_params, + const struct NT_RpcParamDef* params, size_t num_results, + const struct NT_RpcResultDef* results, int* struct_size); // No need for free as one already exists in the main library -struct NT_RpcCallInfo* NT_GetRpcCallInfoForTesting(unsigned int rpc_id, - unsigned int call_uid, - const char* name, - const char* params, - size_t params_len, - int* struct_size); +struct NT_RpcCallInfo* NT_GetRpcCallInfoForTesting( + unsigned int rpc_id, unsigned int call_uid, const char* name, + const char* params, size_t params_len, int* struct_size); // No need for free as one already exists in the main library } diff --git a/include/tables/ITable.h b/include/tables/ITable.h index eb9978d422..1f94321205 100644 --- a/include/tables/ITable.h +++ b/include/tables/ITable.h @@ -122,8 +122,8 @@ class ITable { * does not exist */ virtual std::shared_ptr GetValue(llvm::StringRef key) const = 0; - - /** + + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. @@ -150,8 +150,8 @@ class ITable { * @return False if the table key already exists with a different type */ virtual bool PutNumber(llvm::StringRef key, double value) = 0; - - /** + + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. @@ -192,14 +192,14 @@ class ITable { * @return False if the table key already exists with a different type */ virtual bool PutString(llvm::StringRef key, llvm::StringRef value) = 0; - - /** + + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. * @returns False if the table key exists with a different type */ - virtual bool SetDefaultString(llvm::StringRef key, + virtual bool SetDefaultString(llvm::StringRef key, llvm::StringRef defaultValue) = 0; /** @@ -240,8 +240,8 @@ class ITable { * @return False if the table key already exists with a different type */ virtual bool PutBoolean(llvm::StringRef key, bool value) = 0; - - /** + + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. @@ -287,13 +287,13 @@ class ITable { */ virtual bool PutBooleanArray(llvm::StringRef key, llvm::ArrayRef value) = 0; - - /** + + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. * @returns False if the table key exists with a different type - */ + */ virtual bool SetDefaultBooleanArray(llvm::StringRef key, llvm::ArrayRef defaultValue) = 0; @@ -323,8 +323,8 @@ class ITable { */ virtual bool PutNumberArray(llvm::StringRef key, llvm::ArrayRef value) = 0; - - /** + + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. @@ -355,15 +355,15 @@ class ITable { */ virtual bool PutStringArray(llvm::StringRef key, llvm::ArrayRef value) = 0; - - /** + + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. * @returns False if the table key exists with a different type */ - virtual bool SetDefaultStringArray(llvm::StringRef key, - llvm::ArrayRef defaultValue) = 0; + virtual bool SetDefaultStringArray( + llvm::StringRef key, llvm::ArrayRef defaultValue) = 0; /** * Returns the string array the key maps to. If the key does not exist or is @@ -386,14 +386,14 @@ class ITable { * @return False if the table key already exists with a different type */ virtual bool PutRaw(llvm::StringRef key, llvm::StringRef value) = 0; - - /** + + /** * Gets the current value in the table, setting it if it does not exist. * @param key the key * @param defaultValue the default value to set if key doesn't exist. * @returns False if the table key exists with a different type */ - virtual bool SetDefaultRaw(llvm::StringRef key, + virtual bool SetDefaultRaw(llvm::StringRef key, llvm::StringRef defaultValue) = 0; /** diff --git a/include/tables/ITableListener.h b/include/tables/ITableListener.h index 6b728d28a7..76e071588a 100644 --- a/include/tables/ITableListener.h +++ b/include/tables/ITableListener.h @@ -26,10 +26,8 @@ class ITableListener { * @param isNew true if the key did not previously exist in the table, * otherwise it is false */ - virtual void ValueChanged(ITable* source, - llvm::StringRef key, - std::shared_ptr value, - bool isNew) = 0; + virtual void ValueChanged(ITable* source, llvm::StringRef key, + std::shared_ptr value, bool isNew) = 0; /** * Extended version of ValueChanged. Called when a key-value pair is @@ -41,8 +39,7 @@ class ITableListener { * @param flags update flags; for example, NT_NOTIFY_NEW if the key did not * previously exist in the table */ - virtual void ValueChangedEx(ITable* source, - llvm::StringRef key, + virtual void ValueChangedEx(ITable* source, llvm::StringRef key, std::shared_ptr value, unsigned int flags); }; diff --git a/include/tables/TableKeyNotDefinedException.h b/include/tables/TableKeyNotDefinedException.h index 8f318ba72c..d65bcf2fce 100644 --- a/include/tables/TableKeyNotDefinedException.h +++ b/include/tables/TableKeyNotDefinedException.h @@ -12,9 +12,9 @@ #include "llvm/StringRef.h" #if defined(_MSC_VER) - #define NT_NOEXCEPT throw() +#define NT_NOEXCEPT throw() #else - #define NT_NOEXCEPT noexcept +#define NT_NOEXCEPT noexcept #endif /** diff --git a/src/Dispatcher.cpp b/src/Dispatcher.cpp index 9b6f269456..45642f52a3 100644 --- a/src/Dispatcher.cpp +++ b/src/Dispatcher.cpp @@ -169,8 +169,7 @@ void DispatcherBase::Flush() { { std::lock_guard lock(m_flush_mutex); // don't allow flushes more often than every 10 ms - if ((now - m_last_flush) < std::chrono::milliseconds(10)) - return; + if ((now - m_last_flush) < std::chrono::milliseconds(10)) return; m_last_flush = now; m_do_flush = true; } @@ -207,8 +206,7 @@ void DispatcherBase::DispatchThreadMain() { while (m_active) { // handle loop taking too long auto start = std::chrono::steady_clock::now(); - if (start > timeout_time) - timeout_time = start; + if (start > timeout_time) timeout_time = start; // wait for periodic or when flushed timeout_time += std::chrono::milliseconds(m_update_rate); @@ -265,7 +263,8 @@ void DispatcherBase::QueueOutgoing(std::shared_ptr msg, if (only && conn.get() != only) continue; auto state = conn->state(); if (state != NetworkConnection::kSynchronized && - state != NetworkConnection::kActive) continue; + state != NetworkConnection::kActive) + continue; conn->QueueOutgoing(msg); } } @@ -356,8 +355,7 @@ void DispatcherBase::ClientThreadMain() { } bool DispatcherBase::ClientHandshake( - NetworkConnection& conn, - std::function()> get_msg, + NetworkConnection& conn, std::function()> get_msg, std::function>)> send_msgs) { // get identity std::string self_id; @@ -411,7 +409,9 @@ bool DispatcherBase::ClientHandshake( } if (!msg->Is(Message::kEntryAssign)) { // unexpected message - DEBUG("client: received message (" << msg->type() << ") other than entry assignment during initial handshake"); + DEBUG("client: received message (" + << msg->type() + << ") other than entry assignment during initial handshake"); return false; } incoming.emplace_back(std::move(msg)); @@ -435,8 +435,7 @@ bool DispatcherBase::ClientHandshake( } bool DispatcherBase::ServerHandshake( - NetworkConnection& conn, - std::function()> get_msg, + NetworkConnection& conn, std::function()> get_msg, std::function>)> send_msgs) { // Wait for the client to send us a hello. auto msg = get_msg(); diff --git a/src/Dispatcher.h b/src/Dispatcher.h index 57e7ecfea3..87b97e29f2 100644 --- a/src/Dispatcher.h +++ b/src/Dispatcher.h @@ -33,6 +33,7 @@ namespace nt { class DispatcherBase { friend class DispatcherTest; + public: typedef std::function()> Connector; @@ -91,7 +92,7 @@ class DispatcherBase { std::vector> m_connections; std::string m_identity; - std::atomic_bool m_active; // set to false to terminate threads + std::atomic_bool m_active; // set to false to terminate threads std::atomic_uint m_update_rate; // periodic dispatch update rate, in ms // Condition variable for forced dispatch wakeup (flush) @@ -108,6 +109,7 @@ class DispatcherBase { class Dispatcher : public DispatcherBase { friend class DispatcherTest; + public: static Dispatcher& GetInstance() { ATOMIC_STATIC(Dispatcher, instance); @@ -127,7 +129,6 @@ class Dispatcher : public DispatcherBase { ATOMIC_STATIC_DECL(Dispatcher) }; - } // namespace nt #endif // NT_DISPATCHER_H_ diff --git a/src/Log.cpp b/src/Log.cpp index 826102cced..c51ee96950 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -9,17 +9,17 @@ #include #ifdef _WIN32 - #include +#include #else - #include +#include #endif #ifdef __APPLE__ - #include +#include #endif #ifdef __ANDROID__ - #include +#include #endif using namespace nt; diff --git a/src/Log.h b/src/Log.h index 05cf27cea6..d56ba04069 100644 --- a/src/Log.h +++ b/src/Log.h @@ -40,6 +40,6 @@ class Logger : public wpi::Logger { #define DEBUG3(x) WPI_DEBUG3(nt::Logger::GetInstance(), x) #define DEBUG4(x) WPI_DEBUG4(nt::Logger::GetInstance(), x) -} // namespace nt +} // namespace nt #endif // NT_LOG_H_ diff --git a/src/Message.cpp b/src/Message.cpp index 3c62820d6c..411c44ae15 100644 --- a/src/Message.cpp +++ b/src/Message.cpp @@ -58,8 +58,8 @@ std::shared_ptr Message::Read(WireDecoder& decoder, case kEntryAssign: { if (!decoder.ReadString(&msg->m_str)) return nullptr; NT_Type type; - if (!decoder.ReadType(&type)) return nullptr; // name - if (!decoder.Read16(&msg->m_id)) return nullptr; // id + if (!decoder.ReadType(&type)) return nullptr; // name + if (!decoder.Read16(&msg->m_id)) return nullptr; // id if (!decoder.Read16(&msg->m_seq_num_uid)) return nullptr; // seq num if (decoder.proto_rev() >= 0x0300u) { if (!decoder.Read8(&msg->m_flags)) return nullptr; // flags @@ -69,7 +69,7 @@ std::shared_ptr Message::Read(WireDecoder& decoder, break; } case kEntryUpdate: { - if (!decoder.Read16(&msg->m_id)) return nullptr; // id + if (!decoder.Read16(&msg->m_id)) return nullptr; // id if (!decoder.Read16(&msg->m_seq_num_uid)) return nullptr; // seq num NT_Type type; if (decoder.proto_rev() >= 0x0300u) { diff --git a/src/NetworkConnection.cpp b/src/NetworkConnection.cpp index 794f5fe558..a0b06c6da6 100644 --- a/src/NetworkConnection.cpp +++ b/src/NetworkConnection.cpp @@ -151,8 +151,7 @@ void NetworkConnection::ReadThreadMain() { set_state(kActive); while (m_active) { - if (!m_stream) - break; + if (!m_stream) break; decoder.set_proto_rev(m_proto_rev); decoder.Reset(); auto msg = Message::Read(decoder, m_get_entry_type); diff --git a/src/NetworkConnection.h b/src/NetworkConnection.h index 14d282d91e..6f79fa3f14 100644 --- a/src/NetworkConnection.h +++ b/src/NetworkConnection.h @@ -35,13 +35,13 @@ class NetworkConnection { std::function>)> send_msgs)> HandshakeFunc; typedef std::function msg, - NetworkConnection* conn)> ProcessIncomingFunc; + NetworkConnection* conn)> + ProcessIncomingFunc; typedef std::vector> Outgoing; typedef wpi::ConcurrentQueue OutgoingQueue; NetworkConnection(std::unique_ptr stream, - Notifier& notifier, - HandshakeFunc handshake, + Notifier& notifier, HandshakeFunc handshake, Message::GetEntryTypeFunc get_entry_type); ~NetworkConnection(); diff --git a/src/Notifier.cpp b/src/Notifier.cpp index 874ba336ed..b19d979a68 100644 --- a/src/Notifier.cpp +++ b/src/Notifier.cpp @@ -85,10 +85,7 @@ class Notifier::Thread : public wpi::SafeThread { struct EntryNotification { EntryNotification(llvm::StringRef name_, std::shared_ptr value_, unsigned int flags_, EntryListenerCallback only_) - : name(name_), - value(value_), - flags(flags_), - only(only_) {} + : name(name_), value(value_), flags(flags_), only(only_) {} std::string name; std::shared_ptr value; @@ -154,7 +151,7 @@ void Notifier::Thread::Main() { } // Use index because iterator might get invalidated. - for (std::size_t i=0; im_conn_listeners.erase(conn_listener_uid); } -void Notifier::NotifyConnection(bool connected, - const ConnectionInfo& conn_info, +void Notifier::NotifyConnection(bool connected, const ConnectionInfo& conn_info, ConnectionListenerCallback only) { auto thr = m_owner.GetThread(); if (!thr) return; diff --git a/src/Notifier.h b/src/Notifier.h index e1a382a5b0..49c9ec9d92 100644 --- a/src/Notifier.h +++ b/src/Notifier.h @@ -18,6 +18,7 @@ namespace nt { class Notifier { friend class NotifierTest; + public: static Notifier& GetInstance() { ATOMIC_STATIC(Notifier, instance); diff --git a/src/RpcServer.cpp b/src/RpcServer.cpp index 7857bbec86..df3cb4f793 100644 --- a/src/RpcServer.cpp +++ b/src/RpcServer.cpp @@ -28,9 +28,7 @@ class RpcServer::Thread : public wpi::SafeThread { std::function m_on_exit; }; -RpcServer::RpcServer() { - m_terminating = false; -} +RpcServer::RpcServer() { m_terminating = false; } RpcServer::~RpcServer() { Logger::GetInstance().SetLogger(nullptr); @@ -47,12 +45,12 @@ void RpcServer::Stop() { m_owner.Stop(); } void RpcServer::ProcessRpc(StringRef name, std::shared_ptr msg, RpcCallback func, unsigned int conn_id, - SendMsgFunc send_response, + SendMsgFunc send_response, const ConnectionInfo& conn_info) { if (func) { auto thr = m_owner.GetThread(); if (!thr) return; - thr->m_call_queue.emplace(name, msg, func, conn_id, send_response, + thr->m_call_queue.emplace(name, msg, func, conn_id, send_response, conn_info); thr->m_cond.notify_one(); } else { @@ -66,15 +64,16 @@ bool RpcServer::PollRpc(bool blocking, RpcCallInfo* call_info) { return PollRpc(blocking, kTimeout_Indefinite, call_info); } -bool RpcServer::PollRpc(bool blocking, double time_out, RpcCallInfo* call_info) { +bool RpcServer::PollRpc(bool blocking, double time_out, + RpcCallInfo* call_info) { std::unique_lock lock(m_mutex); #if defined(_MSC_VER) && _MSC_VER < 1900 - auto timeout_time = std::chrono::steady_clock::now() + - std::chrono::duration(static_cast - (time_out * 1e9)); + auto timeout_time = std::chrono::steady_clock::now() + + std::chrono::duration( + static_cast(time_out * 1e9)); #else - auto timeout_time = std::chrono::steady_clock::now() + - std::chrono::duration(time_out); + auto timeout_time = std::chrono::steady_clock::now() + + std::chrono::duration(time_out); #endif while (m_poll_queue.empty()) { if (!blocking || m_terminating) return false; @@ -95,7 +94,7 @@ bool RpcServer::PollRpc(bool blocking, double time_out, RpcCallInfo* call_info) if (item.conn_id != 0xffff) call_uid = (item.conn_id << 16) | item.msg->seq_num_uid(); else - call_uid = item.msg->seq_num_uid(); + call_uid = item.msg->seq_num_uid(); call_info->rpc_id = item.msg->id(); call_info->call_uid = call_uid; call_info->name = std::move(item.name); diff --git a/src/RpcServer.h b/src/RpcServer.h index 04a0d333fe..326ad18a6c 100644 --- a/src/RpcServer.h +++ b/src/RpcServer.h @@ -24,6 +24,7 @@ namespace nt { class RpcServer { friend class RpcServerTest; + public: static RpcServer& GetInstance() { ATOMIC_STATIC(RpcServer, instance); @@ -41,8 +42,7 @@ class RpcServer { void ProcessRpc(StringRef name, std::shared_ptr msg, RpcCallback func, unsigned int conn_id, - SendMsgFunc send_response, - const ConnectionInfo& conn_info); + SendMsgFunc send_response, const ConnectionInfo& conn_info); bool PollRpc(bool blocking, RpcCallInfo* call_info); bool PollRpc(bool blocking, double time_out, RpcCallInfo* call_info); diff --git a/src/Storage.cpp b/src/Storage.cpp index 66dc21dc03..4129fdd86b 100644 --- a/src/Storage.cpp +++ b/src/Storage.cpp @@ -41,9 +41,7 @@ void Storage::SetOutgoing(QueueOutgoingFunc queue_outgoing, bool server) { m_server = server; } -void Storage::ClearOutgoing() { - m_queue_outgoing = nullptr; -} +void Storage::ClearOutgoing() { m_queue_outgoing = nullptr; } NT_Type Storage::GetEntryType(unsigned int id) const { std::lock_guard lock(m_mutex); @@ -99,8 +97,8 @@ void Storage::ProcessIncoming(std::shared_ptr msg, // send the assignment to everyone (including the originator) if (m_queue_outgoing) { auto queue_outgoing = m_queue_outgoing; - auto outmsg = Message::EntryAssign( - name, id, entry->seq_num.value(), msg->value(), msg->flags()); + auto outmsg = Message::EntryAssign(name, id, entry->seq_num.value(), + msg->value(), msg->flags()); lock.unlock(); queue_outgoing(outmsg, nullptr, nullptr); } @@ -121,7 +119,7 @@ void Storage::ProcessIncoming(std::shared_ptr msg, DEBUG("client: received entry assignment request?"); return; } - if (id >= m_idmap.size()) m_idmap.resize(id+1); + if (id >= m_idmap.size()) m_idmap.resize(id + 1); entry = m_idmap[id]; if (!entry) { // create local @@ -186,8 +184,7 @@ void Storage::ProcessIncoming(std::shared_ptr msg, // update persistent dirty flag if persistent flag changed if ((entry->flags & NT_PERSISTENT) != (msg->flags() & NT_PERSISTENT)) m_persistent_dirty = true; - if (entry->flags != msg->flags()) - notify_flags |= NT_NOTIFY_FLAGS; + if (entry->flags != msg->flags()) notify_flags |= NT_NOTIFY_FLAGS; entry->flags = msg->flags(); } @@ -206,9 +203,8 @@ void Storage::ProcessIncoming(std::shared_ptr msg, // be any other connections, so don't bother) if (m_server && m_queue_outgoing) { auto queue_outgoing = m_queue_outgoing; - auto outmsg = - Message::EntryAssign(entry->name, id, msg->seq_num_uid(), - msg->value(), entry->flags); + auto outmsg = Message::EntryAssign(entry->name, id, msg->seq_num_uid(), + msg->value(), entry->flags); lock.unlock(); queue_outgoing(outmsg, nullptr, conn); } @@ -347,7 +343,7 @@ void Storage::ProcessIncoming(std::shared_ptr msg, DEBUG("received RPC call to non-RPC entry"); return; } - ConnectionInfo conn_info; + ConnectionInfo conn_info; auto c = conn_weak.lock(); if (c) { conn_info = c->info(); @@ -357,12 +353,14 @@ void Storage::ProcessIncoming(std::shared_ptr msg, conn_info.remote_port = 0; conn_info.last_update = 0; conn_info.protocol_version = 0; - } + } m_rpc_server.ProcessRpc(entry->name, msg, entry->rpc_callback, - conn->uid(), [=](std::shared_ptr msg) { + conn->uid(), + [=](std::shared_ptr msg) { auto c = conn_weak.lock(); if (c) c->QueueOutgoing(msg); - }, conn_info); + }, + conn_info); break; } case Message::kRpcResponse: { @@ -453,7 +451,7 @@ void Storage::ApplyInitialAssignments( // set id and save to idmap entry->id = id; - if (id >= m_idmap.size()) m_idmap.resize(id+1); + if (id >= m_idmap.size()) m_idmap.resize(id + 1); m_idmap[id] = entry.get(); } @@ -476,19 +474,21 @@ std::shared_ptr Storage::GetEntryValue(StringRef name) const { return i == m_entries.end() ? nullptr : i->getValue()->value; } -bool Storage::SetDefaultEntryValue(StringRef name, +bool Storage::SetDefaultEntryValue(StringRef name, std::shared_ptr value) { - if (!value) return false; // can't compare to a null value - if (name.empty()) return false; // can't compare empty name + if (!value) return false; // can't compare to a null value + if (name.empty()) return false; // can't compare empty name std::unique_lock lock(m_mutex); auto& new_entry = m_entries[name]; - if (new_entry) { // entry already exists - auto old_value = new_entry->value; - // if types match return true - if (old_value && old_value->type() == value->type()) return true; - else return false; // entry exists but doesn't match type + if (new_entry) { // entry already exists + auto old_value = new_entry->value; + // if types match return true + if (old_value && old_value->type() == value->type()) + return true; + else + return false; // entry exists but doesn't match type } - + // if we've gotten here, entry does not exist, and we can write it. new_entry.reset(new Entry(name)); Entry* entry = new_entry.get(); @@ -560,8 +560,7 @@ bool Storage::SetEntryValue(StringRef name, std::shared_ptr value) { ++entry->seq_num; // don't send an update if we don't have an assigned id yet if (entry->id != 0xffff) { - auto msg = - Message::EntryUpdate(entry->id, entry->seq_num.value(), value); + auto msg = Message::EntryUpdate(entry->id, entry->seq_num.value(), value); lock.unlock(); queue_outgoing(msg, nullptr, nullptr); } @@ -611,8 +610,7 @@ void Storage::SetEntryTypeValue(StringRef name, std::shared_ptr value) { ++entry->seq_num; // don't send an update if we don't have an assigned id yet if (entry->id != 0xffff) { - auto msg = - Message::EntryUpdate(entry->id, entry->seq_num.value(), value); + auto msg = Message::EntryUpdate(entry->id, entry->seq_num.value(), value); lock.unlock(); queue_outgoing(msg, nullptr, nullptr); } @@ -664,7 +662,7 @@ void Storage::DeleteEntry(StringRef name) { if (entry->IsPersistent()) m_persistent_dirty = true; m_entries.erase(i); // erase from map - if (id < m_idmap.size()) m_idmap[id] = nullptr; + if (id < m_idmap.size()) m_idmap[id] = nullptr; if (!entry->value) return; @@ -1011,12 +1009,12 @@ static void UnescapeString(llvm::StringRef source, std::string* dest) { dest->push_back('\n'); break; case 'x': { - if (!isxdigit(*(s+1))) { + if (!isxdigit(*(s + 1))) { dest->push_back('x'); // treat it like a unknown escape break; } int ch = fromxdigit(*++s); - if (isxdigit(*(s+1))) { + if (isxdigit(*(s + 1))) { ch <<= 4; ch |= fromxdigit(*++s); } @@ -1048,8 +1046,7 @@ bool Storage::LoadPersistent( // ignore blank lines and lines that start with ; or # (comments) while (std::getline(is, line_str)) { llvm::StringRef line = llvm::StringRef(line_str).trim(); - if (!line.empty() && line.front() != ';' && line.front() != '#') - break; + if (!line.empty() && line.front() != ';' && line.front() != '#') break; } // header @@ -1063,23 +1060,29 @@ bool Storage::LoadPersistent( ++line_num; // ignore blank lines and lines that start with ; or # (comments) - if (line.empty() || line.front() == ';' || line.front() == '#') - continue; + if (line.empty() || line.front() == ';' || line.front() == '#') continue; // type llvm::StringRef type_tok; std::tie(type_tok, line) = line.split(' '); NT_Type type = NT_UNASSIGNED; - if (type_tok == "boolean") type = NT_BOOLEAN; - else if (type_tok == "double") type = NT_DOUBLE; - else if (type_tok == "string") type = NT_STRING; - else if (type_tok == "raw") type = NT_RAW; + if (type_tok == "boolean") + type = NT_BOOLEAN; + else if (type_tok == "double") + type = NT_DOUBLE; + else if (type_tok == "string") + type = NT_STRING; + else if (type_tok == "raw") + type = NT_RAW; else if (type_tok == "array") { llvm::StringRef array_tok; std::tie(array_tok, line) = line.split(' '); - if (array_tok == "boolean") type = NT_BOOLEAN_ARRAY; - else if (array_tok == "double") type = NT_DOUBLE_ARRAY; - else if (array_tok == "string") type = NT_STRING_ARRAY; + if (array_tok == "boolean") + type = NT_BOOLEAN_ARRAY; + else if (array_tok == "double") + type = NT_DOUBLE_ARRAY; + else if (array_tok == "string") + type = NT_STRING_ARRAY; } if (type == NT_UNASSIGNED) { if (warn) warn(line_num, "unrecognized type"); @@ -1230,8 +1233,8 @@ bool Storage::LoadPersistent( } if (!name.empty() && value) entries.push_back(std::make_pair(std::move(name), std::move(value))); -next_line: - ; + next_line: + continue; } // copy values into storage as quickly as possible so lock isn't held @@ -1272,8 +1275,8 @@ next_line: // put on update queue if (!old_value || old_value->type() != i.second->type()) msgs.emplace_back(Message::EntryAssign(i.first, entry->id, - entry->seq_num.value(), - i.second, entry->flags)); + entry->seq_num.value(), i.second, + entry->flags)); else if (entry->id != 0xffff) { // don't send an update if we don't have an assigned id yet if (*old_value != *i.second) @@ -1306,7 +1309,7 @@ const char* Storage::LoadPersistent( void Storage::CreateRpc(StringRef name, StringRef def, RpcCallback callback) { if (name.empty() || def.empty() || !callback) return; std::unique_lock lock(m_mutex); - if (!m_server) return; // only server can create RPCs + if (!m_server) return; // only server can create RPCs auto& new_entry = m_entries[name]; if (!new_entry) new_entry.reset(new Entry(name)); @@ -1350,7 +1353,7 @@ void Storage::CreateRpc(StringRef name, StringRef def, RpcCallback callback) { void Storage::CreatePolledRpc(StringRef name, StringRef def) { if (name.empty() || def.empty()) return; std::unique_lock lock(m_mutex); - if (!m_server) return; // only server can create RPCs + if (!m_server) return; // only server can create RPCs auto& new_entry = m_entries[name]; if (!new_entry) new_entry.reset(new Entry(name)); @@ -1411,12 +1414,14 @@ unsigned int Storage::CallRpc(StringRef name, StringRef params) { conn_info.last_update = wpi::Now(); conn_info.protocol_version = 0x0300; m_rpc_server.ProcessRpc( - name, msg, rpc_callback, 0xffffU, [this](std::shared_ptr msg) { + name, msg, rpc_callback, 0xffffU, + [this](std::shared_ptr msg) { std::lock_guard lock(m_mutex); m_rpc_results.insert(std::make_pair( std::make_pair(msg->id(), msg->seq_num_uid()), msg->str())); m_rpc_results_cond.notify_all(); - }, conn_info); + }, + conn_info); } else { auto queue_outgoing = m_queue_outgoing; lock.unlock(); @@ -1425,23 +1430,23 @@ unsigned int Storage::CallRpc(StringRef name, StringRef params) { return combined_uid; } -bool Storage::GetRpcResult(bool blocking, unsigned int call_uid, +bool Storage::GetRpcResult(bool blocking, unsigned int call_uid, std::string* result) { return GetRpcResult(blocking, call_uid, -1, result); } -bool Storage::GetRpcResult(bool blocking, unsigned int call_uid, double time_out, - std::string* result) { +bool Storage::GetRpcResult(bool blocking, unsigned int call_uid, + double time_out, std::string* result) { std::unique_lock lock(m_mutex); // only allow one blocking call per rpc call uid if (!m_rpc_blocking_calls.insert(call_uid).second) return false; #if defined(_MSC_VER) && _MSC_VER < 1900 - auto timeout_time = std::chrono::steady_clock::now() + - std::chrono::duration(static_cast - (time_out * 1e9)); + auto timeout_time = std::chrono::steady_clock::now() + + std::chrono::duration( + static_cast(time_out * 1e9)); #else - auto timeout_time = std::chrono::steady_clock::now() + - std::chrono::duration(time_out); + auto timeout_time = std::chrono::steady_clock::now() + + std::chrono::duration(time_out); #endif for (;;) { auto i = diff --git a/src/Storage.h b/src/Storage.h index 5acab47429..9e7d23f2d3 100644 --- a/src/Storage.h +++ b/src/Storage.h @@ -34,6 +34,7 @@ class StorageTest; class Storage { friend class StorageTest; + public: static Storage& GetInstance() { ATOMIC_STATIC(Storage, instance); @@ -47,7 +48,8 @@ class Storage { // Dispatcher::QueueOutgoing. typedef std::function msg, NetworkConnection* only, - NetworkConnection* except)> QueueOutgoingFunc; + NetworkConnection* except)> + QueueOutgoingFunc; void SetOutgoing(QueueOutgoingFunc queue_outgoing, bool server); void ClearOutgoing(); @@ -100,7 +102,7 @@ class Storage { unsigned int CallRpc(StringRef name, StringRef params); bool GetRpcResult(bool blocking, unsigned int call_uid, std::string* result); - bool GetRpcResult(bool blocking, unsigned int call_uid, double time_out, + bool GetRpcResult(bool blocking, unsigned int call_uid, double time_out, std::string* result); void CancelBlockingRpcResult(unsigned int call_uid); diff --git a/src/Value.cpp b/src/Value.cpp index a16f1726bb..ae9d779e0a 100644 --- a/src/Value.cpp +++ b/src/Value.cpp @@ -59,7 +59,7 @@ std::shared_ptr Value::MakeStringArray( // point NT_Value to the contents in the vector. val->m_val.data.arr_string.arr = new NT_String[value.size()]; val->m_val.data.arr_string.size = val->m_string_array.size(); - for (std::size_t i=0; im_val.data.arr_string.arr[i].str = const_cast(value[i].c_str()); val->m_val.data.arr_string.arr[i].len = value[i].size(); } @@ -74,7 +74,7 @@ std::shared_ptr Value::MakeStringArray( // point NT_Value to the contents in the vector. val->m_val.data.arr_string.arr = new NT_String[val->m_string_array.size()]; val->m_val.data.arr_string.size = val->m_string_array.size(); - for (std::size_t i=0; im_string_array.size(); ++i) { + for (std::size_t i = 0; i < val->m_string_array.size(); ++i) { val->m_val.data.arr_string.arr[i].str = const_cast(val->m_string_array[i].c_str()); val->m_val.data.arr_string.arr[i].len = val->m_string_array[i].size(); @@ -121,7 +121,7 @@ void nt::ConvertToC(const Value& in, NT_Value* out) { case NT_STRING_ARRAY: { auto v = in.GetStringArray(); out->data.arr_string.arr = - static_cast(std::malloc(v.size()*sizeof(NT_String))); + static_cast(std::malloc(v.size() * sizeof(NT_String))); for (size_t i = 0; i < v.size(); ++i) ConvertToC(v[i], &out->data.arr_string.arr[i]); out->data.arr_string.size = v.size(); @@ -136,7 +136,7 @@ void nt::ConvertToC(const Value& in, NT_Value* out) { void nt::ConvertToC(llvm::StringRef in, NT_String* out) { out->len = in.size(); - out->str = static_cast(std::malloc(in.size()+1)); + out->str = static_cast(std::malloc(in.size() + 1)); std::memcpy(out->str, in.data(), in.size()); out->str[in.size()] = '\0'; } @@ -164,7 +164,7 @@ std::shared_ptr nt::ConvertFromC(const NT_Value& value) { case NT_STRING_ARRAY: { std::vector v; v.reserve(value.data.arr_string.size); - for (size_t i=0; i WireDecoder::ReadValue(NT_Type type) { const char* buf; if (!Read(&buf, size)) return nullptr; std::vector v(size); - for (unsigned int i = 0; i < size; ++i) - v[i] = buf[i] ? 1 : 0; + for (unsigned int i = 0; i < size; ++i) v[i] = buf[i] ? 1 : 0; return Value::MakeBooleanArray(std::move(v)); } case NT_DOUBLE_ARRAY: { @@ -167,8 +166,7 @@ std::shared_ptr WireDecoder::ReadValue(NT_Type type) { const char* buf; if (!Read(&buf, size * 8)) return nullptr; std::vector v(size); - for (unsigned int i = 0; i < size; ++i) - v[i] = ::ReadDouble(buf); + for (unsigned int i = 0; i < size; ++i) v[i] = ::ReadDouble(buf); return Value::MakeDoubleArray(std::move(v)); } case NT_STRING_ARRAY: { diff --git a/src/WireDecoder.h b/src/WireDecoder.h index 0136910843..b428fa34a6 100644 --- a/src/WireDecoder.h +++ b/src/WireDecoder.h @@ -112,9 +112,7 @@ class WireDecoder { bool ReadDouble(double* val); /* Reads an ULEB128-encoded unsigned integer. */ - bool ReadUleb128(unsigned long* val) { - return wpi::ReadUleb128(m_is, val); - } + bool ReadUleb128(unsigned long* val) { return wpi::ReadUleb128(m_is, val); } bool ReadType(NT_Type* type); bool ReadString(std::string* str); diff --git a/src/WireEncoder.cpp b/src/WireEncoder.cpp index d947dfdc0a..884d4b845f 100644 --- a/src/WireEncoder.cpp +++ b/src/WireEncoder.cpp @@ -25,16 +25,10 @@ WireEncoder::WireEncoder(unsigned int proto_rev) { void WireEncoder::WriteDouble(double val) { // The highest performance way to do this, albeit non-portable. std::uint64_t v = llvm::DoubleToBits(val); - m_data.append({ - (char)((v >> 56) & 0xff), - (char)((v >> 48) & 0xff), - (char)((v >> 40) & 0xff), - (char)((v >> 32) & 0xff), - (char)((v >> 24) & 0xff), - (char)((v >> 16) & 0xff), - (char)((v >> 8) & 0xff), - (char)(v & 0xff) - }); + m_data.append({(char)((v >> 56) & 0xff), (char)((v >> 48) & 0xff), + (char)((v >> 40) & 0xff), (char)((v >> 32) & 0xff), + (char)((v >> 24) & 0xff), (char)((v >> 16) & 0xff), + (char)((v >> 8) & 0xff), (char)(v & 0xff)}); } void WireEncoder::WriteUleb128(unsigned long val) { @@ -101,22 +95,21 @@ std::size_t WireEncoder::GetValueSize(const Value& value) const { case NT_BOOLEAN_ARRAY: { // 1-byte size, 1 byte per element std::size_t size = value.GetBooleanArray().size(); - if (size > 0xff) size = 0xff; // size is only 1 byte, truncate + if (size > 0xff) size = 0xff; // size is only 1 byte, truncate return 1 + size; } case NT_DOUBLE_ARRAY: { // 1-byte size, 8 bytes per element std::size_t size = value.GetDoubleArray().size(); - if (size > 0xff) size = 0xff; // size is only 1 byte, truncate + if (size > 0xff) size = 0xff; // size is only 1 byte, truncate return 1 + size * 8; } case NT_STRING_ARRAY: { auto v = value.GetStringArray(); std::size_t size = v.size(); - if (size > 0xff) size = 0xff; // size is only 1 byte, truncate - std::size_t len = 1; // 1-byte size - for (std::size_t i = 0; i < size; ++i) - len += GetStringSize(v[i]); + if (size > 0xff) size = 0xff; // size is only 1 byte, truncate + std::size_t len = 1; // 1-byte size + for (std::size_t i = 0; i < size; ++i) len += GetStringSize(v[i]); return len; } default: @@ -152,31 +145,28 @@ void WireEncoder::WriteValue(const Value& value) { case NT_BOOLEAN_ARRAY: { auto v = value.GetBooleanArray(); std::size_t size = v.size(); - if (size > 0xff) size = 0xff; // size is only 1 byte, truncate + if (size > 0xff) size = 0xff; // size is only 1 byte, truncate Write8(size); - for (std::size_t i = 0; i < size; ++i) - Write8(v[i] ? 1 : 0); + for (std::size_t i = 0; i < size; ++i) Write8(v[i] ? 1 : 0); break; } case NT_DOUBLE_ARRAY: { auto v = value.GetDoubleArray(); std::size_t size = v.size(); - if (size > 0xff) size = 0xff; // size is only 1 byte, truncate + if (size > 0xff) size = 0xff; // size is only 1 byte, truncate Write8(size); - for (std::size_t i = 0; i < size; ++i) - WriteDouble(v[i]); + for (std::size_t i = 0; i < size; ++i) WriteDouble(v[i]); break; } case NT_STRING_ARRAY: { auto v = value.GetStringArray(); std::size_t size = v.size(); - if (size > 0xff) size = 0xff; // size is only 1 byte, truncate + if (size > 0xff) size = 0xff; // size is only 1 byte, truncate Write8(size); - for (std::size_t i = 0; i < size; ++i) - WriteString(v[i]); + for (std::size_t i = 0; i < size; ++i) WriteString(v[i]); break; } default: @@ -188,7 +178,7 @@ void WireEncoder::WriteValue(const Value& value) { std::size_t WireEncoder::GetStringSize(llvm::StringRef str) const { if (m_proto_rev < 0x0300u) { std::size_t len = str.size(); - if (len > 0xffff) len = 0xffff; // Limited to 64K length; truncate + if (len > 0xffff) len = 0xffff; // Limited to 64K length; truncate return 2 + len; } return wpi::SizeUleb128(str.size()) + str.size(); @@ -198,7 +188,7 @@ void WireEncoder::WriteString(llvm::StringRef str) { // length std::size_t len = str.size(); if (m_proto_rev < 0x0300u) { - if (len > 0xffff) len = 0xffff; // Limited to 64K length; truncate + if (len > 0xffff) len = 0xffff; // Limited to 64K length; truncate Write16(len); } else WriteUleb128(len); diff --git a/src/WireEncoder.h b/src/WireEncoder.h index 40a4ca070b..de122a672e 100644 --- a/src/WireEncoder.h +++ b/src/WireEncoder.h @@ -63,10 +63,8 @@ class WireEncoder { /* Writes a 32-bit word. */ void Write32(unsigned long val) { - m_data.append({(char)((val >> 24) & 0xff), - (char)((val >> 16) & 0xff), - (char)((val >> 8) & 0xff), - (char)(val & 0xff)}); + m_data.append({(char)((val >> 24) & 0xff), (char)((val >> 16) & 0xff), + (char)((val >> 8) & 0xff), (char)(val & 0xff)}); } /* Writes a double. */ diff --git a/src/networktables/NetworkTable.cpp b/src/networktables/NetworkTable.cpp index ecd924f0af..c45cac8a46 100644 --- a/src/networktables/NetworkTable.cpp +++ b/src/networktables/NetworkTable.cpp @@ -49,7 +49,7 @@ void NetworkTable::SetTeam(int team) { sprintf_s(tmp, "roboRIO-%d-FRC.local", team); #else using namespace std; - snprintf(tmp, 30, "roboRIO-%d-FRC.local",team); + snprintf(tmp, 30, "roboRIO-%d-FRC.local", team); #endif SetIPAddress(tmp); } @@ -107,8 +107,7 @@ NetworkTable::NetworkTable(StringRef path, const private_init&) : m_path(path) {} NetworkTable::~NetworkTable() { - for (auto& i : m_listeners) - nt::RemoveEntryListener(i.second); + for (auto& i : m_listeners) nt::RemoveEntryListener(i.second); } void NetworkTable::AddTableListener(ITableListener* listener) { @@ -156,8 +155,8 @@ void NetworkTable::AddTableListenerEx(StringRef key, ITableListener* listener, path += key; unsigned int id = nt::AddEntryListener( path, - [=](unsigned int /*uid*/, StringRef name, std::shared_ptr value, - unsigned int flags_) { + [=](unsigned int /*uid*/, StringRef name, + std::shared_ptr value, unsigned int flags_) { if (name != path) return; listener->ValueChangedEx(this, name.substr(prefix_len), value, flags_); }, @@ -238,8 +237,7 @@ std::vector NetworkTable::GetKeys(int types) const { path += PATH_SEPARATOR_CHAR; for (auto& entry : nt::GetEntryInfo(path, types)) { auto relative_key = StringRef(entry.name).substr(path.size()); - if (relative_key.find(PATH_SEPARATOR_CHAR) != StringRef::npos) - continue; + if (relative_key.find(PATH_SEPARATOR_CHAR) != StringRef::npos) continue; keys.push_back(relative_key); } return keys; @@ -327,8 +325,7 @@ double NetworkTable::GetNumber(StringRef key, double defaultValue) const { path += PATH_SEPARATOR_CHAR; path += key; auto value = nt::GetEntryValue(path); - if (!value || value->type() != NT_DOUBLE) - return defaultValue; + if (!value || value->type() != NT_DOUBLE) return defaultValue; return value->GetDouble(); } @@ -362,8 +359,7 @@ std::string NetworkTable::GetString(StringRef key, path += PATH_SEPARATOR_CHAR; path += key; auto value = nt::GetEntryValue(path); - if (!value || value->type() != NT_STRING) - return defaultValue; + if (!value || value->type() != NT_STRING) return defaultValue; return value->GetString(); } @@ -396,8 +392,7 @@ bool NetworkTable::GetBoolean(StringRef key, bool defaultValue) const { path += PATH_SEPARATOR_CHAR; path += key; auto value = nt::GetEntryValue(path); - if (!value || value->type() != NT_BOOLEAN) - return defaultValue; + if (!value || value->type() != NT_BOOLEAN) return defaultValue; return value->GetBoolean(); } @@ -414,7 +409,8 @@ bool NetworkTable::SetDefaultBooleanArray(StringRef key, llvm::SmallString<128> path(m_path); path += PATH_SEPARATOR_CHAR; path += key; - return nt::SetDefaultEntryValue(path, nt::Value::MakeBooleanArray(defaultValue)); + return nt::SetDefaultEntryValue(path, + nt::Value::MakeBooleanArray(defaultValue)); } std::vector NetworkTable::GetBooleanArray( @@ -423,8 +419,7 @@ std::vector NetworkTable::GetBooleanArray( path += PATH_SEPARATOR_CHAR; path += key; auto value = nt::GetEntryValue(path); - if (!value || value->type() != NT_BOOLEAN_ARRAY) - return defaultValue; + if (!value || value->type() != NT_BOOLEAN_ARRAY) return defaultValue; return value->GetBooleanArray(); } @@ -441,7 +436,8 @@ bool NetworkTable::SetDefaultNumberArray(StringRef key, llvm::SmallString<128> path(m_path); path += PATH_SEPARATOR_CHAR; path += key; - return nt::SetDefaultEntryValue(path, nt::Value::MakeDoubleArray(defaultValue)); + return nt::SetDefaultEntryValue(path, + nt::Value::MakeDoubleArray(defaultValue)); } std::vector NetworkTable::GetNumberArray( @@ -450,8 +446,7 @@ std::vector NetworkTable::GetNumberArray( path += PATH_SEPARATOR_CHAR; path += key; auto value = nt::GetEntryValue(path); - if (!value || value->type() != NT_DOUBLE_ARRAY) - return defaultValue; + if (!value || value->type() != NT_DOUBLE_ARRAY) return defaultValue; return value->GetDoubleArray(); } @@ -463,12 +458,13 @@ bool NetworkTable::PutStringArray(llvm::StringRef key, return nt::SetEntryValue(path, nt::Value::MakeStringArray(value)); } -bool NetworkTable::SetDefaultStringArray(StringRef key, - llvm::ArrayRef defaultValue) { +bool NetworkTable::SetDefaultStringArray( + StringRef key, llvm::ArrayRef defaultValue) { llvm::SmallString<128> path(m_path); path += PATH_SEPARATOR_CHAR; path += key; - return nt::SetDefaultEntryValue(path, nt::Value::MakeStringArray(defaultValue)); + return nt::SetDefaultEntryValue(path, + nt::Value::MakeStringArray(defaultValue)); } std::vector NetworkTable::GetStringArray( @@ -477,8 +473,7 @@ std::vector NetworkTable::GetStringArray( path += PATH_SEPARATOR_CHAR; path += key; auto value = nt::GetEntryValue(path); - if (!value || value->type() != NT_STRING_ARRAY) - return defaultValue; + if (!value || value->type() != NT_STRING_ARRAY) return defaultValue; return value->GetStringArray(); } @@ -489,8 +484,7 @@ bool NetworkTable::PutRaw(llvm::StringRef key, llvm::StringRef value) { return nt::SetEntryValue(path, nt::Value::MakeRaw(value)); } -bool NetworkTable::SetDefaultRaw(StringRef key, - StringRef defaultValue) { +bool NetworkTable::SetDefaultRaw(StringRef key, StringRef defaultValue) { llvm::SmallString<128> path(m_path); path += PATH_SEPARATOR_CHAR; path += key; @@ -503,8 +497,7 @@ std::string NetworkTable::GetRaw(llvm::StringRef key, path += PATH_SEPARATOR_CHAR; path += key; auto value = nt::GetEntryValue(path); - if (!value || value->type() != NT_RAW) - return defaultValue; + if (!value || value->type() != NT_RAW) return defaultValue; return value->GetRaw(); } diff --git a/src/ntcore_c.cpp b/src/ntcore_c.cpp index 42a1a47287..e8a40f1ba8 100644 --- a/src/ntcore_c.cpp +++ b/src/ntcore_c.cpp @@ -72,14 +72,12 @@ static void ConvertToC(const RpcCallInfo& in, NT_RpcCallInfo* out) { ConvertToC(in.params, &out->params); } -static void DisposeConnectionInfo(NT_ConnectionInfo *info) { +static void DisposeConnectionInfo(NT_ConnectionInfo* info) { std::free(info->remote_id.str); std::free(info->remote_ip.str); } -static void DisposeEntryInfo(NT_EntryInfo *info) { - std::free(info->name.str); -} +static void DisposeEntryInfo(NT_EntryInfo* info) { std::free(info->name.str); } static RpcParamDef ConvertFromC(const NT_RpcParamDef& in) { RpcParamDef out; @@ -117,8 +115,8 @@ extern "C" { * Table Functions */ -void NT_GetEntryValue(const char *name, size_t name_len, - struct NT_Value *value) { +void NT_GetEntryValue(const char* name, size_t name_len, + struct NT_Value* value) { NT_InitValue(value); auto v = nt::GetEntryValue(StringRef(name, name_len)); if (!v) return; @@ -126,37 +124,37 @@ void NT_GetEntryValue(const char *name, size_t name_len, } int NT_SetDefaultEntryValue(const char* name, size_t name_len, - const struct NT_Value *set_value) { - return nt::SetDefaultEntryValue(StringRef(name, name_len), - ConvertFromC(*set_value)); + const struct NT_Value* set_value) { + return nt::SetDefaultEntryValue(StringRef(name, name_len), + ConvertFromC(*set_value)); } -int NT_SetEntryValue(const char *name, size_t name_len, - const struct NT_Value *value) { +int NT_SetEntryValue(const char* name, size_t name_len, + const struct NT_Value* value) { return nt::SetEntryValue(StringRef(name, name_len), ConvertFromC(*value)); } -void NT_SetEntryTypeValue(const char *name, size_t name_len, - const struct NT_Value *value) { +void NT_SetEntryTypeValue(const char* name, size_t name_len, + const struct NT_Value* value) { nt::SetEntryTypeValue(StringRef(name, name_len), ConvertFromC(*value)); } -void NT_SetEntryFlags(const char *name, size_t name_len, unsigned int flags) { +void NT_SetEntryFlags(const char* name, size_t name_len, unsigned int flags) { nt::SetEntryFlags(StringRef(name, name_len), flags); } -unsigned int NT_GetEntryFlags(const char *name, size_t name_len) { +unsigned int NT_GetEntryFlags(const char* name, size_t name_len) { return nt::GetEntryFlags(StringRef(name, name_len)); } -void NT_DeleteEntry(const char *name, size_t name_len) { +void NT_DeleteEntry(const char* name, size_t name_len) { nt::DeleteEntry(StringRef(name, name_len)); } void NT_DeleteAllEntries(void) { nt::DeleteAllEntries(); } -struct NT_EntryInfo *NT_GetEntryInfo(const char *prefix, size_t prefix_len, - unsigned int types, size_t *count) { +struct NT_EntryInfo* NT_GetEntryInfo(const char* prefix, size_t prefix_len, + unsigned int types, size_t* count) { auto info_v = nt::GetEntryInfo(StringRef(prefix, prefix_len), types); *count = info_v.size(); if (info_v.size() == 0) return nullptr; @@ -174,17 +172,16 @@ void NT_Flush(void) { nt::Flush(); } * Callback Creation Functions */ -void NT_SetListenerOnStart(void (*on_start)(void *data), void *data) { +void NT_SetListenerOnStart(void (*on_start)(void* data), void* data) { nt::SetListenerOnStart([=]() { on_start(data); }); } -void NT_SetListenerOnExit(void (*on_exit)(void *data), void *data) { +void NT_SetListenerOnExit(void (*on_exit)(void* data), void* data) { nt::SetListenerOnExit([=]() { on_exit(data); }); } -unsigned int NT_AddEntryListener(const char *prefix, size_t prefix_len, - void *data, - NT_EntryListenerCallback callback, +unsigned int NT_AddEntryListener(const char* prefix, size_t prefix_len, + void* data, NT_EntryListenerCallback callback, unsigned int flags) { return nt::AddEntryListener( StringRef(prefix, prefix_len), @@ -199,11 +196,11 @@ void NT_RemoveEntryListener(unsigned int entry_listener_uid) { nt::RemoveEntryListener(entry_listener_uid); } -unsigned int NT_AddConnectionListener(void *data, +unsigned int NT_AddConnectionListener(void* data, NT_ConnectionListenerCallback callback, int immediate_notify) { return nt::AddConnectionListener( - [=](unsigned int uid, bool connected, const ConnectionInfo &conn) { + [=](unsigned int uid, bool connected, const ConnectionInfo& conn) { NT_ConnectionInfo conn_c; ConvertToC(conn, &conn_c); callback(uid, data, connected ? 1 : 0, &conn_c); @@ -222,85 +219,82 @@ int NT_NotifierDestroyed() { return nt::NotifierDestroyed(); } * Remote Procedure Call Functions */ -void NT_SetRpcServerOnStart(void (*on_start)(void *data), void *data) { +void NT_SetRpcServerOnStart(void (*on_start)(void* data), void* data) { nt::SetRpcServerOnStart([=]() { on_start(data); }); } -void NT_SetRpcServerOnExit(void (*on_exit)(void *data), void *data) { +void NT_SetRpcServerOnExit(void (*on_exit)(void* data), void* data) { nt::SetRpcServerOnExit([=]() { on_exit(data); }); } -void NT_CreateRpc(const char *name, size_t name_len, const char *def, - size_t def_len, void *data, NT_RpcCallback callback) { - nt::CreateRpc( - StringRef(name, name_len), StringRef(def, def_len), - [=](StringRef name, StringRef params, - const ConnectionInfo& conn_info) -> std::string { - size_t results_len; - NT_ConnectionInfo conn_c; - ConvertToC(conn_info, &conn_c); - char* results_c = callback(data, name.data(), name.size(), - params.data(), params.size(), &results_len, - &conn_c); - std::string results(results_c, results_len); - std::free(results_c); - DisposeConnectionInfo(&conn_c); - return results; - }); +void NT_CreateRpc(const char* name, size_t name_len, const char* def, + size_t def_len, void* data, NT_RpcCallback callback) { + nt::CreateRpc(StringRef(name, name_len), StringRef(def, def_len), + [=](StringRef name, StringRef params, + const ConnectionInfo& conn_info) -> std::string { + size_t results_len; + NT_ConnectionInfo conn_c; + ConvertToC(conn_info, &conn_c); + char* results_c = + callback(data, name.data(), name.size(), params.data(), + params.size(), &results_len, &conn_c); + std::string results(results_c, results_len); + std::free(results_c); + DisposeConnectionInfo(&conn_c); + return results; + }); } -void NT_CreatePolledRpc(const char *name, size_t name_len, const char *def, +void NT_CreatePolledRpc(const char* name, size_t name_len, const char* def, size_t def_len) { nt::CreatePolledRpc(StringRef(name, name_len), StringRef(def, def_len)); } int NT_PollRpc(int blocking, NT_RpcCallInfo* call_info) { RpcCallInfo call_info_cpp; - if (!nt::PollRpc(blocking != 0, &call_info_cpp)) - return 0; + if (!nt::PollRpc(blocking != 0, &call_info_cpp)) return 0; ConvertToC(call_info_cpp, call_info); return 1; } -int NT_PollRpcTimeout(int blocking, double time_out, +int NT_PollRpcTimeout(int blocking, double time_out, NT_RpcCallInfo* call_info) { RpcCallInfo call_info_cpp; - if (!nt::PollRpc(blocking != 0, time_out, &call_info_cpp)) - return 0; + if (!nt::PollRpc(blocking != 0, time_out, &call_info_cpp)) return 0; ConvertToC(call_info_cpp, call_info); return 1; } void NT_PostRpcResponse(unsigned int rpc_id, unsigned int call_uid, - const char *result, size_t result_len) { + const char* result, size_t result_len) { nt::PostRpcResponse(rpc_id, call_uid, StringRef(result, result_len)); } -unsigned int NT_CallRpc(const char *name, size_t name_len, - const char *params, size_t params_len) { +unsigned int NT_CallRpc(const char* name, size_t name_len, const char* params, + size_t params_len) { return nt::CallRpc(StringRef(name, name_len), StringRef(params, params_len)); } -char *NT_GetRpcResult(int blocking, unsigned int call_uid, size_t *result_len) { +char* NT_GetRpcResult(int blocking, unsigned int call_uid, size_t* result_len) { std::string result; if (!nt::GetRpcResult(blocking != 0, call_uid, &result)) return nullptr; // convert result *result_len = result.size(); - char *result_cstr; + char* result_cstr; ConvertToC(result, &result_cstr); return result_cstr; } -char *NT_GetRpcResultTimeout(int blocking, unsigned int call_uid, - double time_out, size_t *result_len) { +char* NT_GetRpcResultTimeout(int blocking, unsigned int call_uid, + double time_out, size_t* result_len) { std::string result; - if (!nt::GetRpcResult(blocking != 0, call_uid, time_out, &result)) + if (!nt::GetRpcResult(blocking != 0, call_uid, time_out, &result)) return nullptr; // convert result *result_len = result.size(); - char *result_cstr; + char* result_cstr; ConvertToC(result, &result_cstr); return result_cstr; } @@ -309,29 +303,28 @@ void NT_CancelBlockingRpcResult(unsigned int call_uid) { nt::CancelBlockingRpcResult(call_uid); } -char *NT_PackRpcDefinition(const NT_RpcDefinition *def, size_t *packed_len) { +char* NT_PackRpcDefinition(const NT_RpcDefinition* def, size_t* packed_len) { auto packed = nt::PackRpcDefinition(ConvertFromC(*def)); // convert result *packed_len = packed.size(); - char *packed_cstr; + char* packed_cstr; ConvertToC(packed, &packed_cstr); return packed_cstr; } -int NT_UnpackRpcDefinition(const char *packed, size_t packed_len, - NT_RpcDefinition *def) { +int NT_UnpackRpcDefinition(const char* packed, size_t packed_len, + NT_RpcDefinition* def) { nt::RpcDefinition def_v; - if (!nt::UnpackRpcDefinition(StringRef(packed, packed_len), &def_v)) - return 0; + if (!nt::UnpackRpcDefinition(StringRef(packed, packed_len), &def_v)) return 0; // convert result ConvertToC(def_v, def); return 1; } -char *NT_PackRpcValues(const NT_Value **values, size_t values_len, - size_t *packed_len) { +char* NT_PackRpcValues(const NT_Value** values, size_t values_len, + size_t* packed_len) { // create input vector std::vector> values_v; values_v.reserve(values_len); @@ -343,20 +336,20 @@ char *NT_PackRpcValues(const NT_Value **values, size_t values_len, // convert result *packed_len = packed.size(); - char *packed_cstr; + char* packed_cstr; ConvertToC(packed, &packed_cstr); return packed_cstr; } -NT_Value **NT_UnpackRpcValues(const char *packed, size_t packed_len, - const NT_Type *types, size_t types_len) { +NT_Value** NT_UnpackRpcValues(const char* packed, size_t packed_len, + const NT_Type* types, size_t types_len) { auto values_v = nt::UnpackRpcValues(StringRef(packed, packed_len), ArrayRef(types, types_len)); if (values_v.size() == 0) return nullptr; // create array and copy into it - NT_Value** values = static_cast( - std::malloc(values_v.size() * sizeof(NT_Value*))); + NT_Value** values = + static_cast(std::malloc(values_v.size() * sizeof(NT_Value*))); for (size_t i = 0; i < values_v.size(); ++i) { values[i] = static_cast(std::malloc(sizeof(NT_Value))); ConvertToC(*values_v[i], values[i]); @@ -368,53 +361,45 @@ NT_Value **NT_UnpackRpcValues(const char *packed, size_t packed_len, * Client/Server Functions */ -void NT_SetNetworkIdentity(const char *name, size_t name_len) { +void NT_SetNetworkIdentity(const char* name, size_t name_len) { nt::SetNetworkIdentity(StringRef(name, name_len)); } -void NT_StartServer(const char *persist_filename, const char *listen_address, +void NT_StartServer(const char* persist_filename, const char* listen_address, unsigned int port) { nt::StartServer(persist_filename, listen_address, port); } void NT_StopServer(void) { nt::StopServer(); } -void NT_StartClient(const char *server_name, unsigned int port) { +void NT_StartClient(const char* server_name, unsigned int port) { nt::StartClient(server_name, port); } -void NT_StartClientMulti(size_t count, const char **server_names, - const unsigned int *ports) { +void NT_StartClientMulti(size_t count, const char** server_names, + const unsigned int* ports) { std::vector> servers; servers.reserve(count); - for (size_t i=0; i( + NT_ConnectionInfo* conn = static_cast( std::malloc(conn_v.size() * sizeof(NT_ConnectionInfo))); for (size_t i = 0; i < conn_v.size(); ++i) ConvertToC(conn_v[i], &conn[i]); return conn; @@ -424,12 +409,12 @@ struct NT_ConnectionInfo *NT_GetConnections(size_t *count) { * Persistent Functions */ -const char *NT_SavePersistent(const char *filename) { +const char* NT_SavePersistent(const char* filename) { return nt::SavePersistent(filename); } -const char *NT_LoadPersistent(const char *filename, - void (*warn)(size_t line, const char *msg)) { +const char* NT_LoadPersistent(const char* filename, + void (*warn)(size_t line, const char* msg)) { return nt::LoadPersistent(filename, warn); } @@ -437,15 +422,13 @@ const char *NT_LoadPersistent(const char *filename, * Utility Functions */ -unsigned long long NT_Now() { - return wpi::Now(); -} +unsigned long long NT_Now() { return wpi::Now(); } void NT_SetLogger(NT_LogFunc func, unsigned int min_level) { nt::SetLogger(func, min_level); } -void NT_DisposeValue(NT_Value *value) { +void NT_DisposeValue(NT_Value* value) { switch (value->type) { case NT_UNASSIGNED: case NT_BOOLEAN: @@ -475,39 +458,39 @@ void NT_DisposeValue(NT_Value *value) { value->last_change = 0; } -void NT_InitValue(NT_Value *value) { +void NT_InitValue(NT_Value* value) { value->type = NT_UNASSIGNED; value->last_change = 0; } -void NT_DisposeString(NT_String *str) { +void NT_DisposeString(NT_String* str) { std::free(str->str); str->str = nullptr; str->len = 0; } -void NT_InitString(NT_String *str) { +void NT_InitString(NT_String* str) { str->str = nullptr; str->len = 0; } -enum NT_Type NT_GetType(const char *name, size_t name_len) { +enum NT_Type NT_GetType(const char* name, size_t name_len) { auto v = nt::GetEntryValue(StringRef(name, name_len)); if (!v) return NT_Type::NT_UNASSIGNED; return v->type(); } -void NT_DisposeConnectionInfoArray(NT_ConnectionInfo *arr, size_t count) { +void NT_DisposeConnectionInfoArray(NT_ConnectionInfo* arr, size_t count) { for (size_t i = 0; i < count; i++) DisposeConnectionInfo(&arr[i]); std::free(arr); } -void NT_DisposeEntryInfoArray(NT_EntryInfo *arr, size_t count){ +void NT_DisposeEntryInfoArray(NT_EntryInfo* arr, size_t count) { for (size_t i = 0; i < count; i++) DisposeEntryInfo(&arr[i]); std::free(arr); } -void NT_DisposeRpcDefinition(NT_RpcDefinition *def) { +void NT_DisposeRpcDefinition(NT_RpcDefinition* def) { NT_DisposeString(&def->name); for (size_t i = 0; i < def->num_params; ++i) { @@ -525,7 +508,7 @@ void NT_DisposeRpcDefinition(NT_RpcDefinition *def) { def->num_results = 0; } -void NT_DisposeRpcCallInfo(NT_RpcCallInfo *call_info) { +void NT_DisposeRpcCallInfo(NT_RpcCallInfo* call_info) { NT_DisposeString(&call_info->name); NT_DisposeString(&call_info->params); } @@ -535,39 +518,39 @@ void NT_DisposeRpcCallInfo(NT_RpcCallInfo *call_info) { /* Array and Struct Allocations */ /* Allocates a char array of the specified size.*/ -char *NT_AllocateCharArray(size_t size) { - char *retVal = static_cast(std::malloc(size * sizeof(char))); +char* NT_AllocateCharArray(size_t size) { + char* retVal = static_cast(std::malloc(size * sizeof(char))); return retVal; } /* Allocates an integer or boolean array of the specified size. */ -int *NT_AllocateBooleanArray(size_t size) { - int *retVal = static_cast(std::malloc(size * sizeof(int))); +int* NT_AllocateBooleanArray(size_t size) { + int* retVal = static_cast(std::malloc(size * sizeof(int))); return retVal; } /* Allocates a double array of the specified size. */ -double *NT_AllocateDoubleArray(size_t size) { - double *retVal = static_cast(std::malloc(size * sizeof(double))); +double* NT_AllocateDoubleArray(size_t size) { + double* retVal = static_cast(std::malloc(size * sizeof(double))); return retVal; } /* Allocates an NT_String array of the specified size. */ -struct NT_String *NT_AllocateStringArray(size_t size) { - NT_String *retVal = - static_cast(std::malloc(size * sizeof(NT_String))); +struct NT_String* NT_AllocateStringArray(size_t size) { + NT_String* retVal = + static_cast(std::malloc(size * sizeof(NT_String))); return retVal; } -void NT_FreeCharArray(char *v_char) { std::free(v_char); } -void NT_FreeDoubleArray(double *v_double) { std::free(v_double); } -void NT_FreeBooleanArray(int *v_boolean) { std::free(v_boolean); } -void NT_FreeStringArray(struct NT_String *v_string, size_t arr_size) { +void NT_FreeCharArray(char* v_char) { std::free(v_char); } +void NT_FreeDoubleArray(double* v_double) { std::free(v_double); } +void NT_FreeBooleanArray(int* v_boolean) { std::free(v_boolean); } +void NT_FreeStringArray(struct NT_String* v_string, size_t arr_size) { for (size_t i = 0; i < arr_size; i++) std::free(v_string[i].str); std::free(v_string); } -int NT_SetEntryDouble(const char *name, size_t name_len, double v_double, +int NT_SetEntryDouble(const char* name, size_t name_len, double v_double, int force) { if (force != 0) { nt::SetEntryTypeValue(StringRef(name, name_len), @@ -579,7 +562,7 @@ int NT_SetEntryDouble(const char *name, size_t name_len, double v_double, } } -int NT_SetEntryBoolean(const char *name, size_t name_len, int v_boolean, +int NT_SetEntryBoolean(const char* name, size_t name_len, int v_boolean, int force) { if (force != 0) { nt::SetEntryTypeValue(StringRef(name, name_len), @@ -591,7 +574,7 @@ int NT_SetEntryBoolean(const char *name, size_t name_len, int v_boolean, } } -int NT_SetEntryString(const char *name, size_t name_len, const char *str, +int NT_SetEntryString(const char* name, size_t name_len, const char* str, size_t str_len, int force) { if (force != 0) { nt::SetEntryTypeValue(StringRef(name, name_len), @@ -603,7 +586,7 @@ int NT_SetEntryString(const char *name, size_t name_len, const char *str, } } -int NT_SetEntryRaw(const char *name, size_t name_len, const char *raw, +int NT_SetEntryRaw(const char* name, size_t name_len, const char* raw, size_t raw_len, int force) { if (force != 0) { nt::SetEntryTypeValue(StringRef(name, name_len), @@ -615,7 +598,7 @@ int NT_SetEntryRaw(const char *name, size_t name_len, const char *raw, } } -int NT_SetEntryBooleanArray(const char *name, size_t name_len, const int *arr, +int NT_SetEntryBooleanArray(const char* name, size_t name_len, const int* arr, size_t size, int force) { if (force != 0) { nt::SetEntryTypeValue( @@ -629,7 +612,7 @@ int NT_SetEntryBooleanArray(const char *name, size_t name_len, const int *arr, } } -int NT_SetEntryDoubleArray(const char *name, size_t name_len, const double *arr, +int NT_SetEntryDoubleArray(const char* name, size_t name_len, const double* arr, size_t size, int force) { if (force != 0) { nt::SetEntryTypeValue( @@ -643,8 +626,8 @@ int NT_SetEntryDoubleArray(const char *name, size_t name_len, const double *arr, } } -int NT_SetEntryStringArray(const char *name, size_t name_len, - const struct NT_String *arr, size_t size, +int NT_SetEntryStringArray(const char* name, size_t name_len, + const struct NT_String* arr, size_t size, int force) { std::vector v; v.reserve(size); @@ -660,79 +643,79 @@ int NT_SetEntryStringArray(const char *name, size_t name_len, } } -enum NT_Type NT_GetValueType(const struct NT_Value *value) { +enum NT_Type NT_GetValueType(const struct NT_Value* value) { if (!value) return NT_Type::NT_UNASSIGNED; return value->type; } -int NT_GetValueBoolean(const struct NT_Value *value, - unsigned long long *last_change, int *v_boolean) { +int NT_GetValueBoolean(const struct NT_Value* value, + unsigned long long* last_change, int* v_boolean) { if (!value || value->type != NT_Type::NT_BOOLEAN) return 0; *v_boolean = value->data.v_boolean; *last_change = value->last_change; return 1; } -int NT_GetValueDouble(const struct NT_Value *value, - unsigned long long *last_change, double *v_double) { +int NT_GetValueDouble(const struct NT_Value* value, + unsigned long long* last_change, double* v_double) { if (!value || value->type != NT_Type::NT_DOUBLE) return 0; *last_change = value->last_change; *v_double = value->data.v_double; return 1; } -char *NT_GetValueString(const struct NT_Value *value, - unsigned long long *last_change, size_t *str_len) { +char* NT_GetValueString(const struct NT_Value* value, + unsigned long long* last_change, size_t* str_len) { if (!value || value->type != NT_Type::NT_STRING) return nullptr; *last_change = value->last_change; *str_len = value->data.v_string.len; - char *str = (char*)std::malloc(value->data.v_string.len + 1); + char* str = (char*)std::malloc(value->data.v_string.len + 1); std::memcpy(str, value->data.v_string.str, value->data.v_string.len + 1); return str; } -char *NT_GetValueRaw(const struct NT_Value *value, - unsigned long long *last_change, size_t *raw_len) { +char* NT_GetValueRaw(const struct NT_Value* value, + unsigned long long* last_change, size_t* raw_len) { if (!value || value->type != NT_Type::NT_RAW) return nullptr; *last_change = value->last_change; *raw_len = value->data.v_string.len; - char *raw = (char*)std::malloc(value->data.v_string.len + 1); + char* raw = (char*)std::malloc(value->data.v_string.len + 1); std::memcpy(raw, value->data.v_string.str, value->data.v_string.len + 1); return raw; } -int *NT_GetValueBooleanArray(const struct NT_Value *value, - unsigned long long *last_change, - size_t *arr_size) { +int* NT_GetValueBooleanArray(const struct NT_Value* value, + unsigned long long* last_change, + size_t* arr_size) { if (!value || value->type != NT_Type::NT_BOOLEAN_ARRAY) return nullptr; *last_change = value->last_change; *arr_size = value->data.arr_boolean.size; - int *arr = (int*)std::malloc(value->data.arr_boolean.size * sizeof(int)); + int* arr = (int*)std::malloc(value->data.arr_boolean.size * sizeof(int)); std::memcpy(arr, value->data.arr_boolean.arr, value->data.arr_boolean.size * sizeof(int)); return arr; } -double *NT_GetValueDoubleArray(const struct NT_Value *value, - unsigned long long *last_change, - size_t *arr_size) { +double* NT_GetValueDoubleArray(const struct NT_Value* value, + unsigned long long* last_change, + size_t* arr_size) { if (!value || value->type != NT_Type::NT_DOUBLE_ARRAY) return nullptr; *last_change = value->last_change; *arr_size = value->data.arr_double.size; - double *arr = - (double *)std::malloc(value->data.arr_double.size * sizeof(double)); + double* arr = + (double*)std::malloc(value->data.arr_double.size * sizeof(double)); std::memcpy(arr, value->data.arr_double.arr, value->data.arr_double.size * sizeof(double)); return arr; } -NT_String *NT_GetValueStringArray(const struct NT_Value *value, - unsigned long long *last_change, - size_t *arr_size) { +NT_String* NT_GetValueStringArray(const struct NT_Value* value, + unsigned long long* last_change, + size_t* arr_size) { if (!value || value->type != NT_Type::NT_STRING_ARRAY) return nullptr; *last_change = value->last_change; *arr_size = value->data.arr_string.size; - NT_String *arr = static_cast( + NT_String* arr = static_cast( std::malloc(value->data.arr_string.size * sizeof(NT_String))); for (size_t i = 0; i < value->data.arr_string.size; ++i) { size_t len = value->data.arr_string.arr[i].len; @@ -743,59 +726,62 @@ NT_String *NT_GetValueStringArray(const struct NT_Value *value, return arr; } -int NT_SetDefaultEntryBoolean(const char *name, size_t name_len, +int NT_SetDefaultEntryBoolean(const char* name, size_t name_len, int default_boolean) { return nt::SetDefaultEntryValue(StringRef(name, name_len), Value::MakeBoolean(default_boolean != 0)); } -int NT_SetDefaultEntryDouble(const char *name, size_t name_len, +int NT_SetDefaultEntryDouble(const char* name, size_t name_len, double default_double) { return nt::SetDefaultEntryValue(StringRef(name, name_len), Value::MakeDouble(default_double)); } -int NT_SetDefaultEntryString(const char *name, size_t name_len, - const char *default_value, size_t default_len) { - return nt::SetDefaultEntryValue(StringRef(name, name_len), - Value::MakeString(StringRef(default_value, - default_len))); +int NT_SetDefaultEntryString(const char* name, size_t name_len, + const char* default_value, size_t default_len) { + return nt::SetDefaultEntryValue( + StringRef(name, name_len), + Value::MakeString(StringRef(default_value, default_len))); } -int NT_SetDefaultEntryRaw(const char *name, size_t name_len, - const char *default_value, size_t default_len) { - return nt::SetDefaultEntryValue(StringRef(name, name_len), - Value::MakeString(StringRef(default_value, - default_len))); +int NT_SetDefaultEntryRaw(const char* name, size_t name_len, + const char* default_value, size_t default_len) { + return nt::SetDefaultEntryValue( + StringRef(name, name_len), + Value::MakeString(StringRef(default_value, default_len))); } -int NT_SetDefaultEntryBooleanArray(const char *name, size_t name_len, - const int *default_value, +int NT_SetDefaultEntryBooleanArray(const char* name, size_t name_len, + const int* default_value, size_t default_size) { - return nt::SetDefaultEntryValue(StringRef(name, name_len), - Value::MakeBooleanArray(llvm::makeArrayRef(default_value, default_size))); + return nt::SetDefaultEntryValue( + StringRef(name, name_len), + Value::MakeBooleanArray(llvm::makeArrayRef(default_value, default_size))); } -int NT_SetDefaultEntryDoubleArray(const char *name, size_t name_len, const double *default_value, +int NT_SetDefaultEntryDoubleArray(const char* name, size_t name_len, + const double* default_value, size_t default_size) { - return nt::SetDefaultEntryValue(StringRef(name, name_len), - Value::MakeDoubleArray(llvm::makeArrayRef(default_value, default_size))); + return nt::SetDefaultEntryValue( + StringRef(name, name_len), + Value::MakeDoubleArray(llvm::makeArrayRef(default_value, default_size))); } -int NT_SetDefaultEntryStringArray(const char *name, size_t name_len, +int NT_SetDefaultEntryStringArray(const char* name, size_t name_len, const struct NT_String* default_value, size_t default_size) { std::vector vec; vec.reserve(default_size); - for (size_t i = 0; i < default_size; ++i) + for (size_t i = 0; i < default_size; ++i) vec.push_back(ConvertFromC(default_value[i])); - - return nt::SetDefaultEntryValue(StringRef(name, name_len), + + return nt::SetDefaultEntryValue(StringRef(name, name_len), Value::MakeStringArray(std::move(vec))); } -int NT_GetEntryBoolean(const char *name, size_t name_len, - unsigned long long *last_change, int *v_boolean) { +int NT_GetEntryBoolean(const char* name, size_t name_len, + unsigned long long* last_change, int* v_boolean) { auto v = nt::GetEntryValue(StringRef(name, name_len)); if (!v || !v->IsBoolean()) return 0; *v_boolean = v->GetBoolean(); @@ -803,8 +789,8 @@ int NT_GetEntryBoolean(const char *name, size_t name_len, return 1; } -int NT_GetEntryDouble(const char *name, size_t name_len, - unsigned long long *last_change, double *v_double) { +int NT_GetEntryDouble(const char* name, size_t name_len, + unsigned long long* last_change, double* v_double) { auto v = nt::GetEntryValue(StringRef(name, name_len)); if (!v || !v->IsDouble()) return 0; *last_change = v->last_change(); @@ -812,8 +798,8 @@ int NT_GetEntryDouble(const char *name, size_t name_len, return 1; } -char *NT_GetEntryString(const char *name, size_t name_len, - unsigned long long *last_change, size_t *str_len) { +char* NT_GetEntryString(const char* name, size_t name_len, + unsigned long long* last_change, size_t* str_len) { auto v = nt::GetEntryValue(StringRef(name, name_len)); if (!v || !v->IsString()) return nullptr; *last_change = v->last_change(); @@ -823,8 +809,8 @@ char *NT_GetEntryString(const char *name, size_t name_len, return v_string.str; } -char *NT_GetEntryRaw(const char *name, size_t name_len, - unsigned long long *last_change, size_t *raw_len) { +char* NT_GetEntryRaw(const char* name, size_t name_len, + unsigned long long* last_change, size_t* raw_len) { auto v = nt::GetEntryValue(StringRef(name, name_len)); if (!v || !v->IsRaw()) return nullptr; *last_change = v->last_change(); @@ -834,42 +820,41 @@ char *NT_GetEntryRaw(const char *name, size_t name_len, return v_raw.str; } -int *NT_GetEntryBooleanArray(const char *name, size_t name_len, - unsigned long long *last_change, - size_t *arr_size) { +int* NT_GetEntryBooleanArray(const char* name, size_t name_len, + unsigned long long* last_change, + size_t* arr_size) { auto v = nt::GetEntryValue(StringRef(name, name_len)); if (!v || !v->IsBooleanArray()) return nullptr; *last_change = v->last_change(); auto vArr = v->GetBooleanArray(); - int *arr = static_cast(std::malloc(vArr.size() * sizeof(int))); + int* arr = static_cast(std::malloc(vArr.size() * sizeof(int))); *arr_size = vArr.size(); std::copy(vArr.begin(), vArr.end(), arr); return arr; } -double *NT_GetEntryDoubleArray(const char *name, size_t name_len, - unsigned long long *last_change, - size_t *arr_size) { +double* NT_GetEntryDoubleArray(const char* name, size_t name_len, + unsigned long long* last_change, + size_t* arr_size) { auto v = nt::GetEntryValue(StringRef(name, name_len)); if (!v || !v->IsDoubleArray()) return nullptr; *last_change = v->last_change(); auto vArr = v->GetDoubleArray(); - double *arr = - static_cast(std::malloc(vArr.size() * sizeof(double))); + double* arr = static_cast(std::malloc(vArr.size() * sizeof(double))); *arr_size = vArr.size(); std::copy(vArr.begin(), vArr.end(), arr); return arr; } -NT_String *NT_GetEntryStringArray(const char *name, size_t name_len, - unsigned long long *last_change, - size_t *arr_size) { +NT_String* NT_GetEntryStringArray(const char* name, size_t name_len, + unsigned long long* last_change, + size_t* arr_size) { auto v = nt::GetEntryValue(StringRef(name, name_len)); if (!v || !v->IsStringArray()) return nullptr; *last_change = v->last_change(); auto vArr = v->GetStringArray(); - NT_String *arr = - static_cast(std::malloc(vArr.size() * sizeof(NT_String))); + NT_String* arr = + static_cast(std::malloc(vArr.size() * sizeof(NT_String))); for (size_t i = 0; i < vArr.size(); ++i) { ConvertToC(vArr[i], &arr[i]); } diff --git a/src/ntcore_cpp.cpp b/src/ntcore_cpp.cpp index 47bc66f96f..f80fcc871a 100644 --- a/src/ntcore_cpp.cpp +++ b/src/ntcore_cpp.cpp @@ -50,21 +50,15 @@ unsigned int GetEntryFlags(StringRef name) { return Storage::GetInstance().GetEntryFlags(name); } -void DeleteEntry(StringRef name) { - Storage::GetInstance().DeleteEntry(name); -} +void DeleteEntry(StringRef name) { Storage::GetInstance().DeleteEntry(name); } -void DeleteAllEntries() { - Storage::GetInstance().DeleteAllEntries(); -} +void DeleteAllEntries() { Storage::GetInstance().DeleteAllEntries(); } std::vector GetEntryInfo(StringRef prefix, unsigned int types) { return Storage::GetInstance().GetEntryInfo(prefix, types); } -void Flush() { - Dispatcher::GetInstance().Flush(); -} +void Flush() { Dispatcher::GetInstance().Flush(); } /* * Callback Creation Functions @@ -145,9 +139,9 @@ bool GetRpcResult(bool blocking, unsigned int call_uid, std::string* result) { return Storage::GetInstance().GetRpcResult(blocking, call_uid, result); } -bool GetRpcResult(bool blocking, unsigned int call_uid, - double time_out, std::string* result) { - return Storage::GetInstance().GetRpcResult(blocking, call_uid, time_out, +bool GetRpcResult(bool blocking, unsigned int call_uid, double time_out, + std::string* result) { + return Storage::GetInstance().GetRpcResult(blocking, call_uid, time_out, result); } @@ -245,16 +239,14 @@ void SetNetworkIdentity(StringRef name) { Dispatcher::GetInstance().SetIdentity(name); } -void StartServer(StringRef persist_filename, const char *listen_address, +void StartServer(StringRef persist_filename, const char* listen_address, unsigned int port) { Dispatcher::GetInstance().StartServer(persist_filename, listen_address, port); } -void StopServer() { - Dispatcher::GetInstance().Stop(); -} +void StopServer() { Dispatcher::GetInstance().Stop(); } -void StartClient(const char *server_name, unsigned int port) { +void StartClient(const char* server_name, unsigned int port) { Dispatcher::GetInstance().StartClient(server_name, port); } @@ -262,17 +254,11 @@ void StartClient(ArrayRef> servers) { Dispatcher::GetInstance().StartClient(servers); } -void StopClient() { - Dispatcher::GetInstance().Stop(); -} +void StopClient() { Dispatcher::GetInstance().Stop(); } -void StopRpcServer() { - RpcServer::GetInstance().Stop(); -} +void StopRpcServer() { RpcServer::GetInstance().Stop(); } -void StopNotifier() { - Notifier::GetInstance().Stop(); -} +void StopNotifier() { Notifier::GetInstance().Stop(); } void SetUpdateRate(double interval) { Dispatcher::GetInstance().SetUpdateRate(interval); @@ -296,9 +282,7 @@ const char* LoadPersistent( return Storage::GetInstance().LoadPersistent(filename, warn); } -unsigned long long Now() { - return wpi::Now(); -} +unsigned long long Now() { return wpi::Now(); } void SetLogger(LogFunc func, unsigned int min_level) { Logger& logger = Logger::GetInstance(); diff --git a/src/ntcore_test.cpp b/src/ntcore_test.cpp index fa4d15c7b7..9d6cca0861 100644 --- a/src/ntcore_test.cpp +++ b/src/ntcore_test.cpp @@ -11,18 +11,20 @@ extern "C" { struct NT_String* NT_GetStringForTesting(const char* string, int* struct_size) { - struct NT_String* str = static_cast(std::calloc(1, sizeof(NT_String))); + struct NT_String* str = + static_cast(std::calloc(1, sizeof(NT_String))); nt::ConvertToC(llvm::StringRef(string), str); *struct_size = sizeof(NT_String); return str; } -struct NT_EntryInfo* NT_GetEntryInfoForTesting(const char* name, enum NT_Type type, - unsigned int flags, +struct NT_EntryInfo* NT_GetEntryInfoForTesting(const char* name, + enum NT_Type type, + unsigned int flags, unsigned long long last_change, int* struct_size) { - struct NT_EntryInfo* entry_info = static_cast(std::calloc(1, - sizeof(NT_EntryInfo))); + struct NT_EntryInfo* entry_info = + static_cast(std::calloc(1, sizeof(NT_EntryInfo))); nt::ConvertToC(llvm::StringRef(name), &entry_info->name); entry_info->type = type; entry_info->flags = flags; @@ -36,12 +38,10 @@ void NT_FreeEntryInfoForTesting(struct NT_EntryInfo* info) { std::free(info); } -struct NT_ConnectionInfo* NT_GetConnectionInfoForTesting(const char* remote_id, - const char* remote_ip, - unsigned int remote_port, - unsigned long long last_update, - unsigned int protocol_version, - int* struct_size) { +struct NT_ConnectionInfo* NT_GetConnectionInfoForTesting( + const char* remote_id, const char* remote_ip, unsigned int remote_port, + unsigned long long last_update, unsigned int protocol_version, + int* struct_size) { struct NT_ConnectionInfo* conn_info = static_cast( std::calloc(1, sizeof(NT_ConnectionInfo))); nt::ConvertToC(llvm::StringRef(remote_id), &conn_info->remote_id); @@ -50,7 +50,7 @@ struct NT_ConnectionInfo* NT_GetConnectionInfoForTesting(const char* remote_id, conn_info->last_update = last_update; conn_info->protocol_version = protocol_version; *struct_size = sizeof(NT_ConnectionInfo); - return conn_info; + return conn_info; } void NT_FreeConnectionInfoForTesting(struct NT_ConnectionInfo* info) { @@ -59,10 +59,10 @@ void NT_FreeConnectionInfoForTesting(struct NT_ConnectionInfo* info) { std::free(info); } -struct NT_Value* NT_GetValueBooleanForTesting( - unsigned long long last_change, int val, - int* struct_size) { - struct NT_Value* value = static_cast(std::calloc(1, sizeof(NT_Value))); +struct NT_Value* NT_GetValueBooleanForTesting(unsigned long long last_change, + int val, int* struct_size) { + struct NT_Value* value = + static_cast(std::calloc(1, sizeof(NT_Value))); value->type = NT_BOOLEAN; value->last_change = last_change; value->data.v_boolean = val; @@ -70,10 +70,10 @@ struct NT_Value* NT_GetValueBooleanForTesting( return value; } -struct NT_Value* NT_GetValueDoubleForTesting( - unsigned long long last_change, double val, - int* struct_size) { - struct NT_Value* value = static_cast(std::calloc(1, sizeof(NT_Value))); +struct NT_Value* NT_GetValueDoubleForTesting(unsigned long long last_change, + double val, int* struct_size) { + struct NT_Value* value = + static_cast(std::calloc(1, sizeof(NT_Value))); value->type = NT_DOUBLE; value->last_change = last_change; value->data.v_double = val; @@ -81,10 +81,11 @@ struct NT_Value* NT_GetValueDoubleForTesting( return value; } -struct NT_Value* NT_GetValueStringForTesting( - unsigned long long last_change, - const char* str, int* struct_size) { - struct NT_Value* value = static_cast(std::calloc(1, sizeof(NT_Value))); +struct NT_Value* NT_GetValueStringForTesting(unsigned long long last_change, + const char* str, + int* struct_size) { + struct NT_Value* value = + static_cast(std::calloc(1, sizeof(NT_Value))); value->type = NT_STRING; value->last_change = last_change; nt::ConvertToC(llvm::StringRef(str), &value->data.v_string); @@ -92,10 +93,11 @@ struct NT_Value* NT_GetValueStringForTesting( return value; } -struct NT_Value* NT_GetValueRawForTesting( - unsigned long long last_change, - const char* raw, int raw_len, int* struct_size) { - struct NT_Value* value = static_cast(std::calloc(1, sizeof(NT_Value))); +struct NT_Value* NT_GetValueRawForTesting(unsigned long long last_change, + const char* raw, int raw_len, + int* struct_size) { + struct NT_Value* value = + static_cast(std::calloc(1, sizeof(NT_Value))); value->type = NT_RAW; value->last_change = last_change; nt::ConvertToC(llvm::StringRef(raw, raw_len), &value->data.v_string); @@ -104,10 +106,10 @@ struct NT_Value* NT_GetValueRawForTesting( } struct NT_Value* NT_GetValueBooleanArrayForTesting( - unsigned long long last_change, - const int* arr, size_t array_len, + unsigned long long last_change, const int* arr, size_t array_len, int* struct_size) { - struct NT_Value* value = static_cast(std::calloc(1, sizeof(NT_Value))); + struct NT_Value* value = + static_cast(std::calloc(1, sizeof(NT_Value))); value->type = NT_BOOLEAN_ARRAY; value->last_change = last_change; value->data.arr_boolean.arr = NT_AllocateBooleanArray(array_len); @@ -119,54 +121,56 @@ struct NT_Value* NT_GetValueBooleanArrayForTesting( } struct NT_Value* NT_GetValueDoubleArrayForTesting( - unsigned long long last_change, - const double* arr, size_t array_len, + unsigned long long last_change, const double* arr, size_t array_len, int* struct_size) { - struct NT_Value* value = static_cast(std::calloc(1, sizeof(NT_Value))); + struct NT_Value* value = + static_cast(std::calloc(1, sizeof(NT_Value))); value->type = NT_BOOLEAN; value->last_change = last_change; value->data.arr_double.arr = NT_AllocateDoubleArray(array_len); value->data.arr_double.size = array_len; std::memcpy(value->data.arr_double.arr, arr, - value->data.arr_double.size * sizeof(int)); + value->data.arr_double.size * sizeof(int)); *struct_size = sizeof(NT_Value); return value; } struct NT_Value* NT_GetValueStringArrayForTesting( - unsigned long long last_change, - const struct NT_String* arr, - size_t array_len, int* struct_size) { - struct NT_Value* value = static_cast(std::calloc(1, sizeof(NT_Value))); + unsigned long long last_change, const struct NT_String* arr, + size_t array_len, int* struct_size) { + struct NT_Value* value = + static_cast(std::calloc(1, sizeof(NT_Value))); value->type = NT_BOOLEAN; value->last_change = last_change; value->data.arr_string.arr = NT_AllocateStringArray(array_len); value->data.arr_string.size = array_len; for (size_t i = 0; i < value->data.arr_string.size; ++i) { - size_t len = arr[i].len; - value->data.arr_string.arr[i].len = len; - value->data.arr_string.arr[i].str = (char*)std::malloc(len + 1); - std::memcpy(value->data.arr_string.arr[i].str, arr[i].str, len + 1); - } + size_t len = arr[i].len; + value->data.arr_string.arr[i].len = len; + value->data.arr_string.arr[i].str = (char*)std::malloc(len + 1); + std::memcpy(value->data.arr_string.arr[i].str, arr[i].str, len + 1); + } *struct_size = sizeof(NT_Value); return value; } // No need for free as one already exists in the main library -static void CopyNtValue(const struct NT_Value* copy_from, struct NT_Value* copy_to) { +static void CopyNtValue(const struct NT_Value* copy_from, + struct NT_Value* copy_to) { auto cpp_value = nt::ConvertFromC(*copy_from); nt::ConvertToC(*cpp_value, copy_to); } -static void CopyNtString(const struct NT_String* copy_from, struct NT_String* copy_to) { +static void CopyNtString(const struct NT_String* copy_from, + struct NT_String* copy_to) { nt::ConvertToC(llvm::StringRef(copy_from->str, copy_from->len), copy_to); } struct NT_RpcParamDef* NT_GetRpcParamDefForTesting(const char* name, const struct NT_Value* val, int* struct_size) { - struct NT_RpcParamDef* def = static_cast(std::calloc(1, - sizeof(NT_RpcParamDef))); + struct NT_RpcParamDef* def = + static_cast(std::calloc(1, sizeof(NT_RpcParamDef))); nt::ConvertToC(llvm::StringRef(name), &def->name); CopyNtValue(val, &def->def_value); *struct_size = sizeof(NT_RpcParamDef); @@ -182,8 +186,8 @@ void NT_FreeRpcParamDefForTesting(struct NT_RpcParamDef* def) { struct NT_RpcResultDef* NT_GetRpcResultsDefForTesting(const char* name, enum NT_Type type, int* struct_size) { - struct NT_RpcResultDef* def = static_cast(std::calloc(1, - sizeof(NT_RpcResultDef))); + struct NT_RpcResultDef* def = + static_cast(std::calloc(1, sizeof(NT_RpcResultDef))); nt::ConvertToC(llvm::StringRef(name), &def->name); def->type = type; *struct_size = sizeof(NT_RpcResultDef); @@ -195,28 +199,25 @@ void NT_FreeRpcResultsDefForTesting(struct NT_RpcResultDef* def) { std::free(def); } -struct NT_RpcDefinition* NT_GetRpcDefinitionForTesting(unsigned int version, - const char* name, - size_t num_params, - const struct NT_RpcParamDef* params, - size_t num_results, - const struct NT_RpcResultDef* results, - int* struct_size) { - struct NT_RpcDefinition* def = static_cast(std::calloc(1, - sizeof(NT_RpcDefinition))); +struct NT_RpcDefinition* NT_GetRpcDefinitionForTesting( + unsigned int version, const char* name, size_t num_params, + const struct NT_RpcParamDef* params, size_t num_results, + const struct NT_RpcResultDef* results, int* struct_size) { + struct NT_RpcDefinition* def = + static_cast(std::calloc(1, sizeof(NT_RpcDefinition))); def->version = version; nt::ConvertToC(llvm::StringRef(name), &def->name); def->num_params = num_params; - def->params = static_cast(std::malloc(num_params * - sizeof(NT_RpcParamDef))); - for (size_t i=0; iparams = static_cast( + std::malloc(num_params * sizeof(NT_RpcParamDef))); + for (size_t i = 0; i < num_params; ++i) { CopyNtString(¶ms[i].name, &def->params[i].name); CopyNtValue(¶ms[i].def_value, &def->params[i].def_value); } def->num_results = num_results; - def->results = static_cast(std::malloc(num_results * - sizeof(NT_RpcResultDef))); - for (size_t i=0; iresults = static_cast( + std::malloc(num_results * sizeof(NT_RpcResultDef))); + for (size_t i = 0; i < num_results; ++i) { CopyNtString(&results[i].name, &def->results[i].name); def->results[i].type = results[i].type; } @@ -225,14 +226,11 @@ struct NT_RpcDefinition* NT_GetRpcDefinitionForTesting(unsigned int version, } // No need for free as one already exists in the main library -struct NT_RpcCallInfo* NT_GetRpcCallInfoForTesting(unsigned int rpc_id, - unsigned int call_uid, - const char* name, - const char* params, - size_t params_len, - int* struct_size) { - struct NT_RpcCallInfo* info = static_cast(std::calloc(1, - sizeof(NT_RpcCallInfo))); +struct NT_RpcCallInfo* NT_GetRpcCallInfoForTesting( + unsigned int rpc_id, unsigned int call_uid, const char* name, + const char* params, size_t params_len, int* struct_size) { + struct NT_RpcCallInfo* info = + static_cast(std::calloc(1, sizeof(NT_RpcCallInfo))); info->rpc_id = rpc_id; info->call_uid = call_uid; nt::ConvertToC(llvm::StringRef(name), &info->name);