Squashed 'third_party/allwpilib/' changes from 66b57f032..e473a00f9
e473a00f9 [wpiutil] Base64: Add unsigned span/vector variants (#3702)
52f2d580e [wpiutil] raw_uv_ostream: Add reset() (#3701)
d7b1e3576 [wpiutil] WebSocket: move std::function (#3700)
93799fbe9 [examples] Fix description of TrapezoidProfileSubsystem (#3699)
b84644740 [wpimath] Document pose estimator states, inputs, and outputs (#3698)
2dc35c139 [wpimath] Fix classpaths for JNI class loads (#3697)
2cb171f6f [docs] Set Doxygen extract_all to true and fix Doxygen failures (#3695)
a939cd9c8 [wpimath] Print uncontrollable/unobservable models in LQR and KF (#3694)
d5270d113 [wpimath] Clean up C++ StateSpaceUtil tests (#3692)
b20903960 [wpimath] Remove redundant discretization tests from StateSpaceUtilTest (#3689)
c0cb545b4 [wpilibc] Add deprecated Doxygen attribute to SpeedController (#3691)
35c9f66a7 [wpilib] Rename PneumaticsHub to PneumaticHub (#3686)
796d03d10 [wpiutil] Remove unused LLVM header (#3688)
8723caf78 [wpilibj] Make Java TrapezoidProfile.Constraints an immutable class (#3687)
187f50a34 [wpimath] Catch incorrect parameters to state-space models earlier (#3680)
8d04606c4 Replace instances of frc-characterization with SysId (NFC) (#3681)
b82d4f6e5 [hal, cscore, ntcore] Use WPI common handle type base
87e34967e [wpiutil] Add synchronization primitives
e32499c54 [wpiutil] Add ParallelTcpConnector (#3655)
aa0b49228 [wpilib] Remove redundant "quick turn" docs for curvature drive (NFC) (#3674)
57301a7f9 [hal] REVPH: Start closed-loop compressor control on init (#3673)
d1842ea8f [wpilib] Improve interrupt docs (NFC) (#3679)
558151061 [wpiutil] Add DsClient (#3654)
181723e57 Replace `.to<double>()` and `.template to<double>()` with `.value()` (#3667)
6bc1db44b [commands] Add pointer overload of AddRequirements (artf6003) (#3669)
737b57ed5 [wpimath] Update to drake v0.35.0 (#3665)
4d287d1ae [build] Upgrade WPIJREArtifact to JRE 2022-11.0.12u5 (#3666)
f26eb5ada [hal] Fix another typo (get -> gets) (NFC) (#3663)
94ed275ba [hal] Fix misspelling (numer -> number) (NFC) (#3662)
ac2f44da3 [wpiutil] uv: use move for std::function (#3653)
75fa1fbfb [wpiutil] json::serializer: Optimize construction (#3647)
5e689faea [wpiutil] Import MessagePack implementation (mpack) (#3650)
649a50b40 [wpiutil] Add LEB128 byte-by-byte reader (#3652)
e94397a97 [wpiutil] Move json_serializer.h to public headers (#3646)
4ec58724d [wpiutil] uv::Tcp: Clarify SetNoDelay documentation (#3649)
8cb294aa4 [wpiutil] WebSocket: Make Shutdown() public (#3651)
2b3a9a52b [wpiutil] json: Fix map iterator key() for std::string_view (#3645)
138cbb94b [wpiutil] uv::Async: Add direct call for no-parameter specialization (#3648)
e56d6dea8 [ci] Update testbench pool image to ubuntu-latest (#3643)
43f30e44e [build] Enable comments in doxygen source files (#3644)
9e6db17ef [build] Enable doxygen preprocessor expansion of WPI_DEPRECATED (#3642)
0e631ad2f Add WPILib version to issue template (#3641)
6229d8d2f [build] Docs: set case_sense_names to false (#3392)
4647d09b5 [docs] Fix Doxygen warnings, add CI docs lint job (#3639)
4ad3a5402 [hal] Fix PWM allocation channel (#3637)
05e5feac4 [docs] Fix brownout docs (NFC) (#3638)
67df469c5 [examples] Remove old command-based templates and examples (#3263)
689e9ccfb [hal, wpilib] Add brownout voltage configuration (#3632)
9cd4bc407 [docs] Add useLocal to avoid using installer artifacts (#3634)
61996c2bb [cscore] Fix Java direct callback notifications (#3631)
6d3dd99eb [build] Update to newest native-utils (#3633)
f0b484892 [wpiutil] Fix StringMap iterator equality check (#3629)
8352cbb7a Update development build instructions for 2022 (#3616)
6da08b71d [examples] Fix Intermediate Vision Java Example description (#3628)
5d99059bf [wpiutil] Remove optional.h (#3627)
fa41b106a [glass, wpiutil] Add missing format args (#3626)
4e3fd7d42 [build] Enable Zc:__cplusplus for Windows (#3625)
791d8354d [build] Suppress deprecation/removal warnings for old commands (#3618)
10f19e6fc [hal, wpilib] Add REV PneumaticsHub (#3600)
4c61a1305 [ntcore] Revert to per-element copy for toNative() (#3621)
7b3f62244 [wpiutil] SendableRegistry: Print exception stacktrace (#3620)
d347928e4 [hal] Use better error for when console out is enabled while attempting to use onboard serial port (#3622)
cc31079a1 [hal] Use setcap instead of setuid for setting thread priorities (#3613)
4676648b7 [wpimath] Upgrade to Drake v0.34.0 (#3607)
c7594c911 [build] Allow building wpilibc in cmake without cscore and opencv (#3605)
173cb7359 [wpilib] Add TimesliceRobot (#3502)
af295879f [hal] Set error status for I2C port out of range (#3603)
95dd20a15 [build] Enable spotbugs (#3601)
b65fce86b [wpilib] Remove Timer lock in wpilibj and update docs (#3602)
3b8d3bbcb Remove unused and add missing deprecated.h includes (#3599)
f9e976467 [examples] Rename DriveTrain classes to Drivetrain (#3594)
118a27be2 [wpilib] Add Timer tests (#3595)
59c89428e [wpilib] Deprecate Timer::HasPeriodPassed() (#3598)
202ca5e78 Force C++17 in .clang-format (#3597)
d6f185d8e Rename tests for consistency (#3592)
54ca474db [ci] Enable asan and tsan in CI for tests that pass (#3591)
1ca383b23 Add Debouncer (#3590)
179fde3a7 [build] Update to 2022 native utils and gradle 7 (#3588)
50198ffcf [examples] Add Mechanism2d visualization to Elevator Sim (#3587)
a446c2559 [examples] Synchronize C++ and Java Mechanism2d examples (#3589)
a7fb83103 [ci] clang-tidy: Generate compilation commands DB with Gradle (#3585)
4f5e0c9f8 [examples] Update ArmSimulation example to use Mechanism2d (#3572)
8164b91dc [CI] Print CMake test output on failure (#3583)
4d5fca27e [wpilib] Impove Mechanism2D documentation (NFC) (#3584)
fe59e4b9f Make C++ test names more consistent (#3586)
5c8868549 [wpilibc] Fix C++ MechanisimRoot2D to use same NT entries as Java/Glass (#3582)
9359431ba [wpimath] Clean up Eigen usage
72716f51c [wpimath] Upgrade to Eigen 3.4
382deef75 [wpimath] Explicitly export wpimath symbols
161e21173 [ntcore] Match standard handle layout, only allow 16 instances (#3577)
263a24811 [wpimath] Use jinja for codegen (#3574)
725251d29 [wpilib] Increase tolerances of DifferentialDriveSimTest (#3581)
4dff87301 [wpimath] Make LinearFilter::Factorial() constexpr (#3579)
60ede67ab [hal, wpilib] Switch PCM to be a single object that is allowed to be duplicated (#3475)
906bfc846 [build] Add CMake build support for sanitizers (#3576)
0d4f08ad9 [hal] Simplify string copy of joystick name (#3575)
a52bf87b7 [wpiutil] Add Java function package (#3570)
40c7645d6 [wpiutil] UidVector: Return old object from erase() (#3571)
5b886a23f [wpiutil] jni_util: Add size, operator[] to JArrayRef (#3569)
65797caa7 [sim] Fix halsim_ds_socket stringop overflow warning from GCC 10 (#3568)
66abb3988 [hal] Update runtime enum to allow selecting roborio 2 (#3565)
95a12e0ee [hal] UidSetter: Don't revert euid if its already current value (#3566)
27951442b [wpimath] Use external Eigen headers only (#3564)
c42e053ae [docs] Update to doxygen 1.9.2 (#3562)
e7048c8c8 [docs] Disable doxygen linking for common words that are also classes (#3563)
d8e0b6c97 [wpilibj] Fix java async interrupts (#3559)
5e6c34c61 Update to 2022 roborio image (#3537)
828f073eb [wpiutil] Fix uv::Buffer memory leaks caught by asan (#3555)
2dd5701ac [cscore] Fix mutex use-after-free in cscore test (#3557)
531439198 [ntcore] Fix NetworkTables memory leaks caught by asan (#3556)
3d9a4d585 [wpilibc] Fix AnalogTriggerOutput memory leak reported by asan (#3554)
54eda5928 [wpiutil] Ignore ubsan vptr upcast warning in SendableHelper moves (#3553)
5a4f75c9f [wpilib] Replace Speed controller comments with motor controller (NFC) (#3551)
7810f665f [wpiutil] Fix bug in uleb128 (#3540)
697e2dd33 [wpilib] Fix errant jaguar reference in comments (NFC) (#3550)
936c64ff5 [docs] Enable -linksource for javadocs (#3549)
1ea654954 [build] Upgrade CMake build to googletest 1.11.0 (#3548)
32d9949e4 [wpimath] Move controller tests to wpimath (#3541)
01ba56a8a [hal] Replace strncpy with memcpy (#3539)
e109c4251 [build] Rename makeSim flag to forceGazebo to better describe what it does (#3535)
e4c709164 [docs] Use a doxygen theme and add logo (#3533)
960b6e589 [wpimath] Fix Javadoc warning (#3532)
82eef8d5e [hal] Remove over current fault HAL functions from REV PDH (#3526)
aa3848b2c [wpimath] Move RobotDriveBase::ApplyDeadband() to MathUtil (#3529)
3b5d0d141 [wpimath] Add LinearFilter::BackwardFiniteDifference() (#3528)
c8fc715fe [wpimath] Upgrade drake files to v0.33.0 (#3531)
e5fe3a8e1 [build] Treat javadoc warnings as errors in CI and fix warnings (#3530)
e0c6cd3dc [wpimath] Add an operator for composing two Transform2ds (#3527)
2edd510ab [sim] Add sim wrappers for sensors that use SimDevice (#3517)
2b3e2ebc1 [hal] Fix HAL Notifier thread priority setting (#3522)
ab4cb5932 [gitignore] Update gitignore to ignore bazel / clion files (#3524)
57c8615af [build] Generate spotless patch on failure (#3523)
b90317321 Replace std::cout and std::cerr with fmt::print() (#3519)
10cc8b89c [hal] [wpilib] Add initial support for the REV PDH (#3503)
5d9ae3cdb [hal] Set HAL Notifier thread as RT by default (#3482)
192d251ee [wpilibcIntegrationTests] Properly disable DMA integration tests (#3514)
031962608 [wpilib] Add PS4Controller, remove Hand from GenericHID/XboxController (#3345)
25f6f478a [wpilib] Rename DriverStation::IsOperatorControl() to IsTeleop() (#3505)
e80f09f84 [wpilibj] Add unit tests (#3501)
c159f91f0 [wpilib] Only read DS control word once in IterativeRobotBase (#3504)
eb790a74d Add rio development docs documenting myRobot deploy tasks (#3508)
e47451f5a [wpimath] Replace auto with Eigen types (#3511)
252b8c83b Remove Java formatting from build task in CI (#3507)
09666ff29 Shorten Gazebo CI build (#3506)
baf2e501d Update myRobot to use 2021 java (#3509)
5ac60f0a2 [wpilib] Remove IterativeRobotBase mode init prints (#3500)
fb2ee8ec3 [wpilib] Add TimedRobot functions for running code on mode exit (#3499)
94e0db796 [wpilibc] Add more unit tests (#3494)
b25324695 [wpilibj] Add units to parameter names (NFC) (#3497)
1ac73a247 [hal] Rename PowerDistributionPanel to PowerDistribution (#3466)
2014115bc [examples] frisbeebot: Fix typo and reflow comments (NFC) (#3498)
4a944dc39 [examples] Consistently use 0 for controller port (#3496)
3838cc4ec Use unicode characters in docs equations (#3487)
85748f2e6 [examples] Add C++ TankDrive example (#3493)
d7b8aa56d [wpilibj] Rename DriverStation In[Mode] functions to follow style guide (#3488)
16e096cf8 [build] Fix CMake Windows CI (#3490)
50af74c38 [wpimath] Clean up NumericalIntegration and add Discretization tests (#3489)
bfc209b12 Automate fmt update (#3486)
e7f9331e4 [build] Update to Doxygen 1.9.1 (#3008)
ab8e8aa2a [wpimath] Update drake with upstream (#3484)
1ef826d1d [wpimath] Fix IOException path in WPIMath JNI (#3485)
52bddaa97 [wpimath] Disable iostream support for units and enable fmtlib (#3481)
e4dc3908b [wpiutil] Upgrade to fmtlib 8.0.1 (#3483)
1daadb812 [wpimath] Implement Dormand-Prince integration method (#3476)
9c2723391 [cscore] Add [[nodiscard]] to GrabFrame functions (#3479)
7a8796414 [wpilib] Add Notifier integration tests (#3480)
f8f13c536 [wpilibcExamples] Prefix decimal numbers with 0 (#3478)
1adb69c0f [ntcore] Use "NetworkTables" instead of "Network Tables" in NT specs (#3477)
5f5830b96 Upload wpiformat diff if one exists (#3474)
9fb4f35bb [wpimath] Add tests for DARE overload with Q, R, and N matrices (#3472)
c002e6f92 Run wpiformat (#3473)
c154e5262 [wpilib] Make solenoids exclusive use, PCM act like old sendable compressor (#3464)
6ddef1cca [hal] JNI setDIO: use a boolean and not a short (#3469)
9d68d9582 Remove extra newlines after open curly braces (NFC) (#3471)
a4233e1a1 [wpimath] Add script for updating Drake (#3470)
39373c6d2 Update README.md for new GCC version requirement (#3467)
d29acc90a [wpigui] Add option to reset UI on exit (#3463)
a371235b0 [ntcore] Fix dangling pointer in logger (#3465)
53b4891a5 [wpilibcintegrationtests] Fix deprecated Preferences usage (#3461)
646ded912 [wpimath] Remove incorrect discretization in pose estimators (#3460)
ea0b8f48e Fix some deprecation warnings due to fmtlib upgrade (#3459)
2067d7e30 [wpilibjexamples] Add wpimathjni, wpiutiljni to library path (#3455)
866571ab4 [wpiutil] Upgrade to fmtlib 8.0.0 (#3457)
4e1fa0308 [build] Skip PDB copy on windows build servers (#3458)
b45572167 [build] Change CI back to 18.04 docker images (#3456)
57a160f1b [wpilibc] Fix LiveWindow deprecation warning in RobotBase skeleton template (#3454)
29ae8640d [HLT] Implement duty cycle cross connect tests (#3453)
ee6377e54 [HLT] Add relay and analog cross connects (#3452)
b0f1ae7ea [build] CMake: Build the HAL even if WITH_CSCORE=OFF (#3449)
7aae2b72d Replace std::to_string() with fmt::format() (#3451)
73fcbbd74 [HLT] Add relay digital cross connect tests (#3450)
e7bedde83 [HLT] Add PWM tests that use DMA as the back end (#3447)
7253edb1e [wpilibc] Timer: Fix deprecated warning (#3446)
efa28125c [wpilibc] Add message to RobotBase on how to read stacktrace (#3444)
9832fcfe1 [hal] Fix DIO direction getter (#3445)
49c71f9f2 [wpilibj] Clarify robot quit message (#3364)
791770cf6 [wpimath] Move controller from wpilibj to wpimath (#3439)
9ce9188ff [wpimath] Add ReportWarning to MathShared (#3441)
362066a9b [wpilib] Deprecate getInstance() in favor of static functions (#3440)
26ff9371d Initial commit of cross connect integration test project (#3434)
4a36f86c8 [hal] Add support for DMA to Java (#3158)
85144e47f [commands] Unbreak build (#3438)
b417d961e Split Sendable into NT and non-NT portions (#3432)
ef4ea84cb [commands] Change grouping decorator impl to flatten nested group structures (#3335)
b422665a3 [examples] Invert right side of drive subsystems (#3437)
186dadf14 [hal] Error if attempting to set DIO output on an input port (#3436)
04e64db94 Remove redundant C++ lambda parentheses (NFC) (#3433)
f60994ad2 [wpiutil] Rename Java package to edu.wpi.first.util (#3431)
cfa1ca96f [wpilibc] Make ShuffleboardValue non-copyable (#3430)
4d9ff7643 Fix documentation warnings generated by JavaDoc (NFC) (#3428)
9e1b7e046 [build] Fix clang-tidy and clang-format (#3429)
a77c6ff3a [build] Upgrade clang-format and clang-tidy (NFC) (#3422)
099fde97d [wpilib] Improve PDP comments (NFC) (#3427)
f8fc2463e [wpilibc, wpiutil] Clean up includes (NFC) (#3426)
e246b7884 [wpimath] Clean up member initialization in feedforward classes (#3425)
c1e128bd5 Disable frivolous PMD warnings and enable PMD in ntcore (#3419)
8284075ee Run "Lint and Format" CI job on push as well as pull request (#3412)
f7db09a12 [wpimath] Move C++ filters into filter folder to match Java (#3417)
f9c3d54bd [wpimath] Reset error covariance in pose estimator ResetPosition() (#3418)
0773f4033 [hal] Ensure HAL status variables are initialized to zero (#3421)
d068fb321 [build] Upgrade CI to use 20.04 docker images (#3420)
8d054c940 [wpiutil] Remove STLExtras.h
80f1d7921 [wpiutil] Split function_ref to a separate header
64f541325 Use wpi::span instead of wpi::ArrayRef across all libraries (#3414)
2abbbd9e7 [build] clang-tidy: Remove bugprone-exception-escape (#3415)
a5c471af7 [wpimath] Add LQR template specialization for 2x2 system
edd2f0232 [wpimath] Add DARE solver for Q, R, and N with LQR ctor overloads
b2c3b2dd8 Use std::string_view and fmtlib across all libraries (#3402)
4f1cecb8e [wpiutil] Remove Path.h (#3413)
b336eac34 [build] Publish halsim_ws_core to Maven
2a09f6fa4 [build] Also build sim modules as static libraries
0e702eb79 [hal] Add a unified PCM object (#3331)
dea841103 [wpimath] Add fmtlib formatter overloads for Eigen::Matrix and units (#3409)
82856cf81 [wpiutil] Improve wpi::circular_buffer iterators (#3410)
8aecda03e [wpilib] Fix a documentation typo (#3408)
5c817082a [wpilib] Remove InterruptableSensorBase and replace with interrupt classes (#2410)
15c521a7f [wpimath] Fix drivetrain system identification (#3406)
989de4a1b [build] Force all linker warnings to be fatal for rio builds (#3407)
d9eeb45b0 [wpilibc] Add units to Ultrasonic class API (#3403)
fe570e000 [wpiutil] Replace llvm filesystem with C++17 filesystem (#3401)
01dc0249d [wpimath] Move SlewRateLimiter from wpilib to wpimath (#3399)
93523d572 [wpilibc] Clean up integration tests (#3400)
4f7a4464d [wpiutil] Rewrite StringExtras for std::string_view (#3394)
e09293a15 [wpilibc] Transition C++ classes to units::second_t (#3396)
827b17a52 [build] Create run tasks for Glass and OutlineViewer (#3397)
a61037996 [wpiutil] Avoid MSVC warning on span include (#3393)
4e2c3051b [wpilibc] Use std::string_view instead of Twine (#3380)
50915cb7e [wpilibc] MotorSafety::GetDescription(): Return std::string (#3390)
f4e2d26d5 [wpilibc] Move NullDeleter from frc/Base.h to wpi/NullDeleter.h (#3387)
cb0051ae6 [wpilibc] SimDeviceSim: use fmtlib (#3389)
a238cec12 [wpiutil] Deprecate wpi::math constants in favor of wpi::numbers (#3383)
393bf23c0 [ntcore, cscore, wpiutil] Standardize template impl files on .inc extension (NFC) (#3124)
e7d9ba135 [sim] Disable flaky web server integration tests (#3388)
0a0003c11 [wpilibjExamples] Fix name of Java swerve drive pose estimator example (#3382)
7e1b27554 [wpilibc] Use default copies and moves when possible (#3381)
fb2a56e2d [wpilibc] Remove START_ROBOT_CLASS macro (#3384)
84218bfb4 [wpilibc] Remove frc namespace shim (#3385)
dd7824340 [wpilibc] Remove C++ compiler version static asserts (#3386)
484cf9c0e [wpimath] Suppress the -Wmaybe-uninitialized warning in Eigen (#3378)
a04d1b4f9 [wpilibc] DriverStation: Remove ReportError and ReportWarning
831c10bdf [wpilibc] Errors: Use fmtlib
87603e400 [wpiutil] Import fmtlib (#3375)
442621672 [wpiutil] Add ArrayRef/std::span/wpi::span implicit conversions
bc15b953b [wpiutil] Add std::span implementation
6d20b1204 [wpiutil] StringRef, Twine, raw_ostream: Add std::string_view support (#3373)
2385c2a43 [wpilibc] Remove Utility.h (#3376)
87384ea68 [wpilib] Fix PIDController continuous range error calculations (#3170)
04dae799a [wpimath] Add SimpleMotorFeedforward::Calculate(velocity, nextVelocity) overload (#3183)
0768c3903 [wpilib] DifferentialDrive: Remove right side inversion (#3340)
8dd8d4d2d [wpimath] Fix redundant nested math package introduced by #3316 (#3368)
49b06beed [examples] Add Field2d to RamseteController example (#3371)
4c562a445 [wpimath] Fix typo in comment of update_eigen.py (#3369)
fdbbf1188 [wpimath] Add script for updating Eigen
f1e64b349 [wpimath] Move Eigen unsupported folder into eigeninclude
224f3a05c [sim] Fix build error when building with GCC 11.1 (#3361)
ff56d6861 [wpilibj] Fix SpeedController deprecated warnings (#3360)
1873fbefb [examples] Fix Swerve and Mecanum examples (#3359)
80b479e50 [examples] Fix SwerveBot example to use unique encoder ports (#3358)
1f7c9adee [wpilibjExamples] Fix pose estimator examples (#3356)
9ebc3b058 [outlineviewer] Change default size to 600x400 (#3353)
e21b443a4 [build] Gradle: Make C++ examples runnable (#3348)
da590120c [wpilibj] Add MotorController.setVoltage default (#3347)
561d53885 [build] Update opencv to 4.5.2, imgui/implot to latest (#3344)
44ad67ca8 [wpilibj] Preferences: Add missing Deprecated annotation (#3343)
3fe8fc75a [wpilibc] Revert "Return reference from GetInstance" (#3342)
3cc2da332 Merge branch '2022'
a3cd90dd7 [wpimath] Fix classpath used by generate_numbers.py (#3339)
d6cfdd3ba [wpilib] Preferences: Deprecate Put* in favor of Set* (#3337)
ba08baabb [wpimath] Update Drake DARE solver to v0.29.0 (#3336)
497b712f6 [wpilib] Make IterativeRobotBase::m_period private with getter
f00dfed7a [wpilib] Remove IterativeRobot base class
3c0846168 [hal] Use last error reporting instead of PARAMETER_OUT_OF_RANGE (#3328)
5ef2b4fdc [wpilibj] Fix @deprecated warning for SerialPort constructor (#3329)
23d2326d1 [hal] Report previous allocation location for indexed resource duplicates (#3322)
e338f9f19 [build] Fix wpilibc runCpp task (#3327)
c8ff626fe [wpimath] Move Java classes to edu.wpi.first.math (#3316)
4e424d51f [wpilibj] DifferentialDrivetrainSim: Rename constants to match the style guide (#3312)
6b50323b0 [cscore] Use Lock2DSize if possible for Windows USB cameras (#3326)
65c148536 [wpilibc] Fix "control reaches end of non-void function" warning (#3324)
f99f62bee [wpiutil] uv Handle: Use malloc/free instead of new/delete (#3325)
365f5449c [wpimath] Fix MecanumDriveKinematics (#3266)
ff52f207c [glass, wpilib] Rewrite Mechanism2d (#3281)
ee0eed143 [wpimath] Add DCMotor factory function for Romi motors (#3319)
512738072 [hal] Add HAL_GetLastError to enable better error messages from HAL calls (#3320)
ced654880 [glass, outlineviewer] Update Mac icons to macOS 11 style (#3313)
936d3b9f8 [templates] Add Java template for educational robot (#3309)
6e31230ad [examples] Fix odometry update in SwerveControllerCommand example (#3310)
05ebe9318 Merge branch 'main' into 2022
aaf24e255 [wpilib] Fix initial heading behavior in HolonomicDriveController (#3290)
8d961dfd2 [wpilibc] Remove ErrorBase (#3306)
659b37ef9 [wpiutil] StackTrace: Include offset on Linux (#3305)
0abf6c904 [wpilib] Move motor controllers to motorcontrol package (#3302)
4630191fa [wpiutil] circular_buffer: Use value initialization instead of passing zero (#3303)
b7b178f49 [wpilib] Remove Potentiometer interface
687066af3 [wpilib] Remove GyroBase
6b168ab0c [wpilib] Remove PIDController, PIDOutput, PIDSource
948625de9 [wpimath] Document conversion from filter cutoff frequency to time constant (#3299)
3848eb8b1 [wpilibc] Fix flywhel -> flywheel typo in FlywheelSim (#3298)
3abe0b9d4 [cscore] Move java package to edu.wpi.first.cscore (#3294)
d7fabe81f [wpilib] Remove RobotDrive (#3295)
1dc81669c [wpilib] Remove GearTooth (#3293)
01d0e1260 [wpilib] Revert move of RomiGyro into main wpilibc/j (#3296)
397e569aa [ntcore] Remove "using wpi" from nt namespace
79267f9e6 [ntcore] Remove NetworkTable -> nt::NetworkTable shim
48ebe5736 [ntcore] Remove deprecated Java interfaces and classes
c2064c78b [ntcore] Remove deprecated ITable interfaces
36608a283 [ntcore] Remove deprecated C++ APIs
a1c87e1e1 [glass] LogView: Add "copy to clipboard" button (#3274)
fa7240a50 [wpimath] Fix typo in quintic spline basis matrix
ffb4d38e2 [wpimath] Add derivation for spline basis matrices
f57c188f2 [wpilib] Add AnalogEncoder(int) ctor (#3273)
8471c4fb2 [wpilib] FieldObject2d: Add setTrajectory() method (#3277)
c97acd18e [glass] Field2d enhancements (#3234)
ffb590bfc [wpilib] Fix Compressor sendable properties (#3269)
6137f98eb [hal] Rename SimValueCallback2 to SimValueCallback (#3212)
a6f653969 [hal] Move registerSimPeriodic functions to HAL package (#3211)
10c038d9b [glass] Plot: Fix window creation after removal (#3264)
2d2eaa3ef [wpigui] Ensure window will be initially visible (#3256)
4d28b1f0c [wpimath] Use JNI for trajectory serialization (#3257)
3de800a60 [wpimath] TrajectoryUtil.h: Comment formatting (NFC) (#3262)
eff592377 [glass] Plot: Don't overwrite series ID (#3260)
a79faace1 [wpilibc] Return reference from GetInstance (#3247)
9550777b9 [wpilib] PWMSpeedController: Use PWM by composition (#3248)
c8521a3c3 [glass] Plot: Set reasonable default window size (#3261)
d71eb2cf3 [glass] Plot: Show full source name as tooltip and in popup (#3255)
160fb740f [hal] Use std::lround() instead of adding 0.5 and truncating (#3012)
48e9f3951 [wpilibj] Remove wpilibj package CameraServer (#3213)
8afa596fd [wpilib] Remove deprecated Sendable functions and SendableBase (#3210)
d3e45c297 [wpimath] Make C++ geometry classes immutable (#3249)
2c98939c1 [glass] StringChooser: Don't call SameLine() at end
a18a7409f [glass] NTStringChooser: Clear value of deleted entries
2f19cf452 [glass] NetworkTablesHelper: listen to delete events
da96707dc Merge branch 'main' into 2022
c3a8bdc24 [build] Fix clang-tidy action (#3246)
21624ef27 Add ImGui OutlineViewer (#3220)
1032c9b91 [wpiutil] Unbreak wpi::Format on Windows (#3242)
2e07902d7 [glass] NTField2D: Fix name lookup (#3233)
6e23e1840 [wpilibc] Remove WPILib.h (#3235)
3e22e4506 [wpilib] Make KoP drivetrain simulation weight 60 lbs (#3228)
79d1bd6c8 [glass] NetworkTablesSetting: Allow disable of server option (#3227)
fe341a16f [examples] Use more logical elevator setpoints in GearsBot (#3198)
62abf46b3 [glass] NetworkTablesSettings: Don't block GUI (#3226)
a95a5e0d9 [glass] Move NetworkTablesSettings to libglassnt (#3224)
d6f6ceaba [build] Run Spotless formatter (NFC) (#3221)
0922f8af5 [commands] CommandScheduler.requiring(): Note return can be null (NFC) (#2934)
6812302ff [examples] Make DriveDistanceOffboard example work in sim (#3199)
f3f86b8e7 [wpimath] Add pose estimator overload for vision + std dev measurement (#3200)
1a2680b9e [wpilibj] Change CommandBase.withName() to return CommandBase (#3209)
435bbb6a8 [command] RamseteCommand: Output 0 if interrupted (#3216)
3cf44e0a5 [hal] Add function for changing HAL Notifier thread priority (#3218)
40b367513 [wpimath] Units.java: Add kg-lb conversions (#3203)
9f563d584 [glass] NT: Fix return value in StringToDoubleArray (#3208)
af4adf537 [glass] Auto-size plots to fit window (#3193)
2560146da [sim] GUI: Add option to show prefix in Other Devices (#3186)
eae3a6397 gitignore: Ignore .cache directory (#3196)
959611420 [wpilib] Require non-zero positive value for PIDController.period (#3175)
9522f2e8c [wpimath] Add methods to concatenate trajectories (#3139)
e42a0b6cf [wpimath] Rotation2d comment formatting (NFC) (#3162)
d1c7032de [wpimath] Fix order of setting gyro offset in pose estimators (#3176)
d241bc81a [sim] Add DoubleSolenoidSim and SolenoidSim classes (#3177)
cb7f39afa [wpilibc] Add RobotController::GetBatteryVoltage() to C++ (#3179)
99b5ad9eb [wpilibj] Fix warnings that are not unused variables or deprecation (#3161)
c14b23775 [build] Fixup doxygen generated include dirs to match what users would need (#3154)
d447c7dc3 [sim] Add SimDeviceSim ctor overloads (#3134)
247420c9c [build] Remove jcenter repo (#3157)
04b112e00 [build] Include debug info in plugin published artifacts (#3149)
be0ce9900 [examples] Use PWMSparkMax instead of PWMVictorSPX (#3156)
69e8d0b65 [wpilib] Move RomiGyro into main wpilibc/j (#3143)
94e685e1b [wpimath] Add custom residual support to EKF (#3148)
5899f3dd2 [sim] GUI: Make keyboard settings loading more robust (#3167)
f82aa1d56 [wpilib] Fix HolonomicDriveController atReference() behavior (#3163)
fe5c2cf4b [wpimath] Remove ControllerUtil.java (#3169)
43d40c6e9 [wpiutil] Suppress unchecked cast in CombinedRuntimeLoader (#3155)
3d44d8f79 [wpimath] Fix argument order in UKF docs (NFC) (#3147)
ba6fe8ff2 [cscore] Add USB camera change event (#3123)
533725888 [build] Tweak OpenCV cmake search paths to work better on Linux (#3144)
29bf9d6ef [cscore] Add polled support to listener
483beb636 [ntcore] Move CallbackManager to wpiutil
fdaec7759 [examples] Instantiate m_ramseteController in example (#3142)
8494a5761 Rename default branch to main (#3140)
45590eea2 [wpigui] Hardcode window scale to 1 on macOS (#3135)
834a64920 [build] Publish libglass and libglassnt to Maven (#3127)
2c2ccb361 [wpimath] Fix Rotation2d equality operator (#3128)
fb5c8c39a [wpigui] clang-tidy: readability-braces-around-statements
f7d39193a [wpigui] Fix copyright in pfd and wpigui_metal.mm
aec796b21 [ntcore] Fix conditional jump on uninitialized value (#3125)
fb13bb239 [sim] GUI: Add right click popup for keyboard joystick settings (#3119)
c517ec677 [build] Update thirdparty-imgui to 1.79-2 (#3118)
e8cbf2a71 [wpimath] Fix typo in SwerveDrivePoseEstimator doc (NFC) (#3112)
e9c86df46 [wpimath] Add tests for swerve module optimization (#3100)
6ba8c289c [examples] Remove negative of ArcadeDrive(fwd, ..) in the C++ Getting Started Example (#3102)
3f1672e89 [hal] Add SimDevice createInt() and createLong() (#3110)
15be5cbf1 [examples] Fix segfault in GearsBot C++ example (#3111)
4cf0e5e6d Add quick links to API documentation in README (#3082)
6b1898f12 Fix RT priority docs (NFC) (#3098)
b3426e9c0 [wpimath] Fix missing whitespace in pose estimator doc (#3097)
38c1a1f3e [examples] Fix feildRelative -> fieldRelative typo in XControllerCommand examples (#3104)
4488e25f1 [glass] Shorten SmartDashboard window names (#3096)
cfdb3058e [wpilibj] Update SimDeviceSimTest (#3095)
64adff5fe [examples] Fix typo in ArcadeDrive constructor parameter name (#3092)
6efc58e3d [build] Fix issues with build on windows, deprecations, and native utils (#3090)
f393989a5 [wpimath, wpiutil] Add wpi::array for compile time size checking (#3087)
d6ed20c1e [build] Set macOS deployment target to 10.14 (#3088)
7c524014c [hal] Add [[nodiscard]] to HAL_WaitForNotifierAlarm() (#3085)
406d055f0 [wpilib] Fixup wouldHitLowerLimit in elevator and arm simulation classes. (#3076)
04a90b5dd [examples] Don't continually set setpoint in PotentiometerPID Examples (#3084)
8c5bfa013 [sim] GUI: Add max value setting for keyboard joysticks (#3083)
bc80c5535 [hal] Add SimValue reset() function (#3064)
9c3b51ca0 [wpilib] Document simulation APIs (#3079)
26584ff14 [wpimath] Add model description to LinearSystemId Javadocs (#3080)
42c3d5286 [examples] Sync Java and C++ trajectories in sim example (#3081)
64e72f710 [wpilibc] Add missing function RoboRioSim::ResetData (#3073)
e95503798 [wpimath] Add optimize() to SwerveModuleState (#3065)
fb99910c2 [hal] Add SimInt and SimLong wrappers for int/long SimValue (#3066)
e620bd4d3 [doc] Add machine-readable websocket specification (#3059)
a44e761d9 [glass] Add support for plot Y axis labels
ea1974d57 [wpigui] Update imgui and implot to latest
85a0bd43c [wpimath] Add RKF45 integration (#3047)
278e0f126 [glass] Use .controllable to set widgets' read-only state (#3035)
d8652cfd4 [wpimath] Make Java DCMotor API consistent with C++ and fix motor calcs (#3046)
377b7065a [build] Add toggleOffOn to Java spotless (#3053)
1e9c79c58 [sim] Use plant output to retrieve simulated position (#3043)
78147aa34 [sim] GUI: Fix Keyboard Joystick (#3052)
cd4a2265b [ntcore] Fix NetworkTableEntry::GetRaw() (#3051)
767ac1de1 [build] Use deploy key for doc publish (#3048)
d762215d1 [build] Add publish documentation script (#3040)
1fd09593c [examples] Add missing TestInit method to GettingStarted Example (#3039)
e45a0f6ce [examples] Add RomiGyro to the Romi Reference example (#3037)
94f852572 Update imaging link and fix typo (#3038)
d73cf64e5 [examples] Update RomiReference to match motor directions (#3036)
f945462ba Bump copyright year to 2021 (#3033)
b05946175 [wpimath] Catch Drake JNI exceptions and rethrow them (#3032)
62f0f8190 [wpimath] Deduplicate angle modulus functions (#2998)
bf8c0da4b [glass] Add "About" popup with version number (#3031)
dfdd6b389 [build] Increase Gradle heap size in Gazebo build (#3028)
f5e0fc3e9 Finish clang-tidy cleanups (#3003)
d741101fe [sim] Revert accidental commit of WSProvider_PDP.h (#3027)
e1620799c [examples] Add C++ RomiReference example (#2969)
749c7adb1 [command] Fix use-after-free in CommandScheduler (#3024)
921a73391 [sim] Add WS providers for AddressableLED, PCM, and Solenoid (#3026)
26d0004fe [build] Split Actions into different yml files (#3025)
948af6d5b [wpilib] PWMSpeedController.get(): Apply Inversion (#3016)
670a187a3 [wpilibc] SuppliedValueWidget.h: Forward declare ShuffleboardContainer (#3021)
be9f72502 [ntcore] NetworkTableValue: Use std::forward instead of std::move (#3022)
daf3f4cb1 [cscore] cscore_raw_cv.h: Fix error in PutFrame() (#3019)
5acda4cc7 [wpimath] ElevatorFeedforward.h: Add time.h include
8452af606 [wpimath] units/radiation.h: Add mass.h include
630d44952 [hal] ErrorsInternal.h: Add stdint.h include
7372cf7d9 [cscore] Windows NetworkUtil.cpp: Add missing include
b7e46c558 Include .h from .inc/.inl files (NFC) (#3017)
bf8f8710e [examples] Update Romi template and example (#2996)
6ffe5b775 [glass] Ensure NetworkTableTree parent context menu has an id (#3015)
be0805b85 [build] Update to WPILibVersioningPlugin 4.1.0 (#3014)
65b2359b2 [build] Add spotless for other files (#3007)
8651aa73e [examples] Enable NT Flush in Field2d examples (#3013)
78b542737 [build] Add Gazebo build to Actions CI (#3004)
fccf86532 [sim] DriverStationGui: Fix two bugs (#3010)
185741760 [sim] WSProvider_Joystick: Fix off-by-1 in incoming buttons (#3011)
ee7114a58 [glass] Add drive class widgets (#2975)
00fa91d0d [glass] Use ImGui style for gyro widget colors (#3009)
b7a25bfc3 ThirdPartyNotices: Add portable file dialogs license (#3005)
a2e46b9a1 [glass] modernize-use-nullptr (NFC) (#3006)
a751fa22d [build] Apply spotless for java formatting (#1768)
e563a0b7d [wpimath] Make LinearSystemLoop move-constructible and move-assignable (#2967)
49085ca94 [glass] Add context menus to remove and add NetworkTables values (#2979)
560a850a2 [glass] Add NetworkTables Log window (#2997)
66782e231 [sim] Create Left/Right drivetrain current accessors (#3001)
b60eb1544 clang-tidy: bugprone-virtual-near-miss
cbe59fa3b clang-tidy: google-explicit-constructor
c97c6dc06 clang-tidy: google-readability-casting (NFC)
32fa97d68 clang-tidy: modernize-use-nullptr (NFC)
aee460326 clang-tidy: modernize-pass-by-value
29c7da5f1 clang-tidy: modernize-make-unique
6131f4e32 clang-tidy: modernize-concat-nested-namespaces (NFC)
67e03e625 clang-tidy: modernize-use-equals-default
b124f9101 clang-tidy: modernize-use-default-member-init
d11a3a638 clang-tidy: modernize-use-override (NFC)
4cc0706b0 clang-tidy: modernize-use-using (NFC)
885f5a978 [wpilibc] Speed up ScopedTracerTest (#2999)
60b596457 [wpilibj] Fix typos (NFC) (#3000)
6e1919414 [build] Bring naming checkstyle rules up to date with Google Style guide (#1781)
8c8ec5e63 [wpilibj] Suppress unchecked cast warnings (#2995)
b8413ddd5 [wpiutil] Add noexcept to timestamp static functions (#2994)
5d976b6e1 [glass] Load NetworkTableView settings on first draw (#2993)
2b4317452 Replace NOLINT(runtime/explicit) comments with NOLINT (NFC) (#2992)
1c3011ba4 [glass] Fix handling of "/" NetworkTables key (#2991)
574a42f3b [hal] Fix UnsafeManipulateDIO status check (#2987)
9005cd59e [wpilib] Clamp input voltage in sim classes (#2955)
dd494d4ab [glass] NetworkTablesModel::Update(): Avoid use-after-move (#2988)
7cca469a1 [wpimath] NormalizeAngle: Make inline, remove unnamed namespace (#2986)
2aed432b4 Add braces to C++ single-line loops and conditionals (NFC) (#2973)
0291a3ff5 [wpiutil] StringRef: Add noexcept to several constructors (#2984)
5d7315280 [wpimath] Update UnitsTest.cpp copyright (#2985)
254931b9a [wpimath] Remove LinearSystem from LinearSystemLoop (#2968)
aa89744c9 Update OtherVersions.md to include wpimath info (#2983)
1cda3f5ad [glass] Fix styleguide (#2976)
8f1f64ffb Remove year from file copyright message (NFC) (#2972)
2bc0a7795 [examples] Fix wpiformat warning about utility include (#2971)
4204da6ad [glass] Add application icon
7ac39b10f [wpigui] Add icon support
6b567e006 [wpimath] Add support for varying vision standard deviations in pose estimators (#2956)
df299d6ed [wpimath] Add UnscentedKalmanFilter::Correct() overload (#2966)
4e34f0523 [examples] Use ADXRS450_GyroSim class in simulation example (#2964)
9962f6fd7 [wpilib] Give Field2d a default Sendable name (#2953)
f9d492f4b [sim] GUI: Show "Other Devices" window by default (#2961)
a8bb2ef1c [sim] Fix ADXRS450_GyroSim and DutyCycleEncoderSim (#2963)
240c629cd [sim] Try to guess "Map Gamepad" setting (#2960)
952567dd3 [wpilibc] Add missing move constructors and assignment operators (#2959)
10b396b4c [sim] Various WebSockets fixes and enhancements (#2952)
699bbe21a [examples] Fix comments in Gearsbot to match implementation (NFC) (#2957)
27b67deca [glass] Add more widgets (#2947)
581b7ec55 [wpilib] Add option to flush NetworkTables every iterative loop
acfbb1a44 [ntcore] DispatcherBase::Flush: Use wpi::Now()
d85a6d8fe [ntcore] Reduce limit on flush and update rate to 5 ms
20fbb5c63 [sim] Fix stringop truncation warning from GCC 10 (#2945)
1051a06a7 [glass] Show NT timestamps in seconds (#2944)
98dfc2620 [glass] Fix plots (#2943)
1ba0a2ced [sim] GUI: Add keyboard virtual joystick support (#2940)
4afb13f98 [examples] Replace M_PI with wpi::math::pi (#2938)
b27d33675 [examples] Enhance Romi templates (#2931)
00b9ae77f [sim] Change default WS port number to 3300 (#2932)
65219f309 [examples] Update Field2d position in periodic() (#2928)
f78d1d434 [sim] Process WS Encoder reset internally (#2927)
941edca59 [hal] Add Java SimDeviceDataJNI.getSimDeviceName (#2924)
a699435ed [wpilibj] Fix FlywheelSim argument order in constructor (#2922)
66d641718 [examples] Add tasks to run Java examples (#2920)
558e37c41 [examples] Add simple differential drive simulation example (#2918)
4f40d991e [glass] Switch name of Glass back to glass (#2919)
549af9900 [build] Update native-utils to 2021.0.6 (#2914)
b33693009 [glass] Change basename of glass to Glass (#2915)
c9a0edfb8 [glass] Package macOS application bundle
2c5668af4 [wpigui] Add platform-specific preferences save
751dea32a [wpilibc] Try to work around ABI break introduced in #2901 (#2917)
cd8f4bfb1 [build] Package up msvc runtime into maven artifact (#2913)
a6cfcc686 [wpilibc] Move SendableChooser Doxygen comments to header (NFC) (#2911)
b8c4f603d [wpimath] Upgrade to Eigen 3.3.9 (#2910)
0075e4b39 [wpilibj] Fix NPE in Field2d (#2909)
125af556c [simulation] Fix halsim_gui ntcore and wpiutil deps (#2908)
963ad5c25 [wpilib] Add noise to Differential Drive simulator (#2903)
387f56cb7 [examples] Add Romi reference Java example and templates (#2905)
b3deda38c [examples] Zero motors on disabledInit() in sim physics examples (#2906)
2a5ca7745 [glass] Add glass: an application for display of robot data
727940d84 [wpilib] Move Field2d to SmartDashboard
8cd42478e [wpilib] SendableBuilder: Make GetTable() visible
c11d34b26 [command] Use addCommands in command group templates (#2900)
339d7445b [sim] Add HAL hooks for simulationPeriodic (#2881)
d16f05f2c [wpilib] Fix SmartDashboard update order (#2896)
5427b32a4 [wpiutil] unique_function: Restrict implicit conversion (#2899)
f73701239 [ntcore] Add missing SetDefault initializer_list functions (#2898)
f5a6fc070 [sim] Add initialized flag for all solenoids on a PCM (#2897)
bdf5ba91a [wpilibj] Fix typo in ElevatorSim (#2895)
bc8f33877 [wpilib] Add pose estimators (#2867)
3413bfc06 [wpilib] PIDController: Recompute the error in AtSetpoint() (#2822)
2056f0ce0 [wpilib] Fix bugs in Hatchbot examples (#2893)
5eb8cfd69 [wpilibc] Fix MatchDataSender (#2892)
e6a425448 [build] Delete test folders after tests execute (#2891)
d478ad00d [imgui] Allow usage of imgui_stdlib (#2889)
53eda861d [build] Add unit-testing infrastructure to examples (#2863)
cc1d86ba6 [sim] Add title to simulator GUI window (#2888)
f0528f00e [build] CMake: Use project-specific binary and source dirs (#2886)
5cd2ad124 [wpilibc] Add Color::operator!= (#2887)
6c00e7a90 [build] CI CMake: build with GUI enabled (#2884)
53170bbb5 Update roboRIO toolchain installation instructions (#2883)
467258e05 [sim] GUI: Add option to not zero disconnected joysticks (#2876)
129be23c9 Clarify JDK installation instructions in readme (#2882)
8e9290e86 [build] Add separate CMake setting for wpimath (#2885)
7cf5bebf8 [wpilibj] Cache NT writes from DriverStation (#2780)
f7f9087fb [command] Fix timing issue in RamseteCommand (#2871)
256e7904f [wpilibj] SimDeviceSim: Fix sim value changed callback (#2880)
c8ea1b6c3 [wpilib] Add function to adjust LQR controller gain for pure time delay (#2878)
2816b06c0 [sim] HAL_GetControlWord: Fully zero output (#2873)
4c695ea08 Add toolchain installation instructions to README (#2875)
a14d51806 [wpimath] DCMotor: fix doc typo (NFC) (#2868)
017097791 [build] CMake: build sim extensions as shared libs (#2866)
f61726b5a [build] Fix cmake-config files (#2865)
fc27fdac5 [wpilibc] Cache NT values from driver station (#2768)
47c59859e [sim] Make SimDevice callbacks synchronous (#2861)
6e76ab9c0 [build] Turn on WITH_GUI for Windows cmake CI
5f78b7670 [build] Set GLFW_INSTALL to OFF
5e0808c84 [wpigui] Fix Windows cmake build
508f05a47 [imgui] Fix typo in Windows CMake target sources
Change-Id: I1737b45965f31803a96676bedc7dc40e337aa321
git-subtree-dir: third_party/allwpilib
git-subtree-split: e473a00f9785f9949e5ced30901baeaf426d2fc9
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/ntcore/.styleguide b/ntcore/.styleguide
index 67befdb..8cf3a51 100644
--- a/ntcore/.styleguide
+++ b/ntcore/.styleguide
@@ -27,6 +27,7 @@
}
includeOtherLibs {
+ ^fmt/
^support/
^wpi/
}
diff --git a/ntcore/CMakeLists.txt b/ntcore/CMakeLists.txt
index e095fa9..44992ef 100644
--- a/ntcore/CMakeLists.txt
+++ b/ntcore/CMakeLists.txt
@@ -13,6 +13,7 @@
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/include>
$<INSTALL_INTERFACE:${include_dest}/ntcore>)
wpilib_target_warnings(ntcore)
+target_compile_features(ntcore PUBLIC cxx_std_17)
target_link_libraries(ntcore PUBLIC wpiutil)
set_property(TARGET ntcore PROPERTY FOLDER "libraries")
@@ -26,8 +27,8 @@
set (ntcore_config_dir share/ntcore)
endif()
-configure_file(ntcore-config.cmake.in ${CMAKE_BINARY_DIR}/ntcore-config.cmake )
-install(FILES ${CMAKE_BINARY_DIR}/ntcore-config.cmake DESTINATION ${ntcore_config_dir})
+configure_file(ntcore-config.cmake.in ${WPILIB_BINARY_DIR}/ntcore-config.cmake )
+install(FILES ${WPILIB_BINARY_DIR}/ntcore-config.cmake DESTINATION ${ntcore_config_dir})
install(EXPORT ntcore DESTINATION ${ntcore_config_dir})
# Java bindings
@@ -35,7 +36,7 @@
find_package(Java REQUIRED)
find_package(JNI REQUIRED)
include(UseJava)
- set(CMAKE_JAVA_COMPILE_FLAGS "-Xlint:unchecked")
+ set(CMAKE_JAVA_COMPILE_FLAGS "-encoding" "UTF8" "-Xlint:unchecked")
file(GLOB
ntcore_jni_src src/main/native/cpp/jni/NetworkTablesJNI.cpp)
diff --git a/ntcore/build.gradle b/ntcore/build.gradle
index 5739b71..67674c6 100644
--- a/ntcore/build.gradle
+++ b/ntcore/build.gradle
@@ -5,29 +5,41 @@
apply from: "${rootDir}/shared/jni/setupBuild.gradle"
+Action<List<String>> symbolFilter = { symbols ->
+ symbols.removeIf({ !it.startsWith('NT_') })
+} as Action<List<String>>;
+
nativeUtils.exportsConfigs {
ntcore {
- x86ExcludeSymbols = ['_CT??_R0?AV_System_error', '_CT??_R0?AVexception', '_CT??_R0?AVfailure',
- '_CT??_R0?AVruntime_error', '_CT??_R0?AVsystem_error', '_CTA5?AVfailure',
- '_TI5?AVfailure', '_CT??_R0?AVout_of_range', '_CTA3?AVout_of_range',
- '_TI3?AVout_of_range', '_CT??_R0?AVbad_cast']
- x64ExcludeSymbols = ['_CT??_R0?AV_System_error', '_CT??_R0?AVexception', '_CT??_R0?AVfailure',
- '_CT??_R0?AVruntime_error', '_CT??_R0?AVsystem_error', '_CTA5?AVfailure',
- '_TI5?AVfailure', '_CT??_R0?AVout_of_range', '_CTA3?AVout_of_range',
- '_TI3?AVout_of_range', '_CT??_R0?AVbad_cast']
+ x86ExcludeSymbols = [
+ '_CT??_R0?AV_System_error',
+ '_CT??_R0?AVexception',
+ '_CT??_R0?AVfailure',
+ '_CT??_R0?AVruntime_error',
+ '_CT??_R0?AVsystem_error',
+ '_CTA5?AVfailure',
+ '_TI5?AVfailure',
+ '_CT??_R0?AVout_of_range',
+ '_CTA3?AVout_of_range',
+ '_TI3?AVout_of_range',
+ '_CT??_R0?AVbad_cast'
+ ]
+ x64ExcludeSymbols = [
+ '_CT??_R0?AV_System_error',
+ '_CT??_R0?AVexception',
+ '_CT??_R0?AVfailure',
+ '_CT??_R0?AVruntime_error',
+ '_CT??_R0?AVsystem_error',
+ '_CTA5?AVfailure',
+ '_TI5?AVfailure',
+ '_CT??_R0?AVout_of_range',
+ '_CTA3?AVout_of_range',
+ '_TI3?AVout_of_range',
+ '_CT??_R0?AVbad_cast'
+ ]
}
ntcoreJNI {
- x86SymbolFilter = { symbols ->
- symbols.removeIf({ !it.startsWith('NT_') })
- }
- x64SymbolFilter = { symbols ->
- symbols.removeIf({ !it.startsWith('NT_') })
- }
- }
-}
-
-if (!project.hasProperty('skipPMD')) {
- pmdMain {
- pmdMain.enabled = false
+ x86SymbolFilter = symbolFilter
+ x64SymbolFilter = symbolFilter
}
}
diff --git a/ntcore/doc/networktables2.adoc b/ntcore/doc/networktables2.adoc
index 8471f86..e090801 100644
--- a/ntcore/doc/networktables2.adoc
+++ b/ntcore/doc/networktables2.adoc
@@ -1,4 +1,4 @@
-= Network Tables Protocol Specification, Version 2.0
+= NetworkTables Protocol Specification, Version 2.0
WPILib Developers <wpilib@wpi.edu>
Protocol Revision 2.0 (0x0200), 1/8/2013
:toc:
@@ -19,9 +19,9 @@
a client must have received a server's most recent state before it can replace
it with a new value.
-This is a backwards-incompatible rework of the Network Tables network protocol
+This is a backwards-incompatible rework of the NetworkTables network protocol
originally introduced for the 2012 FIRST Robotics Competition. Note that this
-revision of the Network Tables protocol no longer includes the concept of
+revision of the NetworkTables protocol no longer includes the concept of
sub-tables. We suggest that instead of representing sub-tables as first-class
data types in the network protocol, it would be easy for an implementation to
provide a similar API abstraction by adding prefixes to keys. For example, we
diff --git a/ntcore/doc/networktables3.adoc b/ntcore/doc/networktables3.adoc
index af8b953..2850168 100644
--- a/ntcore/doc/networktables3.adoc
+++ b/ntcore/doc/networktables3.adoc
@@ -1,4 +1,4 @@
-= Network Tables Protocol Specification, Version 3.0
+= NetworkTables Protocol Specification, Version 3.0
WPILib Developers <wpilib@wpi.edu>
Protocol Revision 3.0 (0x0300), 6/12/2015
:toc:
@@ -20,7 +20,7 @@
it with a new value.
This is a backwards-compatible update of <<networktables2,version 2.0>> of the
-Network Tables network protocol. The protocol is designed such that 3.0 clients
+NetworkTables network protocol. The protocol is designed such that 3.0 clients
and servers can interoperate with 2.0 clients and servers with the only loss of
functionality being the extended features introduced in 3.0.
@@ -50,7 +50,7 @@
Remote procedure call:: The Server may create specially-typed entries that
inform Clients of remotely callable functions on the Server. Clients can then
-execute these functions via the Network Tables protocol. See <<rpc-operation>>.
+execute these functions via the NetworkTables protocol. See <<rpc-operation>>.
Raw data type:: An arbitrary data type has been added. While string could be
used to encode raw data, the reason for a different data type is so that
@@ -108,7 +108,7 @@
== References
[[networktables2]]
-* <<networktables2.adoc#,Network Tables Protocol Specification, Protocol
+* <<networktables2.adoc#,NetworkTables Protocol Specification, Protocol
Revision 2.0 (0x0200)>>, dated 1/8/2013.
[[leb128,LEB128]]
diff --git a/ntcore/manualTests/java/Client.java b/ntcore/manualTests/java/Client.java
index 1d5f181..a329c83 100644
--- a/ntcore/manualTests/java/Client.java
+++ b/ntcore/manualTests/java/Client.java
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
import edu.wpi.first.wpilibj.networktables.*;
import edu.wpi.first.wpilibj.tables.*;
@@ -21,10 +18,13 @@
NetworkTable.setPort(10000);
NetworkTable.setClientMode();
NetworkTable nt = NetworkTable.getTable("");
- try { Thread.sleep(2000); } catch (InterruptedException e) {}
+ try {
+ Thread.sleep(2000);
+ } catch (InterruptedException e) {
+ }
try {
System.out.println("Got foo: " + nt.getNumber("foo"));
- } catch(TableKeyNotDefinedException ex) {
+ } catch (TableKeyNotDefinedException ex) {
}
nt.putBoolean("bar", false);
nt.setFlags("bar", NetworkTable.PERSISTENT);
@@ -41,6 +41,9 @@
strs[0] = "Hello";
strs[1] = "World";
nt.putStringArray("strarray", strs);
- try { Thread.sleep(10000); } catch (InterruptedException e) {}
+ try {
+ Thread.sleep(10000);
+ } catch (InterruptedException e) {
+ }
}
}
diff --git a/ntcore/manualTests/java/Server.java b/ntcore/manualTests/java/Server.java
index 7f4dbc6..9527c96 100644
--- a/ntcore/manualTests/java/Server.java
+++ b/ntcore/manualTests/java/Server.java
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
import edu.wpi.first.wpilibj.networktables.*;
import edu.wpi.first.wpilibj.tables.*;
@@ -21,13 +18,19 @@
NetworkTable.setPort(10000);
NetworkTable.setServerMode();
NetworkTable nt = NetworkTable.getTable("");
- try { Thread.sleep(1000); } catch (InterruptedException e) {}
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ }
nt.putNumber("foo", 0.5);
nt.setFlags("foo", NetworkTable.PERSISTENT);
nt.putNumber("foo2", 0.5);
nt.putNumber("foo2", 0.7);
nt.putNumber("foo2", 0.6);
nt.putNumber("foo2", 0.5);
- try { Thread.sleep(10000); } catch (InterruptedException e) {}
+ try {
+ Thread.sleep(10000);
+ } catch (InterruptedException e) {
+ }
}
}
diff --git a/ntcore/manualTests/native/client.cpp b/ntcore/manualTests/native/client.cpp
index bb16a9c..f946673 100644
--- a/ntcore/manualTests/native/client.cpp
+++ b/ntcore/manualTests/native/client.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <chrono>
#include <climits>
@@ -26,8 +23,9 @@
auto foo = nt::GetEntry(inst, "/foo");
auto foo_val = nt::GetEntryValue(foo);
- if (foo_val && foo_val->IsDouble())
+ if (foo_val && foo_val->IsDouble()) {
std::printf("Got foo: %g\n", foo_val->GetDouble());
+ }
auto bar = nt::GetEntry(inst, "/bar");
nt::SetEntryValue(bar, nt::Value::MakeBoolean(false));
diff --git a/ntcore/manualTests/native/rpc_local.cpp b/ntcore/manualTests/native/rpc_local.cpp
index 3311463..2879c33 100644
--- a/ntcore/manualTests/native/rpc_local.cpp
+++ b/ntcore/manualTests/native/rpc_local.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <chrono>
#include <climits>
diff --git a/ntcore/manualTests/native/rpc_speed.cpp b/ntcore/manualTests/native/rpc_speed.cpp
index 70558ef..6f9dbbf 100644
--- a/ntcore/manualTests/native/rpc_speed.cpp
+++ b/ntcore/manualTests/native/rpc_speed.cpp
@@ -1,16 +1,12 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <chrono>
#include <climits>
-#include <cstdio>
-#include <iostream>
#include <thread>
+#include <fmt/core.h>
#include <support/json.h>
#include "ntcore.h"
@@ -20,11 +16,11 @@
try {
params = wpi::json::from_cbor(answer.params);
} catch (wpi::json::parse_error err) {
- std::fputs("could not decode params?\n", stderr);
+ fmt::print(stderr, "could not decode params?\n");
return;
}
if (!params.is_number()) {
- std::fputs("did not get number\n", stderr);
+ fmt::print(stderr, "did not get number\n");
return;
}
double val = params.get<double>();
@@ -47,29 +43,25 @@
try {
call1_result = wpi::json::from_cbor(call1_result_str);
} catch (wpi::json::parse_error err) {
- std::fputs("could not decode result?\n", stderr);
+ fmt::print(stderr, "could not decode result?\n");
return 1;
}
if (!call1_result.is_number()) {
- std::fputs("result is not number?\n", stderr);
+ fmt::print(stderr, "result is not number?\n");
return 1;
}
}
auto end2 = std::chrono::high_resolution_clock::now();
auto end = nt::Now();
- std::cerr << "nt::Now start=" << start << " end=" << end << '\n';
- std::cerr << "std::chrono start="
- << std::chrono::duration_cast<std::chrono::nanoseconds>(
- start2.time_since_epoch())
- .count()
- << " end="
- << std::chrono::duration_cast<std::chrono::nanoseconds>(
- end2.time_since_epoch())
- .count()
- << '\n';
- std::fprintf(stderr, "time/call = %g us\n", (end - start) / 10.0 / 10000.0);
+ fmt::print(stderr, "nt::Now start={} end={}\n", start, end);
+ fmt::print(stderr, "std::chrono start={} end={}\n",
+ std::chrono::duration_cast<std::chrono::nanoseconds>(
+ start2.time_since_epoch())
+ .count(),
+ std::chrono::duration_cast<std::chrono::nanoseconds>(
+ end2.time_since_epoch())
+ .count());
+ fmt::print(stderr, "time/call = %g us\n", (end - start) / 10.0 / 10000.0);
std::chrono::duration<double, std::micro> diff = end2 - start2;
- std::cerr << "time/call = " << (diff.count() / 10000.0) << " us\n";
-
- return 0;
+ fmt::print(stderr, "time/call = {} us\n", diff.count() / 10000.0);
}
diff --git a/ntcore/manualTests/native/server.cpp b/ntcore/manualTests/native/server.cpp
index 087ddb4..0825960 100644
--- a/ntcore/manualTests/native/server.cpp
+++ b/ntcore/manualTests/native/server.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <chrono>
#include <climits>
diff --git a/ntcore/ntcore-config.cmake.in b/ntcore/ntcore-config.cmake.in
index fb677b3..17006a5 100644
--- a/ntcore/ntcore-config.cmake.in
+++ b/ntcore/ntcore-config.cmake.in
@@ -2,4 +2,5 @@
@FILENAME_DEP_REPLACE@
@WPIUTIL_DEP_REPLACE@
+@FILENAME_DEP_REPLACE@
include(${SELF_DIR}/ntcore.cmake)
diff --git a/ntcore/src/dev/java/edu/wpi/first/ntcore/DevMain.java b/ntcore/src/dev/java/edu/wpi/first/ntcore/DevMain.java
index 547af0a..466731c 100644
--- a/ntcore/src/dev/java/edu/wpi/first/ntcore/DevMain.java
+++ b/ntcore/src/dev/java/edu/wpi/first/ntcore/DevMain.java
@@ -1,25 +1,19 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.ntcore;
import edu.wpi.first.networktables.NetworkTablesJNI;
-import edu.wpi.first.wpiutil.RuntimeDetector;
+import edu.wpi.first.util.RuntimeDetector;
public final class DevMain {
- /**
- * Main method.
- */
+ /** Main method. */
public static void main(String[] args) {
System.out.println("Hello World!");
System.out.println(RuntimeDetector.getPlatformPath());
NetworkTablesJNI.flush(NetworkTablesJNI.getDefaultInstance());
}
- private DevMain() {
- }
+ private DevMain() {}
}
diff --git a/ntcore/src/dev/native/cpp/main.cpp b/ntcore/src/dev/native/cpp/main.cpp
index 1153347..f863018 100644
--- a/ntcore/src/dev/native/cpp/main.cpp
+++ b/ntcore/src/dev/native/cpp/main.cpp
@@ -1,11 +1,8 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
-#include <iostream>
+#include <fmt/core.h>
#include "ntcore.h"
@@ -14,5 +11,5 @@
nt::SetEntryValue(myValue, nt::Value::MakeString("Hello World"));
- std::cout << nt::GetEntryValue(myValue)->GetString() << std::endl;
+ fmt::print("{}\n", nt::GetEntryValue(myValue)->GetString());
}
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/ConnectionInfo.java b/ntcore/src/main/java/edu/wpi/first/networktables/ConnectionInfo.java
index b060f2f..477d53b 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/ConnectionInfo.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/ConnectionInfo.java
@@ -1,51 +1,42 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
-/**
- * NetworkTables Connection information.
- */
+/** NetworkTables Connection information. */
public final class ConnectionInfo {
/**
- * The remote identifier (as set on the remote node by
- * {@link NetworkTableInstance#setNetworkIdentity(String)}).
+ * The remote identifier (as set on the remote node by {@link
+ * NetworkTableInstance#setNetworkIdentity(String)}).
*/
@SuppressWarnings("MemberName")
public final String remote_id;
- /**
- * The IP address of the remote node.
- */
+ /** The IP address of the remote node. */
@SuppressWarnings("MemberName")
public final String remote_ip;
- /**
- * The port number of the remote node.
- */
+ /** The port number of the remote node. */
@SuppressWarnings("MemberName")
public final int remote_port;
/**
- * The last time any update was received from the remote node (same scale as
- * returned by {@link NetworkTablesJNI#now()}).
+ * The last time any update was received from the remote node (same scale as returned by {@link
+ * NetworkTablesJNI#now()}).
*/
@SuppressWarnings("MemberName")
public final long last_update;
/**
- * The protocol version being used for this connection. This is in protocol
- * layer format, so 0x0200 = 2.0, 0x0300 = 3.0).
+ * The protocol version being used for this connection. This is in protocol layer format, so
+ * 0x0200 = 2.0, 0x0300 = 3.0).
*/
@SuppressWarnings("MemberName")
public final int protocol_version;
- /** Constructor.
- * This should generally only be used internally to NetworkTables.
+ /**
+ * Constructor. This should generally only be used internally to NetworkTables.
*
* @param remoteId Remote identifier
* @param remoteIp Remote IP address
@@ -53,8 +44,8 @@
* @param lastUpdate Last time an update was received
* @param protocolVersion The protocol version used for the connection
*/
- public ConnectionInfo(String remoteId, String remoteIp, int remotePort, long lastUpdate,
- int protocolVersion) {
+ public ConnectionInfo(
+ String remoteId, String remoteIp, int remotePort, long lastUpdate, int protocolVersion) {
remote_id = remoteId;
remote_ip = remoteIp;
remote_port = remotePort;
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/ConnectionNotification.java b/ntcore/src/main/java/edu/wpi/first/networktables/ConnectionNotification.java
index 129bf1a..544e075 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/ConnectionNotification.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/ConnectionNotification.java
@@ -1,44 +1,33 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
-/**
- * NetworkTables Connection notification.
- */
+/** NetworkTables Connection notification. */
public final class ConnectionNotification {
- /**
- * Listener that was triggered.
- */
+ /** Listener that was triggered. */
@SuppressWarnings("MemberName")
public final int listener;
- /**
- * True if event is due to connection being established.
- */
+ /** True if event is due to connection being established. */
@SuppressWarnings("MemberName")
public final boolean connected;
- /**
- * Connection information.
- */
+ /** Connection information. */
@SuppressWarnings("MemberName")
public final ConnectionInfo conn;
- /** Constructor.
- * This should generally only be used internally to NetworkTables.
+ /**
+ * Constructor. This should generally only be used internally to NetworkTables.
*
* @param inst Instance
* @param listener Listener that was triggered
* @param connected Connected if true
* @param conn Connection information
*/
- public ConnectionNotification(NetworkTableInstance inst, int listener, boolean connected,
- ConnectionInfo conn) {
+ public ConnectionNotification(
+ NetworkTableInstance inst, int listener, boolean connected, ConnectionInfo conn) {
this.m_inst = inst;
this.listener = listener;
this.connected = connected;
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/EntryInfo.java b/ntcore/src/main/java/edu/wpi/first/networktables/EntryInfo.java
index b516cc7..471edc8 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/EntryInfo.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/EntryInfo.java
@@ -1,15 +1,10 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
-/**
- * NetworkTables Entry information.
- */
+/** NetworkTables Entry information. */
public final class EntryInfo {
/** Entry handle. */
@SuppressWarnings("MemberName")
@@ -31,8 +26,8 @@
@SuppressWarnings("MemberName")
public final long last_change;
- /** Constructor.
- * This should generally only be used internally to NetworkTables.
+ /**
+ * Constructor. This should generally only be used internally to NetworkTables.
*
* @param inst Instance
* @param entry Entry handle
@@ -41,8 +36,8 @@
* @param flags Flags
* @param lastChange Timestamp of last change
*/
- public EntryInfo(NetworkTableInstance inst, int entry, String name, int type, int flags,
- long lastChange) {
+ public EntryInfo(
+ NetworkTableInstance inst, int entry, String name, int type, int flags, long lastChange) {
this.m_inst = inst;
this.entry = entry;
this.name = name;
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/EntryListenerFlags.java b/ntcore/src/main/java/edu/wpi/first/networktables/EntryListenerFlags.java
index 9cdc0f0..856c962 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/EntryListenerFlags.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/EntryListenerFlags.java
@@ -1,42 +1,38 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
/**
* Flag values for use with entry listeners.
*
- * <p>The flags are a bitmask and must be OR'ed together to indicate the
- * combination of events desired to be received.
+ * <p>The flags are a bitmask and must be OR'ed together to indicate the combination of events
+ * desired to be received.
*
- * <p>The constants kNew, kDelete, kUpdate, and kFlags represent different events
- * that can occur to entries.
+ * <p>The constants kNew, kDelete, kUpdate, and kFlags represent different events that can occur to
+ * entries.
*
- * <p>By default, notifications are only generated for remote changes occurring
- * after the listener is created. The constants kImmediate and kLocal are
- * modifiers that cause notifications to be generated at other times.
+ * <p>By default, notifications are only generated for remote changes occurring after the listener
+ * is created. The constants kImmediate and kLocal are modifiers that cause notifications to be
+ * generated at other times.
*/
public interface EntryListenerFlags {
/**
* Initial listener addition.
*
- * <p>Set this flag to receive immediate notification of entries matching the
- * flag criteria (generally only useful when combined with kNew).
+ * <p>Set this flag to receive immediate notification of entries matching the flag criteria
+ * (generally only useful when combined with kNew).
*/
int kImmediate = 0x01;
/**
* Changed locally.
*
- * <p>Set this flag to receive notification of both local changes and changes
- * coming from remote nodes. By default, notifications are only generated
- * for remote changes. Must be combined with some combination of kNew,
- * kDelete, kUpdate, and kFlags to receive notifications of those respective
- * events.
+ * <p>Set this flag to receive notification of both local changes and changes coming from remote
+ * nodes. By default, notifications are only generated for remote changes. Must be combined with
+ * some combination of kNew, kDelete, kUpdate, and kFlags to receive notifications of those
+ * respective events.
*/
int kLocal = 0x02;
@@ -57,16 +53,14 @@
/**
* Entry's value changed.
*
- * <p>Set this flag to receive a notification when an entry's value (or type)
- * changes.
+ * <p>Set this flag to receive a notification when an entry's value (or type) changes.
*/
int kUpdate = 0x10;
/**
* Entry's flags changed.
*
- * <p>Set this flag to receive a notification when an entry's flags value
- * changes.
+ * <p>Set this flag to receive a notification when an entry's flags value changes.
*/
int kFlags = 0x20;
}
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/EntryNotification.java b/ntcore/src/main/java/edu/wpi/first/networktables/EntryNotification.java
index 159b968..0f5cb51 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/EntryNotification.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/EntryNotification.java
@@ -1,49 +1,35 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
-/**
- * NetworkTables Entry notification.
- */
+/** NetworkTables Entry notification. */
public final class EntryNotification {
- /**
- * Listener that was triggered.
- */
+ /** Listener that was triggered. */
@SuppressWarnings("MemberName")
public final int listener;
- /**
- * Entry handle.
- */
+ /** Entry handle. */
@SuppressWarnings("MemberName")
public final int entry;
- /**
- * Entry name.
- */
+ /** Entry name. */
@SuppressWarnings("MemberName")
public final String name;
- /**
- * The new value.
- */
+ /** The new value. */
@SuppressWarnings("MemberName")
public final NetworkTableValue value;
/**
- * Update flags. For example, {@link EntryListenerFlags#kNew} if the key did
- * not previously exist.
+ * Update flags. For example, {@link EntryListenerFlags#kNew} if the key did not previously exist.
*/
@SuppressWarnings("MemberName")
public final int flags;
- /** Constructor.
- * This should generally only be used internally to NetworkTables.
+ /**
+ * Constructor. This should generally only be used internally to NetworkTables.
*
* @param inst Instance
* @param listener Listener that was triggered
@@ -52,8 +38,13 @@
* @param value The new value
* @param flags Update flags
*/
- public EntryNotification(NetworkTableInstance inst, int listener, int entry, String name,
- NetworkTableValue value, int flags) {
+ public EntryNotification(
+ NetworkTableInstance inst,
+ int listener,
+ int entry,
+ String name,
+ NetworkTableValue value,
+ int flags) {
this.m_inst = inst;
this.listener = listener;
this.entry = entry;
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/LogMessage.java b/ntcore/src/main/java/edu/wpi/first/networktables/LogMessage.java
index 2cf22ec..5d77d8b 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/LogMessage.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/LogMessage.java
@@ -1,20 +1,14 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
-/**
- * NetworkTables log message.
- */
+/** NetworkTables log message. */
public final class LogMessage {
- /**
- * Logging levels.
- */
+ /** Logging levels. */
public static final int kCritical = 50;
+
public static final int kError = 40;
public static final int kWarning = 30;
public static final int kInfo = 20;
@@ -24,38 +18,28 @@
public static final int kDebug3 = 7;
public static final int kDebug4 = 6;
- /**
- * The logger that generated the message.
- */
+ /** The logger that generated the message. */
@SuppressWarnings("MemberName")
public final int logger;
- /**
- * Log level of the message.
- */
+ /** Log level of the message. */
@SuppressWarnings("MemberName")
public final int level;
- /**
- * The filename of the source file that generated the message.
- */
+ /** The filename of the source file that generated the message. */
@SuppressWarnings("MemberName")
public final String filename;
- /**
- * The line number in the source file that generated the message.
- */
+ /** The line number in the source file that generated the message. */
@SuppressWarnings("MemberName")
public final int line;
- /**
- * The message.
- */
+ /** The message. */
@SuppressWarnings("MemberName")
public final String message;
- /** Constructor.
- * This should generally only be used internally to NetworkTables.
+ /**
+ * Constructor. This should generally only be used internally to NetworkTables.
*
* @param inst Instance
* @param logger Logger
@@ -64,8 +48,8 @@
* @param line Line number
* @param message Message
*/
- public LogMessage(NetworkTableInstance inst, int logger, int level, String filename, int line,
- String message) {
+ public LogMessage(
+ NetworkTableInstance inst, int logger, int level, String filename, int line, String message) {
this.m_inst = inst;
this.logger = logger;
this.level = level;
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/NTSendable.java b/ntcore/src/main/java/edu/wpi/first/networktables/NTSendable.java
new file mode 100644
index 0000000..641224d
--- /dev/null
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/NTSendable.java
@@ -0,0 +1,25 @@
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
+
+package edu.wpi.first.networktables;
+
+import edu.wpi.first.util.sendable.Sendable;
+import edu.wpi.first.util.sendable.SendableBuilder;
+
+/** Interface for NetworkTable Sendable objects. */
+public interface NTSendable extends Sendable {
+ /**
+ * Initializes this {@link Sendable} object.
+ *
+ * @param builder sendable builder
+ */
+ void initSendable(NTSendableBuilder builder);
+
+ @Override
+ default void initSendable(SendableBuilder builder) {
+ if (builder.getBackendKind() == SendableBuilder.BackendKind.kNetworkTables) {
+ initSendable((NTSendableBuilder) builder);
+ }
+ }
+}
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/NTSendableBuilder.java b/ntcore/src/main/java/edu/wpi/first/networktables/NTSendableBuilder.java
new file mode 100644
index 0000000..65bfbca
--- /dev/null
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/NTSendableBuilder.java
@@ -0,0 +1,51 @@
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
+
+package edu.wpi.first.networktables;
+
+import edu.wpi.first.util.sendable.SendableBuilder;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+
+public interface NTSendableBuilder extends SendableBuilder {
+ /**
+ * Set the function that should be called to update the network table for things other than
+ * properties. Note this function is not passed the network table object; instead it should use
+ * the entry handles returned by getEntry().
+ *
+ * @param func function
+ */
+ void setUpdateTable(Runnable func);
+
+ /**
+ * Add a property without getters or setters. This can be used to get entry handles for the
+ * function called by setUpdateTable().
+ *
+ * @param key property name
+ * @return Network table entry
+ */
+ NetworkTableEntry getEntry(String key);
+
+ /**
+ * Add a NetworkTableValue property.
+ *
+ * @param key property name
+ * @param getter getter function (returns current value)
+ * @param setter setter function (sets new value)
+ */
+ void addValueProperty(
+ String key, Supplier<NetworkTableValue> getter, Consumer<NetworkTableValue> setter);
+
+ /**
+ * Get the network table.
+ *
+ * @return The network table
+ */
+ NetworkTable getTable();
+
+ @Override
+ default BackendKind getBackendKind() {
+ return BackendKind.kNetworkTables;
+ }
+}
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTable.java b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTable.java
index ad25f23..7837a21 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTable.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTable.java
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
@@ -16,13 +13,9 @@
import java.util.concurrent.ConcurrentMap;
import java.util.function.Consumer;
-/**
- * A network table that knows its subtable path.
- */
+/** A network table that knows its subtable path. */
public final class NetworkTable {
- /**
- * The path separator for sub-tables and keys.
- */
+ /** The path separator for sub-tables and keys. */
public static final char PATH_SEPARATOR = '/';
private final String m_path;
@@ -30,8 +23,8 @@
private final NetworkTableInstance m_inst;
/**
- * Gets the "base name" of a key. For example, "/foo/bar" becomes "bar".
- * If the key has a trailing slash, returns an empty string.
+ * Gets the "base name" of a key. For example, "/foo/bar" becomes "bar". If the key has a trailing
+ * slash, returns an empty string.
*
* @param key key
* @return base name
@@ -45,8 +38,8 @@
}
/**
- * Normalizes an network table key to contain no consecutive slashes and
- * optionally start with a leading slash. For example:
+ * Normalizes an network table key to contain no consecutive slashes and optionally start with a
+ * leading slash. For example:
*
* <pre><code>
* normalizeKey("/foo/bar", true) == "/foo/bar"
@@ -55,9 +48,8 @@
* normalizeKey("foo//bar", false) == "foo/bar"
* </code></pre>
*
- * @param key the key to normalize
- * @param withLeadingSlash whether or not the normalized key should begin
- * with a leading slash
+ * @param key the key to normalize
+ * @param withLeadingSlash whether or not the normalized key should begin with a leading slash
* @return normalized key
*/
public static String normalizeKey(String key, boolean withLeadingSlash) {
@@ -77,10 +69,9 @@
}
/**
- * Normalizes a network table key to start with exactly one leading slash
- * ("/") and contain no consecutive slashes. For example,
- * {@code "//foo/bar/"} becomes {@code "/foo/bar/"} and
- * {@code "///a/b/c"} becomes {@code "/a/b/c"}.
+ * Normalizes a network table key to start with exactly one leading slash ("/") and contain no
+ * consecutive slashes. For example, {@code "//foo/bar/"} becomes {@code "/foo/bar/"} and {@code
+ * "///a/b/c"} becomes {@code "/a/b/c"}.
*
* <p>This is equivalent to {@code normalizeKey(key, true)}
*
@@ -92,9 +83,8 @@
}
/**
- * Gets a list of the names of all the super tables of a given key. For
- * example, the key "/foo/bar/baz" has a hierarchy of "/", "/foo",
- * "/foo/bar", and "/foo/bar/baz".
+ * Gets a list of the names of all the super tables of a given key. For example, the key
+ * "/foo/bar/baz" has a hierarchy of "/", "/foo", "/foo/bar", and "/foo/bar/baz".
*
* @param key the key
* @return List of super tables
@@ -118,9 +108,7 @@
return hierarchy;
}
- /**
- * Constructor. Use NetworkTableInstance.getTable() or getSubTable() instead.
- */
+ /** Constructor. Use NetworkTableInstance.getTable() or getSubTable() instead. */
NetworkTable(NetworkTableInstance inst, String path) {
m_path = path;
m_pathWithSep = path + PATH_SEPARATOR;
@@ -161,52 +149,54 @@
/**
* Listen to keys only within this table.
*
- * @param listener listener to add
- * @param flags {@link EntryListenerFlags} bitmask
+ * @param listener listener to add
+ * @param flags {@link EntryListenerFlags} bitmask
* @return Listener handle
*/
public int addEntryListener(TableEntryListener listener, int flags) {
final int prefixLen = m_path.length() + 1;
- return m_inst.addEntryListener(m_pathWithSep, event -> {
- String relativeKey = event.name.substring(prefixLen);
- if (relativeKey.indexOf(PATH_SEPARATOR) != -1) {
- // part of a sub table
- return;
- }
- listener.valueChanged(this, relativeKey, event.getEntry(), event.value, event.flags);
- }, flags);
+ return m_inst.addEntryListener(
+ m_pathWithSep,
+ event -> {
+ String relativeKey = event.name.substring(prefixLen);
+ if (relativeKey.indexOf(PATH_SEPARATOR) != -1) {
+ // part of a sub table
+ return;
+ }
+ listener.valueChanged(this, relativeKey, event.getEntry(), event.value, event.flags);
+ },
+ flags);
}
/**
* Listen to a single key.
*
- * @param key the key name
- * @param listener listener to add
- * @param flags {@link EntryListenerFlags} bitmask
+ * @param key the key name
+ * @param listener listener to add
+ * @param flags {@link EntryListenerFlags} bitmask
* @return Listener handle
*/
public int addEntryListener(String key, TableEntryListener listener, int flags) {
final NetworkTableEntry entry = getEntry(key);
- return m_inst.addEntryListener(entry,
- event -> listener.valueChanged(this, key, entry, event.value, event.flags), flags);
+ return m_inst.addEntryListener(
+ entry, event -> listener.valueChanged(this, key, entry, event.value, event.flags), flags);
}
/**
* Remove an entry listener.
*
- * @param listener listener handle
+ * @param listener listener handle
*/
public void removeEntryListener(int listener) {
m_inst.removeEntryListener(listener);
}
/**
- * Listen for sub-table creation.
- * This calls the listener once for each newly created sub-table.
+ * Listen for sub-table creation. This calls the listener once for each newly created sub-table.
* It immediately calls the listener for any existing sub-tables.
*
- * @param listener listener to add
- * @param localNotify notify local changes as well as remote
+ * @param listener listener to add
+ * @param localNotify notify local changes as well as remote
* @return Listener handle
*/
public int addSubTableListener(TableListener listener, boolean localNotify) {
@@ -218,38 +208,41 @@
final int prefixLen = m_path.length() + 1;
final NetworkTable parent = this;
- return m_inst.addEntryListener(m_pathWithSep, new Consumer<>() {
- final Set<String> m_notifiedTables = new HashSet<>();
+ return m_inst.addEntryListener(
+ m_pathWithSep,
+ new Consumer<>() {
+ final Set<String> m_notifiedTables = new HashSet<>();
- @Override
- public void accept(EntryNotification event) {
- String relativeKey = event.name.substring(prefixLen);
- int endSubTable = relativeKey.indexOf(PATH_SEPARATOR);
- if (endSubTable == -1) {
- return;
- }
- String subTableKey = relativeKey.substring(0, endSubTable);
- if (m_notifiedTables.contains(subTableKey)) {
- return;
- }
- m_notifiedTables.add(subTableKey);
- listener.tableCreated(parent, subTableKey, parent.getSubTable(subTableKey));
- }
- }, flags);
+ @Override
+ public void accept(EntryNotification event) {
+ String relativeKey = event.name.substring(prefixLen);
+ int endSubTable = relativeKey.indexOf(PATH_SEPARATOR);
+ if (endSubTable == -1) {
+ return;
+ }
+ String subTableKey = relativeKey.substring(0, endSubTable);
+ if (m_notifiedTables.contains(subTableKey)) {
+ return;
+ }
+ m_notifiedTables.add(subTableKey);
+ listener.tableCreated(parent, subTableKey, parent.getSubTable(subTableKey));
+ }
+ },
+ flags);
}
/**
* Remove a sub-table listener.
*
- * @param listener listener handle
+ * @param listener listener handle
*/
public void removeTableListener(int listener) {
m_inst.removeEntryListener(listener);
}
/**
- * Returns the table at the specified key. If there is no table at the
- * specified key, it will create a new table
+ * Returns the table at the specified key. If there is no table at the specified key, it will
+ * create a new table
*
* @param key the name of the table relative to this one
* @return a sub table relative to this one
@@ -276,8 +269,8 @@
* its own
*/
public boolean containsSubTable(String key) {
- int[] handles = NetworkTablesJNI.getEntries(m_inst.getHandle(),
- m_pathWithSep + key + PATH_SEPARATOR, 0);
+ int[] handles =
+ NetworkTablesJNI.getEntries(m_inst.getHandle(), m_pathWithSep + key + PATH_SEPARATOR, 0);
return handles.length != 0;
}
@@ -333,8 +326,7 @@
}
/**
- * Deletes the specified key in this table. The key can
- * not be null.
+ * Deletes the specified key in this table. The key can not be null.
*
* @param key the key name
*/
@@ -376,16 +368,17 @@
/**
* Get the path of the NetworkTable.
+ *
+ * @return The path of the NetworkTable.
*/
public String getPath() {
return m_path;
}
/**
- * Save table values to a file. The file format used is identical to
- * that used for SavePersistent.
+ * Save table values to a file. The file format used is identical to that used for SavePersistent.
*
- * @param filename filename
+ * @param filename filename
* @throws PersistentException if error saving file
*/
public void saveEntries(String filename) throws PersistentException {
@@ -393,10 +386,10 @@
}
/**
- * Load table values from a file. The file format used is identical to
- * that used for SavePersistent / LoadPersistent.
+ * Load table values from a file. The file format used is identical to that used for
+ * SavePersistent / LoadPersistent.
*
- * @param filename filename
+ * @param filename filename
* @return List of warnings (errors result in an exception instead)
* @throws PersistentException if error saving file
*/
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableEntry.java b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableEntry.java
index 6eea5ca..872bc89 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableEntry.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableEntry.java
@@ -1,22 +1,15 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
import java.nio.ByteBuffer;
import java.util.function.Consumer;
-/**
- * NetworkTables Entry.
- */
+/** NetworkTables Entry. */
public final class NetworkTableEntry {
- /**
- * Flag values (as returned by {@link #getFlags()}).
- */
+ /** Flag values (as returned by {@link #getFlags()}). */
public static final int kPersistent = 0x01;
/**
@@ -112,8 +105,7 @@
}
/**
- * Gets the entry's value.
- * Returns a value with type NetworkTableType.kUnassigned if the value
+ * Gets the entry's value. Returns a value with type NetworkTableType.kUnassigned if the value
* does not exist.
*
* @return the entry's value
@@ -123,8 +115,8 @@
}
/**
- * Gets the entry's value as a boolean. If the entry does not exist or is of
- * different type, it will return the default value.
+ * Gets the entry's value as a boolean. If the entry does not exist or is of different type, it
+ * will return the default value.
*
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
@@ -134,8 +126,8 @@
}
/**
- * Gets the entry's value as a double. If the entry does not exist or is of
- * different type, it will return the default value.
+ * Gets the entry's value as a double. If the entry does not exist or is of different type, it
+ * will return the default value.
*
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
@@ -145,8 +137,8 @@
}
/**
- * Gets the entry's value as a double. If the entry does not exist or is of
- * different type, it will return the default value.
+ * Gets the entry's value as a double. If the entry does not exist or is of different type, it
+ * will return the default value.
*
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
@@ -156,8 +148,8 @@
}
/**
- * Gets the entry's value as a string. If the entry does not exist or is of
- * different type, it will return the default value.
+ * Gets the entry's value as a string. If the entry does not exist or is of different type, it
+ * will return the default value.
*
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
@@ -167,8 +159,8 @@
}
/**
- * Gets the entry's value as a raw value (byte array). If the entry does not
- * exist or is of different type, it will return the default value.
+ * Gets the entry's value as a raw value (byte array). If the entry does not exist or is of
+ * different type, it will return the default value.
*
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
@@ -178,8 +170,8 @@
}
/**
- * Gets the entry's value as a boolean array. If the entry does not exist
- * or is of different type, it will return the default value.
+ * Gets the entry's value as a boolean array. If the entry does not exist or is of different type,
+ * it will return the default value.
*
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
@@ -189,20 +181,20 @@
}
/**
- * Gets the entry's value as a boolean array. If the entry does not exist
- * or is of different type, it will return the default value.
+ * Gets the entry's value as a boolean array. If the entry does not exist or is of different type,
+ * it will return the default value.
*
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
public Boolean[] getBooleanArray(Boolean[] defaultValue) {
- return NetworkTableValue.fromNative(NetworkTablesJNI.getBooleanArray(m_handle,
- NetworkTableValue.toNative(defaultValue)));
+ return NetworkTableValue.fromNative(
+ NetworkTablesJNI.getBooleanArray(m_handle, NetworkTableValue.toNative(defaultValue)));
}
/**
- * Gets the entry's value as a double array. If the entry does not exist
- * or is of different type, it will return the default value.
+ * Gets the entry's value as a double array. If the entry does not exist or is of different type,
+ * it will return the default value.
*
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
@@ -212,32 +204,32 @@
}
/**
- * Gets the entry's value as a double array. If the entry does not exist
- * or is of different type, it will return the default value.
+ * Gets the entry's value as a double array. If the entry does not exist or is of different type,
+ * it will return the default value.
*
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
public Double[] getDoubleArray(Double[] defaultValue) {
- return NetworkTableValue.fromNative(NetworkTablesJNI.getDoubleArray(m_handle,
- NetworkTableValue.toNative(defaultValue)));
+ return NetworkTableValue.fromNative(
+ NetworkTablesJNI.getDoubleArray(m_handle, NetworkTableValue.toNative(defaultValue)));
}
/**
- * Gets the entry's value as a double array. If the entry does not exist
- * or is of different type, it will return the default value.
+ * Gets the entry's value as a double array. If the entry does not exist or is of different type,
+ * it will return the default value.
*
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
public Number[] getNumberArray(Number[] defaultValue) {
- return NetworkTableValue.fromNative(NetworkTablesJNI.getDoubleArray(m_handle,
- NetworkTableValue.toNative(defaultValue)));
+ return NetworkTableValue.fromNative(
+ NetworkTablesJNI.getDoubleArray(m_handle, NetworkTableValue.toNative(defaultValue)));
}
/**
- * Gets the entry's value as a string array. If the entry does not exist
- * or is of different type, it will return the default value.
+ * Gets the entry's value as a string array. If the entry does not exist or is of different type,
+ * it will return the default value.
*
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
@@ -279,11 +271,10 @@
Object otherValue = ((NetworkTableValue) defaultValue).getValue();
switch (((NetworkTableValue) defaultValue).getType()) {
case kBoolean:
- return NetworkTablesJNI.setDefaultBoolean(m_handle, time,
- (Boolean) otherValue);
+ return NetworkTablesJNI.setDefaultBoolean(m_handle, time, (Boolean) otherValue);
case kDouble:
- return NetworkTablesJNI.setDefaultDouble(m_handle, time,
- ((Number) otherValue).doubleValue());
+ return NetworkTablesJNI.setDefaultDouble(
+ m_handle, time, ((Number) otherValue).doubleValue());
case kString:
return NetworkTablesJNI.setDefaultString(m_handle, time, (String) otherValue);
case kRaw:
@@ -305,21 +296,21 @@
return setDefaultNumber((Number) defaultValue);
} else if (defaultValue instanceof String) {
return setDefaultString((String) defaultValue);
- } else if (defaultValue instanceof byte[]) {
+ } else if (defaultValue instanceof byte[]) {
return setDefaultRaw((byte[]) defaultValue);
- } else if (defaultValue instanceof boolean[]) {
+ } else if (defaultValue instanceof boolean[]) {
return setDefaultBooleanArray((boolean[]) defaultValue);
- } else if (defaultValue instanceof double[]) {
+ } else if (defaultValue instanceof double[]) {
return setDefaultDoubleArray((double[]) defaultValue);
- } else if (defaultValue instanceof Boolean[]) {
+ } else if (defaultValue instanceof Boolean[]) {
return setDefaultBooleanArray((Boolean[]) defaultValue);
- } else if (defaultValue instanceof Number[]) {
+ } else if (defaultValue instanceof Number[]) {
return setDefaultNumberArray((Number[]) defaultValue);
- } else if (defaultValue instanceof String[]) {
+ } else if (defaultValue instanceof String[]) {
return setDefaultStringArray((String[]) defaultValue);
} else {
- throw new IllegalArgumentException("Value of type " + defaultValue.getClass().getName()
- + " cannot be put into a table");
+ throw new IllegalArgumentException(
+ "Value of type " + defaultValue.getClass().getName() + " cannot be put into a table");
}
}
@@ -390,8 +381,8 @@
* @return False if the entry exists with a different type
*/
public boolean setDefaultBooleanArray(Boolean[] defaultValue) {
- return NetworkTablesJNI.setDefaultBooleanArray(m_handle,
- 0, NetworkTableValue.toNative(defaultValue));
+ return NetworkTablesJNI.setDefaultBooleanArray(
+ m_handle, 0, NetworkTableValue.toNative(defaultValue));
}
/**
@@ -411,8 +402,8 @@
* @return False if the entry exists with a different type
*/
public boolean setDefaultNumberArray(Number[] defaultValue) {
- return NetworkTablesJNI.setDefaultDoubleArray(m_handle,
- 0, NetworkTableValue.toNative(defaultValue));
+ return NetworkTablesJNI.setDefaultDoubleArray(
+ m_handle, 0, NetworkTableValue.toNative(defaultValue));
}
/**
@@ -438,11 +429,10 @@
Object otherValue = ((NetworkTableValue) value).getValue();
switch (((NetworkTableValue) value).getType()) {
case kBoolean:
- return NetworkTablesJNI.setBoolean(m_handle, time, (Boolean) otherValue,
- false);
+ return NetworkTablesJNI.setBoolean(m_handle, time, (Boolean) otherValue, false);
case kDouble:
- return NetworkTablesJNI.setDouble(m_handle, time, ((Number) otherValue).doubleValue(),
- false);
+ return NetworkTablesJNI.setDouble(
+ m_handle, time, ((Number) otherValue).doubleValue(), false);
case kString:
return NetworkTablesJNI.setString(m_handle, time, (String) otherValue, false);
case kRaw:
@@ -477,8 +467,8 @@
} else if (value instanceof String[]) {
return setStringArray((String[]) value);
} else {
- throw new IllegalArgumentException("Value of type " + value.getClass().getName()
- + " cannot be put into a table");
+ throw new IllegalArgumentException(
+ "Value of type " + value.getClass().getName() + " cannot be put into a table");
}
}
@@ -600,8 +590,8 @@
}
/**
- * Sets the entry's value. If the value is of different type, the type is
- * changed to match the new value.
+ * Sets the entry's value. If the value is of different type, the type is changed to match the new
+ * value.
*
* @param value the value to set
* @throws IllegalArgumentException if the value is not a known type
@@ -656,14 +646,14 @@
} else if (value instanceof String[]) {
forceSetStringArray((String[]) value);
} else {
- throw new IllegalArgumentException("Value of type " + value.getClass().getName()
- + " cannot be put into a table");
+ throw new IllegalArgumentException(
+ "Value of type " + value.getClass().getName() + " cannot be put into a table");
}
}
/**
- * Sets the entry's value. If the value is of different type, the type is
- * changed to match the new value.
+ * Sets the entry's value. If the value is of different type, the type is changed to match the new
+ * value.
*
* @param value the value to set
*/
@@ -672,8 +662,8 @@
}
/**
- * Sets the entry's value. If the value is of different type, the type is
- * changed to match the new value.
+ * Sets the entry's value. If the value is of different type, the type is changed to match the new
+ * value.
*
* @param value the value to set
*/
@@ -682,8 +672,8 @@
}
/**
- * Sets the entry's value. If the value is of different type, the type is
- * changed to match the new value.
+ * Sets the entry's value. If the value is of different type, the type is changed to match the new
+ * value.
*
* @param value the value to set
*/
@@ -692,8 +682,8 @@
}
/**
- * Sets the entry's value. If the value is of different type, the type is
- * changed to match the new value.
+ * Sets the entry's value. If the value is of different type, the type is changed to match the new
+ * value.
*
* @param value the value to set
*/
@@ -702,8 +692,8 @@
}
/**
- * Sets the entry's value. If the value is of different type, the type is
- * changed to match the new value.
+ * Sets the entry's value. If the value is of different type, the type is changed to match the new
+ * value.
*
* @param value the value to set
*/
@@ -712,8 +702,8 @@
}
/**
- * Sets the entry's value. If the value is of different type, the type is
- * changed to match the new value.
+ * Sets the entry's value. If the value is of different type, the type is changed to match the new
+ * value.
*
* @param value the value to set
*/
@@ -722,8 +712,8 @@
}
/**
- * Sets the entry's value. If the value is of different type, the type is
- * changed to match the new value.
+ * Sets the entry's value. If the value is of different type, the type is changed to match the new
+ * value.
*
* @param value the value to set
*/
@@ -732,8 +722,8 @@
}
/**
- * Sets the entry's value. If the value is of different type, the type is
- * changed to match the new value.
+ * Sets the entry's value. If the value is of different type, the type is changed to match the new
+ * value.
*
* @param value the value to set
*/
@@ -742,8 +732,8 @@
}
/**
- * Sets the entry's value. If the value is of different type, the type is
- * changed to match the new value.
+ * Sets the entry's value. If the value is of different type, the type is changed to match the new
+ * value.
*
* @param value the value to set
*/
@@ -752,8 +742,8 @@
}
/**
- * Sets the entry's value. If the value is of different type, the type is
- * changed to match the new value.
+ * Sets the entry's value. If the value is of different type, the type is changed to match the new
+ * value.
*
* @param value the value to set
*/
@@ -779,16 +769,12 @@
NetworkTablesJNI.setEntryFlags(m_handle, getFlags() & ~flags);
}
- /**
- * Make value persistent through program restarts.
- */
+ /** Make value persistent through program restarts. */
public void setPersistent() {
setFlags(kPersistent);
}
- /**
- * Stop making value persistent through program restarts.
- */
+ /** Stop making value persistent through program restarts. */
public void clearPersistent() {
clearFlags(kPersistent);
}
@@ -802,31 +788,28 @@
return (getFlags() & kPersistent) != 0;
}
- /**
- * Deletes the entry.
- */
+ /** Deletes the entry. */
public void delete() {
NetworkTablesJNI.deleteEntry(m_handle);
}
/**
- * Create a callback-based RPC entry point. Only valid to use on the server.
- * The callback function will be called when the RPC is called.
- * This function creates RPC version 0 definitions (raw data in and out).
+ * Create a callback-based RPC entry point. Only valid to use on the server. The callback function
+ * will be called when the RPC is called. This function creates RPC version 0 definitions (raw
+ * data in and out).
*
- * @param callback callback function
+ * @param callback callback function
*/
public void createRpc(Consumer<RpcAnswer> callback) {
m_inst.createRpc(this, callback);
}
/**
- * Call a RPC function. May be used on either the client or server.
- * This function is non-blocking. Either {@link RpcCall#getResult()} or
- * {@link RpcCall#cancelResult()} must be called on the return value to either
- * get or ignore the result of the call.
+ * Call a RPC function. May be used on either the client or server. This function is non-blocking.
+ * Either {@link RpcCall#getResult()} or {@link RpcCall#cancelResult()} must be called on the
+ * return value to either get or ignore the result of the call.
*
- * @param params parameter
+ * @param params parameter
* @return RPC call object.
*/
public RpcCall callRpc(byte[] params) {
@@ -870,6 +853,6 @@
return m_handle;
}
- private NetworkTableInstance m_inst;
- private int m_handle;
+ private final NetworkTableInstance m_inst;
+ private final int m_handle;
}
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableInstance.java b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableInstance.java
index 06dad09..162327e 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableInstance.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableInstance.java
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
@@ -19,36 +16,33 @@
/**
* NetworkTables Instance.
*
- * <p>Instances are completely independent from each other. Table operations on
- * one instance will not be visible to other instances unless the instances are
- * connected via the network. The main limitation on instances is that you
- * cannot have two servers on the same network port. The main utility of
- * instances is for unit testing, but they can also enable one program to
- * connect to two different NetworkTables networks.
+ * <p>Instances are completely independent from each other. Table operations on one instance will
+ * not be visible to other instances unless the instances are connected via the network. The main
+ * limitation on instances is that you cannot have two servers on the same network port. The main
+ * utility of instances is for unit testing, but they can also enable one program to connect to two
+ * different NetworkTables networks.
*
- * <p>The global "default" instance (as returned by {@link #getDefault()}) is
- * always available, and is intended for the common case when there is only
- * a single NetworkTables instance being used in the program.
+ * <p>The global "default" instance (as returned by {@link #getDefault()}) is always available, and
+ * is intended for the common case when there is only a single NetworkTables instance being used in
+ * the program.
*
- * <p>Additional instances can be created with the {@link #create()} function.
- * A reference must be kept to the NetworkTableInstance returned by this
- * function to keep it from being garbage collected.
+ * <p>Additional instances can be created with the {@link #create()} function. A reference must be
+ * kept to the NetworkTableInstance returned by this function to keep it from being garbage
+ * collected.
*/
public final class NetworkTableInstance implements AutoCloseable {
/**
- * Client/server mode flag values (as returned by {@link #getNetworkMode()}).
- * This is a bitmask.
+ * Client/server mode flag values (as returned by {@link #getNetworkMode()}). This is a bitmask.
*/
public static final int kNetModeNone = 0x00;
+
public static final int kNetModeServer = 0x01;
public static final int kNetModeClient = 0x02;
public static final int kNetModeStarting = 0x04;
public static final int kNetModeFailure = 0x08;
public static final int kNetModeLocal = 0x10;
- /**
- * The default port that network tables operates on.
- */
+ /** The default port that network tables operates on. */
public static final int kDefaultPort = 1735;
/**
@@ -61,9 +55,7 @@
m_handle = handle;
}
- /**
- * Destroys the instance (if created by {@link #create()}).
- */
+ /** Destroys the instance (if created by {@link #create()}). */
@Override
public synchronized void close() {
if (m_owned && m_handle != 0) {
@@ -96,8 +88,7 @@
}
/**
- * Create an instance.
- * Note: A reference to the returned instance must be retained to ensure the
+ * Create an instance. Note: A reference to the returned instance must be retained to ensure the
* instance is not garbage collected.
*
* @return Newly created instance
@@ -128,12 +119,11 @@
}
/**
- * Get entries starting with the given prefix.
- * The results are optionally filtered by string prefix and entry type to
- * only return a subset of all entries.
+ * Get entries starting with the given prefix. The results are optionally filtered by string
+ * prefix and entry type to only return a subset of all entries.
*
- * @param prefix entry name required prefix; only entries whose name
- * starts with this string are returned
+ * @param prefix entry name required prefix; only entries whose name starts with this string are
+ * returned
* @param types bitmask of types; 0 is treated as a "don't care"
* @return Array of entries.
*/
@@ -147,12 +137,11 @@
}
/**
- * Get information about entries starting with the given prefix.
- * The results are optionally filtered by string prefix and entry type to
- * only return a subset of all entries.
+ * Get information about entries starting with the given prefix. The results are optionally
+ * filtered by string prefix and entry type to only return a subset of all entries.
*
- * @param prefix entry name required prefix; only entries whose name
- * starts with this string are returned
+ * @param prefix entry name required prefix; only entries whose name starts with this string are
+ * returned
* @param types bitmask of types; 0 is treated as a "don't care"
* @return Array of entry information.
*/
@@ -172,7 +161,7 @@
public NetworkTable getTable(String key) {
// prepend leading / if not present
String theKey;
- if (key.isEmpty() || key.equals("/")) {
+ if (key.isEmpty() || "/".equals(key)) {
theKey = "";
} else if (key.charAt(0) == NetworkTable.PATH_SEPARATOR) {
theKey = key;
@@ -192,10 +181,7 @@
return table;
}
- /**
- * Deletes ALL keys in ALL subtables (except persistent values).
- * Use with caution!
- */
+ /** Deletes ALL keys in ALL subtables (except persistent values). Use with caution! */
public void deleteAllEntries() {
NetworkTablesJNI.deleteAllEntries(m_handle);
}
@@ -216,74 +202,78 @@
private final ReentrantLock m_entryListenerLock = new ReentrantLock();
private final Map<Integer, EntryConsumer<EntryNotification>> m_entryListeners = new HashMap<>();
- private Thread m_entryListenerThread;
private int m_entryListenerPoller;
private boolean m_entryListenerWaitQueue;
private final Condition m_entryListenerWaitQueueCond = m_entryListenerLock.newCondition();
+ @SuppressWarnings("PMD.AvoidCatchingThrowable")
private void startEntryListenerThread() {
- m_entryListenerThread = new Thread(() -> {
- boolean wasInterrupted = false;
- while (!Thread.interrupted()) {
- EntryNotification[] events;
- try {
- events = NetworkTablesJNI.pollEntryListener(this, m_entryListenerPoller);
- } catch (InterruptedException ex) {
- m_entryListenerLock.lock();
- try {
- if (m_entryListenerWaitQueue) {
- m_entryListenerWaitQueue = false;
- m_entryListenerWaitQueueCond.signalAll();
- continue;
- }
- } finally {
- m_entryListenerLock.unlock();
- }
- Thread.currentThread().interrupt();
- // don't try to destroy poller, as its handle is likely no longer valid
- wasInterrupted = true;
- break;
- }
- for (EntryNotification event : events) {
- EntryConsumer<EntryNotification> listener;
- m_entryListenerLock.lock();
- try {
- listener = m_entryListeners.get(event.listener);
- } finally {
- m_entryListenerLock.unlock();
- }
- if (listener != null) {
- event.m_entryObject = listener.m_entry;
- try {
- listener.m_consumer.accept(event);
- } catch (Throwable throwable) {
- System.err.println("Unhandled exception during entry listener callback: "
- + throwable.toString());
- throwable.printStackTrace();
- }
- }
- }
- }
- m_entryListenerLock.lock();
- try {
- if (!wasInterrupted) {
- NetworkTablesJNI.destroyEntryListenerPoller(m_entryListenerPoller);
- }
- m_entryListenerPoller = 0;
- } finally {
- m_entryListenerLock.unlock();
- }
- }, "NTEntryListener");
- m_entryListenerThread.setDaemon(true);
- m_entryListenerThread.start();
+ var entryListenerThread =
+ new Thread(
+ () -> {
+ boolean wasInterrupted = false;
+ while (!Thread.interrupted()) {
+ EntryNotification[] events;
+ try {
+ events = NetworkTablesJNI.pollEntryListener(this, m_entryListenerPoller);
+ } catch (InterruptedException ex) {
+ m_entryListenerLock.lock();
+ try {
+ if (m_entryListenerWaitQueue) {
+ m_entryListenerWaitQueue = false;
+ m_entryListenerWaitQueueCond.signalAll();
+ continue;
+ }
+ } finally {
+ m_entryListenerLock.unlock();
+ }
+ Thread.currentThread().interrupt();
+ // don't try to destroy poller, as its handle is likely no longer valid
+ wasInterrupted = true;
+ break;
+ }
+ for (EntryNotification event : events) {
+ EntryConsumer<EntryNotification> listener;
+ m_entryListenerLock.lock();
+ try {
+ listener = m_entryListeners.get(event.listener);
+ } finally {
+ m_entryListenerLock.unlock();
+ }
+ if (listener != null) {
+ event.m_entryObject = listener.m_entry;
+ try {
+ listener.m_consumer.accept(event);
+ } catch (Throwable throwable) {
+ System.err.println(
+ "Unhandled exception during entry listener callback: "
+ + throwable.toString());
+ throwable.printStackTrace();
+ }
+ }
+ }
+ }
+ m_entryListenerLock.lock();
+ try {
+ if (!wasInterrupted) {
+ NetworkTablesJNI.destroyEntryListenerPoller(m_entryListenerPoller);
+ }
+ m_entryListenerPoller = 0;
+ } finally {
+ m_entryListenerLock.unlock();
+ }
+ },
+ "NTEntryListener");
+ entryListenerThread.setDaemon(true);
+ entryListenerThread.start();
}
/**
* Add a listener for all entries starting with a certain prefix.
*
- * @param prefix UTF-8 string prefix
- * @param listener listener to add
- * @param flags {@link EntryListenerFlags} bitmask
+ * @param prefix UTF-8 string prefix
+ * @param listener listener to add
+ * @param flags {@link EntryListenerFlags} bitmask
* @return Listener handle
*/
public int addEntryListener(String prefix, Consumer<EntryNotification> listener, int flags) {
@@ -304,14 +294,13 @@
/**
* Add a listener for a particular entry.
*
- * @param entry the entry
- * @param listener listener to add
- * @param flags {@link EntryListenerFlags} bitmask
+ * @param entry the entry
+ * @param listener listener to add
+ * @param flags {@link EntryListenerFlags} bitmask
* @return Listener handle
*/
- public int addEntryListener(NetworkTableEntry entry,
- Consumer<EntryNotification> listener,
- int flags) {
+ public int addEntryListener(
+ NetworkTableEntry entry, Consumer<EntryNotification> listener, int flags) {
if (!equals(entry.getInstance())) {
throw new IllegalArgumentException("entry does not belong to this instance");
}
@@ -321,8 +310,8 @@
m_entryListenerPoller = NetworkTablesJNI.createEntryListenerPoller(m_handle);
startEntryListenerThread();
}
- int handle = NetworkTablesJNI.addPolledEntryListener(m_entryListenerPoller, entry.getHandle(),
- flags);
+ int handle =
+ NetworkTablesJNI.addPolledEntryListener(m_entryListenerPoller, entry.getHandle(), flags);
m_entryListeners.put(handle, new EntryConsumer<>(entry, listener));
return handle;
} finally {
@@ -340,15 +329,14 @@
}
/**
- * Wait for the entry listener queue to be empty. This is primarily useful
- * for deterministic testing. This blocks until either the entry listener
- * queue is empty (e.g. there are no more events that need to be passed along
- * to callbacks or poll queues) or the timeout expires.
+ * Wait for the entry listener queue to be empty. This is primarily useful for deterministic
+ * testing. This blocks until either the entry listener queue is empty (e.g. there are no more
+ * events that need to be passed along to callbacks or poll queues) or the timeout expires.
*
- * @param timeout timeout, in seconds. Set to 0 for non-blocking behavior,
- * or a negative value to block indefinitely
+ * @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or a negative value to
+ * block indefinitely
* @return False if timed out, otherwise true.
- */
+ */
public boolean waitForEntryListenerQueue(double timeout) {
if (!NetworkTablesJNI.waitForEntryListenerQueue(m_handle, timeout)) {
return false;
@@ -363,8 +351,8 @@
if (timeout < 0) {
m_entryListenerWaitQueueCond.await();
} else {
- return m_entryListenerWaitQueueCond.await((long) (timeout * 1e9),
- TimeUnit.NANOSECONDS);
+ return m_entryListenerWaitQueueCond.await(
+ (long) (timeout * 1e9), TimeUnit.NANOSECONDS);
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
@@ -379,68 +367,73 @@
}
private final ReentrantLock m_connectionListenerLock = new ReentrantLock();
- private final Map<Integer, Consumer<ConnectionNotification>> m_connectionListeners
- = new HashMap<>();
- private Thread m_connectionListenerThread;
+ private final Map<Integer, Consumer<ConnectionNotification>> m_connectionListeners =
+ new HashMap<>();
private int m_connectionListenerPoller;
private boolean m_connectionListenerWaitQueue;
- private final Condition m_connectionListenerWaitQueueCond
- = m_connectionListenerLock.newCondition();
+ private final Condition m_connectionListenerWaitQueueCond =
+ m_connectionListenerLock.newCondition();
+ @SuppressWarnings("PMD.AvoidCatchingThrowable")
private void startConnectionListenerThread() {
- m_connectionListenerThread = new Thread(() -> {
- boolean wasInterrupted = false;
- while (!Thread.interrupted()) {
- ConnectionNotification[] events;
- try {
- events = NetworkTablesJNI.pollConnectionListener(this, m_connectionListenerPoller);
- } catch (InterruptedException ex) {
- m_connectionListenerLock.lock();
- try {
- if (m_connectionListenerWaitQueue) {
- m_connectionListenerWaitQueue = false;
- m_connectionListenerWaitQueueCond.signalAll();
- continue;
- }
- } finally {
- m_connectionListenerLock.unlock();
- }
- Thread.currentThread().interrupt();
- // don't try to destroy poller, as its handle is likely no longer valid
- wasInterrupted = true;
- break;
- }
- for (ConnectionNotification event : events) {
- Consumer<ConnectionNotification> listener;
- m_connectionListenerLock.lock();
- try {
- listener = m_connectionListeners.get(event.listener);
- } finally {
- m_connectionListenerLock.unlock();
- }
- if (listener != null) {
- try {
- listener.accept(event);
- } catch (Throwable throwable) {
- System.err.println("Unhandled exception during connection listener callback: "
- + throwable.toString());
- throwable.printStackTrace();
- }
- }
- }
- }
- m_connectionListenerLock.lock();
- try {
- if (!wasInterrupted) {
- NetworkTablesJNI.destroyConnectionListenerPoller(m_connectionListenerPoller);
- }
- m_connectionListenerPoller = 0;
- } finally {
- m_connectionListenerLock.unlock();
- }
- }, "NTConnectionListener");
- m_connectionListenerThread.setDaemon(true);
- m_connectionListenerThread.start();
+ var connectionListenerThread =
+ new Thread(
+ () -> {
+ boolean wasInterrupted = false;
+ while (!Thread.interrupted()) {
+ ConnectionNotification[] events;
+ try {
+ events =
+ NetworkTablesJNI.pollConnectionListener(this, m_connectionListenerPoller);
+ } catch (InterruptedException ex) {
+ m_connectionListenerLock.lock();
+ try {
+ if (m_connectionListenerWaitQueue) {
+ m_connectionListenerWaitQueue = false;
+ m_connectionListenerWaitQueueCond.signalAll();
+ continue;
+ }
+ } finally {
+ m_connectionListenerLock.unlock();
+ }
+ Thread.currentThread().interrupt();
+ // don't try to destroy poller, as its handle is likely no longer valid
+ wasInterrupted = true;
+ break;
+ }
+ for (ConnectionNotification event : events) {
+ Consumer<ConnectionNotification> listener;
+ m_connectionListenerLock.lock();
+ try {
+ listener = m_connectionListeners.get(event.listener);
+ } finally {
+ m_connectionListenerLock.unlock();
+ }
+ if (listener != null) {
+ try {
+ listener.accept(event);
+ } catch (Throwable throwable) {
+ System.err.println(
+ "Unhandled exception during connection listener callback: "
+ + throwable.toString());
+ throwable.printStackTrace();
+ }
+ }
+ }
+ }
+ m_connectionListenerLock.lock();
+ try {
+ if (!wasInterrupted) {
+ NetworkTablesJNI.destroyConnectionListenerPoller(m_connectionListenerPoller);
+ }
+ m_connectionListenerPoller = 0;
+ } finally {
+ m_connectionListenerLock.unlock();
+ }
+ },
+ "NTConnectionListener");
+ connectionListenerThread.setDaemon(true);
+ connectionListenerThread.start();
}
/**
@@ -450,16 +443,16 @@
* @param immediateNotify Notify listener of all existing connections
* @return Listener handle
*/
- public int addConnectionListener(Consumer<ConnectionNotification> listener,
- boolean immediateNotify) {
+ public int addConnectionListener(
+ Consumer<ConnectionNotification> listener, boolean immediateNotify) {
m_connectionListenerLock.lock();
try {
if (m_connectionListenerPoller == 0) {
m_connectionListenerPoller = NetworkTablesJNI.createConnectionListenerPoller(m_handle);
startConnectionListenerThread();
}
- int handle = NetworkTablesJNI.addPolledConnectionListener(m_connectionListenerPoller,
- immediateNotify);
+ int handle =
+ NetworkTablesJNI.addPolledConnectionListener(m_connectionListenerPoller, immediateNotify);
m_connectionListeners.put(handle, listener);
return handle;
} finally {
@@ -483,13 +476,12 @@
}
/**
- * Wait for the connection listener queue to be empty. This is primarily useful
- * for deterministic testing. This blocks until either the connection listener
- * queue is empty (e.g. there are no more events that need to be passed along
- * to callbacks or poll queues) or the timeout expires.
+ * Wait for the connection listener queue to be empty. This is primarily useful for deterministic
+ * testing. This blocks until either the connection listener queue is empty (e.g. there are no
+ * more events that need to be passed along to callbacks or poll queues) or the timeout expires.
*
- * @param timeout timeout, in seconds. Set to 0 for non-blocking behavior,
- * or a negative value to block indefinitely
+ * @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or a negative value to
+ * block indefinitely
* @return False if timed out, otherwise true.
*/
public boolean waitForConnectionListenerQueue(double timeout) {
@@ -506,8 +498,8 @@
if (timeout < 0) {
m_connectionListenerWaitQueueCond.await();
} else {
- return m_connectionListenerWaitQueueCond.await((long) (timeout * 1e9),
- TimeUnit.NANOSECONDS);
+ return m_connectionListenerWaitQueueCond.await(
+ (long) (timeout * 1e9), TimeUnit.NANOSECONDS);
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
@@ -527,78 +519,81 @@
private final ReentrantLock m_rpcCallLock = new ReentrantLock();
private final Map<Integer, EntryConsumer<RpcAnswer>> m_rpcCalls = new HashMap<>();
- private Thread m_rpcCallThread;
private int m_rpcCallPoller;
private boolean m_rpcCallWaitQueue;
private final Condition m_rpcCallWaitQueueCond = m_rpcCallLock.newCondition();
+ @SuppressWarnings("PMD.AvoidCatchingThrowable")
private void startRpcCallThread() {
- m_rpcCallThread = new Thread(() -> {
- boolean wasInterrupted = false;
- while (!Thread.interrupted()) {
- RpcAnswer[] events;
- try {
- events = NetworkTablesJNI.pollRpc(this, m_rpcCallPoller);
- } catch (InterruptedException ex) {
- m_rpcCallLock.lock();
- try {
- if (m_rpcCallWaitQueue) {
- m_rpcCallWaitQueue = false;
- m_rpcCallWaitQueueCond.signalAll();
- continue;
- }
- } finally {
- m_rpcCallLock.unlock();
- }
- Thread.currentThread().interrupt();
- // don't try to destroy poller, as its handle is likely no longer valid
- wasInterrupted = true;
- break;
- }
- for (RpcAnswer event : events) {
- EntryConsumer<RpcAnswer> listener;
- m_rpcCallLock.lock();
- try {
- listener = m_rpcCalls.get(event.entry);
- } finally {
- m_rpcCallLock.unlock();
- }
- if (listener != null) {
- event.m_entryObject = listener.m_entry;
- try {
- listener.m_consumer.accept(event);
- } catch (Throwable throwable) {
- System.err.println("Unhandled exception during RPC callback: "
- + throwable.toString());
- throwable.printStackTrace();
- }
- event.finish();
- }
- }
- }
- m_rpcCallLock.lock();
- try {
- if (!wasInterrupted) {
- NetworkTablesJNI.destroyRpcCallPoller(m_rpcCallPoller);
- }
- m_rpcCallPoller = 0;
- } finally {
- m_rpcCallLock.unlock();
- }
- }, "NTRpcCall");
- m_rpcCallThread.setDaemon(true);
- m_rpcCallThread.start();
+ var rpcCallThread =
+ new Thread(
+ () -> {
+ boolean wasInterrupted = false;
+ while (!Thread.interrupted()) {
+ RpcAnswer[] events;
+ try {
+ events = NetworkTablesJNI.pollRpc(this, m_rpcCallPoller);
+ } catch (InterruptedException ex) {
+ m_rpcCallLock.lock();
+ try {
+ if (m_rpcCallWaitQueue) {
+ m_rpcCallWaitQueue = false;
+ m_rpcCallWaitQueueCond.signalAll();
+ continue;
+ }
+ } finally {
+ m_rpcCallLock.unlock();
+ }
+ Thread.currentThread().interrupt();
+ // don't try to destroy poller, as its handle is likely no longer valid
+ wasInterrupted = true;
+ break;
+ }
+ for (RpcAnswer event : events) {
+ EntryConsumer<RpcAnswer> listener;
+ m_rpcCallLock.lock();
+ try {
+ listener = m_rpcCalls.get(event.entry);
+ } finally {
+ m_rpcCallLock.unlock();
+ }
+ if (listener != null) {
+ event.m_entryObject = listener.m_entry;
+ try {
+ listener.m_consumer.accept(event);
+ } catch (Throwable throwable) {
+ System.err.println(
+ "Unhandled exception during RPC callback: " + throwable.toString());
+ throwable.printStackTrace();
+ }
+ event.finish();
+ }
+ }
+ }
+ m_rpcCallLock.lock();
+ try {
+ if (!wasInterrupted) {
+ NetworkTablesJNI.destroyRpcCallPoller(m_rpcCallPoller);
+ }
+ m_rpcCallPoller = 0;
+ } finally {
+ m_rpcCallLock.unlock();
+ }
+ },
+ "NTRpcCall");
+ rpcCallThread.setDaemon(true);
+ rpcCallThread.start();
}
private static final byte[] rev0def = new byte[] {0};
/**
- * Create a callback-based RPC entry point. Only valid to use on the server.
- * The callback function will be called when the RPC is called.
- * This function creates RPC version 0 definitions (raw data in and out).
+ * Create a callback-based RPC entry point. Only valid to use on the server. The callback function
+ * will be called when the RPC is called. This function creates RPC version 0 definitions (raw
+ * data in and out).
*
- * @param entry the entry
- * @param callback callback function
+ * @param entry the entry
+ * @param callback callback function
*/
public void createRpc(NetworkTableEntry entry, Consumer<RpcAnswer> callback) {
m_rpcCallLock.lock();
@@ -615,13 +610,12 @@
}
/**
- * Wait for the incoming RPC call queue to be empty. This is primarily useful
- * for deterministic testing. This blocks until either the RPC call
- * queue is empty (e.g. there are no more events that need to be passed along
- * to callbacks or poll queues) or the timeout expires.
+ * Wait for the incoming RPC call queue to be empty. This is primarily useful for deterministic
+ * testing. This blocks until either the RPC call queue is empty (e.g. there are no more events
+ * that need to be passed along to callbacks or poll queues) or the timeout expires.
*
- * @param timeout timeout, in seconds. Set to 0 for non-blocking behavior,
- * or a negative value to block indefinitely
+ * @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or a negative value to
+ * block indefinitely
* @return False if timed out, otherwise true.
*/
public boolean waitForRpcCallQueue(double timeout) {
@@ -657,11 +651,10 @@
*/
/**
- * Set the network identity of this node.
- * This is the name used during the initial connection handshake, and is
- * visible through ConnectionInfo on the remote node.
+ * Set the network identity of this node. This is the name used during the initial connection
+ * handshake, and is visible through ConnectionInfo on the remote node.
*
- * @param name identity to advertise
+ * @param name identity to advertise
*/
public void setNetworkIdentity(String name) {
NetworkTablesJNI.setNetworkIdentity(m_handle, name);
@@ -677,47 +670,44 @@
}
/**
- * Starts local-only operation. Prevents calls to startServer or startClient
- * from taking effect. Has no effect if startServer or startClient
- * has already been called.
+ * Starts local-only operation. Prevents calls to startServer or startClient from taking effect.
+ * Has no effect if startServer or startClient has already been called.
*/
public void startLocal() {
NetworkTablesJNI.startLocal(m_handle);
}
/**
- * Stops local-only operation. startServer or startClient can be called after
- * this call to start a server or client.
+ * Stops local-only operation. startServer or startClient can be called after this call to start a
+ * server or client.
*/
public void stopLocal() {
NetworkTablesJNI.stopLocal(m_handle);
}
/**
- * Starts a server using the networktables.ini as the persistent file,
- * using the default listening address and port.
+ * Starts a server using the networktables.ini as the persistent file, using the default listening
+ * address and port.
*/
public void startServer() {
startServer("networktables.ini");
}
/**
- * Starts a server using the specified persistent filename, using the default
- * listening address and port.
+ * Starts a server using the specified persistent filename, using the default listening address
+ * and port.
*
- * @param persistFilename the name of the persist file to use
+ * @param persistFilename the name of the persist file to use
*/
public void startServer(String persistFilename) {
startServer(persistFilename, "");
}
/**
- * Starts a server using the specified filename and listening address,
- * using the default port.
+ * Starts a server using the specified filename and listening address, using the default port.
*
- * @param persistFilename the name of the persist file to use
- * @param listenAddress the address to listen on, or empty to listen on any
- * address
+ * @param persistFilename the name of the persist file to use
+ * @param listenAddress the address to listen on, or empty to listen on any address
*/
public void startServer(String persistFilename, String listenAddress) {
startServer(persistFilename, listenAddress, kDefaultPort);
@@ -726,25 +716,20 @@
/**
* Starts a server using the specified filename, listening address, and port.
*
- * @param persistFilename the name of the persist file to use
- * @param listenAddress the address to listen on, or empty to listen on any
- * address
- * @param port port to communicate over
+ * @param persistFilename the name of the persist file to use
+ * @param listenAddress the address to listen on, or empty to listen on any address
+ * @param port port to communicate over
*/
public void startServer(String persistFilename, String listenAddress, int port) {
NetworkTablesJNI.startServer(m_handle, persistFilename, listenAddress, port);
}
- /**
- * Stops the server if it is running.
- */
+ /** Stops the server if it is running. */
public void stopServer() {
NetworkTablesJNI.stopServer(m_handle);
}
- /**
- * Starts a client. Use SetServer to set the server name and port.
- */
+ /** Starts a client. Use SetServer to set the server name and port. */
public void startClient() {
NetworkTablesJNI.startClient(m_handle);
}
@@ -752,7 +737,7 @@
/**
* Starts a client using the specified server and the default port.
*
- * @param serverName server name
+ * @param serverName server name
*/
public void startClient(String serverName) {
startClient(serverName, kDefaultPort);
@@ -761,29 +746,29 @@
/**
* Starts a client using the specified server and port.
*
- * @param serverName server name
- * @param port port to communicate over
+ * @param serverName server name
+ * @param port port to communicate over
*/
public void startClient(String serverName, int port) {
NetworkTablesJNI.startClient(m_handle, serverName, port);
}
/**
- * Starts a client using the specified servers and default port. The
- * client will attempt to connect to each server in round robin fashion.
+ * Starts a client using the specified servers and default port. The client will attempt to
+ * connect to each server in round robin fashion.
*
- * @param serverNames array of server names
+ * @param serverNames array of server names
*/
public void startClient(String[] serverNames) {
startClient(serverNames, kDefaultPort);
}
/**
- * Starts a client using the specified servers and port number. The
- * client will attempt to connect to each server in round robin fashion.
+ * Starts a client using the specified servers and port number. The client will attempt to connect
+ * to each server in round robin fashion.
*
- * @param serverNames array of server names
- * @param port port to communicate over
+ * @param serverNames array of server names
+ * @param port port to communicate over
*/
public void startClient(String[] serverNames, int port) {
int[] ports = new int[serverNames.length];
@@ -794,49 +779,46 @@
}
/**
- * Starts a client using the specified (server, port) combinations. The
- * client will attempt to connect to each server in round robin fashion.
+ * Starts a client using the specified (server, port) combinations. The client will attempt to
+ * connect to each server in round robin fashion.
*
- * @param serverNames array of server names
- * @param ports array of port numbers
+ * @param serverNames array of server names
+ * @param ports array of port numbers
*/
public void startClient(String[] serverNames, int[] ports) {
NetworkTablesJNI.startClient(m_handle, serverNames, ports);
}
/**
- * Starts a client using commonly known robot addresses for the specified
- * team using the default port number.
+ * Starts a client using commonly known robot addresses for the specified team using the default
+ * port number.
*
- * @param team team number
+ * @param team team number
*/
public void startClientTeam(int team) {
startClientTeam(team, kDefaultPort);
}
/**
- * Starts a client using commonly known robot addresses for the specified
- * team.
+ * Starts a client using commonly known robot addresses for the specified team.
*
- * @param team team number
- * @param port port to communicate over
+ * @param team team number
+ * @param port port to communicate over
*/
public void startClientTeam(int team, int port) {
NetworkTablesJNI.startClientTeam(m_handle, team, port);
}
- /**
- * Stops the client if it is running.
- */
+ /** Stops the client if it is running. */
public void stopClient() {
NetworkTablesJNI.stopClient(m_handle);
}
/**
- * Sets server address and port for client (without restarting client).
- * Changes the port to the default port.
+ * Sets server address and port for client (without restarting client). Changes the port to the
+ * default port.
*
- * @param serverName server name
+ * @param serverName server name
*/
public void setServer(String serverName) {
setServer(serverName, kDefaultPort);
@@ -845,30 +827,29 @@
/**
* Sets server address and port for client (without restarting client).
*
- * @param serverName server name
- * @param port port to communicate over
+ * @param serverName server name
+ * @param port port to communicate over
*/
public void setServer(String serverName, int port) {
NetworkTablesJNI.setServer(m_handle, serverName, port);
}
/**
- * Sets server addresses and port for client (without restarting client).
- * Changes the port to the default port. The client will attempt to connect
- * to each server in round robin fashion.
+ * Sets server addresses and port for client (without restarting client). Changes the port to the
+ * default port. The client will attempt to connect to each server in round robin fashion.
*
- * @param serverNames array of server names
+ * @param serverNames array of server names
*/
public void setServer(String[] serverNames) {
setServer(serverNames, kDefaultPort);
}
/**
- * Sets server addresses and port for client (without restarting client).
- * The client will attempt to connect to each server in round robin fashion.
+ * Sets server addresses and port for client (without restarting client). The client will attempt
+ * to connect to each server in round robin fashion.
*
- * @param serverNames array of server names
- * @param port port to communicate over
+ * @param serverNames array of server names
+ * @param port port to communicate over
*/
public void setServer(String[] serverNames, int port) {
int[] ports = new int[serverNames.length];
@@ -879,51 +860,48 @@
}
/**
- * Sets server addresses and ports for client (without restarting client).
- * The client will attempt to connect to each server in round robin fashion.
+ * Sets server addresses and ports for client (without restarting client). The client will attempt
+ * to connect to each server in round robin fashion.
*
- * @param serverNames array of server names
- * @param ports array of port numbers
+ * @param serverNames array of server names
+ * @param ports array of port numbers
*/
public void setServer(String[] serverNames, int[] ports) {
NetworkTablesJNI.setServer(m_handle, serverNames, ports);
}
/**
- * Sets server addresses and port for client (without restarting client).
- * Changes the port to the default port. The client will attempt to connect
- * to each server in round robin fashion.
+ * Sets server addresses and port for client (without restarting client). Changes the port to the
+ * default port. The client will attempt to connect to each server in round robin fashion.
*
- * @param team team number
+ * @param team team number
*/
public void setServerTeam(int team) {
setServerTeam(team, kDefaultPort);
}
/**
- * Sets server addresses and port for client (without restarting client).
- * Connects using commonly known robot addresses for the specified team.
+ * Sets server addresses and port for client (without restarting client). Connects using commonly
+ * known robot addresses for the specified team.
*
- * @param team team number
- * @param port port to communicate over
+ * @param team team number
+ * @param port port to communicate over
*/
public void setServerTeam(int team, int port) {
NetworkTablesJNI.setServerTeam(m_handle, team, port);
}
/**
- * Starts requesting server address from Driver Station.
- * This connects to the Driver Station running on localhost to obtain the
- * server IP address, and connects with the default port.
+ * Starts requesting server address from Driver Station. This connects to the Driver Station
+ * running on localhost to obtain the server IP address, and connects with the default port.
*/
public void startDSClient() {
startDSClient(kDefaultPort);
}
/**
- * Starts requesting server address from Driver Station.
- * This connects to the Driver Station running on localhost to obtain the
- * server IP address.
+ * Starts requesting server address from Driver Station. This connects to the Driver Station
+ * running on localhost to obtain the server IP address.
*
* @param port server port to use in combination with IP from DS
*/
@@ -931,16 +909,14 @@
NetworkTablesJNI.startDSClient(m_handle, port);
}
- /**
- * Stops requesting server address from Driver Station.
- */
+ /** Stops requesting server address from Driver Station. */
public void stopDSClient() {
NetworkTablesJNI.stopDSClient(m_handle);
}
/**
- * Set the periodic update rate.
- * Sets how frequently updates are sent to other nodes over the network.
+ * Set the periodic update rate. Sets how frequently updates are sent to other nodes over the
+ * network.
*
* @param interval update interval in seconds (range 0.01 to 1.0)
*/
@@ -949,18 +925,17 @@
}
/**
- * Flushes all updated values immediately to the network.
- * Note: This is rate-limited to protect the network from flooding.
- * This is primarily useful for synchronizing network updates with
- * user code.
+ * Flushes all updated values immediately to the network. Note: This is rate-limited to protect
+ * the network from flooding. This is primarily useful for synchronizing network updates with user
+ * code.
*/
public void flush() {
NetworkTablesJNI.flush(m_handle);
}
/**
- * Gets information on the currently established network connections.
- * If operating as a client, this will return either zero or one values.
+ * Gets information on the currently established network connections. If operating as a client,
+ * this will return either zero or one values.
*
* @return array of connection information
*/
@@ -978,7 +953,7 @@
}
/**
- * Saves persistent keys to a file. The server does this automatically.
+ * Saves persistent keys to a file. The server does this automatically.
*
* @param filename file name
* @throws PersistentException if error saving file
@@ -988,7 +963,7 @@
}
/**
- * Loads persistent keys from a file. The server does this automatically.
+ * Loads persistent keys from a file. The server does this automatically.
*
* @param filename file name
* @return List of warnings (errors result in an exception instead)
@@ -999,11 +974,10 @@
}
/**
- * Save table values to a file. The file format used is identical to
- * that used for SavePersistent.
+ * Save table values to a file. The file format used is identical to that used for SavePersistent.
*
- * @param filename filename
- * @param prefix save only keys starting with this prefix
+ * @param filename filename
+ * @param prefix save only keys starting with this prefix
* @throws PersistentException if error saving file
*/
public void saveEntries(String filename, String prefix) throws PersistentException {
@@ -1011,11 +985,11 @@
}
/**
- * Load table values from a file. The file format used is identical to
- * that used for SavePersistent / LoadPersistent.
+ * Load table values from a file. The file format used is identical to that used for
+ * SavePersistent / LoadPersistent.
*
- * @param filename filename
- * @param prefix load only keys starting with this prefix
+ * @param filename filename
+ * @param prefix load only keys starting with this prefix
* @return List of warnings (errors result in an exception instead)
* @throws PersistentException if error saving file
*/
@@ -1025,67 +999,69 @@
private final ReentrantLock m_loggerLock = new ReentrantLock();
private final Map<Integer, Consumer<LogMessage>> m_loggers = new HashMap<>();
- private Thread m_loggerThread;
private int m_loggerPoller;
private boolean m_loggerWaitQueue;
private final Condition m_loggerWaitQueueCond = m_loggerLock.newCondition();
+ @SuppressWarnings("PMD.AvoidCatchingThrowable")
private void startLogThread() {
- m_loggerThread = new Thread(() -> {
- boolean wasInterrupted = false;
- while (!Thread.interrupted()) {
- LogMessage[] events;
- try {
- events = NetworkTablesJNI.pollLogger(this, m_loggerPoller);
- } catch (InterruptedException ex) {
- Thread.currentThread().interrupt();
- // don't try to destroy poller, as its handle is likely no longer valid
- wasInterrupted = true;
- break;
- }
- for (LogMessage event : events) {
- Consumer<LogMessage> logger;
- m_loggerLock.lock();
- try {
- logger = m_loggers.get(event.logger);
- } finally {
- m_loggerLock.unlock();
- }
- if (logger != null) {
- try {
- logger.accept(event);
- } catch (Throwable throwable) {
- System.err.println("Unhandled exception during logger callback: "
- + throwable.toString());
- throwable.printStackTrace();
- }
- }
- }
- }
- m_loggerLock.lock();
- try {
- if (!wasInterrupted) {
- NetworkTablesJNI.destroyLoggerPoller(m_loggerPoller);
- }
- m_rpcCallPoller = 0;
- } finally {
- m_loggerLock.unlock();
- }
- }, "NTLogger");
- m_loggerThread.setDaemon(true);
- m_loggerThread.start();
+ var loggerThread =
+ new Thread(
+ () -> {
+ boolean wasInterrupted = false;
+ while (!Thread.interrupted()) {
+ LogMessage[] events;
+ try {
+ events = NetworkTablesJNI.pollLogger(this, m_loggerPoller);
+ } catch (InterruptedException ex) {
+ Thread.currentThread().interrupt();
+ // don't try to destroy poller, as its handle is likely no longer valid
+ wasInterrupted = true;
+ break;
+ }
+ for (LogMessage event : events) {
+ Consumer<LogMessage> logger;
+ m_loggerLock.lock();
+ try {
+ logger = m_loggers.get(event.logger);
+ } finally {
+ m_loggerLock.unlock();
+ }
+ if (logger != null) {
+ try {
+ logger.accept(event);
+ } catch (Throwable throwable) {
+ System.err.println(
+ "Unhandled exception during logger callback: " + throwable.toString());
+ throwable.printStackTrace();
+ }
+ }
+ }
+ }
+ m_loggerLock.lock();
+ try {
+ if (!wasInterrupted) {
+ NetworkTablesJNI.destroyLoggerPoller(m_loggerPoller);
+ }
+ m_rpcCallPoller = 0;
+ } finally {
+ m_loggerLock.unlock();
+ }
+ },
+ "NTLogger");
+ loggerThread.setDaemon(true);
+ loggerThread.start();
}
/**
- * Add logger callback function. By default, log messages are sent to stderr;
- * this function sends log messages with the specified levels to the provided
- * callback function instead. The callback function will only be called for
- * log messages with level greater than or equal to minLevel and less than or
- * equal to maxLevel; messages outside this range will be silently ignored.
+ * Add logger callback function. By default, log messages are sent to stderr; this function sends
+ * log messages with the specified levels to the provided callback function instead. The callback
+ * function will only be called for log messages with level greater than or equal to minLevel and
+ * less than or equal to maxLevel; messages outside this range will be silently ignored.
*
- * @param func log callback function
- * @param minLevel minimum log level
- * @param maxLevel maximum log level
+ * @param func log callback function
+ * @param minLevel minimum log level
+ * @param maxLevel maximum log level
* @return Logger handle
*/
public int addLogger(Consumer<LogMessage> func, int minLevel, int maxLevel) {
@@ -1119,13 +1095,12 @@
}
/**
- * Wait for the incoming log event queue to be empty. This is primarily useful
- * for deterministic testing. This blocks until either the log event
- * queue is empty (e.g. there are no more events that need to be passed along
- * to callbacks or poll queues) or the timeout expires.
+ * Wait for the incoming log event queue to be empty. This is primarily useful for deterministic
+ * testing. This blocks until either the log event queue is empty (e.g. there are no more events
+ * that need to be passed along to callbacks or poll queues) or the timeout expires.
*
- * @param timeout timeout, in seconds. Set to 0 for non-blocking behavior,
- * or a negative value to block indefinitely
+ * @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or a negative value to
+ * block indefinitely
* @return False if timed out, otherwise true.
*/
public boolean waitForLoggerQueue(double timeout) {
@@ -1174,5 +1149,5 @@
}
private boolean m_owned;
- private int m_handle;
+ private final int m_handle;
}
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableType.java b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableType.java
index 54a9f55..a173bb2 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableType.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableType.java
@@ -1,15 +1,10 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
-/**
- * Network table data types.
- */
+/** Network table data types. */
public enum NetworkTableType {
kUnassigned(0),
kBoolean(0x01),
@@ -21,7 +16,6 @@
kStringArray(0x40),
kRpc(0x80);
- @SuppressWarnings("MemberName")
private final int value;
NetworkTableType(int value) {
@@ -40,15 +34,24 @@
*/
public static NetworkTableType getFromInt(int value) {
switch (value) {
- case 0x01: return kBoolean;
- case 0x02: return kDouble;
- case 0x04: return kString;
- case 0x08: return kRaw;
- case 0x10: return kBooleanArray;
- case 0x20: return kDoubleArray;
- case 0x40: return kStringArray;
- case 0x80: return kRpc;
- default: return kUnassigned;
+ case 0x01:
+ return kBoolean;
+ case 0x02:
+ return kDouble;
+ case 0x04:
+ return kString;
+ case 0x08:
+ return kRaw;
+ case 0x10:
+ return kBooleanArray;
+ case 0x20:
+ return kDoubleArray;
+ case 0x40:
+ return kStringArray;
+ case 0x80:
+ return kRpc;
+ default:
+ return kUnassigned;
}
}
}
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableValue.java b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableValue.java
index eb7e2c2..f4b9290 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableValue.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableValue.java
@@ -1,17 +1,12 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
import java.util.Objects;
-/**
- * A network table entry value.
- */
+/** A network table entry value. */
public final class NetworkTableValue {
NetworkTableValue(NetworkTableType type, Object value, long time) {
m_type = type;
@@ -188,6 +183,7 @@
* @return The raw value.
* @throws ClassCastException if the entry value is not of raw type.
*/
+ @SuppressWarnings("PMD.MethodReturnsInternalArray")
public byte[] getRaw() {
if (m_type != NetworkTableType.kRaw) {
throw new ClassCastException("cannot convert " + m_type + " to raw");
@@ -201,6 +197,7 @@
* @return The rpc definition value.
* @throws ClassCastException if the entry value is not of rpc definition type.
*/
+ @SuppressWarnings("PMD.MethodReturnsInternalArray")
public byte[] getRpc() {
if (m_type != NetworkTableType.kRpc) {
throw new ClassCastException("cannot convert " + m_type + " to rpc");
@@ -214,6 +211,7 @@
* @return The boolean array value.
* @throws ClassCastException if the entry value is not of boolean array type.
*/
+ @SuppressWarnings("PMD.MethodReturnsInternalArray")
public boolean[] getBooleanArray() {
if (m_type != NetworkTableType.kBooleanArray) {
throw new ClassCastException("cannot convert " + m_type + " to boolean array");
@@ -227,6 +225,7 @@
* @return The double array value.
* @throws ClassCastException if the entry value is not of double array type.
*/
+ @SuppressWarnings("PMD.MethodReturnsInternalArray")
public double[] getDoubleArray() {
if (m_type != NetworkTableType.kDoubleArray) {
throw new ClassCastException("cannot convert " + m_type + " to double array");
@@ -240,6 +239,7 @@
* @return The string array value.
* @throws ClassCastException if the entry value is not of string array type.
*/
+ @SuppressWarnings("PMD.MethodReturnsInternalArray")
public String[] getStringArray() {
if (m_type != NetworkTableType.kStringArray) {
throw new ClassCastException("cannot convert " + m_type + " to string array");
@@ -478,6 +478,9 @@
return Objects.hash(m_type, m_value);
}
+ // arraycopy() doesn't know how to unwrap boxed values; this is a false positive in PMD
+ // (see https://sourceforge.net/p/pmd/bugs/804/)
+ @SuppressWarnings("PMD.AvoidArrayLoops")
static boolean[] toNative(Boolean[] arr) {
boolean[] out = new boolean[arr.length];
for (int i = 0; i < arr.length; i++) {
@@ -486,6 +489,7 @@
return out;
}
+ @SuppressWarnings("PMD.AvoidArrayLoops")
static double[] toNative(Number[] arr) {
double[] out = new double[arr.length];
for (int i = 0; i < arr.length; i++) {
@@ -494,6 +498,7 @@
return out;
}
+ @SuppressWarnings("PMD.AvoidArrayLoops")
static Boolean[] fromNative(boolean[] arr) {
Boolean[] out = new Boolean[arr.length];
for (int i = 0; i < arr.length; i++) {
@@ -502,6 +507,7 @@
return out;
}
+ @SuppressWarnings("PMD.AvoidArrayLoops")
static Double[] fromNative(double[] arr) {
Double[] out = new Double[arr.length];
for (int i = 0; i < arr.length; i++) {
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTablesJNI.java b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTablesJNI.java
index 742c0ca..8c4916f 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTablesJNI.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTablesJNI.java
@@ -1,18 +1,14 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
+import edu.wpi.first.util.RuntimeLoader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicBoolean;
-import edu.wpi.first.wpiutil.RuntimeLoader;
-
public final class NetworkTablesJNI {
static boolean libraryLoaded = false;
static RuntimeLoader<NetworkTablesJNI> loader = null;
@@ -32,7 +28,9 @@
static {
if (Helper.getExtractOnStaticLoad()) {
try {
- loader = new RuntimeLoader<>("ntcorejni", RuntimeLoader.getDefaultExtractionRoot(), NetworkTablesJNI.class);
+ loader =
+ new RuntimeLoader<>(
+ "ntcorejni", RuntimeLoader.getDefaultExtractionRoot(), NetworkTablesJNI.class);
loader.loadLibrary();
} catch (IOException ex) {
ex.printStackTrace();
@@ -44,56 +42,88 @@
/**
* Force load the library.
+ *
+ * @throws IOException if the library fails to load
*/
public static synchronized void forceLoad() throws IOException {
if (libraryLoaded) {
return;
}
- loader = new RuntimeLoader<>("ntcorejni", RuntimeLoader.getDefaultExtractionRoot(), NetworkTablesJNI.class);
+ loader =
+ new RuntimeLoader<>(
+ "ntcorejni", RuntimeLoader.getDefaultExtractionRoot(), NetworkTablesJNI.class);
loader.loadLibrary();
libraryLoaded = true;
}
public static native int getDefaultInstance();
+
public static native int createInstance();
+
public static native void destroyInstance(int inst);
+
public static native int getInstanceFromHandle(int handle);
public static native int getEntry(int inst, String key);
+
public static native int[] getEntries(int inst, String prefix, int types);
+
public static native String getEntryName(int entry);
+
public static native long getEntryLastChange(int entry);
public static native int getType(int entry);
public static native boolean setBoolean(int entry, long time, boolean value, boolean force);
+
public static native boolean setDouble(int entry, long time, double value, boolean force);
+
public static native boolean setString(int entry, long time, String value, boolean force);
+
public static native boolean setRaw(int entry, long time, byte[] value, boolean force);
- public static native boolean setRaw(int entry, long time, ByteBuffer value, int len, boolean force);
- public static native boolean setBooleanArray(int entry, long time, boolean[] value, boolean force);
+
+ public static native boolean setRaw(
+ int entry, long time, ByteBuffer value, int len, boolean force);
+
+ public static native boolean setBooleanArray(
+ int entry, long time, boolean[] value, boolean force);
+
public static native boolean setDoubleArray(int entry, long time, double[] value, boolean force);
+
public static native boolean setStringArray(int entry, long time, String[] value, boolean force);
public static native NetworkTableValue getValue(int entry);
public static native boolean getBoolean(int entry, boolean defaultValue);
+
public static native double getDouble(int entry, double defaultValue);
+
public static native String getString(int entry, String defaultValue);
+
public static native byte[] getRaw(int entry, byte[] defaultValue);
+
public static native boolean[] getBooleanArray(int entry, boolean[] defaultValue);
+
public static native double[] getDoubleArray(int entry, double[] defaultValue);
+
public static native String[] getStringArray(int entry, String[] defaultValue);
+
public static native boolean setDefaultBoolean(int entry, long time, boolean defaultValue);
public static native boolean setDefaultDouble(int entry, long time, double defaultValue);
+
public static native boolean setDefaultString(int entry, long time, String defaultValue);
+
public static native boolean setDefaultRaw(int entry, long time, byte[] defaultValue);
+
public static native boolean setDefaultBooleanArray(int entry, long time, boolean[] defaultValue);
+
public static native boolean setDefaultDoubleArray(int entry, long time, double[] defaultValue);
+
public static native boolean setDefaultStringArray(int entry, long time, String[] defaultValue);
public static native void setEntryFlags(int entry, int flags);
+
public static native int getEntryFlags(int entry);
public static native void deleteEntry(int entry);
@@ -101,58 +131,109 @@
public static native void deleteAllEntries(int inst);
public static native EntryInfo getEntryInfoHandle(NetworkTableInstance inst, int entry);
- public static native EntryInfo[] getEntryInfo(NetworkTableInstance instObject, int inst, String prefix, int types);
+
+ public static native EntryInfo[] getEntryInfo(
+ NetworkTableInstance instObject, int inst, String prefix, int types);
public static native int createEntryListenerPoller(int inst);
+
public static native void destroyEntryListenerPoller(int poller);
+
public static native int addPolledEntryListener(int poller, String prefix, int flags);
+
public static native int addPolledEntryListener(int poller, int entry, int flags);
- public static native EntryNotification[] pollEntryListener(NetworkTableInstance inst, int poller) throws InterruptedException;
- public static native EntryNotification[] pollEntryListenerTimeout(NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
+
+ public static native EntryNotification[] pollEntryListener(NetworkTableInstance inst, int poller)
+ throws InterruptedException;
+
+ public static native EntryNotification[] pollEntryListenerTimeout(
+ NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
+
public static native void cancelPollEntryListener(int poller);
+
public static native void removeEntryListener(int entryListener);
+
public static native boolean waitForEntryListenerQueue(int inst, double timeout);
public static native int createConnectionListenerPoller(int inst);
+
public static native void destroyConnectionListenerPoller(int poller);
+
public static native int addPolledConnectionListener(int poller, boolean immediateNotify);
- public static native ConnectionNotification[] pollConnectionListener(NetworkTableInstance inst, int poller) throws InterruptedException;
- public static native ConnectionNotification[] pollConnectionListenerTimeout(NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
+
+ public static native ConnectionNotification[] pollConnectionListener(
+ NetworkTableInstance inst, int poller) throws InterruptedException;
+
+ public static native ConnectionNotification[] pollConnectionListenerTimeout(
+ NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
+
public static native void cancelPollConnectionListener(int poller);
+
public static native void removeConnectionListener(int connListener);
+
public static native boolean waitForConnectionListenerQueue(int inst, double timeout);
public static native int createRpcCallPoller(int inst);
+
public static native void destroyRpcCallPoller(int poller);
+
public static native void createPolledRpc(int entry, byte[] def, int poller);
- public static native RpcAnswer[] pollRpc(NetworkTableInstance inst, int poller) throws InterruptedException;
- public static native RpcAnswer[] pollRpcTimeout(NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
+
+ public static native RpcAnswer[] pollRpc(NetworkTableInstance inst, int poller)
+ throws InterruptedException;
+
+ public static native RpcAnswer[] pollRpcTimeout(
+ NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
+
public static native void cancelPollRpc(int poller);
+
public static native boolean waitForRpcCallQueue(int inst, double timeout);
+
public static native boolean postRpcResponse(int entry, int call, byte[] result);
+
public static native int callRpc(int entry, byte[] params);
+
public static native byte[] getRpcResult(int entry, int call);
+
public static native byte[] getRpcResult(int entry, int call, double timeout);
+
public static native void cancelRpcResult(int entry, int call);
public static native byte[] getRpc(int entry, byte[] defaultValue);
public static native void setNetworkIdentity(int inst, String name);
+
public static native int getNetworkMode(int inst);
+
public static native void startLocal(int inst);
+
public static native void stopLocal(int inst);
- public static native void startServer(int inst, String persistFilename, String listenAddress, int port);
+
+ public static native void startServer(
+ int inst, String persistFilename, String listenAddress, int port);
+
public static native void stopServer(int inst);
+
public static native void startClient(int inst);
+
public static native void startClient(int inst, String serverName, int port);
+
public static native void startClient(int inst, String[] serverNames, int[] ports);
+
public static native void startClientTeam(int inst, int team, int port);
+
public static native void stopClient(int inst);
+
public static native void setServer(int inst, String serverName, int port);
+
public static native void setServer(int inst, String[] serverNames, int[] ports);
+
public static native void setServerTeam(int inst, int team, int port);
+
public static native void startDSClient(int inst, int port);
+
public static native void stopDSClient(int inst);
+
public static native void setUpdateRate(int inst, double interval);
public static native void flush(int inst);
@@ -162,19 +243,33 @@
public static native boolean isConnected(int inst);
public static native void savePersistent(int inst, String filename) throws PersistentException;
- public static native String[] loadPersistent(int inst, String filename) throws PersistentException; // returns warnings
- public static native void saveEntries(int inst, String filename, String prefix) throws PersistentException;
- public static native String[] loadEntries(int inst, String filename, String prefix) throws PersistentException; // returns warnings
+ public static native String[] loadPersistent(int inst, String filename)
+ throws PersistentException; // returns warnings
+
+ public static native void saveEntries(int inst, String filename, String prefix)
+ throws PersistentException;
+
+ public static native String[] loadEntries(int inst, String filename, String prefix)
+ throws PersistentException; // returns warnings
public static native long now();
public static native int createLoggerPoller(int inst);
+
public static native void destroyLoggerPoller(int poller);
+
public static native int addPolledLogger(int poller, int minLevel, int maxLevel);
- public static native LogMessage[] pollLogger(NetworkTableInstance inst, int poller) throws InterruptedException;
- public static native LogMessage[] pollLoggerTimeout(NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
+
+ public static native LogMessage[] pollLogger(NetworkTableInstance inst, int poller)
+ throws InterruptedException;
+
+ public static native LogMessage[] pollLoggerTimeout(
+ NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
+
public static native void cancelPollLogger(int poller);
+
public static native void removeLogger(int logger);
+
public static native boolean waitForLoggerQueue(int inst, double timeout);
}
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/PersistentException.java b/ntcore/src/main/java/edu/wpi/first/networktables/PersistentException.java
index 205b015..1c61595 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/PersistentException.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/PersistentException.java
@@ -1,17 +1,12 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
import java.io.IOException;
-/**
- * An exception thrown when persistent load/save fails in a {@link NetworkTable}.
- */
+/** An exception thrown when persistent load/save fails in a {@link NetworkTable}. */
public final class PersistentException extends IOException {
public static final long serialVersionUID = 0;
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/RpcAnswer.java b/ntcore/src/main/java/edu/wpi/first/networktables/RpcAnswer.java
index 91e1aa4..a244bde 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/RpcAnswer.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/RpcAnswer.java
@@ -1,15 +1,10 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
-/**
- * NetworkTables Remote Procedure Call (Server Side).
- */
+/** NetworkTables Remote Procedure Call (Server Side). */
public final class RpcAnswer {
/** Entry handle. */
@SuppressWarnings("MemberName")
@@ -31,8 +26,8 @@
@SuppressWarnings("MemberName")
public final ConnectionInfo conn;
- /** Constructor.
- * This should generally only be used internally to NetworkTables.
+ /**
+ * Constructor. This should generally only be used internally to NetworkTables.
*
* @param inst Instance
* @param entry Entry handle
@@ -41,8 +36,14 @@
* @param params Call raw parameters
* @param conn Connection info
*/
- public RpcAnswer(NetworkTableInstance inst, int entry, int call, String name, byte[] params,
- ConnectionInfo conn) {
+ @SuppressWarnings("PMD.ArrayIsStoredDirectly")
+ public RpcAnswer(
+ NetworkTableInstance inst,
+ int entry,
+ int call,
+ String name,
+ byte[] params,
+ ConnectionInfo conn) {
this.m_inst = inst;
this.entry = entry;
this.call = call;
@@ -76,8 +77,8 @@
/**
* Post RPC response (return value) for a polled RPC.
*
- * @param result result raw data that will be provided to remote caller
- * @return true if the response was posted, otherwise false
+ * @param result result raw data that will be provided to remote caller
+ * @return true if the response was posted, otherwise false
*/
public boolean postResponse(byte[] result) {
boolean ret = NetworkTablesJNI.postRpcResponse(entry, call, result);
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/RpcCall.java b/ntcore/src/main/java/edu/wpi/first/networktables/RpcCall.java
index b9148c9..790ff5f 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/RpcCall.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/RpcCall.java
@@ -1,18 +1,13 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
-/**
- * NetworkTables Remote Procedure Call.
- */
+/** NetworkTables Remote Procedure Call. */
public final class RpcCall implements AutoCloseable {
- /** Constructor.
- * This should generally only be used internally to NetworkTables.
+ /**
+ * Constructor. This should generally only be used internally to NetworkTables.
*
* @param entry Entry
* @param call Call handle
@@ -22,9 +17,7 @@
m_call = call;
}
- /**
- * Cancels the result if no other action taken.
- */
+ /** Cancels the result if no other action taken. */
@Override
public synchronized void close() {
if (m_call != 0) {
@@ -60,8 +53,7 @@
}
/**
- * Get the result (return value). This function blocks until
- * the result is received.
+ * Get the result (return value). This function blocks until the result is received.
*
* @return Received result (output)
*/
@@ -74,10 +66,10 @@
}
/**
- * Get the result (return value). This function blocks until
- * the result is received or it times out.
+ * Get the result (return value). This function blocks until the result is received or it times
+ * out.
*
- * @param timeout timeout, in seconds
+ * @param timeout timeout, in seconds
* @return Received result (output)
*/
public byte[] getResult(double timeout) {
@@ -88,9 +80,7 @@
return result;
}
- /**
- * Ignore the result. This function is non-blocking.
- */
+ /** Ignore the result. This function is non-blocking. */
public void cancelResult() {
NetworkTablesJNI.cancelRpcResult(m_entry.getHandle(), m_call);
}
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/TableEntryListener.java b/ntcore/src/main/java/edu/wpi/first/networktables/TableEntryListener.java
index 676e57e..e0781e1 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/TableEntryListener.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/TableEntryListener.java
@@ -1,15 +1,10 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
-/**
- * A listener that listens to changes in values in a {@link NetworkTable}.
- */
+/** A listener that listens to changes in values in a {@link NetworkTable}. */
@FunctionalInterface
public interface TableEntryListener extends EntryListenerFlags {
/**
@@ -19,9 +14,9 @@
* @param key the key associated with the value that changed
* @param entry the entry associated with the value that changed
* @param value the new value
- * @param flags update flags; for example, EntryListenerFlags.kNew if the key
- * did not previously exist in the table
+ * @param flags update flags; for example, EntryListenerFlags.kNew if the key did not previously
+ * exist in the table
*/
- void valueChanged(NetworkTable table, String key, NetworkTableEntry entry,
- NetworkTableValue value, int flags);
+ void valueChanged(
+ NetworkTable table, String key, NetworkTableEntry entry, NetworkTableValue value, int flags);
}
diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/TableListener.java b/ntcore/src/main/java/edu/wpi/first/networktables/TableListener.java
index 3c686e5..24fae2d 100644
--- a/ntcore/src/main/java/edu/wpi/first/networktables/TableListener.java
+++ b/ntcore/src/main/java/edu/wpi/first/networktables/TableListener.java
@@ -1,15 +1,10 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
-/**
- * A listener that listens to new tables in a {@link NetworkTable}.
- */
+/** A listener that listens to new tables in a {@link NetworkTable}. */
@FunctionalInterface
public interface TableListener {
/**
diff --git a/ntcore/src/main/java/edu/wpi/first/wpilibj/networktables/NetworkTable.java b/ntcore/src/main/java/edu/wpi/first/wpilibj/networktables/NetworkTable.java
deleted file mode 100644
index f360a19..0000000
--- a/ntcore/src/main/java/edu/wpi/first/wpilibj/networktables/NetworkTable.java
+++ /dev/null
@@ -1,1189 +0,0 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
-
-package edu.wpi.first.wpilibj.networktables;
-
-import java.nio.ByteBuffer;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Objects;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.function.Consumer;
-
-import edu.wpi.first.networktables.ConnectionInfo;
-import edu.wpi.first.networktables.ConnectionNotification;
-import edu.wpi.first.networktables.EntryInfo;
-import edu.wpi.first.networktables.EntryNotification;
-import edu.wpi.first.networktables.NetworkTableEntry;
-import edu.wpi.first.networktables.NetworkTableInstance;
-import edu.wpi.first.networktables.NetworkTableType;
-import edu.wpi.first.networktables.NetworkTableValue;
-import edu.wpi.first.networktables.NetworkTablesJNI;
-import edu.wpi.first.networktables.PersistentException;
-import edu.wpi.first.wpilibj.tables.IRemote;
-import edu.wpi.first.wpilibj.tables.IRemoteConnectionListener;
-import edu.wpi.first.wpilibj.tables.ITable;
-import edu.wpi.first.wpilibj.tables.ITableListener;
-
-/**
- * A network table that knows its subtable path.
- * @deprecated Use {@link edu.wpi.first.networktables.NetworkTable} instead.
- */
-@Deprecated
-@SuppressWarnings("checkstyle:all")
-public class NetworkTable implements ITable, IRemote {
- /**
- * The path separator for sub-tables and keys
- *
- */
- public static final char PATH_SEPARATOR = '/';
- /**
- * The default port that network tables operates on
- */
- public static final int DEFAULT_PORT = 1735;
-
- private static boolean client = false;
- private static boolean enableDS = true;
- private static boolean running = false;
- private static int port = DEFAULT_PORT;
- private static String persistentFilename = "networktables.ini";
-
- private synchronized static void checkInit() {
- if (running)
- throw new IllegalStateException(
- "Network tables has already been initialized");
- }
-
- /**
- * initializes network tables
- * @deprecated Use {@link NetworkTableInstance#startServer()} or
- * {@link NetworkTableInstance#startClient()} instead.
- */
- @Deprecated
- public synchronized static void initialize() {
- if (running)
- shutdown();
- NetworkTableInstance inst = NetworkTableInstance.getDefault();
- if (client) {
- inst.startClient();
- if (enableDS)
- inst.startDSClient(port);
- } else
- inst.startServer(persistentFilename, "", port);
- running = true;
- }
-
- /**
- * shuts down network tables
- * @deprecated Use {@link NetworkTableInstance#stopServer()} or
- * {@link NetworkTableInstance#stopClient()} instead.
- */
- @Deprecated
- public synchronized static void shutdown() {
- if (!running)
- return;
- NetworkTableInstance inst = NetworkTableInstance.getDefault();
- if (client) {
- inst.stopDSClient();
- inst.stopClient();
- } else
- inst.stopServer();
- running = false;
- }
-
- /**
- * set that network tables should be a server
- * This must be called before initialize or getTable
- * @deprecated Use {@link NetworkTableInstance#startServer()} instead.
- */
- @Deprecated
- public synchronized static void setServerMode() {
- if (!client)
- return;
- checkInit();
- client = false;
- }
-
- /**
- * set that network tables should be a client
- * This must be called before initialize or getTable
- * @deprecated Use {@link NetworkTableInstance#startClient()} instead.
- */
- @Deprecated
- public synchronized static void setClientMode() {
- if (client)
- return;
- checkInit();
- client = true;
- }
-
- /**
- * set the team the robot is configured for (this will set the mdns address that
- * network tables will connect to in client mode)
- * This must be called before initialize or getTable
- * @param team the team number
- * @deprecated Use {@link NetworkTableInstance#setServerTeam(int)} or
- * {@link NetworkTableInstance#startClientTeam(int)} instead.
- */
- @Deprecated
- public synchronized static void setTeam(int team) {
- NetworkTableInstance inst = NetworkTableInstance.getDefault();
- inst.setServerTeam(team, port);
- if (enableDS)
- inst.startDSClient(port);
- }
-
- /**
- * @param address the address that network tables will connect to in client
- * mode
- * @deprecated Use {@link NetworkTableInstance#setServer(String)} or
- * {@link NetworkTableInstance#startClient(String)} instead.
- */
- @Deprecated
- public synchronized static void setIPAddress(final String address) {
- String[] addresses = new String[1];
- addresses[0] = address;
- setIPAddress(addresses);
- }
-
- /**
- * @param addresses the adresses that network tables will connect to in
- * client mode (in round robin order)
- * @deprecated Use {@link NetworkTableInstance#setServer(String[])} or
- * {@link NetworkTableInstance#startClient(String[])} instead.
- */
- @Deprecated
- public synchronized static void setIPAddress(final String[] addresses) {
- NetworkTableInstance inst = NetworkTableInstance.getDefault();
- inst.setServer(addresses, port);
-
- // Stop the DS client if we're explicitly connecting to localhost
- if (addresses.length > 0 &&
- (addresses[0].equals("localhost") || addresses[0].equals("127.0.0.1")))
- inst.stopDSClient();
- else if (enableDS)
- inst.startDSClient(port);
- }
-
- /**
- * Set the port number that network tables will connect to in client
- * mode or listen to in server mode.
- * @param aport the port number
- * @deprecated Use the appropriate parameters to
- * {@link NetworkTableInstance#setServer(String, int)},
- * {@link NetworkTableInstance#startClient(String, int)},
- * {@link NetworkTableInstance#startServer(String, String, int)}, and
- * {@link NetworkTableInstance#startDSClient(int)} instead.
- */
- @Deprecated
- public synchronized static void setPort(int aport) {
- if (port == aport)
- return;
- checkInit();
- port = aport;
- }
-
- /**
- * Enable requesting the server address from the Driver Station.
- * @param enabled whether to enable the connection to the local DS
- * @deprecated Use {@link NetworkTableInstance#startDSClient()} and
- * {@link NetworkTableInstance#stopDSClient()} instead.
- */
- @Deprecated
- public synchronized static void setDSClientEnabled(boolean enabled) {
- NetworkTableInstance inst = NetworkTableInstance.getDefault();
- enableDS = enabled;
- if (enableDS)
- inst.startDSClient(port);
- else
- inst.stopDSClient();
- }
-
- /**
- * Sets the persistent filename.
- * @param filename the filename that the network tables server uses for
- * automatic loading and saving of persistent values
- * @deprecated Use the appropriate parameter to
- * {@link NetworkTableInstance#startServer()} instead.
- */
- @Deprecated
- public synchronized static void setPersistentFilename(final String filename) {
- if (persistentFilename.equals(filename))
- return;
- checkInit();
- persistentFilename = filename;
- }
-
- /**
- * Sets the network identity.
- * This is provided in the connection info on the remote end.
- * @param name identity
- * @deprecated Use {@link NetworkTableInstance#setNetworkIdentity(String)}
- * instead.
- */
- @Deprecated
- public static void setNetworkIdentity(String name) {
- NetworkTableInstance.getDefault().setNetworkIdentity(name);
- }
-
- public static boolean[] toNative(Boolean[] arr) {
- boolean[] out = new boolean[arr.length];
- for (int i = 0; i < arr.length; i++)
- out[i] = arr[i];
- return out;
- }
-
- public static double[] toNative(Number[] arr) {
- double[] out = new double[arr.length];
- for (int i = 0; i < arr.length; i++)
- out[i] = arr[i].doubleValue();
- return out;
- }
-
- public static Boolean[] fromNative(boolean[] arr) {
- Boolean[] out = new Boolean[arr.length];
- for (int i = 0; i < arr.length; i++)
- out[i] = arr[i];
- return out;
- }
-
- public static Double[] fromNative(double[] arr) {
- Double[] out = new Double[arr.length];
- for (int i = 0; i < arr.length; i++)
- out[i] = arr[i];
- return out;
- }
-
- /**
- * Gets the table with the specified key. If the table does not exist, a new
- * table will be created.<br>
- * This will automatically initialize network tables if it has not been
- * already
- *
- * @deprecated Use {@link NetworkTableInstance#getTable(String)} instead.
- *
- * @param key the key name
- * @return the network table requested
- */
- @Deprecated
- public synchronized static NetworkTable getTable(String key) {
- if (!running)
- initialize();
- String theKey;
- if (key.isEmpty() || key.equals("/")) {
- theKey = "";
- } else if (key.charAt(0) == NetworkTable.PATH_SEPARATOR) {
- theKey = key;
- } else {
- theKey = NetworkTable.PATH_SEPARATOR + key;
- }
- return new NetworkTable(NetworkTableInstance.getDefault(), theKey);
- }
-
- private final String path;
- private final String pathWithSep;
- private final NetworkTableInstance inst;
-
- NetworkTable(NetworkTableInstance inst, String path) {
- this.path = path;
- this.pathWithSep = path + PATH_SEPARATOR;
- this.inst = inst;
- }
-
- @Override
- public String toString() { return "NetworkTable: " + path; }
-
- private final ConcurrentMap<String, NetworkTableEntry> entries = new ConcurrentHashMap<String, NetworkTableEntry>();
-
- /**
- * Gets the entry for a subkey.
- * @param key the key name
- * @return Network table entry.
- */
- private NetworkTableEntry getEntry(String key) {
- NetworkTableEntry entry = entries.get(key);
- if (entry == null) {
- entry = inst.getEntry(pathWithSep + key);
- entries.putIfAbsent(key, entry);
- }
- return entry;
- }
-
- /**
- * Gets the current network connections.
- * @return An array of connection information.
- * @deprecated Use {@link NetworkTableInstance#getConnections()} instead.
- */
- @Deprecated
- public static ConnectionInfo[] connections() {
- return NetworkTableInstance.getDefault().getConnections();
- }
-
- /**
- * Determine whether or not a network connection is active.
- * @return True if connected, false if not connected.
- * @deprecated Use {@link NetworkTableInstance#isConnected()} instead.
- */
- @Override
- @Deprecated
- public boolean isConnected() {
- return inst.isConnected();
- }
-
- /**
- * Determine whether NetworkTables is operating as a server or as a client.
- * @return True if operating as a server, false otherwise.
- * @deprecated Use {@link NetworkTableInstance#getNetworkMode()} instead.
- */
- @Override
- @Deprecated
- public boolean isServer() {
- return (inst.getNetworkMode() & NetworkTableInstance.kNetModeServer) != 0;
- }
-
- /* Backwards compatibility shims for IRemoteConnectionListener */
- private static class ConnectionListenerAdapter implements Consumer<ConnectionNotification> {
- public int uid;
- private final IRemote targetSource;
- private final IRemoteConnectionListener targetListener;
-
- public ConnectionListenerAdapter(IRemote targetSource, IRemoteConnectionListener targetListener) {
- this.targetSource = targetSource;
- this.targetListener = targetListener;
- }
-
- @Override
- public void accept(ConnectionNotification event) {
- if (event.connected)
- targetListener.connectedEx(targetSource, event.conn);
- else
- targetListener.disconnectedEx(targetSource, event.conn);
- }
- }
-
- private static final HashMap<IRemoteConnectionListener,ConnectionListenerAdapter> globalConnectionListenerMap = new HashMap<IRemoteConnectionListener,ConnectionListenerAdapter>();
-
- private static IRemote staticRemote = new IRemote() {
- @Override
- public void addConnectionListener(IRemoteConnectionListener listener, boolean immediateNotify) {
- NetworkTable.addGlobalConnectionListener(listener, immediateNotify);
- }
- @Override
- public void removeConnectionListener(IRemoteConnectionListener listener) {
- NetworkTable.removeGlobalConnectionListener(listener);
- }
- @Override
- public boolean isConnected() {
- ConnectionInfo[] conns = NetworkTableInstance.getDefault().getConnections();
- return conns.length > 0;
- }
- @Override
- public boolean isServer() {
- return (NetworkTableInstance.getDefault().getNetworkMode() & NetworkTableInstance.kNetModeServer) != 0;
- }
- };
-
- private final HashMap<IRemoteConnectionListener,ConnectionListenerAdapter> connectionListenerMap = new HashMap<IRemoteConnectionListener,ConnectionListenerAdapter>();
-
- /**
- * Add a connection listener.
- * @param listener connection listener
- * @param immediateNotify call listener immediately for all existing connections
- * @deprecated Use {@link NetworkTableInstance#addConnectionListener(Consumer, boolean)} instead.
- */
- @Deprecated
- public static synchronized void addGlobalConnectionListener(IRemoteConnectionListener listener, boolean immediateNotify) {
- ConnectionListenerAdapter adapter = new ConnectionListenerAdapter(staticRemote, listener);
- if (globalConnectionListenerMap.putIfAbsent(listener, adapter) != null) {
- throw new IllegalStateException("Cannot add the same listener twice");
- }
- adapter.uid = NetworkTableInstance.getDefault().addConnectionListener(adapter, immediateNotify);
- }
-
- /**
- * Remove a connection listener.
- * @param listener connection listener
- * @deprecated Use {@link NetworkTableInstance#removeConnectionListener(int)} instead.
- */
- @Deprecated
- public static synchronized void removeGlobalConnectionListener(IRemoteConnectionListener listener) {
- ConnectionListenerAdapter adapter = globalConnectionListenerMap.remove(listener);
- if (adapter != null) {
- NetworkTableInstance.getDefault().removeConnectionListener(adapter.uid);
- }
- }
-
- /**
- * Add a connection listener.
- * @param listener connection listener
- * @param immediateNotify call listener immediately for all existing connections
- * @deprecated Use {@link NetworkTableInstance#addConnectionListener(Consumer, boolean)} instead.
- */
- @Override
- @Deprecated
- public synchronized void addConnectionListener(IRemoteConnectionListener listener,
- boolean immediateNotify) {
- ConnectionListenerAdapter adapter = new ConnectionListenerAdapter(this, listener);
- if (connectionListenerMap.putIfAbsent(listener, adapter) != null) {
- throw new IllegalStateException("Cannot add the same listener twice");
- }
- adapter.uid = inst.addConnectionListener(adapter, immediateNotify);
- }
-
- /**
- * Remove a connection listener.
- * @param listener connection listener
- * @deprecated Use {@link NetworkTableInstance#removeConnectionListener(int)} instead.
- */
- @Override
- @Deprecated
- public synchronized void removeConnectionListener(IRemoteConnectionListener listener) {
- ConnectionListenerAdapter adapter = connectionListenerMap.get(listener);
- if (adapter != null && connectionListenerMap.remove(listener, adapter)) {
- inst.removeConnectionListener(adapter.uid);
- }
- }
-
- /**
- * {@inheritDoc}
- * @deprecated Use {@link edu.wpi.first.networktables.NetworkTable#addEntryListener(TableEntryListener, int)} instead
- * (with flags value of NOTIFY_NEW | NOTIFY_UPDATE).
- */
- @Override
- @Deprecated
- public void addTableListener(ITableListener listener) {
- addTableListenerEx(listener, NOTIFY_NEW | NOTIFY_UPDATE);
- }
-
- /**
- * {@inheritDoc}
- * @deprecated Use {@link edu.wpi.first.networktables.NetworkTable#addEntryListener(TableEntryListener, int)} instead
- * (with flags value of NOTIFY_NEW | NOTIFY_UPDATE | NOTIFY_IMMEDIATE).
- */
- @Override
- @Deprecated
- public void addTableListener(ITableListener listener,
- boolean immediateNotify) {
- int flags = NOTIFY_NEW | NOTIFY_UPDATE;
- if (immediateNotify)
- flags |= NOTIFY_IMMEDIATE;
- addTableListenerEx(listener, flags);
- }
-
- /* Base class for listeners; stores uid to implement remove functions */
- private static class ListenerBase {
- public int uid;
- }
-
- private static class OldTableListenerAdapter extends ListenerBase implements Consumer<EntryNotification> {
- private final int prefixLen;
- private final ITable targetSource;
- private final ITableListener targetListener;
-
- public OldTableListenerAdapter(int prefixLen, ITable targetSource, ITableListener targetListener) {
- this.prefixLen = prefixLen;
- this.targetSource = targetSource;
- this.targetListener = targetListener;
- }
-
- @Override
- public void accept(EntryNotification event) {
- String relativeKey = event.name.substring(prefixLen);
- if (relativeKey.indexOf(PATH_SEPARATOR) != -1)
- return;
- targetListener.valueChangedEx(targetSource, relativeKey, event.value.getValue(), event.flags);
- }
- }
-
- private final HashMap<ITableListener,List<ListenerBase>> oldListenerMap = new HashMap<ITableListener,List<ListenerBase>>();
-
- /**
- * {@inheritDoc}
- * @deprecated Use {@link edu.wpi.first.networktables.NetworkTable#addEntryListener(TableEntryListener, int)} instead.
- */
- @Override
- @Deprecated
- public synchronized void addTableListenerEx(ITableListener listener,
- int flags) {
- List<ListenerBase> adapters = oldListenerMap.get(listener);
- if (adapters == null) {
- adapters = new ArrayList<ListenerBase>();
- oldListenerMap.put(listener, adapters);
- }
- OldTableListenerAdapter adapter =
- new OldTableListenerAdapter(path.length() + 1, this, listener);
- adapter.uid = inst.addEntryListener(pathWithSep, adapter, flags);
- adapters.add(adapter);
- }
-
- /**
- * {@inheritDoc}
- * @deprecated Use {@link edu.wpi.first.networktables.NetworkTable#addEntryListener(String, TableEntryListener, int)}
- * or {@link NetworkTableEntry#addListener(Consumer, int)} instead.
- */
- @Override
- @Deprecated
- public void addTableListener(String key, ITableListener listener,
- boolean immediateNotify) {
- int flags = NOTIFY_NEW | NOTIFY_UPDATE;
- if (immediateNotify)
- flags |= NOTIFY_IMMEDIATE;
- addTableListenerEx(key, listener, flags);
- }
-
- private static class OldKeyListenerAdapter extends ListenerBase implements Consumer<EntryNotification> {
- private final String relativeKey;
- private final ITable targetSource;
- private final ITableListener targetListener;
-
- public OldKeyListenerAdapter(String relativeKey, ITable targetSource, ITableListener targetListener) {
- this.relativeKey = relativeKey;
- this.targetSource = targetSource;
- this.targetListener = targetListener;
- }
-
- @Override
- public void accept(EntryNotification event) {
- targetListener.valueChangedEx(targetSource, relativeKey, event.value.getValue(), event.flags);
- }
- }
-
- /**
- * {@inheritDoc}
- * @deprecated Use {@link edu.wpi.first.networktables.NetworkTable#addEntryListener(String, TableEntryListener, int)}
- * or {@link NetworkTableEntry#addListener(Consumer, int)} instead.
- */
- @Override
- @Deprecated
- public synchronized void addTableListenerEx(String key,
- ITableListener listener,
- int flags) {
- List<ListenerBase> adapters = oldListenerMap.get(listener);
- if (adapters == null) {
- adapters = new ArrayList<ListenerBase>();
- oldListenerMap.put(listener, adapters);
- }
- OldKeyListenerAdapter adapter = new OldKeyListenerAdapter(key, this, listener);
- adapter.uid = inst.addEntryListener(getEntry(key), adapter, flags);
- adapters.add(adapter);
- }
-
- /**
- * {@inheritDoc}
- * @deprecated Use {@link edu.wpi.first.networktables.NetworkTable#addSubTableListener(TableListener, boolean)}
- * instead.
- */
- @Override
- @Deprecated
- public void addSubTableListener(final ITableListener listener) {
- addSubTableListener(listener, false);
- }
-
- private static class OldSubListenerAdapter extends ListenerBase implements Consumer<EntryNotification> {
- private final int prefixLen;
- private final ITable targetSource;
- private final ITableListener targetListener;
- private final Set<String> notifiedTables = new HashSet<String>();
-
- public OldSubListenerAdapter(int prefixLen, ITable targetSource, ITableListener targetListener) {
- this.prefixLen = prefixLen;
- this.targetSource = targetSource;
- this.targetListener = targetListener;
- }
-
- @Override
- public void accept(EntryNotification event) {
- String relativeKey = event.name.substring(prefixLen);
- int endSubTable = relativeKey.indexOf(PATH_SEPARATOR);
- if (endSubTable == -1)
- return;
- String subTableKey = relativeKey.substring(0, endSubTable);
- if (notifiedTables.contains(subTableKey))
- return;
- notifiedTables.add(subTableKey);
- targetListener.valueChangedEx(targetSource, subTableKey, targetSource.getSubTable(subTableKey), event.flags);
- }
- }
-
- /**
- * {@inheritDoc}
- * @deprecated Use {@link edu.wpi.first.networktables.NetworkTable#addSubTableListener(TableListener, boolean)}
- * instead.
- */
- @Override
- @Deprecated
- public synchronized void addSubTableListener(final ITableListener listener,
- boolean localNotify) {
- List<ListenerBase> adapters = oldListenerMap.get(listener);
- if (adapters == null) {
- adapters = new ArrayList<ListenerBase>();
- oldListenerMap.put(listener, adapters);
- }
- OldSubListenerAdapter adapter =
- new OldSubListenerAdapter(path.length() + 1, this, listener);
- int flags = NOTIFY_NEW | NOTIFY_IMMEDIATE;
- if (localNotify)
- flags |= NOTIFY_LOCAL;
- adapter.uid = inst.addEntryListener(pathWithSep, adapter, flags);
- adapters.add(adapter);
- }
-
- /**
- * {@inheritDoc}
- * @deprecated Use {@link edu.wpi.first.networktables.NetworkTable#removeTableListener(int)} instead.
- */
- @Override
- @Deprecated
- public synchronized void removeTableListener(ITableListener listener) {
- List<ListenerBase> adapters = oldListenerMap.remove(listener);
- if (adapters != null) {
- for (ListenerBase adapter : adapters)
- inst.removeEntryListener(adapter.uid);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public ITable getSubTable(String key) {
- return new NetworkTable(inst, pathWithSep + key);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean containsKey(String key) {
- return getEntry(key).exists();
- }
-
- @Override
- public boolean containsSubTable(String key) {
- int[] handles = NetworkTablesJNI.getEntries(inst.getHandle(), pathWithSep + key + PATH_SEPARATOR, 0);
- return handles.length != 0;
- }
-
- /**
- * @param types bitmask of types; 0 is treated as a "don't care".
- * @return keys currently in the table
- */
- @Override
- public Set<String> getKeys(int types) {
- Set<String> keys = new HashSet<String>();
- int prefixLen = path.length() + 1;
- for (EntryInfo info : inst.getEntryInfo(pathWithSep, types)) {
- String relativeKey = info.name.substring(prefixLen);
- if (relativeKey.indexOf(PATH_SEPARATOR) != -1)
- continue;
- keys.add(relativeKey);
- // populate entries as we go
- if (entries.get(relativeKey) == null) {
- entries.putIfAbsent(relativeKey, new NetworkTableEntry(inst, info.entry));
- }
- }
- return keys;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Set<String> getKeys() {
- return getKeys(0);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Set<String> getSubTables() {
- Set<String> keys = new HashSet<String>();
- int prefixLen = path.length() + 1;
- for (EntryInfo info : inst.getEntryInfo(pathWithSep, 0)) {
- String relativeKey = info.name.substring(prefixLen);
- int endSubTable = relativeKey.indexOf(PATH_SEPARATOR);
- if (endSubTable == -1)
- continue;
- keys.add(relativeKey.substring(0, endSubTable));
- }
- return keys;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean putNumber(String key, double value) {
- return getEntry(key).setNumber(value);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean setDefaultNumber(String key, double defaultValue) {
- return getEntry(key).setDefaultDouble(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public double getNumber(String key, double defaultValue) {
- return getEntry(key).getDouble(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean putString(String key, String value) {
- return getEntry(key).setString(value);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean setDefaultString(String key, String defaultValue) {
- return getEntry(key).setDefaultString(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String getString(String key, String defaultValue) {
- return getEntry(key).getString(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean putBoolean(String key, boolean value) {
- return getEntry(key).setBoolean(value);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean setDefaultBoolean(String key, boolean defaultValue) {
- return getEntry(key).setDefaultBoolean(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean getBoolean(String key, boolean defaultValue) {
- return getEntry(key).getBoolean(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean putBooleanArray(String key, boolean[] value) {
- return getEntry(key).setBooleanArray(value);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean putBooleanArray(String key, Boolean[] value) {
- return getEntry(key).setBooleanArray(value);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean setDefaultBooleanArray(String key, boolean[] defaultValue) {
- return getEntry(key).setDefaultBooleanArray(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean setDefaultBooleanArray(String key, Boolean[] defaultValue) {
- return getEntry(key).setDefaultBooleanArray(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean[] getBooleanArray(String key, boolean[] defaultValue) {
- return getEntry(key).getBooleanArray(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Boolean[] getBooleanArray(String key, Boolean[] defaultValue) {
- return getEntry(key).getBooleanArray(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean putNumberArray(String key, double[] value) {
- return getEntry(key).setDoubleArray(value);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean putNumberArray(String key, Double[] value) {
- return getEntry(key).setNumberArray(value);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean setDefaultNumberArray(String key, double[] defaultValue) {
- return getEntry(key).setDefaultDoubleArray(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean setDefaultNumberArray(String key, Double[] defaultValue) {
- return getEntry(key).setDefaultNumberArray(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public double[] getNumberArray(String key, double[] defaultValue) {
- return getEntry(key).getDoubleArray(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Double[] getNumberArray(String key, Double[] defaultValue) {
- return getEntry(key).getDoubleArray(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean putStringArray(String key, String[] value) {
- return getEntry(key).setStringArray(value);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean setDefaultStringArray(String key, String[] defaultValue) {
- return getEntry(key).setDefaultStringArray(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String[] getStringArray(String key, String[] defaultValue) {
- return getEntry(key).getStringArray(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean putRaw(String key, byte[] value) {
- return getEntry(key).setRaw(value);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean setDefaultRaw(String key, byte[] defaultValue) {
- return getEntry(key).setDefaultRaw(defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean putRaw(String key, ByteBuffer value, int len) {
- return getEntry(key).setRaw(value, len);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public byte[] getRaw(String key, byte[] defaultValue) {
- return getEntry(key).getRaw(defaultValue);
- }
-
- /**
- * Put a value in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- public boolean putValue(String key, NetworkTableValue value) {
- return getEntry(key).setValue(value);
- }
-
- /**
- * Sets the current value in the table if it does not exist.
- * @param key the key
- * @param defaultValue the default value to set if key doens't exist.
- * @return False if the table key exists with a different type
- */
- public boolean setDefaultValue(String key, NetworkTableValue defaultValue) {
- return getEntry(key).setDefaultValue(defaultValue);
- }
-
- /**
- * Gets the value associated with a key as a NetworkTableValue object.
- * @param key the key of the value to look up
- * @return the value associated with the given key
- */
- public NetworkTableValue getValue(String key) {
- return getEntry(key).getValue();
- }
-
- /**
- * {@inheritDoc}
- * @deprecated Use {@link edu.wpi.first.networktables.NetworkTableEntry#setValue(Object)}
- * instead, e.g. `NetworkTable.getEntry(key).setValue(NetworkTableEntry.makeBoolean(false));`
- * or `NetworkTable.getEntry(key).setValue(new Boolean(false));`
- */
- @Override
- @Deprecated
- public boolean putValue(String key, Object value) throws IllegalArgumentException {
- if (value instanceof Boolean)
- return putBoolean(key, ((Boolean)value).booleanValue());
- else if (value instanceof Number)
- return putDouble(key, ((Number)value).doubleValue());
- else if (value instanceof String)
- return putString(key, (String)value);
- else if (value instanceof byte[])
- return putRaw(key, (byte[])value);
- else if (value instanceof boolean[])
- return putBooleanArray(key, (boolean[])value);
- else if (value instanceof double[])
- return putNumberArray(key, (double[])value);
- else if (value instanceof Boolean[])
- return putBooleanArray(key, toNative((Boolean[])value));
- else if (value instanceof Number[])
- return putNumberArray(key, toNative((Number[])value));
- else if (value instanceof String[])
- return putStringArray(key, (String[])value);
- else if (value instanceof NetworkTableValue)
- return getEntry(key).setValue((NetworkTableValue)value);
- else
- throw new IllegalArgumentException("Value of type " + value.getClass().getName() + " cannot be put into a table");
- }
-
- /**
- * {@inheritDoc}
- * @deprecated Use {@link edu.wpi.first.networktables.NetworkTableEntry#getValue()}
- * instead, e.g. `NetworkTable.getEntry(key).getValue();`
- */
- @Override
- @Deprecated
- public Object getValue(String key, Object defaultValue) {
- NetworkTableValue value = getValue(key);
- if (value.getType() == NetworkTableType.kUnassigned) {
- return defaultValue;
- }
- return value.getValue();
- }
-
- /** The persistent flag value. */
- public static final int PERSISTENT = 1;
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void setPersistent(String key) {
- getEntry(key).setPersistent();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void clearPersistent(String key) {
- getEntry(key).clearPersistent();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean isPersistent(String key) {
- return getEntry(key).isPersistent();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void setFlags(String key, int flags) {
- getEntry(key).setFlags(flags);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void clearFlags(String key, int flags) {
- getEntry(key).clearFlags(flags);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int getFlags(String key) {
- return getEntry(key).getFlags();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void delete(String key) {
- getEntry(key).delete();
- }
-
- /**
- * Deletes ALL keys in ALL subtables. Use with caution!
- * @deprecated Use {@link NetworkTableInstance#deleteAllEntries()} instead.
- */
- @Deprecated
- public static void globalDeleteAll() {
- NetworkTableInstance.getDefault().deleteAllEntries();
- }
-
- /**
- * Flushes all updated values immediately to the network.
- * Note: This is rate-limited to protect the network from flooding.
- * This is primarily useful for synchronizing network updates with
- * user code.
- * @deprecated Use {@link NetworkTableInstance#flush()} instead.
- */
- @Deprecated
- public static void flush() {
- NetworkTableInstance.getDefault().flush();
- }
-
- /**
- * Set the periodic update rate.
- *
- * @param interval update interval in seconds (range 0.01 to 1.0)
- * @deprecated Use {@link NetworkTableInstance#setUpdateRate(double)}
- * instead.
- */
- @Deprecated
- public static void setUpdateRate(double interval) {
- NetworkTableInstance.getDefault().setUpdateRate(interval);
- }
-
- /**
- * Saves persistent keys to a file. The server does this automatically.
- *
- * @param filename file name
- * @throws PersistentException if error saving file
- * @deprecated Use {@link NetworkTableInstance#savePersistent(String)}
- * instead.
- */
- @Deprecated
- public static void savePersistent(String filename) throws PersistentException {
- NetworkTableInstance.getDefault().savePersistent(filename);
- }
-
- /**
- * Loads persistent keys from a file. The server does this automatically.
- *
- * @param filename file name
- * @return List of warnings (errors result in an exception instead)
- * @throws PersistentException if error reading file
- * @deprecated Use {@link NetworkTableInstance#loadPersistent(String)}
- * instead.
- */
- @Deprecated
- public static String[] loadPersistent(String filename) throws PersistentException {
- return NetworkTableInstance.getDefault().loadPersistent(filename);
- }
-
- /*
- * Deprecated Methods
- */
-
- /**
- * {@inheritDoc}
- * @deprecated Use {@link #putNumber(String, double)} instead.
- */
- @Override
- @Deprecated
- public boolean putDouble(String key, double value) {
- return putNumber(key, value);
- }
-
- /**
- * {@inheritDoc}
- * @deprecated Use {@link #getNumber(String, double)} instead.
- */
- @Override
- @Deprecated
- public double getDouble(String key, double defaultValue) {
- return getNumber(key, defaultValue);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String getPath() {
- return path;
- }
-
- @Override
- public boolean equals(Object o) {
- if (o == this) {
- return true;
- }
- if (!(o instanceof NetworkTable)) {
- return false;
- }
- NetworkTable other = (NetworkTable) o;
- return inst.equals(other.inst) && path.equals(other.path);
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(inst, path);
- }
-}
diff --git a/ntcore/src/main/java/edu/wpi/first/wpilibj/tables/IRemote.java b/ntcore/src/main/java/edu/wpi/first/wpilibj/tables/IRemote.java
deleted file mode 100644
index 22b6f96..0000000
--- a/ntcore/src/main/java/edu/wpi/first/wpilibj/tables/IRemote.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
-
-package edu.wpi.first.wpilibj.tables;
-
-
-/**
- * Represents an object that has a remote connection.
- * @deprecated Use {@link edu.wpi.first.networktables.NetworkTableInstance}.
- */
-@Deprecated
-@SuppressWarnings("checkstyle:all")
-public interface IRemote {
- /**
- * Register an object to listen for connection and disconnection events
- *
- * @param listener the listener to be register
- * @param immediateNotify if the listener object should be notified of the current connection state
- */
- public void addConnectionListener(IRemoteConnectionListener listener, boolean immediateNotify);
-
- /**
- * Unregister a listener from connection events
- *
- * @param listener the listener to be unregistered
- */
- public void removeConnectionListener(IRemoteConnectionListener listener);
-
- /**
- * Get the current state of the objects connection
- * @return the current connection state
- */
- public boolean isConnected();
-
- /**
- * If the object is acting as a server
- * @return if the object is a server
- */
- public boolean isServer();
-}
diff --git a/ntcore/src/main/java/edu/wpi/first/wpilibj/tables/IRemoteConnectionListener.java b/ntcore/src/main/java/edu/wpi/first/wpilibj/tables/IRemoteConnectionListener.java
deleted file mode 100644
index a3ff118..0000000
--- a/ntcore/src/main/java/edu/wpi/first/wpilibj/tables/IRemoteConnectionListener.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
-
-package edu.wpi.first.wpilibj.tables;
-
-import edu.wpi.first.networktables.ConnectionInfo;
-
-/**
- * A listener that listens for connection changes in a {@link IRemote} object.
- * @deprecated Use Consumer<{@link edu.wpi.first.networktables.ConnectionNotification}>.
- */
-@Deprecated
-@SuppressWarnings("checkstyle:all")
-public interface IRemoteConnectionListener {
- /**
- * Called when an IRemote is connected
- * @param remote the object that connected
- */
- public void connected(IRemote remote);
- /**
- * Called when an IRemote is disconnected
- * @param remote the object that disconnected
- */
- public void disconnected(IRemote remote);
- /**
- * Extended version of connected called when an IRemote is connected.
- * Contains the connection info of the connected remote
- * @param remote the object that connected
- * @param info the connection info for the connected remote
- */
- default public void connectedEx(IRemote remote, ConnectionInfo info) {
- connected(remote);
- }
- /**
- * Extended version of connected called when an IRemote is disconnected.
- * Contains the connection info of the disconnected remote
- * @param remote the object that disconnected
- * @param info the connection info for the disconnected remote
- */
- default public void disconnectedEx(IRemote remote, ConnectionInfo info) {
- disconnected(remote);
- }
-}
diff --git a/ntcore/src/main/java/edu/wpi/first/wpilibj/tables/ITable.java b/ntcore/src/main/java/edu/wpi/first/wpilibj/tables/ITable.java
deleted file mode 100644
index 2d0d5d6..0000000
--- a/ntcore/src/main/java/edu/wpi/first/wpilibj/tables/ITable.java
+++ /dev/null
@@ -1,488 +0,0 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
-
-package edu.wpi.first.wpilibj.tables;
-
-import java.nio.ByteBuffer;
-import java.util.Set;
-
-
-/**
- * A table whose values can be read and written to.
- * @deprecated Use {@link edu.wpi.first.networktables.NetworkTable}.
- */
-@Deprecated
-@SuppressWarnings("checkstyle:all")
-public interface ITable {
-
- /**
- * Checks the table and tells if it contains the specified key
- *
- * @param key the key to search for
- * @return true if the table as a value assigned to the given key
- */
- public boolean containsKey(String key);
-
- /**
- * @param key the key to search for
- * @return true if there is a subtable with the key which contains at least
- * one key/subtable of its own
- */
- public boolean containsSubTable(String key);
-
- /**
- * Returns the table at the specified key. If there is no table at the
- * specified key, it will create a new table
- *
- * @param key the name of the table relative to this one
- * @return a sub table relative to this one
- */
- public ITable getSubTable(String key);
-
- /**
- * Gets all keys in the table (not including sub-tables).
- * @param types bitmask of types; 0 is treated as a "don't care".
- * @return keys currently in the table
- */
- public Set<String> getKeys(int types);
-
- /**
- * Gets all keys in the table (not including sub-tables).
- * @return keys currently in the table
- */
- public Set<String> getKeys();
-
- /**
- * Gets the names of all subtables in the table.
- * @return subtables currently in the table
- */
- public Set<String> getSubTables();
-
- /**
- * Makes a key's value persistent through program restarts.
- * The key cannot be null.
- *
- * @param key the key name
- */
- public void setPersistent(String key);
-
- /**
- * Stop making a key's value persistent through program restarts.
- * The key cannot be null.
- *
- * @param key the key name
- */
- public void clearPersistent(String key);
-
- /**
- * Returns whether the value is persistent through program restarts.
- * The key cannot be null.
- *
- * @param key the key name
- * @return True if the value is persistent.
- */
- public boolean isPersistent(String key);
-
- /**
- * Sets flags on the specified key in this table. The key can
- * not be null.
- *
- * @param key the key name
- * @param flags the flags to set (bitmask)
- */
- public void setFlags(String key, int flags);
-
- /**
- * Clears flags on the specified key in this table. The key can
- * not be null.
- *
- * @param key the key name
- * @param flags the flags to clear (bitmask)
- */
- public void clearFlags(String key, int flags);
-
- /**
- * Returns the flags for the specified key.
- *
- * @param key the key name
- * @return the flags, or 0 if the key is not defined
- */
- public int getFlags(String key);
-
- /**
- * Deletes the specified key in this table. The key can
- * not be null.
- *
- * @param key the key name
- */
- public void delete(String key);
-
- /**
- * Gets the value associated with a key as an object.
- * NOTE: If the value is a double, it will return a Double object,
- * not a primitive. To get the primitive, use
- * {@link #getDouble(String, double)}.
- * @param key the key of the value to look up
- * @param defaultValue the default value if the key is null
- * @return the value associated with the given key
- */
- public Object getValue(String key, Object defaultValue);
-
- /**
- * Put a value in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- * @throws IllegalArgumentException when the value is not supported by the
- * table
- */
- public boolean putValue(String key, Object value)
- throws IllegalArgumentException;
-
- /**
- * Put a number in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- public boolean putNumber(String key, double value);
-
- /**
- * 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 doens't exist.
- * @return False if the table key exists with a different type
- */
- public boolean setDefaultNumber(String key, double defaultValue);
-
- /**
- * Returns the number the key maps to. If the key does not exist or is of
- * different type, it will return the default value.
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- */
- public double getNumber(String key, double defaultValue);
-
- /**
- * Put a string in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- public boolean putString(String key, String value);
-
- /**
- * 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 doens't exist.
- * @return False if the table key exists with a different type
- */
- public boolean setDefaultString(String key, String defaultValue);
-
- /**
- * Returns the string the key maps to. If the key does not exist or is of
- * different type, it will return the default value.
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- */
- public String getString(String key, String defaultValue);
-
- /**
- * Put a boolean in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- public boolean putBoolean(String key, boolean value);
-
- /**
- * 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 doens't exist.
- * @return False if the table key exists with a different type
- */
- public boolean setDefaultBoolean(String key, boolean defaultValue);
-
- /**
- * Returns the boolean the key maps to. If the key does not exist or is of
- * different type, it will return the default value.
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- */
- public boolean getBoolean(String key, boolean defaultValue);
-
- /**
- * Put a boolean array in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- public boolean putBooleanArray(String key, boolean[] value);
-
- /**
- * 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 doens't exist.
- * @return False if the table key exists with a different type
- */
- public boolean setDefaultBooleanArray(String key, boolean[] defaultValue);
-
- /**
- * Put a boolean array in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- public boolean putBooleanArray(String key, Boolean[] value);
-
- /**
- * 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 doens't exist.
- * @return False if the table key exists with a different type
- */
- public boolean setDefaultBooleanArray(String key, Boolean[] defaultValue);
-
- /**
- * Returns the boolean array the key maps to. If the key does not exist or is
- * of different type, it will return the default value.
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- */
- public boolean[] getBooleanArray(String key, boolean[] defaultValue);
- /**
- * Returns the boolean array the key maps to. If the key does not exist or is
- * of different type, it will return the default value.
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- */
- public Boolean[] getBooleanArray(String key, Boolean[] defaultValue);
-
- /**
- * Put a number array in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- public boolean putNumberArray(String key, double[] value);
-
- /**
- * 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 doens't exist.
- * @return False if the table key exists with a different type
- */
- public boolean setDefaultNumberArray(String key, double[] defaultValue);
-
- /**
- * Put a number array in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- public boolean putNumberArray(String key, Double[] value);
-
- /**
- * 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 doens't exist.
- * @return False if the table key exists with a different type
- */
- public boolean setDefaultNumberArray(String key, Double[] defaultValue);
-
- /**
- * Returns the number array the key maps to. If the key does not exist or is
- * of different type, it will return the default value.
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- */
- public double[] getNumberArray(String key, double[] defaultValue);
- /**
- * Returns the number array the key maps to. If the key does not exist or is
- * of different type, it will return the default value.
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- */
- public Double[] getNumberArray(String key, Double[] defaultValue);
-
- /**
- * Put a string array in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- public boolean putStringArray(String key, String[] value);
-
- /**
- * 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 doens't exist.
- * @return False if the table key exists with a different type
- */
- public boolean setDefaultStringArray(String key, String[] defaultValue);
-
- /**
- * Returns the string array the key maps to. If the key does not exist or is
- * of different type, it will return the default value.
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- */
- public String[] getStringArray(String key, String[] defaultValue);
-
- /**
- * Put a raw value (byte array) in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- public boolean putRaw(String key, byte[] value);
-
- /**
- * 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 doens't exist.
- * @return False if the table key exists with a different type
- */
- public boolean setDefaultRaw(String key, byte[] defaultValue);
-
- /**
- * Put a raw value (bytes from a byte buffer) in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @param len the length of the value
- * @return False if the table key already exists with a different type
- */
- public boolean putRaw(String key, ByteBuffer value, int len);
-
- /**
- * Returns the raw value (byte array) the key maps to. If the key does not
- * exist or is of different type, it will return the default value.
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- */
- public byte[] getRaw(String key, byte[] defaultValue);
-
- /** Notifier flag values. */
- public static final int NOTIFY_IMMEDIATE = 0x01;
- public static final int NOTIFY_LOCAL = 0x02;
- public static final int NOTIFY_NEW = 0x04;
- public static final int NOTIFY_DELETE = 0x08;
- public static final int NOTIFY_UPDATE = 0x10;
- public static final int NOTIFY_FLAGS = 0x20;
-
- /**
- * Add a listener for changes to the table
- * @param listener the listener to add
- */
- public void addTableListener(ITableListener listener);
- /**
- * Add a listener for changes to the table
- * @param listener the listener to add
- * @param immediateNotify if true then this listener will be notified of all
- * current entries (marked as new)
- */
- public void addTableListener(ITableListener listener,
- boolean immediateNotify);
- /**
- * Add a listener for changes to the table
- * @param listener the listener to add
- * @param flags bitmask specifying desired notifications
- */
- public void addTableListenerEx(ITableListener listener, int flags);
-
- /**
- * Add a listener for changes to a specific key the table
- * @param key the key to listen for
- * @param listener the listener to add
- * @param immediateNotify if true then this listener will be notified of all
- * current entries (marked as new)
- */
- public void addTableListener(String key, ITableListener listener,
- boolean immediateNotify);
- /**
- * Add a listener for changes to a specific key the table
- * @param key the key to listen for
- * @param listener the listener to add
- * @param flags bitmask specifying desired notifications
- */
- public void addTableListenerEx(String key, ITableListener listener,
- int flags);
- /**
- * This will immediately notify the listener of all current sub tables
- * @param listener the listener to notify
- */
- public void addSubTableListener(final ITableListener listener);
- /**
- * This will immediately notify the listener of all current sub tables
- * @param listener the listener to notify
- * @param localNotify if true then this listener will be notified of all
- * local changes in addition to all remote changes
- */
- public void addSubTableListener(final ITableListener listener,
- boolean localNotify);
- /**
- * Remove a listener from receiving table events
- * @param listener the listener to be removed
- */
- public void removeTableListener(ITableListener listener);
-
- /*
- * Deprecated Methods
- */
-
- /**
- * Maps the specified key to the specified value in this table.
- * The key can not be null.
- * The value can be retrieved by calling the get method with a key that is
- * equal to the original key.
- * @param key the key
- * @param value the value
- * @return False if the table key already exists with a different type
- * @throws IllegalArgumentException if key is null
- * @deprecated Use {@link #putNumber(String, double)} instead.
- */
- @Deprecated
- public boolean putDouble(String key, double value);
-
- /**
- * Returns the value at the specified key.
- * @param key the key
- * @param defaultValue the value returned if the key is undefined
- * @return the value
- * @throws IllegalArgumentException if the value mapped to by the key is not a
- * double
- * @throws IllegalArgumentException if the key is null
- * @deprecated Use {@link #getNumber(String, double)} instead.
- */
- @Deprecated
- public double getDouble(String key, double defaultValue);
-
- /**
- * Gets the full path of this table. Does not include the trailing "/".
- * @return The path to this table (e.g. "", "/foo").
- */
- public String getPath();
-
-}
diff --git a/ntcore/src/main/java/edu/wpi/first/wpilibj/tables/ITableListener.java b/ntcore/src/main/java/edu/wpi/first/wpilibj/tables/ITableListener.java
deleted file mode 100644
index b08312b..0000000
--- a/ntcore/src/main/java/edu/wpi/first/wpilibj/tables/ITableListener.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
-
-package edu.wpi.first.wpilibj.tables;
-
-/**
- * A listener that listens to changes in values in a {@link ITable}.
- * @deprecated Use Consumer<{@link edu.wpi.first.networktables.EntryNotification}>,
- * {@link edu.wpi.first.networktables.TableEntryListener}, or
- * {@link edu.wpi.first.networktables.TableListener} as appropriate.
- */
-@FunctionalInterface
-@Deprecated
-@SuppressWarnings("checkstyle:all")
-public interface ITableListener {
- /**
- * Called when a key-value pair is changed in a {@link ITable}
- * @param source the table the key-value pair exists in
- * @param key the key associated with the value that changed
- * @param value the new value
- * @param isNew true if the key did not previously exist in the table, otherwise it is false
- */
- public void valueChanged(ITable source, String key, Object value, boolean isNew);
-
- /**
- * Extended version of valueChanged. Called when a key-value pair is
- * changed in a {@link ITable}. The default implementation simply calls
- * valueChanged(). If this is overridden, valueChanged() will not be
- * called.
- * @param source the table the key-value pair exists in
- * @param key the key associated with the value that changed
- * @param value the new value
- * @param flags update flags; for example, NOTIFY_NEW if the key did not
- * previously exist in the table
- */
- default public void valueChangedEx(ITable source, String key, Object value, int flags) {
- // NOTIFY_NEW = 0x04
- valueChanged(source, key, value, (flags & 0x04) != 0);
- }
-}
diff --git a/ntcore/src/main/native/cpp/CallbackManager.h b/ntcore/src/main/native/cpp/CallbackManager.h
deleted file mode 100644
index bf9b000..0000000
--- a/ntcore/src/main/native/cpp/CallbackManager.h
+++ /dev/null
@@ -1,328 +0,0 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
-
-#ifndef NTCORE_CALLBACKMANAGER_H_
-#define NTCORE_CALLBACKMANAGER_H_
-
-#include <atomic>
-#include <climits>
-#include <functional>
-#include <memory>
-#include <queue>
-#include <utility>
-#include <vector>
-
-#include <wpi/SafeThread.h>
-#include <wpi/UidVector.h>
-#include <wpi/condition_variable.h>
-#include <wpi/mutex.h>
-#include <wpi/raw_ostream.h>
-
-namespace nt {
-
-namespace impl {
-
-template <typename Callback>
-class ListenerData {
- public:
- ListenerData() = default;
- explicit ListenerData(Callback callback_) : callback(callback_) {}
- explicit ListenerData(unsigned int poller_uid_) : poller_uid(poller_uid_) {}
-
- explicit operator bool() const { return callback || poller_uid != UINT_MAX; }
-
- Callback callback;
- unsigned int poller_uid = UINT_MAX;
-};
-
-// CRTP callback manager thread
-// @tparam Derived derived class
-// @tparam NotifierData data buffered for each callback
-// @tparam ListenerData data stored for each listener
-// Derived must define the following functions:
-// bool Matches(const ListenerData& listener, const NotifierData& data);
-// void SetListener(NotifierData* data, unsigned int listener_uid);
-// void DoCallback(Callback callback, const NotifierData& data);
-template <typename Derived, typename TUserInfo,
- typename TListenerData =
- ListenerData<std::function<void(const TUserInfo& info)>>,
- typename TNotifierData = TUserInfo>
-class CallbackThread : public wpi::SafeThread {
- public:
- typedef TUserInfo UserInfo;
- typedef TNotifierData NotifierData;
- typedef TListenerData ListenerData;
-
- ~CallbackThread() {
- // Wake up any blocked pollers
- for (size_t i = 0; i < m_pollers.size(); ++i) {
- if (auto poller = m_pollers[i]) poller->Terminate();
- }
- }
-
- void Main() override;
-
- wpi::UidVector<ListenerData, 64> m_listeners;
-
- std::queue<std::pair<unsigned int, NotifierData>> m_queue;
- wpi::condition_variable m_queue_empty;
-
- struct Poller {
- void Terminate() {
- {
- std::scoped_lock lock(poll_mutex);
- terminating = true;
- }
- poll_cond.notify_all();
- }
- std::queue<NotifierData> poll_queue;
- wpi::mutex poll_mutex;
- wpi::condition_variable poll_cond;
- bool terminating = false;
- bool canceling = false;
- };
- wpi::UidVector<std::shared_ptr<Poller>, 64> m_pollers;
-
- // Must be called with m_mutex held
- template <typename... Args>
- void SendPoller(unsigned int poller_uid, Args&&... args) {
- if (poller_uid > m_pollers.size()) return;
- auto poller = m_pollers[poller_uid];
- if (!poller) return;
- {
- std::scoped_lock lock(poller->poll_mutex);
- poller->poll_queue.emplace(std::forward<Args>(args)...);
- }
- poller->poll_cond.notify_one();
- }
-};
-
-template <typename Derived, typename TUserInfo, typename TListenerData,
- typename TNotifierData>
-void CallbackThread<Derived, TUserInfo, TListenerData, TNotifierData>::Main() {
- std::unique_lock lock(m_mutex);
- while (m_active) {
- while (m_queue.empty()) {
- m_cond.wait(lock);
- if (!m_active) return;
- }
-
- while (!m_queue.empty()) {
- if (!m_active) return;
- auto item = std::move(m_queue.front());
-
- if (item.first != UINT_MAX) {
- if (item.first < m_listeners.size()) {
- auto& listener = m_listeners[item.first];
- if (listener &&
- static_cast<Derived*>(this)->Matches(listener, item.second)) {
- static_cast<Derived*>(this)->SetListener(&item.second, item.first);
- if (listener.callback) {
- lock.unlock();
- static_cast<Derived*>(this)->DoCallback(listener.callback,
- item.second);
- lock.lock();
- } else if (listener.poller_uid != UINT_MAX) {
- SendPoller(listener.poller_uid, std::move(item.second));
- }
- }
- }
- } else {
- // Use index because iterator might get invalidated.
- for (size_t i = 0; i < m_listeners.size(); ++i) {
- auto& listener = m_listeners[i];
- if (!listener) continue;
- if (!static_cast<Derived*>(this)->Matches(listener, item.second))
- continue;
- static_cast<Derived*>(this)->SetListener(&item.second,
- static_cast<unsigned>(i));
- if (listener.callback) {
- lock.unlock();
- static_cast<Derived*>(this)->DoCallback(listener.callback,
- item.second);
- lock.lock();
- } else if (listener.poller_uid != UINT_MAX) {
- SendPoller(listener.poller_uid, item.second);
- }
- }
- }
- m_queue.pop();
- }
-
- m_queue_empty.notify_all();
- }
-}
-
-} // namespace impl
-
-// CRTP callback manager
-// @tparam Derived derived class
-// @tparam Thread custom thread (must be derived from impl::CallbackThread)
-//
-// Derived must define the following functions:
-// void Start();
-template <typename Derived, typename Thread>
-class CallbackManager {
- friend class RpcServerTest;
-
- public:
- void Stop() { m_owner.Stop(); }
-
- void Remove(unsigned int listener_uid) {
- auto thr = m_owner.GetThread();
- if (!thr) return;
- thr->m_listeners.erase(listener_uid);
- }
-
- unsigned int CreatePoller() {
- static_cast<Derived*>(this)->Start();
- auto thr = m_owner.GetThread();
- return thr->m_pollers.emplace_back(
- std::make_shared<typename Thread::Poller>());
- }
-
- void RemovePoller(unsigned int poller_uid) {
- auto thr = m_owner.GetThread();
- if (!thr) return;
-
- // Remove any listeners that are associated with this poller
- for (size_t i = 0; i < thr->m_listeners.size(); ++i) {
- if (thr->m_listeners[i].poller_uid == poller_uid)
- thr->m_listeners.erase(i);
- }
-
- // Wake up any blocked pollers
- if (poller_uid >= thr->m_pollers.size()) return;
- auto poller = thr->m_pollers[poller_uid];
- if (!poller) return;
- poller->Terminate();
- return thr->m_pollers.erase(poller_uid);
- }
-
- bool WaitForQueue(double timeout) {
- auto thr = m_owner.GetThread();
- if (!thr) return true;
-
- auto& lock = thr.GetLock();
- auto timeout_time = std::chrono::steady_clock::now() +
- std::chrono::duration<double>(timeout);
- while (!thr->m_queue.empty()) {
- if (!thr->m_active) return true;
- if (timeout == 0) return false;
- if (timeout < 0) {
- thr->m_queue_empty.wait(lock);
- } else {
- auto cond_timed_out = thr->m_queue_empty.wait_until(lock, timeout_time);
- if (cond_timed_out == std::cv_status::timeout) return false;
- }
- }
-
- return true;
- }
-
- std::vector<typename Thread::UserInfo> Poll(unsigned int poller_uid) {
- bool timed_out = false;
- return Poll(poller_uid, -1, &timed_out);
- }
-
- std::vector<typename Thread::UserInfo> Poll(unsigned int poller_uid,
- double timeout, bool* timed_out) {
- std::vector<typename Thread::UserInfo> infos;
- std::shared_ptr<typename Thread::Poller> poller;
- {
- auto thr = m_owner.GetThread();
- if (!thr) return infos;
- if (poller_uid > thr->m_pollers.size()) return infos;
- poller = thr->m_pollers[poller_uid];
- if (!poller) return infos;
- }
-
- std::unique_lock lock(poller->poll_mutex);
- auto timeout_time = std::chrono::steady_clock::now() +
- std::chrono::duration<double>(timeout);
- *timed_out = false;
- while (poller->poll_queue.empty()) {
- if (poller->terminating) return infos;
- if (poller->canceling) {
- // Note: this only works if there's a single thread calling this
- // function for any particular poller, but that's the intended use.
- poller->canceling = false;
- return infos;
- }
- if (timeout == 0) {
- *timed_out = true;
- return infos;
- }
- if (timeout < 0) {
- poller->poll_cond.wait(lock);
- } else {
- auto cond_timed_out = poller->poll_cond.wait_until(lock, timeout_time);
- if (cond_timed_out == std::cv_status::timeout) {
- *timed_out = true;
- return infos;
- }
- }
- }
-
- while (!poller->poll_queue.empty()) {
- infos.emplace_back(std::move(poller->poll_queue.front()));
- poller->poll_queue.pop();
- }
- return infos;
- }
-
- void CancelPoll(unsigned int poller_uid) {
- std::shared_ptr<typename Thread::Poller> poller;
- {
- auto thr = m_owner.GetThread();
- if (!thr) return;
- if (poller_uid > thr->m_pollers.size()) return;
- poller = thr->m_pollers[poller_uid];
- if (!poller) return;
- }
-
- {
- std::scoped_lock lock(poller->poll_mutex);
- poller->canceling = true;
- }
- poller->poll_cond.notify_one();
- }
-
- protected:
- template <typename... Args>
- void DoStart(Args&&... args) {
- m_owner.Start(std::forward<Args>(args)...);
- }
-
- template <typename... Args>
- unsigned int DoAdd(Args&&... args) {
- static_cast<Derived*>(this)->Start();
- auto thr = m_owner.GetThread();
- return thr->m_listeners.emplace_back(std::forward<Args>(args)...);
- }
-
- template <typename... Args>
- void Send(unsigned int only_listener, Args&&... args) {
- auto thr = m_owner.GetThread();
- if (!thr || thr->m_listeners.empty()) return;
- thr->m_queue.emplace(std::piecewise_construct,
- std::make_tuple(only_listener),
- std::forward_as_tuple(std::forward<Args>(args)...));
- thr->m_cond.notify_one();
- }
-
- typename wpi::SafeThreadOwner<Thread>::Proxy GetThread() const {
- return m_owner.GetThread();
- }
-
- private:
- wpi::SafeThreadOwner<Thread> m_owner;
-};
-
-} // namespace nt
-
-#endif // NTCORE_CALLBACKMANAGER_H_
diff --git a/ntcore/src/main/native/cpp/ConnectionNotifier.cpp b/ntcore/src/main/native/cpp/ConnectionNotifier.cpp
index 340aad0..60e7303 100644
--- a/ntcore/src/main/native/cpp/ConnectionNotifier.cpp
+++ b/ntcore/src/main/native/cpp/ConnectionNotifier.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "ConnectionNotifier.h"
@@ -11,7 +8,9 @@
ConnectionNotifier::ConnectionNotifier(int inst) : m_inst(inst) {}
-void ConnectionNotifier::Start() { DoStart(m_inst); }
+void ConnectionNotifier::Start() {
+ DoStart(m_inst);
+}
unsigned int ConnectionNotifier::Add(
std::function<void(const ConnectionNotification& event)> callback) {
diff --git a/ntcore/src/main/native/cpp/ConnectionNotifier.h b/ntcore/src/main/native/cpp/ConnectionNotifier.h
index 65eec06..fab4733 100644
--- a/ntcore/src/main/native/cpp/ConnectionNotifier.h
+++ b/ntcore/src/main/native/cpp/ConnectionNotifier.h
@@ -1,14 +1,14 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_CONNECTIONNOTIFIER_H_
#define NTCORE_CONNECTIONNOTIFIER_H_
-#include "CallbackManager.h"
+#include <utility>
+
+#include <wpi/CallbackManager.h>
+
#include "Handle.h"
#include "IConnectionNotifier.h"
#include "ntcore_cpp.h"
@@ -18,9 +18,12 @@
namespace impl {
class ConnectionNotifierThread
- : public CallbackThread<ConnectionNotifierThread, ConnectionNotification> {
+ : public wpi::CallbackThread<ConnectionNotifierThread,
+ ConnectionNotification> {
public:
- explicit ConnectionNotifierThread(int inst) : m_inst(inst) {}
+ ConnectionNotifierThread(std::function<void()> on_start,
+ std::function<void()> on_exit, int inst)
+ : CallbackThread(std::move(on_start), std::move(on_exit)), m_inst(inst) {}
bool Matches(const ListenerData& /*listener*/,
const ConnectionNotification& /*data*/) {
@@ -45,11 +48,11 @@
class ConnectionNotifier
: public IConnectionNotifier,
- public CallbackManager<ConnectionNotifier,
- impl::ConnectionNotifierThread> {
+ public wpi::CallbackManager<ConnectionNotifier,
+ impl::ConnectionNotifierThread> {
friend class ConnectionNotifierTest;
- friend class CallbackManager<ConnectionNotifier,
- impl::ConnectionNotifierThread>;
+ friend class wpi::CallbackManager<ConnectionNotifier,
+ impl::ConnectionNotifierThread>;
public:
explicit ConnectionNotifier(int inst);
diff --git a/ntcore/src/main/native/cpp/Dispatcher.cpp b/ntcore/src/main/native/cpp/Dispatcher.cpp
index f3bff4d..84d9698 100644
--- a/ntcore/src/main/native/cpp/Dispatcher.cpp
+++ b/ntcore/src/main/native/cpp/Dispatcher.cpp
@@ -1,17 +1,17 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "Dispatcher.h"
#include <algorithm>
#include <iterator>
+#include <wpi/SmallVector.h>
+#include <wpi/StringExtras.h>
#include <wpi/TCPAcceptor.h>
#include <wpi/TCPConnector.h>
+#include <wpi/timestamp.h>
#include "IConnectionNotifier.h"
#include "IStorage.h"
@@ -20,9 +20,9 @@
using namespace nt;
-void Dispatcher::StartServer(const Twine& persist_filename,
+void Dispatcher::StartServer(std::string_view persist_filename,
const char* listen_address, unsigned int port) {
- std::string listen_address_copy(StringRef(listen_address).trim());
+ std::string listen_address_copy(wpi::trim(listen_address));
DispatcherBase::StartServer(
persist_filename,
std::unique_ptr<wpi::NetworkAcceptor>(new wpi::TCPAcceptor(
@@ -30,7 +30,7 @@
}
void Dispatcher::SetServer(const char* server_name, unsigned int port) {
- std::string server_name_copy(StringRef(server_name).trim());
+ std::string server_name_copy(wpi::trim(server_name));
SetConnector([=]() -> std::unique_ptr<wpi::NetworkStream> {
return wpi::TCPConnector::connect(server_name_copy.c_str(),
static_cast<int>(port), m_logger, 1);
@@ -38,71 +38,59 @@
}
void Dispatcher::SetServer(
- ArrayRef<std::pair<StringRef, unsigned int>> servers) {
+ wpi::span<const std::pair<std::string_view, unsigned int>> servers) {
wpi::SmallVector<std::pair<std::string, int>, 16> servers_copy;
- for (const auto& server : servers)
- servers_copy.emplace_back(std::string{server.first.trim()},
+ for (const auto& server : servers) {
+ servers_copy.emplace_back(std::string{wpi::trim(server.first)},
static_cast<int>(server.second));
+ }
SetConnector([=]() -> std::unique_ptr<wpi::NetworkStream> {
wpi::SmallVector<std::pair<const char*, int>, 16> servers_copy2;
- for (const auto& server : servers_copy)
+ for (const auto& server : servers_copy) {
servers_copy2.emplace_back(server.first.c_str(), server.second);
+ }
return wpi::TCPConnector::connect_parallel(servers_copy2, m_logger, 1);
});
}
void Dispatcher::SetServerTeam(unsigned int team, unsigned int port) {
- std::pair<StringRef, unsigned int> servers[5];
+ std::pair<std::string_view, unsigned int> servers[5];
// 10.te.am.2
- wpi::SmallString<32> fixed;
- {
- wpi::raw_svector_ostream oss{fixed};
- oss << "10." << static_cast<int>(team / 100) << '.'
- << static_cast<int>(team % 100) << ".2";
- servers[0] = std::make_pair(oss.str(), port);
- }
+ auto fixed = fmt::format("10.{}.{}.2", static_cast<int>(team / 100),
+ static_cast<int>(team % 100));
+ servers[0] = {fixed, port};
// 172.22.11.2
- servers[1] = std::make_pair("172.22.11.2", port);
+ servers[1] = {"172.22.11.2", port};
// roboRIO-<team>-FRC.local
- wpi::SmallString<32> mdns;
- {
- wpi::raw_svector_ostream oss{mdns};
- oss << "roboRIO-" << team << "-FRC.local";
- servers[2] = std::make_pair(oss.str(), port);
- }
+ auto mdns = fmt::format("roboRIO-{}-FRC.local", team);
+ servers[2] = {mdns, port};
// roboRIO-<team>-FRC.lan
- wpi::SmallString<32> mdns_lan;
- {
- wpi::raw_svector_ostream oss{mdns_lan};
- oss << "roboRIO-" << team << "-FRC.lan";
- servers[3] = std::make_pair(oss.str(), port);
- }
+ auto mdns_lan = fmt::format("roboRIO-{}-FRC.lan", team);
+ servers[3] = {mdns_lan, port};
// roboRIO-<team>-FRC.frc-field.local
- wpi::SmallString<64> field_local;
- {
- wpi::raw_svector_ostream oss{field_local};
- oss << "roboRIO-" << team << "-FRC.frc-field.local";
- servers[4] = std::make_pair(oss.str(), port);
- }
+ auto field_local = fmt::format("roboRIO-{}-FRC.frc-field.local", team);
+ servers[4] = {field_local, port};
SetServer(servers);
}
void Dispatcher::SetServerOverride(const char* server_name, unsigned int port) {
- std::string server_name_copy(StringRef(server_name).trim());
+ std::string server_name_copy(wpi::trim(server_name));
SetConnectorOverride([=]() -> std::unique_ptr<wpi::NetworkStream> {
return wpi::TCPConnector::connect(server_name_copy.c_str(),
static_cast<int>(port), m_logger, 1);
});
}
-void Dispatcher::ClearServerOverride() { ClearConnectorOverride(); }
+void Dispatcher::ClearServerOverride() {
+ ClearConnectorOverride();
+}
DispatcherBase::DispatcherBase(IStorage& storage, IConnectionNotifier& notifier,
wpi::Logger& logger)
@@ -111,14 +99,20 @@
m_update_rate = 100;
}
-DispatcherBase::~DispatcherBase() { Stop(); }
+DispatcherBase::~DispatcherBase() {
+ Stop();
+}
-unsigned int DispatcherBase::GetNetworkMode() const { return m_networkMode; }
+unsigned int DispatcherBase::GetNetworkMode() const {
+ return m_networkMode;
+}
void DispatcherBase::StartLocal() {
{
std::scoped_lock lock(m_user_mutex);
- if (m_active) return;
+ if (m_active) {
+ return;
+ }
m_active = true;
}
m_networkMode = NT_NET_MODE_LOCAL;
@@ -126,30 +120,30 @@
}
void DispatcherBase::StartServer(
- const Twine& persist_filename,
+ std::string_view persist_filename,
std::unique_ptr<wpi::NetworkAcceptor> acceptor) {
{
std::scoped_lock lock(m_user_mutex);
- if (m_active) return;
+ if (m_active) {
+ return;
+ }
m_active = true;
}
m_networkMode = NT_NET_MODE_SERVER | NT_NET_MODE_STARTING;
- m_persist_filename = persist_filename.str();
+ m_persist_filename = persist_filename;
m_server_acceptor = std::move(acceptor);
// Load persistent file. Ignore errors, but pass along warnings.
- if (!persist_filename.isTriviallyEmpty() &&
- (!persist_filename.isSingleStringRef() ||
- !persist_filename.getSingleStringRef().empty())) {
+ if (!persist_filename.empty()) {
bool first = true;
m_storage.LoadPersistent(
persist_filename, [&](size_t line, const char* msg) {
if (first) {
first = false;
- WARNING("When reading initial persistent values from '"
- << persist_filename << "':");
+ WARNING("When reading initial persistent values from '{}':",
+ persist_filename);
}
- WARNING(persist_filename << ":" << line << ": " << msg);
+ WARNING("{}:{}: {}", persist_filename, line, msg);
});
}
@@ -162,7 +156,9 @@
void DispatcherBase::StartClient() {
{
std::scoped_lock lock(m_user_mutex);
- if (m_active) return;
+ if (m_active) {
+ return;
+ }
m_active = true;
}
m_networkMode = NT_NET_MODE_CLIENT | NT_NET_MODE_STARTING;
@@ -186,11 +182,17 @@
ClientReconnect();
// wake up server thread by shutting down the socket
- if (m_server_acceptor) m_server_acceptor->shutdown();
+ if (m_server_acceptor) {
+ m_server_acceptor->shutdown();
+ }
// join threads, with timeout
- if (m_dispatch_thread.joinable()) m_dispatch_thread.join();
- if (m_clientserver_thread.joinable()) m_clientserver_thread.join();
+ if (m_dispatch_thread.joinable()) {
+ m_dispatch_thread.join();
+ }
+ if (m_clientserver_thread.joinable()) {
+ m_clientserver_thread.join();
+ }
std::vector<std::shared_ptr<INetworkConnection>> conns;
{
@@ -203,25 +205,28 @@
}
void DispatcherBase::SetUpdateRate(double interval) {
- // don't allow update rates faster than 10 ms or slower than 1 second
- if (interval < 0.01)
- interval = 0.01;
- else if (interval > 1.0)
+ // don't allow update rates faster than 5 ms or slower than 1 second
+ if (interval < 0.005) {
+ interval = 0.005;
+ } else if (interval > 1.0) {
interval = 1.0;
+ }
m_update_rate = static_cast<unsigned int>(interval * 1000);
}
-void DispatcherBase::SetIdentity(const Twine& name) {
+void DispatcherBase::SetIdentity(std::string_view name) {
std::scoped_lock lock(m_user_mutex);
- m_identity = name.str();
+ m_identity = name;
}
void DispatcherBase::Flush() {
- auto now = std::chrono::steady_clock::now();
+ auto now = wpi::Now();
{
std::scoped_lock lock(m_flush_mutex);
- // don't allow flushes more often than every 10 ms
- if ((now - m_last_flush) < std::chrono::milliseconds(10)) return;
+ // don't allow flushes more often than every 5 ms
+ if ((now - m_last_flush) < 5000) {
+ return;
+ }
m_last_flush = now;
m_do_flush = true;
}
@@ -230,11 +235,15 @@
std::vector<ConnectionInfo> DispatcherBase::GetConnections() const {
std::vector<ConnectionInfo> conns;
- if (!m_active) return conns;
+ if (!m_active) {
+ return conns;
+ }
std::scoped_lock lock(m_user_mutex);
for (auto& conn : m_connections) {
- if (conn->state() != NetworkConnection::kActive) continue;
+ if (conn->state() != NetworkConnection::kActive) {
+ continue;
+ }
conns.emplace_back(conn->info());
}
@@ -242,13 +251,19 @@
}
bool DispatcherBase::IsConnected() const {
- if (!m_active) return false;
+ if (!m_active) {
+ return false;
+ }
- if (m_networkMode == NT_NET_MODE_LOCAL) return true;
+ if (m_networkMode == NT_NET_MODE_LOCAL) {
+ return true;
+ }
std::scoped_lock lock(m_user_mutex);
for (auto& conn : m_connections) {
- if (conn->state() == NetworkConnection::kActive) return true;
+ if (conn->state() == NetworkConnection::kActive) {
+ return true;
+ }
}
return false;
@@ -262,7 +277,9 @@
// perform immediate notifications
if (immediate_notify) {
for (auto& conn : m_connections) {
- if (conn->state() != NetworkConnection::kActive) continue;
+ if (conn->state() != NetworkConnection::kActive) {
+ continue;
+ }
m_notifier.NotifyConnection(true, conn->info(), uid);
}
}
@@ -276,7 +293,9 @@
// perform immediate notifications
if (immediate_notify) {
for (auto& conn : m_connections) {
- if (conn->state() != NetworkConnection::kActive) continue;
+ if (conn->state() != NetworkConnection::kActive) {
+ continue;
+ }
m_notifier.NotifyConnection(true, conn->info(), uid);
}
}
@@ -309,7 +328,9 @@
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);
@@ -318,16 +339,22 @@
[&] { return !m_active || m_do_flush; });
m_do_flush = false;
flush_lock.unlock();
- if (!m_active) break; // in case we were woken up to terminate
+ if (!m_active) {
+ break; // in case we were woken up to terminate
+ }
// perform periodic persistent save
if ((m_networkMode & NT_NET_MODE_SERVER) != 0 &&
!m_persist_filename.empty() && start > next_save_time) {
next_save_time += save_delta_time;
// handle loop taking too long
- if (start > next_save_time) next_save_time = start + save_delta_time;
+ if (start > next_save_time) {
+ next_save_time = start + save_delta_time;
+ }
const char* err = m_storage.SavePersistent(m_persist_filename, true);
- if (err) WARNING("periodic persistent save: " << err);
+ if (err) {
+ WARNING("periodic persistent save: {}", err);
+ }
}
{
@@ -335,20 +362,22 @@
bool reconnect = false;
if (++count > 10) {
- DEBUG0("dispatch running " << m_connections.size() << " connections");
+ DEBUG0("dispatch running {} connections", m_connections.size());
count = 0;
}
for (auto& conn : m_connections) {
// post outgoing messages if connection is active
// only send keep-alives on client
- if (conn->state() == NetworkConnection::kActive)
+ if (conn->state() == NetworkConnection::kActive) {
conn->PostOutgoing((m_networkMode & NT_NET_MODE_CLIENT) != 0);
+ }
// if client, reconnect if connection died
if ((m_networkMode & NT_NET_MODE_CLIENT) != 0 &&
- conn->state() == NetworkConnection::kDead)
+ conn->state() == NetworkConnection::kDead) {
reconnect = true;
+ }
}
// reconnect if we disconnected (and a reconnect is not in progress)
if (reconnect && !m_do_reconnect) {
@@ -364,12 +393,17 @@
INetworkConnection* except) {
std::scoped_lock user_lock(m_user_mutex);
for (auto& conn : m_connections) {
- if (conn.get() == except) continue;
- if (only && conn.get() != only) continue;
+ if (conn.get() == except) {
+ continue;
+ }
+ if (only && conn.get() != only) {
+ continue;
+ }
auto state = conn->state();
if (state != NetworkConnection::kSynchronized &&
- state != NetworkConnection::kActive)
+ state != NetworkConnection::kActive) {
continue;
+ }
conn->QueueOutgoing(msg);
}
}
@@ -391,17 +425,17 @@
m_networkMode = NT_NET_MODE_NONE;
return;
}
- DEBUG0("server: client connection from " << stream->getPeerIP() << " port "
- << stream->getPeerPort());
+ DEBUG0("server: client connection from {} port {}", stream->getPeerIP(),
+ stream->getPeerPort());
// add to connections list
using namespace std::placeholders;
auto conn = std::make_shared<NetworkConnection>(
++m_connections_uid, std::move(stream), m_notifier, m_logger,
- std::bind(&Dispatcher::ServerHandshake, this, _1, _2, _3),
- std::bind(&IStorage::GetMessageEntryType, &m_storage, _1));
+ std::bind(&Dispatcher::ServerHandshake, this, _1, _2, _3), // NOLINT
+ std::bind(&IStorage::GetMessageEntryType, &m_storage, _1)); // NOLINT
conn->set_process_incoming(
- std::bind(&IStorage::ProcessIncoming, &m_storage, _1, _2,
+ std::bind(&IStorage::ProcessIncoming, &m_storage, _1, _2, // NOLINT
std::weak_ptr<NetworkConnection>(conn)));
{
std::scoped_lock lock(m_user_mutex);
@@ -414,7 +448,9 @@
break;
}
}
- if (!placed) m_connections.emplace_back(conn);
+ if (!placed) {
+ m_connections.emplace_back(conn);
+ }
conn->Start();
}
}
@@ -442,23 +478,23 @@
}
// try to connect (with timeout)
- DEBUG0("client trying to connect");
+ DEBUG0("{}", "client trying to connect");
auto stream = connect();
if (!stream) {
m_networkMode = NT_NET_MODE_CLIENT | NT_NET_MODE_FAILURE;
continue; // keep retrying
}
- DEBUG0("client connected");
+ DEBUG0("{}", "client connected");
m_networkMode = NT_NET_MODE_CLIENT;
std::unique_lock lock(m_user_mutex);
using namespace std::placeholders;
auto conn = std::make_shared<NetworkConnection>(
++m_connections_uid, std::move(stream), m_notifier, m_logger,
- std::bind(&Dispatcher::ClientHandshake, this, _1, _2, _3),
- std::bind(&IStorage::GetMessageEntryType, &m_storage, _1));
+ std::bind(&Dispatcher::ClientHandshake, this, _1, _2, _3), // NOLINT
+ std::bind(&IStorage::GetMessageEntryType, &m_storage, _1)); // NOLINT
conn->set_process_incoming(
- std::bind(&IStorage::ProcessIncoming, &m_storage, _1, _2,
+ std::bind(&IStorage::ProcessIncoming, &m_storage, _1, _2, // NOLINT
std::weak_ptr<NetworkConnection>(conn)));
m_connections.resize(0); // disconnect any current
m_connections.emplace_back(conn);
@@ -477,7 +513,7 @@
bool DispatcherBase::ClientHandshake(
NetworkConnection& conn, std::function<std::shared_ptr<Message>()> get_msg,
- std::function<void(wpi::ArrayRef<std::shared_ptr<Message>>)> send_msgs) {
+ std::function<void(wpi::span<std::shared_ptr<Message>>)> send_msgs) {
// get identity
std::string self_id;
{
@@ -486,28 +522,35 @@
}
// send client hello
- DEBUG0("client: sending hello");
- send_msgs(Message::ClientHello(self_id));
+ DEBUG0("{}", "client: sending hello");
+ auto msg = Message::ClientHello(self_id);
+ send_msgs(wpi::span(&msg, 1));
// wait for response
- auto msg = get_msg();
+ msg = get_msg();
if (!msg) {
// disconnected, retry
- DEBUG0("client: server disconnected before first response");
+ DEBUG0("{}", "client: server disconnected before first response");
return false;
}
if (msg->Is(Message::kProtoUnsup)) {
- if (msg->id() == 0x0200) ClientReconnect(0x0200);
+ if (msg->id() == 0x0200) {
+ ClientReconnect(0x0200);
+ }
return false;
}
bool new_server = true;
if (conn.proto_rev() >= 0x0300) {
// should be server hello; if not, disconnect.
- if (!msg->Is(Message::kServerHello)) return false;
+ if (!msg->Is(Message::kServerHello)) {
+ return false;
+ }
conn.set_remote_id(msg->str());
- if ((msg->flags() & 1) != 0) new_server = false;
+ if ((msg->flags() & 1) != 0) {
+ new_server = false;
+ }
// get the next message
msg = get_msg();
}
@@ -517,12 +560,14 @@
for (;;) {
if (!msg) {
// disconnected, retry
- DEBUG0("client: server disconnected during initial entries");
+ DEBUG0("{}", "client: server disconnected during initial entries");
return false;
}
- DEBUG4("received init str=" << msg->str() << " id=" << msg->id()
- << " seq_num=" << msg->seq_num_uid());
- if (msg->Is(Message::kServerHelloDone)) break;
+ DEBUG4("received init str={} id={} seq_num={}", msg->str(), msg->id(),
+ msg->seq_num_uid());
+ if (msg->Is(Message::kServerHelloDone)) {
+ break;
+ }
// shouldn't receive a keep alive, but handle gracefully
if (msg->Is(Message::kKeepAlive)) {
msg = get_msg();
@@ -530,9 +575,10 @@
}
if (!msg->Is(Message::kEntryAssign)) {
// unexpected message
- DEBUG0("client: received message ("
- << msg->type()
- << ") other than entry assignment during initial handshake");
+ DEBUG0(
+ "client: received message ({}) other than entry assignment during "
+ "initial handshake",
+ msg->type());
return false;
}
incoming.emplace_back(std::move(msg));
@@ -545,42 +591,48 @@
m_storage.ApplyInitialAssignments(conn, incoming, new_server, &outgoing);
- if (conn.proto_rev() >= 0x0300)
+ if (conn.proto_rev() >= 0x0300) {
outgoing.emplace_back(Message::ClientHelloDone());
+ }
- if (!outgoing.empty()) send_msgs(outgoing);
+ if (!outgoing.empty()) {
+ send_msgs(outgoing);
+ }
- INFO("client: CONNECTED to server " << conn.stream().getPeerIP() << " port "
- << conn.stream().getPeerPort());
+ INFO("client: CONNECTED to server {} port {}", conn.stream().getPeerIP(),
+ conn.stream().getPeerPort());
return true;
}
bool DispatcherBase::ServerHandshake(
NetworkConnection& conn, std::function<std::shared_ptr<Message>()> get_msg,
- std::function<void(wpi::ArrayRef<std::shared_ptr<Message>>)> send_msgs) {
+ std::function<void(wpi::span<std::shared_ptr<Message>>)> send_msgs) {
// Wait for the client to send us a hello.
auto msg = get_msg();
if (!msg) {
- DEBUG0("server: client disconnected before sending hello");
+ DEBUG0("{}", "server: client disconnected before sending hello");
return false;
}
if (!msg->Is(Message::kClientHello)) {
- DEBUG0("server: client initial message was not client hello");
+ DEBUG0("{}", "server: client initial message was not client hello");
return false;
}
// Check that the client requested version is not too high.
unsigned int proto_rev = msg->id();
if (proto_rev > 0x0300) {
- DEBUG0("server: client requested proto > 0x0300");
- send_msgs(Message::ProtoUnsup());
+ DEBUG0("{}", "server: client requested proto > 0x0300");
+ auto toSend = Message::ProtoUnsup();
+ send_msgs(wpi::span(&toSend, 1));
return false;
}
- if (proto_rev >= 0x0300) conn.set_remote_id(msg->str());
+ if (proto_rev >= 0x0300) {
+ conn.set_remote_id(msg->str());
+ }
// Set the proto version to the client requested version
- DEBUG0("server: client protocol " << proto_rev);
+ DEBUG0("server: client protocol {}", proto_rev);
conn.set_proto_rev(proto_rev);
// Send initial set of assignments
@@ -599,7 +651,7 @@
outgoing.emplace_back(Message::ServerHelloDone());
// Batch transmit
- DEBUG0("server: sending initial assignments");
+ DEBUG0("{}", "server: sending initial assignments");
send_msgs(outgoing);
// In proto rev 3.0 and later, the handshake concludes with a client hello
@@ -613,10 +665,12 @@
for (;;) {
if (!msg) {
// disconnected, retry
- DEBUG0("server: disconnected waiting for initial entries");
+ DEBUG0("{}", "server: disconnected waiting for initial entries");
return false;
}
- if (msg->Is(Message::kClientHelloDone)) break;
+ if (msg->Is(Message::kClientHelloDone)) {
+ break;
+ }
// shouldn't receive a keep alive, but handle gracefully
if (msg->Is(Message::kKeepAlive)) {
msg = get_msg();
@@ -624,26 +678,30 @@
}
if (!msg->Is(Message::kEntryAssign)) {
// unexpected message
- DEBUG0("server: received message ("
- << msg->type()
- << ") other than entry assignment during initial handshake");
+ DEBUG0(
+ "server: received message ({}) other than entry assignment during "
+ "initial handshake",
+ msg->type());
return false;
}
incoming.push_back(msg);
// get the next message (blocks)
msg = get_msg();
}
- for (auto& msg : incoming)
+ for (auto& msg : incoming) {
m_storage.ProcessIncoming(msg, &conn, std::weak_ptr<NetworkConnection>());
+ }
}
- INFO("server: client CONNECTED: " << conn.stream().getPeerIP() << " port "
- << conn.stream().getPeerPort());
+ INFO("server: client CONNECTED: {} port {}", conn.stream().getPeerIP(),
+ conn.stream().getPeerPort());
return true;
}
void DispatcherBase::ClientReconnect(unsigned int proto_rev) {
- if ((m_networkMode & NT_NET_MODE_SERVER) != 0) return;
+ if ((m_networkMode & NT_NET_MODE_SERVER) != 0) {
+ return;
+ }
{
std::scoped_lock lock(m_user_mutex);
m_reconnect_proto_rev = proto_rev;
diff --git a/ntcore/src/main/native/cpp/Dispatcher.h b/ntcore/src/main/native/cpp/Dispatcher.h
index bfddae7..51b8ec7 100644
--- a/ntcore/src/main/native/cpp/Dispatcher.h
+++ b/ntcore/src/main/native/cpp/Dispatcher.h
@@ -1,26 +1,22 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_DISPATCHER_H_
#define NTCORE_DISPATCHER_H_
#include <atomic>
-#include <chrono>
#include <functional>
#include <memory>
#include <string>
+#include <string_view>
#include <thread>
#include <utility>
#include <vector>
-#include <wpi/StringRef.h>
-#include <wpi/Twine.h>
#include <wpi/condition_variable.h>
#include <wpi/mutex.h>
+#include <wpi/span.h>
#include "IDispatcher.h"
#include "INetworkConnection.h"
@@ -41,20 +37,20 @@
friend class DispatcherTest;
public:
- typedef std::function<std::unique_ptr<wpi::NetworkStream>()> Connector;
+ using Connector = std::function<std::unique_ptr<wpi::NetworkStream>()>;
DispatcherBase(IStorage& storage, IConnectionNotifier& notifier,
wpi::Logger& logger);
- virtual ~DispatcherBase();
+ ~DispatcherBase() override;
unsigned int GetNetworkMode() const;
void StartLocal();
- void StartServer(const Twine& persist_filename,
+ void StartServer(std::string_view persist_filename,
std::unique_ptr<wpi::NetworkAcceptor> acceptor);
void StartClient();
void Stop();
void SetUpdateRate(double interval);
- void SetIdentity(const Twine& name);
+ void SetIdentity(std::string_view name);
void Flush();
std::vector<ConnectionInfo> GetConnections() const;
bool IsConnected() const;
@@ -82,11 +78,11 @@
bool ClientHandshake(
NetworkConnection& conn,
std::function<std::shared_ptr<Message>()> get_msg,
- std::function<void(wpi::ArrayRef<std::shared_ptr<Message>>)> send_msgs);
+ std::function<void(wpi::span<std::shared_ptr<Message>>)> send_msgs);
bool ServerHandshake(
NetworkConnection& conn,
std::function<std::shared_ptr<Message>()> get_msg,
- std::function<void(wpi::ArrayRef<std::shared_ptr<Message>>)> send_msgs);
+ std::function<void(wpi::span<std::shared_ptr<Message>>)> send_msgs);
void ClientReconnect(unsigned int proto_rev = 0x0300);
@@ -116,7 +112,7 @@
// Condition variable for forced dispatch wakeup (flush)
wpi::mutex m_flush_mutex;
wpi::condition_variable m_flush_cv;
- std::chrono::steady_clock::time_point m_last_flush;
+ uint64_t m_last_flush = 0;
bool m_do_flush = false;
// Condition variable for client reconnect (uses user mutex)
@@ -136,11 +132,12 @@
wpi::Logger& logger)
: DispatcherBase(storage, notifier, logger) {}
- void StartServer(const Twine& persist_filename, const char* listen_address,
- unsigned int port);
+ void StartServer(std::string_view persist_filename,
+ const char* listen_address, unsigned int port);
void SetServer(const char* server_name, unsigned int port);
- void SetServer(ArrayRef<std::pair<StringRef, unsigned int>> servers);
+ void SetServer(
+ wpi::span<const std::pair<std::string_view, unsigned int>> servers);
void SetServerTeam(unsigned int team, unsigned int port);
void SetServerOverride(const char* server_name, unsigned int port);
diff --git a/ntcore/src/main/native/cpp/DsClient.cpp b/ntcore/src/main/native/cpp/DsClient.cpp
index 32a756f..9ed1850 100644
--- a/ntcore/src/main/native/cpp/DsClient.cpp
+++ b/ntcore/src/main/native/cpp/DsClient.cpp
@@ -1,13 +1,11 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "DsClient.h"
#include <wpi/SmallString.h>
+#include <wpi/StringExtras.h>
#include <wpi/TCPConnector.h>
#include <wpi/raw_ostream.h>
#include <wpi/raw_socket_istream.h>
@@ -22,7 +20,7 @@
Thread(Dispatcher& dispatcher, wpi::Logger& logger, unsigned int port)
: m_dispatcher(dispatcher), m_logger(logger), m_port(port) {}
- void Main();
+ void Main() override;
Dispatcher& m_dispatcher;
wpi::Logger& m_logger;
@@ -35,10 +33,11 @@
void DsClient::Start(unsigned int port) {
auto thr = m_owner.GetThread();
- if (!thr)
+ if (!thr) {
m_owner.Start(m_dispatcher, m_logger, port);
- else
+ } else {
thr->m_port = port;
+ }
}
void DsClient::Stop() {
@@ -47,7 +46,9 @@
auto thr = m_owner.GetThread();
if (thr) {
thr->m_active = false;
- if (thr->m_stream) thr->m_stream->close();
+ if (thr->m_stream) {
+ thr->m_stream->close();
+ }
}
}
m_owner.Stop();
@@ -67,14 +68,20 @@
m_cond.wait_until(lock, timeout_time, [&] { return !m_active; });
port = m_port;
}
- if (!m_active) goto done;
+ if (!m_active) {
+ goto done;
+ }
// Try to connect to DS on the local machine
m_stream = wpi::TCPConnector::connect("127.0.0.1", 1742, nolog, 1);
- if (!m_active) goto done;
- if (!m_stream) continue;
+ if (!m_active) {
+ goto done;
+ }
+ if (!m_stream) {
+ continue;
+ }
- DEBUG3("connected to DS");
+ DEBUG3("{}", "connected to DS");
wpi::raw_socket_istream is(*m_stream);
while (m_active && !is.has_error()) {
@@ -86,8 +93,12 @@
// Throw away characters until {
do {
is.read(ch);
- if (is.has_error()) break;
- if (!m_active) goto done;
+ if (is.has_error()) {
+ break;
+ }
+ if (!m_active) {
+ goto done;
+ }
} while (ch != '{');
json += '{';
@@ -99,8 +110,12 @@
// Read characters until }
do {
is.read(ch);
- if (is.has_error()) break;
- if (!m_active) goto done;
+ if (is.has_error()) {
+ break;
+ }
+ if (!m_active) {
+ goto done;
+ }
json += ch;
} while (ch != '}');
@@ -108,20 +123,29 @@
m_stream = nullptr;
break;
}
- DEBUG3("json=" << json);
+ DEBUG3("json={}", json);
// Look for "robotIP":12345, and get 12345 portion
size_t pos = json.find("\"robotIP\"");
- if (pos == wpi::StringRef::npos) continue; // could not find?
+ if (pos == std::string_view::npos) {
+ continue; // could not find?
+ }
pos += 9;
pos = json.find(':', pos);
- if (pos == wpi::StringRef::npos) continue; // could not find?
+ if (pos == std::string_view::npos) {
+ continue; // could not find?
+ }
size_t endpos = json.find_first_not_of("0123456789", pos + 1);
- DEBUG3("found robotIP=" << json.slice(pos + 1, endpos));
+ DEBUG3("found robotIP={}", wpi::slice(json, pos + 1, endpos));
// Parse into number
unsigned int ip = 0;
- if (json.slice(pos + 1, endpos).getAsInteger(10, ip)) continue; // error
+ if (auto v = wpi::parse_integer<unsigned int>(
+ wpi::slice(json, pos + 1, endpos), 10)) {
+ ip = v.value();
+ } else {
+ continue; // error
+ }
// If zero, clear the server override
if (ip == 0) {
@@ -131,16 +155,16 @@
}
// If unchanged, don't reconnect
- if (ip == oldip) continue;
+ if (ip == oldip) {
+ continue;
+ }
oldip = ip;
// Convert number into dotted quad
- json.clear();
- wpi::raw_svector_ostream os{json};
- os << ((ip >> 24) & 0xff) << "." << ((ip >> 16) & 0xff) << "."
- << ((ip >> 8) & 0xff) << "." << (ip & 0xff);
- INFO("client: DS overriding server IP to " << os.str());
- m_dispatcher.SetServerOverride(json.c_str(), port);
+ auto newip = fmt::format("{}.{}.{}.{}", (ip >> 24) & 0xff,
+ (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
+ INFO("client: DS overriding server IP to {}", newip);
+ m_dispatcher.SetServerOverride(newip.c_str(), port);
}
// We disconnected from the DS, clear the server override
diff --git a/ntcore/src/main/native/cpp/DsClient.h b/ntcore/src/main/native/cpp/DsClient.h
index ad13d25..73fc3d3 100644
--- a/ntcore/src/main/native/cpp/DsClient.h
+++ b/ntcore/src/main/native/cpp/DsClient.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_DSCLIENT_H_
#define NTCORE_DSCLIENT_H_
diff --git a/ntcore/src/main/native/cpp/EntryNotifier.cpp b/ntcore/src/main/native/cpp/EntryNotifier.cpp
index c8f8d76..6f251a3 100644
--- a/ntcore/src/main/native/cpp/EntryNotifier.cpp
+++ b/ntcore/src/main/native/cpp/EntryNotifier.cpp
@@ -1,12 +1,11 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "EntryNotifier.h"
+#include <wpi/StringExtras.h>
+
#include "Log.h"
using namespace nt;
@@ -16,13 +15,19 @@
m_local_notifiers = false;
}
-void EntryNotifier::Start() { DoStart(m_inst); }
+void EntryNotifier::Start() {
+ DoStart(m_inst);
+}
-bool EntryNotifier::local_notifiers() const { return m_local_notifiers; }
+bool EntryNotifier::local_notifiers() const {
+ return m_local_notifiers;
+}
bool impl::EntryNotifierThread::Matches(const EntryListenerData& listener,
const EntryNotification& data) {
- if (!data.value) return false;
+ if (!data.value) {
+ return false;
+ }
// Flags must be within requested flag set for this listener.
// Because assign messages can result in both a value and flags update,
@@ -32,58 +37,73 @@
unsigned int flags = data.flags & ~(NT_NOTIFY_IMMEDIATE | NT_NOTIFY_LOCAL);
unsigned int assign_both = NT_NOTIFY_UPDATE | NT_NOTIFY_FLAGS;
if ((flags & assign_both) == assign_both) {
- if ((listen_flags & assign_both) == 0) return false;
+ if ((listen_flags & assign_both) == 0) {
+ return false;
+ }
listen_flags &= ~assign_both;
flags &= ~assign_both;
}
- if ((flags & ~listen_flags) != 0) return false;
+ if ((flags & ~listen_flags) != 0) {
+ return false;
+ }
// must match local id or prefix
- if (listener.entry != 0 && data.entry != listener.entry) return false;
- if (listener.entry == 0 &&
- !wpi::StringRef(data.name).startswith(listener.prefix))
+ if (listener.entry != 0 && data.entry != listener.entry) {
return false;
+ }
+ if (listener.entry == 0 && !wpi::starts_with(data.name, listener.prefix)) {
+ return false;
+ }
return true;
}
unsigned int EntryNotifier::Add(
std::function<void(const EntryNotification& event)> callback,
- StringRef prefix, unsigned int flags) {
- if ((flags & NT_NOTIFY_LOCAL) != 0) m_local_notifiers = true;
+ std::string_view prefix, unsigned int flags) {
+ if ((flags & NT_NOTIFY_LOCAL) != 0) {
+ m_local_notifiers = true;
+ }
return DoAdd(callback, prefix, flags);
}
unsigned int EntryNotifier::Add(
std::function<void(const EntryNotification& event)> callback,
unsigned int local_id, unsigned int flags) {
- if ((flags & NT_NOTIFY_LOCAL) != 0) m_local_notifiers = true;
+ if ((flags & NT_NOTIFY_LOCAL) != 0) {
+ m_local_notifiers = true;
+ }
return DoAdd(callback, Handle(m_inst, local_id, Handle::kEntry), flags);
}
unsigned int EntryNotifier::AddPolled(unsigned int poller_uid,
- wpi::StringRef prefix,
+ std::string_view prefix,
unsigned int flags) {
- if ((flags & NT_NOTIFY_LOCAL) != 0) m_local_notifiers = true;
+ if ((flags & NT_NOTIFY_LOCAL) != 0) {
+ m_local_notifiers = true;
+ }
return DoAdd(poller_uid, prefix, flags);
}
unsigned int EntryNotifier::AddPolled(unsigned int poller_uid,
unsigned int local_id,
unsigned int flags) {
- if ((flags & NT_NOTIFY_LOCAL) != 0) m_local_notifiers = true;
+ if ((flags & NT_NOTIFY_LOCAL) != 0) {
+ m_local_notifiers = true;
+ }
return DoAdd(poller_uid, Handle(m_inst, local_id, Handle::kEntry), flags);
}
-void EntryNotifier::NotifyEntry(unsigned int local_id, StringRef name,
+void EntryNotifier::NotifyEntry(unsigned int local_id, std::string_view name,
std::shared_ptr<Value> value,
unsigned int flags,
unsigned int only_listener) {
// optimization: don't generate needless local queue entries if we have
// no local listeners (as this is a common case on the server side)
- if ((flags & NT_NOTIFY_LOCAL) != 0 && !m_local_notifiers) return;
- DEBUG0("notifying '" << name << "' (local=" << local_id
- << "), flags=" << flags);
+ if ((flags & NT_NOTIFY_LOCAL) != 0 && !m_local_notifiers) {
+ return;
+ }
+ DEBUG0("notifying '{}' (local={}), flags={}", name, local_id, flags);
Send(only_listener, 0, Handle(m_inst, local_id, Handle::kEntry).handle(),
name, value, flags);
}
diff --git a/ntcore/src/main/native/cpp/EntryNotifier.h b/ntcore/src/main/native/cpp/EntryNotifier.h
index 3ccf9ff..bbe2172 100644
--- a/ntcore/src/main/native/cpp/EntryNotifier.h
+++ b/ntcore/src/main/native/cpp/EntryNotifier.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_ENTRYNOTIFIER_H_
#define NTCORE_ENTRYNOTIFIER_H_
@@ -11,8 +8,11 @@
#include <atomic>
#include <memory>
#include <string>
+#include <string_view>
+#include <utility>
-#include "CallbackManager.h"
+#include <wpi/CallbackManager.h>
+
#include "Handle.h"
#include "IEntryNotifier.h"
#include "ntcore_cpp.h"
@@ -26,22 +26,23 @@
namespace impl {
struct EntryListenerData
- : public ListenerData<std::function<void(const EntryNotification& event)>> {
+ : public wpi::CallbackListenerData<
+ std::function<void(const EntryNotification& event)>> {
EntryListenerData() = default;
EntryListenerData(
std::function<void(const EntryNotification& event)> callback_,
- StringRef prefix_, unsigned int flags_)
- : ListenerData(callback_), prefix(prefix_), flags(flags_) {}
+ std::string_view prefix_, unsigned int flags_)
+ : CallbackListenerData(callback_), prefix(prefix_), flags(flags_) {}
EntryListenerData(
std::function<void(const EntryNotification& event)> callback_,
NT_Entry entry_, unsigned int flags_)
- : ListenerData(callback_), entry(entry_), flags(flags_) {}
- EntryListenerData(unsigned int poller_uid_, StringRef prefix_,
+ : CallbackListenerData(callback_), entry(entry_), flags(flags_) {}
+ EntryListenerData(unsigned int poller_uid_, std::string_view prefix_,
unsigned int flags_)
- : ListenerData(poller_uid_), prefix(prefix_), flags(flags_) {}
+ : CallbackListenerData(poller_uid_), prefix(prefix_), flags(flags_) {}
EntryListenerData(unsigned int poller_uid_, NT_Entry entry_,
unsigned int flags_)
- : ListenerData(poller_uid_), entry(entry_), flags(flags_) {}
+ : CallbackListenerData(poller_uid_), entry(entry_), flags(flags_) {}
std::string prefix;
NT_Entry entry = 0;
@@ -49,10 +50,12 @@
};
class EntryNotifierThread
- : public CallbackThread<EntryNotifierThread, EntryNotification,
- EntryListenerData> {
+ : public wpi::CallbackThread<EntryNotifierThread, EntryNotification,
+ EntryListenerData> {
public:
- explicit EntryNotifierThread(int inst) : m_inst(inst) {}
+ EntryNotifierThread(std::function<void()> on_start,
+ std::function<void()> on_exit, int inst)
+ : CallbackThread(std::move(on_start), std::move(on_exit)), m_inst(inst) {}
bool Matches(const EntryListenerData& listener,
const EntryNotification& data);
@@ -74,9 +77,9 @@
class EntryNotifier
: public IEntryNotifier,
- public CallbackManager<EntryNotifier, impl::EntryNotifierThread> {
+ public wpi::CallbackManager<EntryNotifier, impl::EntryNotifierThread> {
friend class EntryNotifierTest;
- friend class CallbackManager<EntryNotifier, impl::EntryNotifierThread>;
+ friend class wpi::CallbackManager<EntryNotifier, impl::EntryNotifierThread>;
public:
explicit EntryNotifier(int inst, wpi::Logger& logger);
@@ -86,15 +89,15 @@
bool local_notifiers() const override;
unsigned int Add(std::function<void(const EntryNotification& event)> callback,
- wpi::StringRef prefix, unsigned int flags) override;
+ std::string_view prefix, unsigned int flags) override;
unsigned int Add(std::function<void(const EntryNotification& event)> callback,
unsigned int local_id, unsigned int flags) override;
- unsigned int AddPolled(unsigned int poller_uid, wpi::StringRef prefix,
+ unsigned int AddPolled(unsigned int poller_uid, std::string_view prefix,
unsigned int flags) override;
unsigned int AddPolled(unsigned int poller_uid, unsigned int local_id,
unsigned int flags) override;
- void NotifyEntry(unsigned int local_id, StringRef name,
+ void NotifyEntry(unsigned int local_id, std::string_view name,
std::shared_ptr<Value> value, unsigned int flags,
unsigned int only_listener = UINT_MAX) override;
diff --git a/ntcore/src/main/native/cpp/Handle.h b/ntcore/src/main/native/cpp/Handle.h
index 47b5edf..df12381 100644
--- a/ntcore/src/main/native/cpp/Handle.h
+++ b/ntcore/src/main/native/cpp/Handle.h
@@ -1,26 +1,25 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_HANDLE_H_
#define NTCORE_HANDLE_H_
+#include <wpi/Synchronization.h>
+
#include "ntcore_c.h"
namespace nt {
// Handle data layout:
-// Bits 30-28: Type
-// Bits 27-20: Instance index
+// Bits 30-24: Type
+// Bits 23-20: Instance index
// Bits 19-0: Handle index (0/unused for instance handles)
class Handle {
public:
enum Type {
- kConnectionListener = 1,
+ kConnectionListener = wpi::kHandleTypeNTBase,
kConnectionListenerPoller,
kEntry,
kEntryListener,
@@ -43,15 +42,15 @@
m_handle = 0;
return;
}
- m_handle = ((static_cast<int>(type) & 0xf) << 27) | ((inst & 0x7f) << 20) |
+ m_handle = ((static_cast<int>(type) & 0x7f) << 24) | ((inst & 0xf) << 20) |
(index & 0xfffff);
}
int GetIndex() const { return static_cast<int>(m_handle) & 0xfffff; }
Type GetType() const {
- return static_cast<Type>((static_cast<int>(m_handle) >> 27) & 0xf);
+ return static_cast<Type>((static_cast<int>(m_handle) >> 24) & 0x7f);
}
- int GetInst() const { return (static_cast<int>(m_handle) >> 20) & 0x7f; }
+ int GetInst() const { return (static_cast<int>(m_handle) >> 20) & 0xf; }
bool IsType(Type type) const { return type == GetType(); }
int GetTypedIndex(Type type) const { return IsType(type) ? GetIndex() : -1; }
int GetTypedInst(Type type) const { return IsType(type) ? GetInst() : -1; }
diff --git a/ntcore/src/main/native/cpp/IConnectionNotifier.h b/ntcore/src/main/native/cpp/IConnectionNotifier.h
index 7a165f2..5983866 100644
--- a/ntcore/src/main/native/cpp/IConnectionNotifier.h
+++ b/ntcore/src/main/native/cpp/IConnectionNotifier.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_ICONNECTIONNOTIFIER_H_
#define NTCORE_ICONNECTIONNOTIFIER_H_
diff --git a/ntcore/src/main/native/cpp/IDispatcher.h b/ntcore/src/main/native/cpp/IDispatcher.h
index aace766..4cf8a5f 100644
--- a/ntcore/src/main/native/cpp/IDispatcher.h
+++ b/ntcore/src/main/native/cpp/IDispatcher.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_IDISPATCHER_H_
#define NTCORE_IDISPATCHER_H_
diff --git a/ntcore/src/main/native/cpp/IEntryNotifier.h b/ntcore/src/main/native/cpp/IEntryNotifier.h
index 80ed97e..b3f91b2 100644
--- a/ntcore/src/main/native/cpp/IEntryNotifier.h
+++ b/ntcore/src/main/native/cpp/IEntryNotifier.h
@@ -1,15 +1,13 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_IENTRYNOTIFIER_H_
#define NTCORE_IENTRYNOTIFIER_H_
#include <climits>
#include <memory>
+#include <string_view>
#include "ntcore_cpp.h"
@@ -25,16 +23,17 @@
virtual unsigned int Add(
std::function<void(const EntryNotification& event)> callback,
- wpi::StringRef prefix, unsigned int flags) = 0;
+ std::string_view prefix, unsigned int flags) = 0;
virtual unsigned int Add(
std::function<void(const EntryNotification& event)> callback,
unsigned int local_id, unsigned int flags) = 0;
- virtual unsigned int AddPolled(unsigned int poller_uid, wpi::StringRef prefix,
+ virtual unsigned int AddPolled(unsigned int poller_uid,
+ std::string_view prefix,
unsigned int flags) = 0;
virtual unsigned int AddPolled(unsigned int poller_uid, unsigned int local_id,
unsigned int flags) = 0;
- virtual void NotifyEntry(unsigned int local_id, StringRef name,
+ virtual void NotifyEntry(unsigned int local_id, std::string_view name,
std::shared_ptr<Value> value, unsigned int flags,
unsigned int only_listener = UINT_MAX) = 0;
};
diff --git a/ntcore/src/main/native/cpp/INetworkConnection.h b/ntcore/src/main/native/cpp/INetworkConnection.h
index 0387cc9..94e9bb1 100644
--- a/ntcore/src/main/native/cpp/INetworkConnection.h
+++ b/ntcore/src/main/native/cpp/INetworkConnection.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_INETWORKCONNECTION_H_
#define NTCORE_INETWORKCONNECTION_H_
diff --git a/ntcore/src/main/native/cpp/IRpcServer.h b/ntcore/src/main/native/cpp/IRpcServer.h
index dc8b0a6..aa16084 100644
--- a/ntcore/src/main/native/cpp/IRpcServer.h
+++ b/ntcore/src/main/native/cpp/IRpcServer.h
@@ -1,14 +1,12 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_IRPCSERVER_H_
#define NTCORE_IRPCSERVER_H_
#include <memory>
+#include <string_view>
#include "Message.h"
#include "ntcore_cpp.h"
@@ -17,7 +15,7 @@
class IRpcServer {
public:
- typedef std::function<void(StringRef result)> SendResponseFunc;
+ using SendResponseFunc = std::function<void(std::string_view result)>;
IRpcServer() = default;
IRpcServer(const IRpcServer&) = delete;
@@ -27,7 +25,7 @@
virtual void RemoveRpc(unsigned int rpc_uid) = 0;
virtual void ProcessRpc(unsigned int local_id, unsigned int call_uid,
- StringRef name, StringRef params,
+ std::string_view name, std::string_view params,
const ConnectionInfo& conn,
SendResponseFunc send_response,
unsigned int rpc_uid) = 0;
diff --git a/ntcore/src/main/native/cpp/IStorage.h b/ntcore/src/main/native/cpp/IStorage.h
index 0fb3a0b..795d032 100644
--- a/ntcore/src/main/native/cpp/IStorage.h
+++ b/ntcore/src/main/native/cpp/IStorage.h
@@ -1,19 +1,16 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_ISTORAGE_H_
#define NTCORE_ISTORAGE_H_
#include <functional>
#include <memory>
+#include <string_view>
#include <vector>
-#include <wpi/ArrayRef.h>
-#include <wpi/Twine.h>
+#include <wpi/span.h>
#include "Message.h"
#include "ntcore_cpp.h"
@@ -48,15 +45,15 @@
INetworkConnection& conn,
std::vector<std::shared_ptr<Message>>* msgs) = 0;
virtual void ApplyInitialAssignments(
- INetworkConnection& conn, wpi::ArrayRef<std::shared_ptr<Message>> msgs,
+ INetworkConnection& conn, wpi::span<std::shared_ptr<Message>> msgs,
bool new_server, std::vector<std::shared_ptr<Message>>* out_msgs) = 0;
// Filename-based save/load functions. Used both by periodic saves and
// accessible directly via the user API.
- virtual const char* SavePersistent(const Twine& filename,
+ virtual const char* SavePersistent(std::string_view filename,
bool periodic) const = 0;
virtual const char* LoadPersistent(
- const Twine& filename,
+ std::string_view filename,
std::function<void(size_t line, const char* msg)> warn) = 0;
};
diff --git a/ntcore/src/main/native/cpp/InstanceImpl.cpp b/ntcore/src/main/native/cpp/InstanceImpl.cpp
index cd35fb0..8d59a0b 100644
--- a/ntcore/src/main/native/cpp/InstanceImpl.cpp
+++ b/ntcore/src/main/native/cpp/InstanceImpl.cpp
@@ -1,24 +1,21 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "InstanceImpl.h"
using namespace nt;
std::atomic<int> InstanceImpl::s_default{-1};
-std::atomic<InstanceImpl*> InstanceImpl::s_fast_instances[10];
-wpi::UidVector<InstanceImpl*, 10> InstanceImpl::s_instances;
+std::atomic<InstanceImpl*> InstanceImpl::s_instances[kNumInstances];
wpi::mutex InstanceImpl::s_mutex;
using namespace std::placeholders;
InstanceImpl::InstanceImpl(int inst)
: logger_impl(inst),
- logger(std::bind(&LoggerImpl::Log, &logger_impl, _1, _2, _3, _4)),
+ logger(
+ std::bind(&LoggerImpl::Log, &logger_impl, _1, _2, _3, _4)), // NOLINT
connection_notifier(inst),
entry_notifier(inst, logger),
rpc_server(inst, logger),
@@ -28,49 +25,35 @@
logger.set_min_level(logger_impl.GetMinLevel());
}
-InstanceImpl::~InstanceImpl() { logger.SetLogger(nullptr); }
+InstanceImpl::~InstanceImpl() {
+ logger.SetLogger(nullptr);
+}
-InstanceImpl* InstanceImpl::GetDefault() { return Get(GetDefaultIndex()); }
+InstanceImpl* InstanceImpl::GetDefault() {
+ return Get(GetDefaultIndex());
+}
InstanceImpl* InstanceImpl::Get(int inst) {
- if (inst < 0) return nullptr;
-
- // fast path, just an atomic read
- if (static_cast<unsigned int>(inst) <
- (sizeof(s_fast_instances) / sizeof(s_fast_instances[0]))) {
- InstanceImpl* ptr = s_fast_instances[inst];
- if (ptr) return ptr;
+ if (inst < 0 || inst >= kNumInstances) {
+ return nullptr;
}
-
- // slow path
- std::scoped_lock lock(s_mutex);
-
- // static fast-path block
- if (static_cast<unsigned int>(inst) <
- (sizeof(s_fast_instances) / sizeof(s_fast_instances[0]))) {
- // double-check
- return s_fast_instances[inst];
- }
-
- // vector
- if (static_cast<unsigned int>(inst) < s_instances.size()) {
- return s_instances[inst];
- }
-
- // doesn't exist
- return nullptr;
+ return s_instances[inst];
}
int InstanceImpl::GetDefaultIndex() {
int inst = s_default;
- if (inst >= 0) return inst;
+ if (inst >= 0) {
+ return inst;
+ }
// slow path
std::scoped_lock lock(s_mutex);
// double-check
inst = s_default;
- if (inst >= 0) return inst;
+ if (inst >= 0) {
+ return inst;
+ }
// alloc and save
inst = AllocImpl();
@@ -84,26 +67,23 @@
}
int InstanceImpl::AllocImpl() {
- unsigned int inst = s_instances.emplace_back(nullptr);
- InstanceImpl* ptr = new InstanceImpl(inst);
- s_instances[inst] = ptr;
-
- if (inst < (sizeof(s_fast_instances) / sizeof(s_fast_instances[0]))) {
- s_fast_instances[inst] = ptr;
+ int inst = 0;
+ for (; inst < kNumInstances; ++inst) {
+ if (!s_instances[inst]) {
+ s_instances[inst] = new InstanceImpl(inst);
+ return inst;
+ }
}
-
- return static_cast<int>(inst);
+ return -1;
}
void InstanceImpl::Destroy(int inst) {
std::scoped_lock lock(s_mutex);
- if (inst < 0 || static_cast<unsigned int>(inst) >= s_instances.size()) return;
-
- if (static_cast<unsigned int>(inst) <
- (sizeof(s_fast_instances) / sizeof(s_fast_instances[0]))) {
- s_fast_instances[inst] = nullptr;
+ if (inst < 0 || inst >= kNumInstances) {
+ return;
}
- delete s_instances[inst];
- s_instances.erase(inst);
+ InstanceImpl* ptr = nullptr;
+ s_instances[inst].exchange(ptr);
+ delete ptr;
}
diff --git a/ntcore/src/main/native/cpp/InstanceImpl.h b/ntcore/src/main/native/cpp/InstanceImpl.h
index 6e732d8..22d94f5 100644
--- a/ntcore/src/main/native/cpp/InstanceImpl.h
+++ b/ntcore/src/main/native/cpp/InstanceImpl.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_INSTANCEIMPL_H_
#define NTCORE_INSTANCEIMPL_H_
@@ -11,7 +8,6 @@
#include <atomic>
#include <memory>
-#include <wpi/UidVector.h>
#include <wpi/mutex.h>
#include "ConnectionNotifier.h"
@@ -50,8 +46,8 @@
static int AllocImpl();
static std::atomic<int> s_default;
- static std::atomic<InstanceImpl*> s_fast_instances[10];
- static wpi::UidVector<InstanceImpl*, 10> s_instances;
+ static constexpr int kNumInstances = 16;
+ static std::atomic<InstanceImpl*> s_instances[kNumInstances];
static wpi::mutex s_mutex;
};
diff --git a/ntcore/src/main/native/cpp/Log.h b/ntcore/src/main/native/cpp/Log.h
index eba8f04..d3066d7 100644
--- a/ntcore/src/main/native/cpp/Log.h
+++ b/ntcore/src/main/native/cpp/Log.h
@@ -1,26 +1,23 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_LOG_H_
#define NTCORE_LOG_H_
#include <wpi/Logger.h>
-#define LOG(level, x) WPI_LOG(m_logger, level, x)
+#define LOG(level, format, ...) WPI_LOG(m_logger, level, format, __VA_ARGS__)
#undef ERROR
-#define ERROR(x) WPI_ERROR(m_logger, x)
-#define WARNING(x) WPI_WARNING(m_logger, x)
-#define INFO(x) WPI_INFO(m_logger, x)
+#define ERROR(format, ...) WPI_ERROR(m_logger, format, __VA_ARGS__)
+#define WARNING(format, ...) WPI_WARNING(m_logger, format, __VA_ARGS__)
+#define INFO(format, ...) WPI_INFO(m_logger, format, __VA_ARGS__)
-#define DEBUG0(x) WPI_DEBUG(m_logger, x)
-#define DEBUG1(x) WPI_DEBUG1(m_logger, x)
-#define DEBUG2(x) WPI_DEBUG2(m_logger, x)
-#define DEBUG3(x) WPI_DEBUG3(m_logger, x)
-#define DEBUG4(x) WPI_DEBUG4(m_logger, x)
+#define DEBUG0(format, ...) WPI_DEBUG(m_logger, format, __VA_ARGS__)
+#define DEBUG1(format, ...) WPI_DEBUG1(m_logger, format, __VA_ARGS__)
+#define DEBUG2(format, ...) WPI_DEBUG2(m_logger, format, __VA_ARGS__)
+#define DEBUG3(format, ...) WPI_DEBUG3(m_logger, format, __VA_ARGS__)
+#define DEBUG4(format, ...) WPI_DEBUG4(m_logger, format, __VA_ARGS__)
#endif // NTCORE_LOG_H_
diff --git a/ntcore/src/main/native/cpp/LoggerImpl.cpp b/ntcore/src/main/native/cpp/LoggerImpl.cpp
index b2c6786..fcb59d6 100644
--- a/ntcore/src/main/native/cpp/LoggerImpl.cpp
+++ b/ntcore/src/main/native/cpp/LoggerImpl.cpp
@@ -1,45 +1,39 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "LoggerImpl.h"
-#include <wpi/Path.h>
-#include <wpi/SmallString.h>
-#include <wpi/StringRef.h>
-#include <wpi/raw_ostream.h>
+#include <fmt/format.h>
+#include <wpi/fs.h>
using namespace nt;
static void DefaultLogger(unsigned int level, const char* file,
unsigned int line, const char* msg) {
- wpi::SmallString<128> buf;
- wpi::raw_svector_ostream oss(buf);
if (level == 20) {
- oss << "NT: " << msg << '\n';
- wpi::errs() << oss.str();
+ fmt::print(stderr, "NT: {}\n", msg);
return;
}
- wpi::StringRef levelmsg;
- if (level >= 50)
- levelmsg = "CRITICAL: ";
- else if (level >= 40)
- levelmsg = "ERROR: ";
- else if (level >= 30)
- levelmsg = "WARNING: ";
- else
+ std::string_view levelmsg;
+ if (level >= 50) {
+ levelmsg = "CRITICAL";
+ } else if (level >= 40) {
+ levelmsg = "ERROR";
+ } else if (level >= 30) {
+ levelmsg = "WARNING";
+ } else {
return;
- oss << "NT: " << levelmsg << msg << " (" << file << ':' << line << ")\n";
- wpi::errs() << oss.str();
+ }
+ fmt::print(stderr, "NT: {}: {} ({}:{})\n", levelmsg, msg, file, line);
}
LoggerImpl::LoggerImpl(int inst) : m_inst(inst) {}
-void LoggerImpl::Start() { DoStart(m_inst); }
+void LoggerImpl::Start() {
+ DoStart(m_inst);
+}
unsigned int LoggerImpl::Add(
std::function<void(const LogMessage& msg)> callback, unsigned int min_level,
@@ -55,23 +49,27 @@
unsigned int LoggerImpl::GetMinLevel() {
auto thr = GetThread();
- if (!thr) return NT_LOG_INFO;
+ if (!thr) {
+ return NT_LOG_INFO;
+ }
unsigned int level = NT_LOG_INFO;
for (size_t i = 0; i < thr->m_listeners.size(); ++i) {
const auto& listener = thr->m_listeners[i];
- if (listener && listener.min_level < level) level = listener.min_level;
+ if (listener && listener.min_level < level) {
+ level = listener.min_level;
+ }
}
return level;
}
void LoggerImpl::Log(unsigned int level, const char* file, unsigned int line,
const char* msg) {
- // this is safe because it's null terminated and always the end
- const char* filename = wpi::sys::path::filename(file).data();
+ auto filename = fs::path{file}.filename();
{
auto thr = GetThread();
- if (!thr || thr->m_listeners.empty())
- DefaultLogger(level, filename, line, msg);
+ if (!thr || thr->m_listeners.empty()) {
+ DefaultLogger(level, filename.string().c_str(), line, msg);
+ }
}
- Send(UINT_MAX, 0, level, filename, line, msg);
+ Send(UINT_MAX, 0, level, filename.string(), line, msg);
}
diff --git a/ntcore/src/main/native/cpp/LoggerImpl.h b/ntcore/src/main/native/cpp/LoggerImpl.h
index 3ac0295..2b577c1 100644
--- a/ntcore/src/main/native/cpp/LoggerImpl.h
+++ b/ntcore/src/main/native/cpp/LoggerImpl.h
@@ -1,14 +1,14 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_LOGGERIMPL_H_
#define NTCORE_LOGGERIMPL_H_
-#include "CallbackManager.h"
+#include <utility>
+
+#include <wpi/CallbackManager.h>
+
#include "Handle.h"
#include "ntcore_cpp.h"
@@ -16,15 +16,17 @@
namespace impl {
-struct LoggerListenerData
- : public ListenerData<std::function<void(const LogMessage& msg)>> {
+struct LoggerListenerData : public wpi::CallbackListenerData<
+ std::function<void(const LogMessage& msg)>> {
LoggerListenerData() = default;
LoggerListenerData(std::function<void(const LogMessage& msg)> callback_,
unsigned int min_level_, unsigned int max_level_)
- : ListenerData(callback_), min_level(min_level_), max_level(max_level_) {}
+ : CallbackListenerData(callback_),
+ min_level(min_level_),
+ max_level(max_level_) {}
LoggerListenerData(unsigned int poller_uid_, unsigned int min_level_,
unsigned int max_level_)
- : ListenerData(poller_uid_),
+ : CallbackListenerData(poller_uid_),
min_level(min_level_),
max_level(max_level_) {}
@@ -33,9 +35,11 @@
};
class LoggerThread
- : public CallbackThread<LoggerThread, LogMessage, LoggerListenerData> {
+ : public wpi::CallbackThread<LoggerThread, LogMessage, LoggerListenerData> {
public:
- explicit LoggerThread(int inst) : m_inst(inst) {}
+ LoggerThread(std::function<void()> on_start, std::function<void()> on_exit,
+ int inst)
+ : CallbackThread(std::move(on_start), std::move(on_exit)), m_inst(inst) {}
bool Matches(const LoggerListenerData& listener, const LogMessage& data) {
return data.level >= listener.min_level && data.level <= listener.max_level;
@@ -55,9 +59,9 @@
} // namespace impl
-class LoggerImpl : public CallbackManager<LoggerImpl, impl::LoggerThread> {
+class LoggerImpl : public wpi::CallbackManager<LoggerImpl, impl::LoggerThread> {
friend class LoggerTest;
- friend class CallbackManager<LoggerImpl, impl::LoggerThread>;
+ friend class wpi::CallbackManager<LoggerImpl, impl::LoggerThread>;
public:
explicit LoggerImpl(int inst);
diff --git a/ntcore/src/main/native/cpp/Message.cpp b/ntcore/src/main/native/cpp/Message.cpp
index 576e444..61efed1 100644
--- a/ntcore/src/main/native/cpp/Message.cpp
+++ b/ntcore/src/main/native/cpp/Message.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "Message.h"
@@ -20,7 +17,9 @@
std::shared_ptr<Message> Message::Read(WireDecoder& decoder,
GetEntryTypeFunc get_entry_type) {
unsigned int msg_type = 0;
- if (!decoder.Read8(&msg_type)) return nullptr;
+ if (!decoder.Read8(&msg_type)) {
+ return nullptr;
+ }
auto msg =
std::make_shared<Message>(static_cast<MsgType>(msg_type), private_init());
switch (msg_type) {
@@ -28,17 +27,23 @@
break;
case kClientHello: {
unsigned int proto_rev;
- if (!decoder.Read16(&proto_rev)) return nullptr;
+ if (!decoder.Read16(&proto_rev)) {
+ return nullptr;
+ }
msg->m_id = proto_rev;
// This intentionally uses the provided proto_rev instead of
// decoder.proto_rev().
if (proto_rev >= 0x0300u) {
- if (!decoder.ReadString(&msg->m_str)) return nullptr;
+ if (!decoder.ReadString(&msg->m_str)) {
+ return nullptr;
+ }
}
break;
}
case kProtoUnsup: {
- if (!decoder.Read16(&msg->m_id)) return nullptr; // proto rev
+ if (!decoder.Read16(&msg->m_id)) {
+ return nullptr; // proto rev
+ }
break;
}
case kServerHelloDone:
@@ -48,8 +53,12 @@
decoder.set_error("received SERVER_HELLO in protocol < 3.0");
return nullptr;
}
- if (!decoder.Read8(&msg->m_flags)) return nullptr;
- if (!decoder.ReadString(&msg->m_str)) return nullptr;
+ if (!decoder.Read8(&msg->m_flags)) {
+ return nullptr;
+ }
+ if (!decoder.ReadString(&msg->m_str)) {
+ return nullptr;
+ }
break;
case kClientHelloDone:
if (decoder.proto_rev() < 0x0300u) {
@@ -58,30 +67,50 @@
}
break;
case kEntryAssign: {
- if (!decoder.ReadString(&msg->m_str)) return nullptr; // name
+ if (!decoder.ReadString(&msg->m_str)) {
+ return nullptr; // name
+ }
NT_Type type;
- if (!decoder.ReadType(&type)) return nullptr; // entry type
- if (!decoder.Read16(&msg->m_id)) return nullptr; // id
- if (!decoder.Read16(&msg->m_seq_num_uid)) return nullptr; // seq num
+ if (!decoder.ReadType(&type)) {
+ return nullptr; // entry type
+ }
+ 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
+ if (!decoder.Read8(&msg->m_flags)) {
+ return nullptr; // flags
+ }
}
msg->m_value = decoder.ReadValue(type);
- if (!msg->m_value) return nullptr;
+ if (!msg->m_value) {
+ return nullptr;
+ }
break;
}
case kEntryUpdate: {
- if (!decoder.Read16(&msg->m_id)) return nullptr; // id
- if (!decoder.Read16(&msg->m_seq_num_uid)) return nullptr; // seq num
+ 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) {
- if (!decoder.ReadType(&type)) return nullptr;
+ if (!decoder.ReadType(&type)) {
+ return nullptr;
+ }
} else {
type = get_entry_type(msg->m_id);
}
- WPI_DEBUG4(decoder.logger(), "update message data type: " << type);
+ WPI_DEBUG4(decoder.logger(), "update message data type: {}", type);
msg->m_value = decoder.ReadValue(type);
- if (!msg->m_value) return nullptr;
+ if (!msg->m_value) {
+ return nullptr;
+ }
break;
}
case kFlagsUpdate: {
@@ -89,8 +118,12 @@
decoder.set_error("received FLAGS_UPDATE in protocol < 3.0");
return nullptr;
}
- if (!decoder.Read16(&msg->m_id)) return nullptr;
- if (!decoder.Read8(&msg->m_flags)) return nullptr;
+ if (!decoder.Read16(&msg->m_id)) {
+ return nullptr;
+ }
+ if (!decoder.Read8(&msg->m_flags)) {
+ return nullptr;
+ }
break;
}
case kEntryDelete: {
@@ -98,7 +131,9 @@
decoder.set_error("received ENTRY_DELETE in protocol < 3.0");
return nullptr;
}
- if (!decoder.Read16(&msg->m_id)) return nullptr;
+ if (!decoder.Read16(&msg->m_id)) {
+ return nullptr;
+ }
break;
}
case kClearEntries: {
@@ -107,7 +142,9 @@
return nullptr;
}
uint32_t magic;
- if (!decoder.Read32(&magic)) return nullptr;
+ if (!decoder.Read32(&magic)) {
+ return nullptr;
+ }
if (magic != kClearAllMagic) {
decoder.set_error(
"received incorrect CLEAR_ENTRIES magic value, ignoring");
@@ -120,13 +157,21 @@
decoder.set_error("received EXECUTE_RPC in protocol < 3.0");
return nullptr;
}
- if (!decoder.Read16(&msg->m_id)) return nullptr;
- if (!decoder.Read16(&msg->m_seq_num_uid)) return nullptr; // uid
+ if (!decoder.Read16(&msg->m_id)) {
+ return nullptr;
+ }
+ if (!decoder.Read16(&msg->m_seq_num_uid)) {
+ return nullptr; // uid
+ }
uint64_t size;
- if (!decoder.ReadUleb128(&size)) return nullptr;
+ if (!decoder.ReadUleb128(&size)) {
+ return nullptr;
+ }
const char* params;
- if (!decoder.Read(¶ms, size)) return nullptr;
- msg->m_str = wpi::StringRef(params, size);
+ if (!decoder.Read(¶ms, size)) {
+ return nullptr;
+ }
+ msg->m_str.assign(params, size);
break;
}
case kRpcResponse: {
@@ -134,38 +179,46 @@
decoder.set_error("received RPC_RESPONSE in protocol < 3.0");
return nullptr;
}
- if (!decoder.Read16(&msg->m_id)) return nullptr;
- if (!decoder.Read16(&msg->m_seq_num_uid)) return nullptr; // uid
+ if (!decoder.Read16(&msg->m_id)) {
+ return nullptr;
+ }
+ if (!decoder.Read16(&msg->m_seq_num_uid)) {
+ return nullptr; // uid
+ }
uint64_t size;
- if (!decoder.ReadUleb128(&size)) return nullptr;
+ if (!decoder.ReadUleb128(&size)) {
+ return nullptr;
+ }
const char* results;
- if (!decoder.Read(&results, size)) return nullptr;
- msg->m_str = wpi::StringRef(results, size);
+ if (!decoder.Read(&results, size)) {
+ return nullptr;
+ }
+ msg->m_str.assign(results, size);
break;
}
default:
decoder.set_error("unrecognized message type");
- WPI_INFO(decoder.logger(), "unrecognized message type: " << msg_type);
+ WPI_INFO(decoder.logger(), "unrecognized message type: {}", msg_type);
return nullptr;
}
return msg;
}
-std::shared_ptr<Message> Message::ClientHello(wpi::StringRef self_id) {
+std::shared_ptr<Message> Message::ClientHello(std::string_view self_id) {
auto msg = std::make_shared<Message>(kClientHello, private_init());
msg->m_str = self_id;
return msg;
}
std::shared_ptr<Message> Message::ServerHello(unsigned int flags,
- wpi::StringRef self_id) {
+ std::string_view self_id) {
auto msg = std::make_shared<Message>(kServerHello, private_init());
msg->m_str = self_id;
msg->m_flags = flags;
return msg;
}
-std::shared_ptr<Message> Message::EntryAssign(wpi::StringRef name,
+std::shared_ptr<Message> Message::EntryAssign(std::string_view name,
unsigned int id,
unsigned int seq_num,
std::shared_ptr<Value> value,
@@ -204,7 +257,7 @@
}
std::shared_ptr<Message> Message::ExecuteRpc(unsigned int id, unsigned int uid,
- wpi::StringRef params) {
+ std::string_view params) {
auto msg = std::make_shared<Message>(kExecuteRpc, private_init());
msg->m_str = params;
msg->m_id = id;
@@ -213,7 +266,7 @@
}
std::shared_ptr<Message> Message::RpcResponse(unsigned int id, unsigned int uid,
- wpi::StringRef result) {
+ std::string_view result) {
auto msg = std::make_shared<Message>(kRpcResponse, private_init());
msg->m_str = result;
msg->m_id = id;
@@ -229,7 +282,9 @@
case kClientHello:
encoder.Write8(kClientHello);
encoder.Write16(encoder.proto_rev());
- if (encoder.proto_rev() < 0x0300u) return;
+ if (encoder.proto_rev() < 0x0300u) {
+ return;
+ }
encoder.WriteString(m_str);
break;
case kProtoUnsup:
@@ -240,13 +295,17 @@
encoder.Write8(kServerHelloDone);
break;
case kServerHello:
- if (encoder.proto_rev() < 0x0300u) return; // new message in version 3.0
+ if (encoder.proto_rev() < 0x0300u) {
+ return; // new message in version 3.0
+ }
encoder.Write8(kServerHello);
encoder.Write8(m_flags);
encoder.WriteString(m_str);
break;
case kClientHelloDone:
- if (encoder.proto_rev() < 0x0300u) return; // new message in version 3.0
+ if (encoder.proto_rev() < 0x0300u) {
+ return; // new message in version 3.0
+ }
encoder.Write8(kClientHelloDone);
break;
case kEntryAssign:
@@ -255,41 +314,55 @@
encoder.WriteType(m_value->type());
encoder.Write16(m_id);
encoder.Write16(m_seq_num_uid);
- if (encoder.proto_rev() >= 0x0300u) encoder.Write8(m_flags);
+ if (encoder.proto_rev() >= 0x0300u) {
+ encoder.Write8(m_flags);
+ }
encoder.WriteValue(*m_value);
break;
case kEntryUpdate:
encoder.Write8(kEntryUpdate);
encoder.Write16(m_id);
encoder.Write16(m_seq_num_uid);
- if (encoder.proto_rev() >= 0x0300u) encoder.WriteType(m_value->type());
+ if (encoder.proto_rev() >= 0x0300u) {
+ encoder.WriteType(m_value->type());
+ }
encoder.WriteValue(*m_value);
break;
case kFlagsUpdate:
- if (encoder.proto_rev() < 0x0300u) return; // new message in version 3.0
+ if (encoder.proto_rev() < 0x0300u) {
+ return; // new message in version 3.0
+ }
encoder.Write8(kFlagsUpdate);
encoder.Write16(m_id);
encoder.Write8(m_flags);
break;
case kEntryDelete:
- if (encoder.proto_rev() < 0x0300u) return; // new message in version 3.0
+ if (encoder.proto_rev() < 0x0300u) {
+ return; // new message in version 3.0
+ }
encoder.Write8(kEntryDelete);
encoder.Write16(m_id);
break;
case kClearEntries:
- if (encoder.proto_rev() < 0x0300u) return; // new message in version 3.0
+ if (encoder.proto_rev() < 0x0300u) {
+ return; // new message in version 3.0
+ }
encoder.Write8(kClearEntries);
encoder.Write32(kClearAllMagic);
break;
case kExecuteRpc:
- if (encoder.proto_rev() < 0x0300u) return; // new message in version 3.0
+ if (encoder.proto_rev() < 0x0300u) {
+ return; // new message in version 3.0
+ }
encoder.Write8(kExecuteRpc);
encoder.Write16(m_id);
encoder.Write16(m_seq_num_uid);
encoder.WriteString(m_str);
break;
case kRpcResponse:
- if (encoder.proto_rev() < 0x0300u) return; // new message in version 3.0
+ if (encoder.proto_rev() < 0x0300u) {
+ return; // new message in version 3.0
+ }
encoder.Write8(kRpcResponse);
encoder.Write16(m_id);
encoder.Write16(m_seq_num_uid);
diff --git a/ntcore/src/main/native/cpp/Message.h b/ntcore/src/main/native/cpp/Message.h
index 9b25abf..ec34a75 100644
--- a/ntcore/src/main/native/cpp/Message.h
+++ b/ntcore/src/main/native/cpp/Message.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_MESSAGE_H_
#define NTCORE_MESSAGE_H_
@@ -11,6 +8,7 @@
#include <functional>
#include <memory>
#include <string>
+#include <string_view>
#include "networktables/NetworkTableValue.h"
@@ -39,18 +37,17 @@
kExecuteRpc = 0x20,
kRpcResponse = 0x21
};
- typedef std::function<NT_Type(unsigned int id)> GetEntryTypeFunc;
+ using GetEntryTypeFunc = std::function<NT_Type(unsigned int id)>;
- Message() : m_type(kUnknown), m_id(0), m_flags(0), m_seq_num_uid(0) {}
- Message(MsgType type, const private_init&)
- : m_type(type), m_id(0), m_flags(0), m_seq_num_uid(0) {}
+ Message() = default;
+ Message(MsgType type, const private_init&) : m_type(type) {}
MsgType type() const { return m_type; }
bool Is(MsgType type) const { return type == m_type; }
// Message data accessors. Callers are responsible for knowing what data is
// actually provided for a particular message.
- wpi::StringRef str() const { return m_str; }
+ std::string_view str() const { return m_str; }
std::shared_ptr<Value> value() const { return m_value; }
unsigned int id() const { return m_id; }
unsigned int flags() const { return m_flags; }
@@ -79,10 +76,10 @@
}
// Create messages with data
- static std::shared_ptr<Message> ClientHello(wpi::StringRef self_id);
+ static std::shared_ptr<Message> ClientHello(std::string_view self_id);
static std::shared_ptr<Message> ServerHello(unsigned int flags,
- wpi::StringRef self_id);
- static std::shared_ptr<Message> EntryAssign(wpi::StringRef name,
+ std::string_view self_id);
+ static std::shared_ptr<Message> EntryAssign(std::string_view name,
unsigned int id,
unsigned int seq_num,
std::shared_ptr<Value> value,
@@ -94,22 +91,22 @@
unsigned int flags);
static std::shared_ptr<Message> EntryDelete(unsigned int id);
static std::shared_ptr<Message> ExecuteRpc(unsigned int id, unsigned int uid,
- wpi::StringRef params);
+ std::string_view params);
static std::shared_ptr<Message> RpcResponse(unsigned int id, unsigned int uid,
- wpi::StringRef result);
+ std::string_view result);
Message(const Message&) = delete;
Message& operator=(const Message&) = delete;
private:
- MsgType m_type;
+ MsgType m_type{kUnknown};
// Message data. Use varies by message type.
std::string m_str;
std::shared_ptr<Value> m_value;
- unsigned int m_id; // also used for proto_rev
- unsigned int m_flags;
- unsigned int m_seq_num_uid;
+ unsigned int m_id{0}; // also used for proto_rev
+ unsigned int m_flags{0};
+ unsigned int m_seq_num_uid{0};
};
} // namespace nt
diff --git a/ntcore/src/main/native/cpp/NetworkConnection.cpp b/ntcore/src/main/native/cpp/NetworkConnection.cpp
index 281b638..838eecb 100644
--- a/ntcore/src/main/native/cpp/NetworkConnection.cpp
+++ b/ntcore/src/main/native/cpp/NetworkConnection.cpp
@@ -1,12 +1,11 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "NetworkConnection.h"
+#include <utility>
+
#include <wpi/NetworkStream.h>
#include <wpi/raw_socket_istream.h>
#include <wpi/timestamp.h>
@@ -28,8 +27,8 @@
m_stream(std::move(stream)),
m_notifier(notifier),
m_logger(logger),
- m_handshake(handshake),
- m_get_entry_type(get_entry_type),
+ m_handshake(std::move(handshake)),
+ m_get_entry_type(std::move(get_entry_type)),
m_state(kCreated) {
m_active = false;
m_proto_rev = 0x0300;
@@ -39,14 +38,20 @@
m_stream->setNoDelay();
}
-NetworkConnection::~NetworkConnection() { Stop(); }
+NetworkConnection::~NetworkConnection() {
+ Stop();
+}
void NetworkConnection::Start() {
- if (m_active) return;
+ if (m_active) {
+ return;
+ }
m_active = true;
set_state(kInit);
// clear queue
- while (!m_outgoing.empty()) m_outgoing.pop();
+ while (!m_outgoing.empty()) {
+ m_outgoing.pop();
+ }
// reset shutdown flags
{
std::scoped_lock lock(m_shutdown_mutex);
@@ -59,11 +64,13 @@
}
void NetworkConnection::Stop() {
- DEBUG2("NetworkConnection stopping (" << this << ")");
+ DEBUG2("NetworkConnection stopping ({})", fmt::ptr(this));
set_state(kDead);
m_active = false;
// closing the stream so the read thread terminates
- if (m_stream) m_stream->close();
+ if (m_stream) {
+ m_stream->close();
+ }
// send an empty outgoing message set so the write thread terminates
m_outgoing.push(Outgoing());
// wait for threads to terminate, with timeout
@@ -72,32 +79,38 @@
auto timeout_time =
std::chrono::steady_clock::now() + std::chrono::milliseconds(200);
if (m_write_shutdown_cv.wait_until(lock, timeout_time,
- [&] { return m_write_shutdown; }))
+ [&] { return m_write_shutdown; })) {
m_write_thread.join();
- else
+ } else {
m_write_thread.detach(); // timed out, detach it
+ }
}
if (m_read_thread.joinable()) {
std::unique_lock lock(m_shutdown_mutex);
auto timeout_time =
std::chrono::steady_clock::now() + std::chrono::milliseconds(200);
if (m_read_shutdown_cv.wait_until(lock, timeout_time,
- [&] { return m_read_shutdown; }))
+ [&] { return m_read_shutdown; })) {
m_read_thread.join();
- else
+ } else {
m_read_thread.detach(); // timed out, detach it
+ }
}
// clear queue
- while (!m_outgoing.empty()) m_outgoing.pop();
+ while (!m_outgoing.empty()) {
+ m_outgoing.pop();
+ }
}
ConnectionInfo NetworkConnection::info() const {
- return ConnectionInfo{remote_id(), m_stream->getPeerIP(),
+ return ConnectionInfo{remote_id(), std::string{m_stream->getPeerIP()},
static_cast<unsigned int>(m_stream->getPeerPort()),
m_last_update, m_proto_rev};
}
-unsigned int NetworkConnection::proto_rev() const { return m_proto_rev; }
+unsigned int NetworkConnection::proto_rev() const {
+ return m_proto_rev;
+}
void NetworkConnection::set_proto_rev(unsigned int proto_rev) {
m_proto_rev = proto_rev;
@@ -111,12 +124,16 @@
void NetworkConnection::set_state(State state) {
std::scoped_lock lock(m_state_mutex);
// Don't update state any more once we've died
- if (m_state == kDead) return;
+ if (m_state == kDead) {
+ return;
+ }
// One-shot notify state changes
- if (m_state != kActive && state == kActive)
+ if (m_state != kActive && state == kActive) {
m_notifier.NotifyConnection(true, info());
- if (m_state != kDead && state == kDead)
+ }
+ if (m_state != kDead && state == kDead) {
m_notifier.NotifyConnection(false, info());
+ }
m_state = state;
}
@@ -125,7 +142,7 @@
return m_remote_id;
}
-void NetworkConnection::set_remote_id(StringRef remote_id) {
+void NetworkConnection::set_remote_id(std::string_view remote_id) {
std::scoped_lock lock(m_remote_id_mutex);
m_remote_id = remote_id;
}
@@ -140,12 +157,14 @@
[&] {
decoder.set_proto_rev(m_proto_rev);
auto msg = Message::Read(decoder, m_get_entry_type);
- if (!msg && decoder.error())
- DEBUG0("error reading in handshake: " << decoder.error());
+ if (!msg && decoder.error()) {
+ DEBUG0("error reading in handshake: {}", decoder.error());
+ }
return msg;
},
- [&](wpi::ArrayRef<std::shared_ptr<Message>> msgs) {
- m_outgoing.emplace(msgs);
+ [&](auto msgs) {
+ m_outgoing.emplace(std::vector<std::shared_ptr<Message>>(
+ msgs.begin(), msgs.end()));
})) {
set_state(kDead);
m_active = false;
@@ -154,23 +173,28 @@
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);
if (!msg) {
- if (decoder.error()) INFO("read error: " << decoder.error());
+ if (decoder.error()) {
+ INFO("read error: {}", decoder.error());
+ }
// terminate connection on bad message
- if (m_stream) m_stream->close();
+ if (m_stream) {
+ m_stream->close();
+ }
break;
}
- DEBUG3("received type=" << msg->type() << " with str=" << msg->str()
- << " id=" << msg->id()
- << " seq_num=" << msg->seq_num_uid());
+ DEBUG3("received type={} with str={} id={} seq_num={}", msg->type(),
+ msg->str(), msg->id(), msg->seq_num_uid());
m_last_update = Now();
m_process_incoming(std::move(msg), this);
}
- DEBUG2("read thread died (" << this << ")");
+ DEBUG2("read thread died ({})", fmt::ptr(this));
set_state(kDead);
m_active = false;
m_outgoing.push(Outgoing()); // also kill write thread
@@ -189,29 +213,38 @@
while (m_active) {
auto msgs = m_outgoing.pop();
- DEBUG4("write thread woke up");
- if (msgs.empty()) continue;
+ DEBUG4("{}", "write thread woke up");
+ if (msgs.empty()) {
+ continue;
+ }
encoder.set_proto_rev(m_proto_rev);
encoder.Reset();
- DEBUG3("sending " << msgs.size() << " messages");
+ DEBUG3("sending {} messages", msgs.size());
for (auto& msg : msgs) {
if (msg) {
- DEBUG3("sending type=" << msg->type() << " with str=" << msg->str()
- << " id=" << msg->id()
- << " seq_num=" << msg->seq_num_uid());
+ DEBUG3("sending type={} with str={} id={} seq_num={}", msg->type(),
+ msg->str(), msg->id(), msg->seq_num_uid());
msg->Write(encoder);
}
}
wpi::NetworkStream::Error err;
- if (!m_stream) break;
- if (encoder.size() == 0) continue;
- if (m_stream->send(encoder.data(), encoder.size(), &err) == 0) break;
- DEBUG4("sent " << encoder.size() << " bytes");
+ if (!m_stream) {
+ break;
+ }
+ if (encoder.size() == 0) {
+ continue;
+ }
+ if (m_stream->send(encoder.data(), encoder.size(), &err) == 0) {
+ break;
+ }
+ DEBUG4("sent {} bytes", encoder.size());
}
- DEBUG2("write thread died (" << this << ")");
+ DEBUG2("write thread died ({})", fmt::ptr(this));
set_state(kDead);
m_active = false;
- if (m_stream) m_stream->close(); // also kill read thread
+ if (m_stream) {
+ m_stream->close(); // also kill read thread
+ }
// use condition variable to signal thread shutdown
{
@@ -249,7 +282,9 @@
// new, but remember it
size_t pos = m_pending_outgoing.size();
m_pending_outgoing.push_back(msg);
- if (id >= m_pending_update.size()) m_pending_update.resize(id + 1);
+ if (id >= m_pending_update.size()) {
+ m_pending_update.resize(id + 1);
+ }
m_pending_update[id].first = pos + 1;
}
break;
@@ -292,7 +327,9 @@
// new, but remember it
size_t pos = m_pending_outgoing.size();
m_pending_outgoing.push_back(msg);
- if (id >= m_pending_update.size()) m_pending_update.resize(id + 1);
+ if (id >= m_pending_update.size()) {
+ m_pending_update.resize(id + 1);
+ }
m_pending_update[id].second = pos + 1;
}
break;
@@ -300,12 +337,15 @@
case Message::kClearEntries: {
// knock out all previous assigns/updates!
for (auto& i : m_pending_outgoing) {
- if (!i) continue;
+ if (!i) {
+ continue;
+ }
auto t = i->type();
if (t == Message::kEntryAssign || t == Message::kEntryUpdate ||
t == Message::kFlagsUpdate || t == Message::kEntryDelete ||
- t == Message::kClearEntries)
+ t == Message::kClearEntries) {
i.reset();
+ }
}
m_pending_update.resize(0);
m_pending_outgoing.push_back(msg);
@@ -321,9 +361,13 @@
std::scoped_lock lock(m_pending_mutex);
auto now = std::chrono::steady_clock::now();
if (m_pending_outgoing.empty()) {
- if (!keep_alive) return;
+ if (!keep_alive) {
+ return;
+ }
// send keep-alives once a second (if no other messages have been sent)
- if ((now - m_last_post) < std::chrono::seconds(1)) return;
+ if ((now - m_last_post) < std::chrono::seconds(1)) {
+ return;
+ }
m_outgoing.emplace(Outgoing{Message::KeepAlive()});
} else {
m_outgoing.emplace(std::move(m_pending_outgoing));
@@ -331,4 +375,4 @@
m_pending_update.resize(0);
}
m_last_post = now;
-}
+} // NOLINT
diff --git a/ntcore/src/main/native/cpp/NetworkConnection.h b/ntcore/src/main/native/cpp/NetworkConnection.h
index 91ad64e..59f18ff 100644
--- a/ntcore/src/main/native/cpp/NetworkConnection.h
+++ b/ntcore/src/main/native/cpp/NetworkConnection.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_NETWORKCONNECTION_H_
#define NTCORE_NETWORKCONNECTION_H_
@@ -14,6 +11,7 @@
#include <chrono>
#include <memory>
#include <string>
+#include <string_view>
#include <thread>
#include <utility>
#include <vector>
@@ -21,6 +19,7 @@
#include <wpi/ConcurrentQueue.h>
#include <wpi/condition_variable.h>
#include <wpi/mutex.h>
+#include <wpi/span.h>
#include "INetworkConnection.h"
#include "Message.h"
@@ -37,23 +36,21 @@
class NetworkConnection : public INetworkConnection {
public:
- typedef std::function<bool(
+ using HandshakeFunc = std::function<bool(
NetworkConnection& conn,
std::function<std::shared_ptr<Message>()> get_msg,
- std::function<void(wpi::ArrayRef<std::shared_ptr<Message>>)> send_msgs)>
- HandshakeFunc;
- typedef std::function<void(std::shared_ptr<Message> msg,
- NetworkConnection* conn)>
- ProcessIncomingFunc;
- typedef std::vector<std::shared_ptr<Message>> Outgoing;
- typedef wpi::ConcurrentQueue<Outgoing> OutgoingQueue;
+ std::function<void(wpi::span<std::shared_ptr<Message>>)> send_msgs)>;
+ using ProcessIncomingFunc =
+ std::function<void(std::shared_ptr<Message>, NetworkConnection*)>;
+ using Outgoing = std::vector<std::shared_ptr<Message>>;
+ using OutgoingQueue = wpi::ConcurrentQueue<Outgoing>;
NetworkConnection(unsigned int uid,
std::unique_ptr<wpi::NetworkStream> stream,
IConnectionNotifier& notifier, wpi::Logger& logger,
HandshakeFunc handshake,
Message::GetEntryTypeFunc get_entry_type);
- ~NetworkConnection();
+ ~NetworkConnection() override;
// Set the input processor function. This must be called before Start().
void set_process_incoming(ProcessIncomingFunc func) {
@@ -63,24 +60,24 @@
void Start();
void Stop();
- ConnectionInfo info() const override;
+ ConnectionInfo info() const final;
bool active() const { return m_active; }
wpi::NetworkStream& stream() { return *m_stream; }
- void QueueOutgoing(std::shared_ptr<Message> msg) override;
+ void QueueOutgoing(std::shared_ptr<Message> msg) final;
void PostOutgoing(bool keep_alive) override;
unsigned int uid() const { return m_uid; }
- unsigned int proto_rev() const override;
- void set_proto_rev(unsigned int proto_rev) override;
+ unsigned int proto_rev() const final;
+ void set_proto_rev(unsigned int proto_rev) final;
- State state() const override;
- void set_state(State state) override;
+ State state() const final;
+ void set_state(State state) final;
std::string remote_id() const;
- void set_remote_id(StringRef remote_id);
+ void set_remote_id(std::string_view remote_id);
uint64_t last_update() const { return m_last_update; }
diff --git a/ntcore/src/main/native/cpp/RpcServer.cpp b/ntcore/src/main/native/cpp/RpcServer.cpp
index 0c6811c..b4bf96a 100644
--- a/ntcore/src/main/native/cpp/RpcServer.cpp
+++ b/ntcore/src/main/native/cpp/RpcServer.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "RpcServer.h"
@@ -12,7 +9,9 @@
RpcServer::RpcServer(int inst, wpi::Logger& logger)
: m_inst(inst), m_logger(logger) {}
-void RpcServer::Start() { DoStart(m_inst, m_logger); }
+void RpcServer::Start() {
+ DoStart(m_inst, m_logger);
+}
unsigned int RpcServer::Add(
std::function<void(const RpcAnswer& answer)> callback) {
@@ -23,10 +22,12 @@
return DoAdd(poller_uid);
}
-void RpcServer::RemoveRpc(unsigned int rpc_uid) { Remove(rpc_uid); }
+void RpcServer::RemoveRpc(unsigned int rpc_uid) {
+ Remove(rpc_uid);
+}
void RpcServer::ProcessRpc(unsigned int local_id, unsigned int call_uid,
- StringRef name, StringRef params,
+ std::string_view name, std::string_view params,
const ConnectionInfo& conn,
SendResponseFunc send_response,
unsigned int rpc_uid) {
@@ -36,11 +37,12 @@
}
bool RpcServer::PostRpcResponse(unsigned int local_id, unsigned int call_uid,
- wpi::StringRef result) {
+ std::string_view result) {
auto thr = GetThread();
auto i = thr->m_response_map.find(impl::RpcIdPair{local_id, call_uid});
if (i == thr->m_response_map.end()) {
- WARNING("posting RPC response to nonexistent call (or duplicate response)");
+ WARNING("{}",
+ "posting RPC response to nonexistent call (or duplicate response)");
return false;
}
(i->getSecond())(result);
diff --git a/ntcore/src/main/native/cpp/RpcServer.h b/ntcore/src/main/native/cpp/RpcServer.h
index cca490f..b8ae6b7 100644
--- a/ntcore/src/main/native/cpp/RpcServer.h
+++ b/ntcore/src/main/native/cpp/RpcServer.h
@@ -1,19 +1,16 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_RPCSERVER_H_
#define NTCORE_RPCSERVER_H_
#include <utility>
+#include <wpi/CallbackManager.h>
#include <wpi/DenseMap.h>
#include <wpi/mutex.h>
-#include "CallbackManager.h"
#include "Handle.h"
#include "IRpcServer.h"
#include "Log.h"
@@ -22,27 +19,30 @@
namespace impl {
-typedef std::pair<unsigned int, unsigned int> RpcIdPair;
+using RpcIdPair = std::pair<unsigned int, unsigned int>;
struct RpcNotifierData : public RpcAnswer {
- RpcNotifierData(NT_Entry entry_, NT_RpcCall call_, StringRef name_,
- StringRef params_, const ConnectionInfo& conn_,
+ RpcNotifierData(NT_Entry entry_, NT_RpcCall call_, std::string_view name_,
+ std::string_view params_, const ConnectionInfo& conn_,
IRpcServer::SendResponseFunc send_response_)
: RpcAnswer{entry_, call_, name_, params_, conn_},
- send_response{send_response_} {}
+ send_response{std::move(send_response_)} {}
IRpcServer::SendResponseFunc send_response;
};
using RpcListenerData =
- ListenerData<std::function<void(const RpcAnswer& answer)>>;
+ wpi::CallbackListenerData<std::function<void(const RpcAnswer& answer)>>;
class RpcServerThread
- : public CallbackThread<RpcServerThread, RpcAnswer, RpcListenerData,
- RpcNotifierData> {
+ : public wpi::CallbackThread<RpcServerThread, RpcAnswer, RpcListenerData,
+ RpcNotifierData> {
public:
- RpcServerThread(int inst, wpi::Logger& logger)
- : m_inst(inst), m_logger(logger) {}
+ RpcServerThread(std::function<void()> on_start, std::function<void()> on_exit,
+ int inst, wpi::Logger& logger)
+ : CallbackThread(std::move(on_start), std::move(on_exit)),
+ m_inst(inst),
+ m_logger(logger) {}
bool Matches(const RpcListenerData& /*listener*/,
const RpcNotifierData& data) {
@@ -58,7 +58,7 @@
void DoCallback(std::function<void(const RpcAnswer& call)> callback,
const RpcNotifierData& data) {
- DEBUG4("rpc calling " << data.name);
+ DEBUG4("rpc calling {}", data.name);
unsigned int local_id = Handle{data.entry}.GetIndex();
unsigned int call_uid = Handle{data.call}.GetIndex();
RpcIdPair lookup_uid{local_id, call_uid};
@@ -81,10 +81,11 @@
} // namespace impl
-class RpcServer : public IRpcServer,
- public CallbackManager<RpcServer, impl::RpcServerThread> {
+class RpcServer
+ : public IRpcServer,
+ public wpi::CallbackManager<RpcServer, impl::RpcServerThread> {
friend class RpcServerTest;
- friend class CallbackManager<RpcServer, impl::RpcServerThread>;
+ friend class wpi::CallbackManager<RpcServer, impl::RpcServerThread>;
public:
RpcServer(int inst, wpi::Logger& logger);
@@ -95,13 +96,13 @@
unsigned int AddPolled(unsigned int poller_uid);
void RemoveRpc(unsigned int rpc_uid) override;
- void ProcessRpc(unsigned int local_id, unsigned int call_uid, StringRef name,
- StringRef params, const ConnectionInfo& conn,
- SendResponseFunc send_response,
+ void ProcessRpc(unsigned int local_id, unsigned int call_uid,
+ std::string_view name, std::string_view params,
+ const ConnectionInfo& conn, SendResponseFunc send_response,
unsigned int rpc_uid) override;
bool PostRpcResponse(unsigned int local_id, unsigned int call_uid,
- wpi::StringRef result);
+ std::string_view result);
private:
int m_inst;
diff --git a/ntcore/src/main/native/cpp/SequenceNumber.cpp b/ntcore/src/main/native/cpp/SequenceNumber.cpp
index 6d61331..e25e636 100644
--- a/ntcore/src/main/native/cpp/SequenceNumber.cpp
+++ b/ntcore/src/main/native/cpp/SequenceNumber.cpp
@@ -1,30 +1,29 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "SequenceNumber.h"
namespace nt {
bool operator<(const SequenceNumber& lhs, const SequenceNumber& rhs) {
- if (lhs.m_value < rhs.m_value)
+ if (lhs.m_value < rhs.m_value) {
return (rhs.m_value - lhs.m_value) < (1u << 15);
- else if (lhs.m_value > rhs.m_value)
+ } else if (lhs.m_value > rhs.m_value) {
return (lhs.m_value - rhs.m_value) > (1u << 15);
- else
+ } else {
return false;
+ }
}
bool operator>(const SequenceNumber& lhs, const SequenceNumber& rhs) {
- if (lhs.m_value < rhs.m_value)
+ if (lhs.m_value < rhs.m_value) {
return (rhs.m_value - lhs.m_value) > (1u << 15);
- else if (lhs.m_value > rhs.m_value)
+ } else if (lhs.m_value > rhs.m_value) {
return (lhs.m_value - rhs.m_value) < (1u << 15);
- else
+ } else {
return false;
+ }
}
} // namespace nt
diff --git a/ntcore/src/main/native/cpp/SequenceNumber.h b/ntcore/src/main/native/cpp/SequenceNumber.h
index 11d9953..719d85c 100644
--- a/ntcore/src/main/native/cpp/SequenceNumber.h
+++ b/ntcore/src/main/native/cpp/SequenceNumber.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_SEQUENCENUMBER_H_
#define NTCORE_SEQUENCENUMBER_H_
@@ -13,13 +10,15 @@
/* A sequence number per RFC 1982 */
class SequenceNumber {
public:
- SequenceNumber() : m_value(0) {}
+ SequenceNumber() = default;
explicit SequenceNumber(unsigned int value) : m_value(value) {}
unsigned int value() const { return m_value; }
SequenceNumber& operator++() {
++m_value;
- if (m_value > 0xffff) m_value = 0;
+ if (m_value > 0xffff) {
+ m_value = 0;
+ }
return *this;
}
SequenceNumber operator++(int) {
@@ -36,7 +35,7 @@
friend bool operator!=(const SequenceNumber& lhs, const SequenceNumber& rhs);
private:
- unsigned int m_value;
+ unsigned int m_value{0};
};
bool operator<(const SequenceNumber& lhs, const SequenceNumber& rhs);
diff --git a/ntcore/src/main/native/cpp/Storage.cpp b/ntcore/src/main/native/cpp/Storage.cpp
index dadb8e7..dadc3f6 100644
--- a/ntcore/src/main/native/cpp/Storage.cpp
+++ b/ntcore/src/main/native/cpp/Storage.cpp
@@ -1,12 +1,10 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "Storage.h"
+#include <wpi/StringExtras.h>
#include <wpi/timestamp.h>
#include "Handle.h"
@@ -35,13 +33,19 @@
m_server = server;
}
-void Storage::ClearDispatcher() { m_dispatcher = nullptr; }
+void Storage::ClearDispatcher() {
+ m_dispatcher = nullptr;
+}
NT_Type Storage::GetMessageEntryType(unsigned int id) const {
std::scoped_lock lock(m_mutex);
- if (id >= m_idmap.size()) return NT_UNASSIGNED;
+ if (id >= m_idmap.size()) {
+ return NT_UNASSIGNED;
+ }
Entry* entry = m_idmap[id];
- if (!entry || !entry->value) return NT_UNASSIGNED;
+ if (!entry || !entry->value) {
+ return NT_UNASSIGNED;
+ }
return entry->value->type();
}
@@ -88,7 +92,7 @@
INetworkConnection* conn) {
std::unique_lock lock(m_mutex);
unsigned int id = msg->id();
- StringRef name = msg->str();
+ std::string_view name = msg->str();
Entry* entry;
bool may_need_update = false;
SequenceNumber seq_num(msg->seq_num_uid());
@@ -99,7 +103,9 @@
if (id == 0xffff) {
entry = GetOrNew(name);
// see if it was already assigned; ignore if so.
- if (entry->id != 0xffff) return;
+ if (entry->id != 0xffff) {
+ return;
+ }
entry->flags = msg->flags();
entry->seq_num = seq_num;
@@ -110,7 +116,7 @@
// ignore arbitrary entry assignments
// this can happen due to e.g. assignment to deleted entry
lock.unlock();
- DEBUG0("server: received assignment to unknown entry");
+ DEBUG0("{}", "server: received assignment to unknown entry");
return;
}
entry = m_idmap[id];
@@ -118,10 +124,12 @@
// clients simply accept new assignments
if (id == 0xffff) {
lock.unlock();
- DEBUG0("client: received entry assignment request?");
+ DEBUG0("{}", "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
@@ -171,7 +179,7 @@
// sanity check: name should match id
if (msg->str() != entry->name) {
lock.unlock();
- DEBUG0("entry assignment for same id with different name?");
+ DEBUG0("{}", "entry assignment for same id with different name?");
return;
}
@@ -181,15 +189,19 @@
// don't update flags if this is a server response to a client id request
if (!may_need_update && conn->proto_rev() >= 0x0300) {
// update persistent dirty flag if persistent flag changed
- if ((entry->flags & NT_PERSISTENT) != (msg->flags() & NT_PERSISTENT))
+ 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();
}
// update persistent dirty flag if the value changed and it's persistent
- if (entry->IsPersistent() && *entry->value != *msg->value())
+ if (entry->IsPersistent() && *entry->value != *msg->value()) {
m_persistent_dirty = true;
+ }
// update local
entry->value = msg->value();
@@ -217,21 +229,25 @@
// ignore arbitrary entry updates;
// this can happen due to deleted entries
lock.unlock();
- DEBUG0("received update to unknown entry");
+ DEBUG0("{}", "received update to unknown entry");
return;
}
Entry* entry = m_idmap[id];
// ignore if sequence number not higher than local
SequenceNumber seq_num(msg->seq_num_uid());
- if (seq_num <= entry->seq_num) return;
+ if (seq_num <= entry->seq_num) {
+ return;
+ }
// update local
entry->value = msg->value();
entry->seq_num = seq_num;
// update persistent dirty flag if it's a persistent value
- if (entry->IsPersistent()) m_persistent_dirty = true;
+ if (entry->IsPersistent()) {
+ m_persistent_dirty = true;
+ }
// notify
m_notifier.NotifyEntry(entry->local_id, entry->name, entry->value,
@@ -254,7 +270,7 @@
// ignore arbitrary entry updates;
// this can happen due to deleted entries
lock.unlock();
- DEBUG0("received flags update to unknown entry");
+ DEBUG0("{}", "received flags update to unknown entry");
return;
}
@@ -278,7 +294,7 @@
// ignore arbitrary entry updates;
// this can happen due to deleted entries
lock.unlock();
- DEBUG0("received delete to unknown entry");
+ DEBUG0("{}", "received delete to unknown entry");
return;
}
@@ -313,19 +329,21 @@
std::shared_ptr<Message> msg, INetworkConnection* /*conn*/,
std::weak_ptr<INetworkConnection> conn_weak) {
std::unique_lock lock(m_mutex);
- if (!m_server) return; // only process on server
+ if (!m_server) {
+ return; // only process on server
+ }
unsigned int id = msg->id();
if (id >= m_idmap.size() || !m_idmap[id]) {
// ignore call to non-existent RPC
// this can happen due to deleted entries
lock.unlock();
- DEBUG0("received RPC call to unknown entry");
+ DEBUG0("{}", "received RPC call to unknown entry");
return;
}
Entry* entry = m_idmap[id];
if (!entry->value || !entry->value->IsRpc()) {
lock.unlock();
- DEBUG0("received RPC call to non-RPC entry");
+ DEBUG0("{}", "received RPC call to non-RPC entry");
return;
}
ConnectionInfo conn_info;
@@ -342,9 +360,11 @@
unsigned int call_uid = msg->seq_num_uid();
m_rpc_server.ProcessRpc(
entry->local_id, call_uid, entry->name, msg->str(), conn_info,
- [=](StringRef result) {
+ [=](std::string_view result) {
auto c = conn_weak.lock();
- if (c) c->QueueOutgoing(Message::RpcResponse(id, call_uid, result));
+ if (c) {
+ c->QueueOutgoing(Message::RpcResponse(id, call_uid, result));
+ }
},
entry->rpc_uid);
}
@@ -352,23 +372,25 @@
void Storage::ProcessIncomingRpcResponse(std::shared_ptr<Message> msg,
INetworkConnection* /*conn*/) {
std::unique_lock lock(m_mutex);
- if (m_server) return; // only process on client
+ if (m_server) {
+ return; // only process on client
+ }
unsigned int id = msg->id();
if (id >= m_idmap.size() || !m_idmap[id]) {
// ignore response to non-existent RPC
// this can happen due to deleted entries
lock.unlock();
- DEBUG0("received rpc response to unknown entry");
+ DEBUG0("{}", "received rpc response to unknown entry");
return;
}
Entry* entry = m_idmap[id];
if (!entry->value || !entry->value->IsRpc()) {
lock.unlock();
- DEBUG0("received RPC response to non-RPC entry");
+ DEBUG0("{}", "received RPC response to non-RPC entry");
return;
}
- m_rpc_results.insert(std::make_pair(
- RpcIdPair{entry->local_id, msg->seq_num_uid()}, msg->str()));
+ m_rpc_results.insert({RpcIdPair{entry->local_id, msg->seq_num_uid()},
+ std::string{msg->str()}});
m_rpc_results_cond.notify_all();
}
@@ -378,7 +400,9 @@
conn.set_state(INetworkConnection::kSynchronized);
for (auto& i : m_entries) {
Entry* entry = i.getValue();
- if (!entry->value) continue;
+ if (!entry->value) {
+ continue;
+ }
msgs->emplace_back(Message::EntryAssign(i.getKey(), entry->id,
entry->seq_num.value(),
entry->value, entry->flags));
@@ -386,17 +410,21 @@
}
void Storage::ApplyInitialAssignments(
- INetworkConnection& conn, wpi::ArrayRef<std::shared_ptr<Message>> msgs,
+ INetworkConnection& conn, wpi::span<std::shared_ptr<Message>> msgs,
bool /*new_server*/, std::vector<std::shared_ptr<Message>>* out_msgs) {
std::unique_lock lock(m_mutex);
- if (m_server) return; // should not do this on server
+ if (m_server) {
+ return; // should not do this on server
+ }
conn.set_state(INetworkConnection::kSynchronized);
std::vector<std::shared_ptr<Message>> update_msgs;
// clear existing id's
- for (auto& i : m_entries) i.getValue()->id = 0xffff;
+ for (auto& i : m_entries) {
+ i.getValue()->id = 0xffff;
+ }
// clear existing idmap
m_idmap.resize(0);
@@ -404,18 +432,18 @@
// apply assignments
for (auto& msg : msgs) {
if (!msg->Is(Message::kEntryAssign)) {
- DEBUG0("client: received non-entry assignment request?");
+ DEBUG0("{}", "client: received non-entry assignment request?");
continue;
}
unsigned int id = msg->id();
if (id == 0xffff) {
- DEBUG0("client: received entry assignment request?");
+ DEBUG0("{}", "client: received entry assignment request?");
continue;
}
SequenceNumber seq_num(msg->seq_num_uid());
- StringRef name = msg->str();
+ std::string_view name = msg->str();
Entry* entry = GetOrNew(name);
entry->seq_num = seq_num;
@@ -440,7 +468,9 @@
unsigned int notify_flags = NT_NOTIFY_UPDATE;
// don't update flags from a <3.0 remote (not part of message)
if (conn.proto_rev() >= 0x0300) {
- if (entry->flags != msg->flags()) notify_flags |= NT_NOTIFY_FLAGS;
+ if (entry->flags != msg->flags()) {
+ notify_flags |= NT_NOTIFY_FLAGS;
+ }
entry->flags = msg->flags();
}
// notify
@@ -450,14 +480,18 @@
}
// save to idmap
- if (id >= m_idmap.size()) m_idmap.resize(id + 1);
+ if (id >= m_idmap.size()) {
+ m_idmap.resize(id + 1);
+ }
m_idmap[id] = entry;
}
// delete or generate assign messages for unassigned local entries
DeleteAllEntriesImpl(false, [&](Entry* entry) -> bool {
// was assigned by the server, don't delete
- if (entry->id != 0xffff) return false;
+ if (entry->id != 0xffff) {
+ return false;
+ }
// if we have written the value locally, we send an assign message to the
// server instead of deleting
if (entry->local_write) {
@@ -471,32 +505,43 @@
});
auto dispatcher = m_dispatcher;
lock.unlock();
- for (auto& msg : update_msgs)
+ for (auto& msg : update_msgs) {
dispatcher->QueueOutgoing(msg, nullptr, nullptr);
+ }
}
-std::shared_ptr<Value> Storage::GetEntryValue(StringRef name) const {
+std::shared_ptr<Value> Storage::GetEntryValue(std::string_view name) const {
std::scoped_lock lock(m_mutex);
auto i = m_entries.find(name);
- if (i == m_entries.end()) return nullptr;
+ if (i == m_entries.end()) {
+ return nullptr;
+ }
return i->getValue()->value;
}
std::shared_ptr<Value> Storage::GetEntryValue(unsigned int local_id) const {
std::scoped_lock lock(m_mutex);
- if (local_id >= m_localmap.size()) return nullptr;
+ if (local_id >= m_localmap.size()) {
+ return nullptr;
+ }
return m_localmap[local_id]->value;
}
-bool Storage::SetDefaultEntryValue(StringRef name,
+bool Storage::SetDefaultEntryValue(std::string_view name,
std::shared_ptr<Value> value) {
- if (name.empty()) return false;
- if (!value) return false;
+ if (name.empty()) {
+ return false;
+ }
+ if (!value) {
+ return false;
+ }
std::unique_lock lock(m_mutex);
Entry* entry = GetOrNew(name);
// we return early if value already exists; if types match return true
- if (entry->value) return entry->value->type() == value->type();
+ if (entry->value) {
+ return entry->value->type() == value->type();
+ }
SetEntryValueImpl(entry, value, lock, true);
return true;
@@ -504,26 +549,38 @@
bool Storage::SetDefaultEntryValue(unsigned int local_id,
std::shared_ptr<Value> value) {
- if (!value) return false;
+ if (!value) {
+ return false;
+ }
std::unique_lock lock(m_mutex);
- if (local_id >= m_localmap.size()) return false;
+ if (local_id >= m_localmap.size()) {
+ return false;
+ }
Entry* entry = m_localmap[local_id].get();
// we return early if value already exists; if types match return true
- if (entry->value) return entry->value->type() == value->type();
+ if (entry->value) {
+ return entry->value->type() == value->type();
+ }
SetEntryValueImpl(entry, value, lock, true);
return true;
}
-bool Storage::SetEntryValue(StringRef name, std::shared_ptr<Value> value) {
- if (name.empty()) return true;
- if (!value) return true;
+bool Storage::SetEntryValue(std::string_view name,
+ std::shared_ptr<Value> value) {
+ if (name.empty()) {
+ return true;
+ }
+ if (!value) {
+ return true;
+ }
std::unique_lock lock(m_mutex);
Entry* entry = GetOrNew(name);
- if (entry->value && entry->value->type() != value->type())
+ if (entry->value && entry->value->type() != value->type()) {
return false; // error on type mismatch
+ }
SetEntryValueImpl(entry, value, lock, true);
return true;
@@ -531,13 +588,18 @@
bool Storage::SetEntryValue(unsigned int local_id,
std::shared_ptr<Value> value) {
- if (!value) return true;
+ if (!value) {
+ return true;
+ }
std::unique_lock lock(m_mutex);
- if (local_id >= m_localmap.size()) return true;
+ if (local_id >= m_localmap.size()) {
+ return true;
+ }
Entry* entry = m_localmap[local_id].get();
- if (entry->value && entry->value->type() != value->type())
+ if (entry->value && entry->value->type() != value->type()) {
return false; // error on type mismatch
+ }
SetEntryValueImpl(entry, value, lock, true);
return true;
@@ -546,7 +608,9 @@
void Storage::SetEntryValueImpl(Entry* entry, std::shared_ptr<Value> value,
std::unique_lock<wpi::mutex>& lock,
bool local) {
- if (!value) return;
+ if (!value) {
+ return;
+ }
auto old_value = entry->value;
entry->value = value;
@@ -558,31 +622,41 @@
}
// update persistent dirty flag if value changed and it's persistent
- if (entry->IsPersistent() && (!old_value || *old_value != *value))
+ if (entry->IsPersistent() && (!old_value || *old_value != *value)) {
m_persistent_dirty = true;
+ }
// notify
- if (!old_value)
+ if (!old_value) {
m_notifier.NotifyEntry(entry->local_id, entry->name, value,
NT_NOTIFY_NEW | (local ? NT_NOTIFY_LOCAL : 0));
- else if (*old_value != *value)
+ } else if (*old_value != *value) {
m_notifier.NotifyEntry(entry->local_id, entry->name, value,
NT_NOTIFY_UPDATE | (local ? NT_NOTIFY_LOCAL : 0));
+ }
// remember local changes
- if (local) entry->local_write = true;
+ if (local) {
+ entry->local_write = true;
+ }
// generate message
- if (!m_dispatcher || (!local && !m_server)) return;
+ if (!m_dispatcher || (!local && !m_server)) {
+ return;
+ }
auto dispatcher = m_dispatcher;
if (!old_value || old_value->type() != value->type()) {
- if (local) ++entry->seq_num;
+ if (local) {
+ ++entry->seq_num;
+ }
auto msg = Message::EntryAssign(
entry->name, entry->id, entry->seq_num.value(), value, entry->flags);
lock.unlock();
dispatcher->QueueOutgoing(msg, nullptr, nullptr);
} else if (*old_value != *value) {
- if (local) ++entry->seq_num;
+ if (local) {
+ ++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);
@@ -592,9 +666,14 @@
}
}
-void Storage::SetEntryTypeValue(StringRef name, std::shared_ptr<Value> value) {
- if (name.empty()) return;
- if (!value) return;
+void Storage::SetEntryTypeValue(std::string_view name,
+ std::shared_ptr<Value> value) {
+ if (name.empty()) {
+ return;
+ }
+ if (!value) {
+ return;
+ }
std::unique_lock lock(m_mutex);
Entry* entry = GetOrNew(name);
@@ -603,37 +682,52 @@
void Storage::SetEntryTypeValue(unsigned int local_id,
std::shared_ptr<Value> value) {
- if (!value) return;
+ if (!value) {
+ return;
+ }
std::unique_lock lock(m_mutex);
- if (local_id >= m_localmap.size()) return;
+ if (local_id >= m_localmap.size()) {
+ return;
+ }
Entry* entry = m_localmap[local_id].get();
- if (!entry) return;
+ if (!entry) {
+ return;
+ }
SetEntryValueImpl(entry, value, lock, true);
}
-void Storage::SetEntryFlags(StringRef name, unsigned int flags) {
- if (name.empty()) return;
+void Storage::SetEntryFlags(std::string_view name, unsigned int flags) {
+ if (name.empty()) {
+ return;
+ }
std::unique_lock lock(m_mutex);
auto i = m_entries.find(name);
- if (i == m_entries.end()) return;
+ if (i == m_entries.end()) {
+ return;
+ }
SetEntryFlagsImpl(i->getValue(), flags, lock, true);
}
void Storage::SetEntryFlags(unsigned int id_local, unsigned int flags) {
std::unique_lock lock(m_mutex);
- if (id_local >= m_localmap.size()) return;
+ if (id_local >= m_localmap.size()) {
+ return;
+ }
SetEntryFlagsImpl(m_localmap[id_local].get(), flags, lock, true);
}
void Storage::SetEntryFlagsImpl(Entry* entry, unsigned int flags,
std::unique_lock<wpi::mutex>& lock,
bool local) {
- if (!entry->value || entry->flags == flags) return;
+ if (!entry->value || entry->flags == flags) {
+ return;
+ }
// update persistent dirty flag if persistent flag changed
- if ((entry->flags & NT_PERSISTENT) != (flags & NT_PERSISTENT))
+ if ((entry->flags & NT_PERSISTENT) != (flags & NT_PERSISTENT)) {
m_persistent_dirty = true;
+ }
entry->flags = flags;
@@ -642,7 +736,9 @@
NT_NOTIFY_FLAGS | (local ? NT_NOTIFY_LOCAL : 0));
// generate message
- if (!local || !m_dispatcher) return;
+ if (!local || !m_dispatcher) {
+ return;
+ }
auto dispatcher = m_dispatcher;
unsigned int id = entry->id;
// don't send an update if we don't have an assigned id yet
@@ -653,29 +749,37 @@
}
}
-unsigned int Storage::GetEntryFlags(StringRef name) const {
+unsigned int Storage::GetEntryFlags(std::string_view name) const {
std::scoped_lock lock(m_mutex);
auto i = m_entries.find(name);
- if (i == m_entries.end()) return 0;
+ if (i == m_entries.end()) {
+ return 0;
+ }
return i->getValue()->flags;
}
unsigned int Storage::GetEntryFlags(unsigned int local_id) const {
std::scoped_lock lock(m_mutex);
- if (local_id >= m_localmap.size()) return 0;
+ if (local_id >= m_localmap.size()) {
+ return 0;
+ }
return m_localmap[local_id]->flags;
}
-void Storage::DeleteEntry(StringRef name) {
+void Storage::DeleteEntry(std::string_view name) {
std::unique_lock lock(m_mutex);
auto i = m_entries.find(name);
- if (i == m_entries.end()) return;
+ if (i == m_entries.end()) {
+ return;
+ }
DeleteEntryImpl(i->getValue(), lock, true);
}
void Storage::DeleteEntry(unsigned int local_id) {
std::unique_lock lock(m_mutex);
- if (local_id >= m_localmap.size()) return;
+ if (local_id >= m_localmap.size()) {
+ return;
+ }
DeleteEntryImpl(m_localmap[local_id].get(), lock, true);
}
@@ -684,7 +788,9 @@
unsigned int id = entry->id;
// Erase entry from id mapping.
- if (id < m_idmap.size()) m_idmap[id] = nullptr;
+ if (id < m_idmap.size()) {
+ m_idmap[id] = nullptr;
+ }
// empty the value and reset id and local_write flag
std::shared_ptr<Value> old_value;
@@ -699,12 +805,16 @@
}
// update persistent dirty flag if it's a persistent value
- if (entry->IsPersistent()) m_persistent_dirty = true;
+ if (entry->IsPersistent()) {
+ m_persistent_dirty = true;
+ }
// reset flags
entry->flags = 0;
- if (!old_value) return; // was not previously assigned
+ if (!old_value) {
+ return; // was not previously assigned
+ }
// notify
m_notifier.NotifyEntry(entry->local_id, entry->name, old_value,
@@ -713,7 +823,9 @@
// if it had a value, generate message
// don't send an update if we don't have an assigned id yet
if (local && id != 0xffff) {
- if (!m_dispatcher) return;
+ if (!m_dispatcher) {
+ return;
+ }
auto dispatcher = m_dispatcher;
lock.unlock();
dispatcher->QueueOutgoing(Message::EntryDelete(id), nullptr, nullptr);
@@ -729,7 +841,9 @@
m_notifier.NotifyEntry(entry->local_id, i.getKey(), entry->value,
NT_NOTIFY_DELETE | (local ? NT_NOTIFY_LOCAL : 0));
// remove it from idmap
- if (entry->id < m_idmap.size()) m_idmap[entry->id] = nullptr;
+ if (entry->id < m_idmap.size()) {
+ m_idmap[entry->id] = nullptr;
+ }
entry->id = 0xffff;
entry->local_write = false;
entry->value.reset();
@@ -746,48 +860,52 @@
void Storage::DeleteAllEntries() {
std::unique_lock lock(m_mutex);
- if (m_entries.empty()) return;
+ if (m_entries.empty()) {
+ return;
+ }
DeleteAllEntriesImpl(true);
// generate message
- if (!m_dispatcher) return;
+ if (!m_dispatcher) {
+ return;
+ }
auto dispatcher = m_dispatcher;
lock.unlock();
dispatcher->QueueOutgoing(Message::ClearEntries(), nullptr, nullptr);
}
-Storage::Entry* Storage::GetOrNew(const Twine& name) {
- wpi::SmallString<128> nameBuf;
- StringRef nameStr = name.toStringRef(nameBuf);
- auto& entry = m_entries[nameStr];
+Storage::Entry* Storage::GetOrNew(std::string_view name) {
+ auto& entry = m_entries[name];
if (!entry) {
- m_localmap.emplace_back(new Entry(nameStr));
+ m_localmap.emplace_back(new Entry(name));
entry = m_localmap.back().get();
entry->local_id = m_localmap.size() - 1;
}
return entry;
}
-unsigned int Storage::GetEntry(const Twine& name) {
- if (name.isTriviallyEmpty() ||
- (name.isSingleStringRef() && name.getSingleStringRef().empty()))
+unsigned int Storage::GetEntry(std::string_view name) {
+ if (name.empty()) {
return UINT_MAX;
+ }
std::unique_lock lock(m_mutex);
return GetOrNew(name)->local_id;
}
-std::vector<unsigned int> Storage::GetEntries(const Twine& prefix,
+std::vector<unsigned int> Storage::GetEntries(std::string_view prefix,
unsigned int types) {
- wpi::SmallString<128> prefixBuf;
- StringRef prefixStr = prefix.toStringRef(prefixBuf);
std::scoped_lock lock(m_mutex);
std::vector<unsigned int> ids;
for (auto& i : m_entries) {
Entry* entry = i.getValue();
auto value = entry->value.get();
- if (!value || !i.getKey().startswith(prefixStr)) continue;
- if (types != 0 && (types & value->type()) == 0) continue;
+ if (!value || !wpi::starts_with(i.getKey(), prefix)) {
+ continue;
+ }
+ if (types != 0 && (types & value->type()) == 0) {
+ continue;
+ }
ids.push_back(entry->local_id);
}
return ids;
@@ -801,9 +919,13 @@
info.last_change = 0;
std::unique_lock lock(m_mutex);
- if (local_id >= m_localmap.size()) return info;
+ if (local_id >= m_localmap.size()) {
+ return info;
+ }
Entry* entry = m_localmap[local_id].get();
- if (!entry->value) return info;
+ if (!entry->value) {
+ return info;
+ }
info.entry = Handle(inst, local_id, Handle::kEntry);
info.name = entry->name;
@@ -815,37 +937,49 @@
std::string Storage::GetEntryName(unsigned int local_id) const {
std::unique_lock lock(m_mutex);
- if (local_id >= m_localmap.size()) return std::string{};
+ if (local_id >= m_localmap.size()) {
+ return {};
+ }
return m_localmap[local_id]->name;
}
NT_Type Storage::GetEntryType(unsigned int local_id) const {
std::unique_lock lock(m_mutex);
- if (local_id >= m_localmap.size()) return NT_UNASSIGNED;
+ if (local_id >= m_localmap.size()) {
+ return NT_UNASSIGNED;
+ }
Entry* entry = m_localmap[local_id].get();
- if (!entry->value) return NT_UNASSIGNED;
+ if (!entry->value) {
+ return NT_UNASSIGNED;
+ }
return entry->value->type();
}
uint64_t Storage::GetEntryLastChange(unsigned int local_id) const {
std::unique_lock lock(m_mutex);
- if (local_id >= m_localmap.size()) return 0;
+ if (local_id >= m_localmap.size()) {
+ return 0;
+ }
Entry* entry = m_localmap[local_id].get();
- if (!entry->value) return 0;
+ if (!entry->value) {
+ return 0;
+ }
return entry->value->last_change();
}
-std::vector<EntryInfo> Storage::GetEntryInfo(int inst, const Twine& prefix,
+std::vector<EntryInfo> Storage::GetEntryInfo(int inst, std::string_view prefix,
unsigned int types) {
- wpi::SmallString<128> prefixBuf;
- StringRef prefixStr = prefix.toStringRef(prefixBuf);
std::scoped_lock lock(m_mutex);
std::vector<EntryInfo> infos;
for (auto& i : m_entries) {
Entry* entry = i.getValue();
auto value = entry->value.get();
- if (!value || !i.getKey().startswith(prefixStr)) continue;
- if (types != 0 && (types & value->type()) == 0) continue;
+ if (!value || !wpi::starts_with(i.getKey(), prefix)) {
+ continue;
+ }
+ if (types != 0 && (types & value->type()) == 0) {
+ continue;
+ }
EntryInfo info;
info.entry = Handle(inst, entry->local_id, Handle::kEntry);
info.name = i.getKey();
@@ -858,18 +992,18 @@
}
unsigned int Storage::AddListener(
- const Twine& prefix,
+ std::string_view prefix,
std::function<void(const EntryNotification& event)> callback,
unsigned int flags) const {
- wpi::SmallString<128> prefixBuf;
- StringRef prefixStr = prefix.toStringRef(prefixBuf);
std::scoped_lock lock(m_mutex);
- unsigned int uid = m_notifier.Add(callback, prefixStr, flags);
+ unsigned int uid = m_notifier.Add(callback, prefix, flags);
// perform immediate notifications
if ((flags & NT_NOTIFY_IMMEDIATE) != 0 && (flags & NT_NOTIFY_NEW) != 0) {
for (auto& i : m_entries) {
Entry* entry = i.getValue();
- if (!entry->value || !i.getKey().startswith(prefixStr)) continue;
+ if (!entry->value || !wpi::starts_with(i.getKey(), prefix)) {
+ continue;
+ }
m_notifier.NotifyEntry(entry->local_id, i.getKey(), entry->value,
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW, uid);
}
@@ -896,18 +1030,20 @@
}
unsigned int Storage::AddPolledListener(unsigned int poller,
- const Twine& prefix,
+ std::string_view prefix,
unsigned int flags) const {
- wpi::SmallString<128> prefixBuf;
- StringRef prefixStr = prefix.toStringRef(prefixBuf);
std::scoped_lock lock(m_mutex);
- unsigned int uid = m_notifier.AddPolled(poller, prefixStr, flags);
+ unsigned int uid = m_notifier.AddPolled(poller, prefix, flags);
// perform immediate notifications
if ((flags & NT_NOTIFY_IMMEDIATE) != 0 && (flags & NT_NOTIFY_NEW) != 0) {
for (auto& i : m_entries) {
- if (!i.getKey().startswith(prefixStr)) continue;
+ if (!wpi::starts_with(i.getKey(), prefix)) {
+ continue;
+ }
Entry* entry = i.getValue();
- if (!entry->value) continue;
+ if (!entry->value) {
+ continue;
+ }
m_notifier.NotifyEntry(entry->local_id, i.getKey(), entry->value,
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW, uid);
}
@@ -941,13 +1077,17 @@
{
std::scoped_lock lock(m_mutex);
// for periodic, don't re-save unless something has changed
- if (periodic && !m_persistent_dirty) return false;
+ if (periodic && !m_persistent_dirty) {
+ return false;
+ }
m_persistent_dirty = false;
entries->reserve(m_entries.size());
for (auto& i : m_entries) {
Entry* entry = i.getValue();
// only write persistent-flagged values
- if (!entry->value || !entry->IsPersistent()) continue;
+ if (!entry->value || !entry->IsPersistent()) {
+ continue;
+ }
entries->emplace_back(i.getKey(), entry->value);
}
}
@@ -962,11 +1102,9 @@
}
bool Storage::GetEntries(
- const Twine& prefix,
+ std::string_view prefix,
std::vector<std::pair<std::string, std::shared_ptr<Value>>>* entries)
const {
- wpi::SmallString<128> prefixBuf;
- StringRef prefixStr = prefix.toStringRef(prefixBuf);
// copy values out of storage as quickly as possible so lock isn't held
{
std::scoped_lock lock(m_mutex);
@@ -974,7 +1112,9 @@
for (auto& i : m_entries) {
Entry* entry = i.getValue();
// only write values with given prefix
- if (!entry->value || !i.getKey().startswith(prefixStr)) continue;
+ if (!entry->value || !wpi::starts_with(i.getKey(), prefix)) {
+ continue;
+ }
entries->emplace_back(i.getKey(), entry->value);
}
}
@@ -988,10 +1128,12 @@
return true;
}
-void Storage::CreateRpc(unsigned int local_id, StringRef def,
+void Storage::CreateRpc(unsigned int local_id, std::string_view def,
unsigned int rpc_uid) {
std::unique_lock lock(m_mutex);
- if (local_id >= m_localmap.size()) return;
+ if (local_id >= m_localmap.size()) {
+ return;
+ }
Entry* entry = m_localmap[local_id].get();
auto old_value = entry->value;
@@ -1001,7 +1143,9 @@
// set up the RPC info
entry->rpc_uid = rpc_uid;
- if (old_value && *old_value == *value) return;
+ if (old_value && *old_value == *value) {
+ return;
+ }
// assign an id if it doesn't have one
if (entry->id == 0xffff) {
@@ -1011,7 +1155,9 @@
}
// generate message
- if (!m_dispatcher) return;
+ if (!m_dispatcher) {
+ return;
+ }
auto dispatcher = m_dispatcher;
if (!old_value || old_value->type() != value->type()) {
++entry->seq_num;
@@ -1027,19 +1173,25 @@
}
}
-unsigned int Storage::CallRpc(unsigned int local_id, StringRef params) {
+unsigned int Storage::CallRpc(unsigned int local_id, std::string_view params) {
std::unique_lock lock(m_mutex);
- if (local_id >= m_localmap.size()) return 0;
+ if (local_id >= m_localmap.size()) {
+ return 0;
+ }
Entry* entry = m_localmap[local_id].get();
- if (!entry->value || !entry->value->IsRpc()) return 0;
+ if (!entry->value || !entry->value->IsRpc()) {
+ return 0;
+ }
++entry->rpc_call_uid;
- if (entry->rpc_call_uid > 0xffff) entry->rpc_call_uid = 0;
+ if (entry->rpc_call_uid > 0xffff) {
+ entry->rpc_call_uid = 0;
+ }
unsigned int call_uid = entry->rpc_call_uid;
auto msg = Message::ExecuteRpc(entry->id, call_uid, params);
- StringRef name{entry->name};
+ std::string_view name{entry->name};
if (m_server) {
// RPCs are unlikely to be used locally on the server, but handle it
@@ -1055,10 +1207,10 @@
unsigned int call_uid = msg->seq_num_uid();
m_rpc_server.ProcessRpc(
local_id, call_uid, name, msg->str(), conn_info,
- [=](StringRef result) {
+ [=](std::string_view result) {
std::scoped_lock lock(m_mutex);
- m_rpc_results.insert(
- std::make_pair(RpcIdPair{local_id, call_uid}, result));
+ m_rpc_results.insert(std::make_pair(RpcIdPair{local_id, call_uid},
+ std::string{result}));
m_rpc_results_cond.notify_all();
},
rpc_uid);
@@ -1084,7 +1236,9 @@
RpcIdPair call_pair{local_id, call_uid};
// only allow one blocking call per rpc call uid
- if (!m_rpc_blocking_calls.insert(call_pair).second) return false;
+ if (!m_rpc_blocking_calls.insert(call_pair).second) {
+ return false;
+ }
auto timeout_time =
std::chrono::steady_clock::now() + std::chrono::duration<double>(timeout);
diff --git a/ntcore/src/main/native/cpp/Storage.h b/ntcore/src/main/native/cpp/Storage.h
index fa9b2bf..f49c071 100644
--- a/ntcore/src/main/native/cpp/Storage.h
+++ b/ntcore/src/main/native/cpp/Storage.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_STORAGE_H_
#define NTCORE_STORAGE_H_
@@ -15,6 +12,7 @@
#include <functional>
#include <memory>
#include <string>
+#include <string_view>
#include <utility>
#include <vector>
@@ -23,6 +21,7 @@
#include <wpi/StringMap.h>
#include <wpi/condition_variable.h>
#include <wpi/mutex.h>
+#include <wpi/span.h>
#include "IStorage.h"
#include "Message.h"
@@ -50,7 +49,7 @@
Storage(const Storage&) = delete;
Storage& operator=(const Storage&) = delete;
- ~Storage();
+ ~Storage() override;
// Accessors required by Dispatcher. An interface is used for
// generation of outgoing messages to break a dependency loop between
@@ -69,41 +68,42 @@
INetworkConnection& conn,
std::vector<std::shared_ptr<Message>>* msgs) override;
void ApplyInitialAssignments(
- INetworkConnection& conn, wpi::ArrayRef<std::shared_ptr<Message>> msgs,
+ INetworkConnection& conn, wpi::span<std::shared_ptr<Message>> msgs,
bool new_server,
std::vector<std::shared_ptr<Message>>* out_msgs) override;
// User functions. These are the actual implementations of the corresponding
// user API functions in ntcore_cpp.
- std::shared_ptr<Value> GetEntryValue(StringRef name) const;
+ std::shared_ptr<Value> GetEntryValue(std::string_view name) const;
std::shared_ptr<Value> GetEntryValue(unsigned int local_id) const;
- bool SetDefaultEntryValue(StringRef name, std::shared_ptr<Value> value);
+ bool SetDefaultEntryValue(std::string_view name,
+ std::shared_ptr<Value> value);
bool SetDefaultEntryValue(unsigned int local_id,
std::shared_ptr<Value> value);
- bool SetEntryValue(StringRef name, std::shared_ptr<Value> value);
+ bool SetEntryValue(std::string_view name, std::shared_ptr<Value> value);
bool SetEntryValue(unsigned int local_id, std::shared_ptr<Value> value);
- void SetEntryTypeValue(StringRef name, std::shared_ptr<Value> value);
+ void SetEntryTypeValue(std::string_view name, std::shared_ptr<Value> value);
void SetEntryTypeValue(unsigned int local_id, std::shared_ptr<Value> value);
- void SetEntryFlags(StringRef name, unsigned int flags);
+ void SetEntryFlags(std::string_view name, unsigned int flags);
void SetEntryFlags(unsigned int local_id, unsigned int flags);
- unsigned int GetEntryFlags(StringRef name) const;
+ unsigned int GetEntryFlags(std::string_view name) const;
unsigned int GetEntryFlags(unsigned int local_id) const;
- void DeleteEntry(StringRef name);
+ void DeleteEntry(std::string_view name);
void DeleteEntry(unsigned int local_id);
void DeleteAllEntries();
- std::vector<EntryInfo> GetEntryInfo(int inst, const Twine& prefix,
+ std::vector<EntryInfo> GetEntryInfo(int inst, std::string_view prefix,
unsigned int types);
unsigned int AddListener(
- const Twine& prefix,
+ std::string_view prefix,
std::function<void(const EntryNotification& event)> callback,
unsigned int flags) const;
unsigned int AddListener(
@@ -111,14 +111,16 @@
std::function<void(const EntryNotification& event)> callback,
unsigned int flags) const;
- unsigned int AddPolledListener(unsigned int poller_uid, const Twine& prefix,
+ unsigned int AddPolledListener(unsigned int poller_uid,
+ std::string_view prefix,
unsigned int flags) const;
unsigned int AddPolledListener(unsigned int poller_uid, unsigned int local_id,
unsigned int flags) const;
// Index-only
- unsigned int GetEntry(const Twine& name);
- std::vector<unsigned int> GetEntries(const Twine& prefix, unsigned int types);
+ unsigned int GetEntry(std::string_view name);
+ std::vector<unsigned int> GetEntries(std::string_view prefix,
+ unsigned int types);
EntryInfo GetEntryInfo(int inst, unsigned int local_id) const;
std::string GetEntryName(unsigned int local_id) const;
NT_Type GetEntryType(unsigned int local_id) const;
@@ -126,29 +128,32 @@
// Filename-based save/load functions. Used both by periodic saves and
// accessible directly via the user API.
- const char* SavePersistent(const Twine& filename,
+ const char* SavePersistent(std::string_view filename,
bool periodic) const override;
const char* LoadPersistent(
- const Twine& filename,
+ std::string_view filename,
std::function<void(size_t line, const char* msg)> warn) override;
- const char* SaveEntries(const Twine& filename, const Twine& prefix) const;
+ const char* SaveEntries(std::string_view filename,
+ std::string_view prefix) const;
const char* LoadEntries(
- const Twine& filename, const Twine& prefix,
+ std::string_view filename, std::string_view prefix,
std::function<void(size_t line, const char* msg)> warn);
// Stream-based save/load functions (exposed for testing purposes). These
// implement the guts of the filename-based functions.
void SavePersistent(wpi::raw_ostream& os, bool periodic) const;
- bool LoadEntries(wpi::raw_istream& is, const Twine& prefix, bool persistent,
+ bool LoadEntries(wpi::raw_istream& is, std::string_view prefix,
+ bool persistent,
std::function<void(size_t line, const char* msg)> warn);
- void SaveEntries(wpi::raw_ostream& os, const Twine& prefix) const;
+ void SaveEntries(wpi::raw_ostream& os, std::string_view prefix) const;
// RPC configuration needs to come through here as RPC definitions are
// actually special Storage value types.
- void CreateRpc(unsigned int local_id, StringRef def, unsigned int rpc_uid);
- unsigned int CallRpc(unsigned int local_id, StringRef params);
+ void CreateRpc(unsigned int local_id, std::string_view def,
+ unsigned int rpc_uid);
+ unsigned int CallRpc(unsigned int local_id, std::string_view params);
bool GetRpcResult(unsigned int local_id, unsigned int call_uid,
std::string* result);
bool GetRpcResult(unsigned int local_id, unsigned int call_uid,
@@ -158,7 +163,7 @@
private:
// Data for each table entry.
struct Entry {
- explicit Entry(wpi::StringRef name_) : name(name_) {}
+ explicit Entry(std::string_view name_) : name(name_) {}
bool IsPersistent() const { return (flags & NT_PERSISTENT) != 0; }
// We redundantly store the name so that it's available when accessing the
@@ -192,12 +197,12 @@
unsigned int rpc_call_uid{0};
};
- typedef wpi::StringMap<Entry*> EntriesMap;
- typedef std::vector<Entry*> IdMap;
- typedef std::vector<std::unique_ptr<Entry>> LocalMap;
- typedef std::pair<unsigned int, unsigned int> RpcIdPair;
- typedef wpi::DenseMap<RpcIdPair, std::string> RpcResultMap;
- typedef wpi::SmallSet<RpcIdPair, 12> RpcBlockingCallSet;
+ using EntriesMap = wpi::StringMap<Entry*>;
+ using IdMap = std::vector<Entry*>;
+ using LocalMap = std::vector<std::unique_ptr<Entry>>;
+ using RpcIdPair = std::pair<unsigned int, unsigned int>;
+ using RpcResultMap = wpi::DenseMap<RpcIdPair, std::string>;
+ using RpcBlockingCallSet = wpi::SmallSet<RpcIdPair, 12>;
mutable wpi::mutex m_mutex;
EntriesMap m_entries;
@@ -240,7 +245,7 @@
bool periodic,
std::vector<std::pair<std::string, std::shared_ptr<Value>>>* entries)
const;
- bool GetEntries(const Twine& prefix,
+ bool GetEntries(std::string_view prefix,
std::vector<std::pair<std::string, std::shared_ptr<Value>>>*
entries) const;
void SetEntryValueImpl(Entry* entry, std::shared_ptr<Value> value,
@@ -254,7 +259,7 @@
template <typename F>
void DeleteAllEntriesImpl(bool local, F should_delete);
void DeleteAllEntriesImpl(bool local);
- Entry* GetOrNew(const Twine& name);
+ Entry* GetOrNew(std::string_view name);
};
} // namespace nt
diff --git a/ntcore/src/main/native/cpp/Storage_load.cpp b/ntcore/src/main/native/cpp/Storage_load.cpp
index 69eb173..11bbb0e 100644
--- a/ntcore/src/main/native/cpp/Storage_load.cpp
+++ b/ntcore/src/main/native/cpp/Storage_load.cpp
@@ -1,12 +1,10 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <cctype>
#include <string>
+#include <utility>
#include <wpi/Base64.h>
#include <wpi/SmallString.h>
@@ -23,19 +21,19 @@
class LoadPersistentImpl {
public:
- typedef std::pair<std::string, std::shared_ptr<Value>> Entry;
- typedef std::function<void(size_t line, const char* msg)> WarnFunc;
+ using Entry = std::pair<std::string, std::shared_ptr<Value>>;
+ using WarnFunc = std::function<void(size_t, const char*)>;
LoadPersistentImpl(wpi::raw_istream& is, WarnFunc warn)
- : m_is(is), m_warn(warn) {}
+ : m_is(is), m_warn(std::move(warn)) {}
- bool Load(StringRef prefix, std::vector<Entry>* entries);
+ bool Load(std::string_view prefix, std::vector<Entry>* entries);
private:
bool ReadLine();
bool ReadHeader();
NT_Type ReadType();
- wpi::StringRef ReadName(wpi::SmallVectorImpl<char>& buf);
+ std::string_view ReadName(wpi::SmallVectorImpl<char>& buf);
std::shared_ptr<Value> ReadValue(NT_Type type);
std::shared_ptr<Value> ReadBooleanValue();
std::shared_ptr<Value> ReadDoubleValue();
@@ -46,13 +44,15 @@
std::shared_ptr<Value> ReadStringArrayValue();
void Warn(const char* msg) {
- if (m_warn) m_warn(m_line_num, msg);
+ if (m_warn) {
+ m_warn(m_line_num, msg);
+ }
}
wpi::raw_istream& m_is;
WarnFunc m_warn;
- wpi::StringRef m_line;
+ std::string_view m_line;
wpi::SmallString<128> m_line_buf;
size_t m_line_num = 0;
@@ -71,11 +71,12 @@
* Returns a pair containing the extracted token (if any) and the remaining
* tail string.
*/
-static std::pair<wpi::StringRef, wpi::StringRef> ReadStringToken(
- wpi::StringRef source) {
+static std::pair<std::string_view, std::string_view> ReadStringToken(
+ std::string_view source) {
// Match opening quote
- if (source.empty() || source.front() != '"')
- return std::make_pair(wpi::StringRef(), source);
+ if (source.empty() || source.front() != '"') {
+ return {{}, source};
+ }
// Scan for ending double quote, checking for escaped as we go.
size_t size = source.size();
@@ -86,20 +87,21 @@
break;
}
}
- return std::make_pair(source.slice(0, pos), source.substr(pos));
+ return {wpi::slice(source, 0, pos), source.substr(pos)};
}
static int fromxdigit(char ch) {
- if (ch >= 'a' && ch <= 'f')
+ if (ch >= 'a' && ch <= 'f') {
return (ch - 'a' + 10);
- else if (ch >= 'A' && ch <= 'F')
+ } else if (ch >= 'A' && ch <= 'F') {
return (ch - 'A' + 10);
- else
+ } else {
return ch - '0';
+ }
}
-static wpi::StringRef UnescapeString(wpi::StringRef source,
- wpi::SmallVectorImpl<char>& buf) {
+static std::string_view UnescapeString(std::string_view source,
+ wpi::SmallVectorImpl<char>& buf) {
assert(source.size() >= 2 && source.front() == '"' && source.back() == '"');
buf.clear();
buf.reserve(source.size() - 2);
@@ -133,11 +135,14 @@
break;
}
}
- return wpi::StringRef{buf.data(), buf.size()};
+ return {buf.data(), buf.size()};
}
-bool LoadPersistentImpl::Load(StringRef prefix, std::vector<Entry>* entries) {
- if (!ReadHeader()) return false; // header
+bool LoadPersistentImpl::Load(std::string_view prefix,
+ std::vector<Entry>* entries) {
+ if (!ReadHeader()) {
+ return false; // header
+ }
while (ReadLine()) {
// type
@@ -149,22 +154,27 @@
// name
wpi::SmallString<128> buf;
- wpi::StringRef name = ReadName(buf);
- if (name.empty() || !name.startswith(prefix)) continue;
+ std::string_view name = ReadName(buf);
+ if (name.empty() || !wpi::starts_with(name, prefix)) {
+ continue;
+ }
// =
- m_line = m_line.ltrim(" \t");
+ m_line = wpi::ltrim(m_line, " \t");
if (m_line.empty() || m_line.front() != '=') {
Warn("expected = after name");
continue;
}
- m_line = m_line.drop_front().ltrim(" \t");
+ m_line.remove_prefix(1);
+ m_line = wpi::ltrim(m_line, " \t");
// value
auto value = ReadValue(type);
// move to entries
- if (value) entries->emplace_back(name, std::move(value));
+ if (value) {
+ entries->emplace_back(name, std::move(value));
+ }
}
return true;
}
@@ -173,9 +183,10 @@
// ignore blank lines and lines that start with ; or # (comments)
while (!m_is.has_error()) {
++m_line_num;
- m_line = m_is.getline(m_line_buf, INT_MAX).trim();
- if (!m_line.empty() && m_line.front() != ';' && m_line.front() != '#')
+ m_line = wpi::trim(m_is.getline(m_line_buf, INT_MAX));
+ if (!m_line.empty() && m_line.front() != ';' && m_line.front() != '#') {
return true;
+ }
}
return false;
}
@@ -190,8 +201,8 @@
}
NT_Type LoadPersistentImpl::ReadType() {
- wpi::StringRef tok;
- std::tie(tok, m_line) = m_line.split(' ');
+ std::string_view tok;
+ std::tie(tok, m_line) = wpi::split(m_line, ' ');
if (tok == "boolean") {
return NT_BOOLEAN;
} else if (tok == "double") {
@@ -201,28 +212,29 @@
} else if (tok == "raw") {
return NT_RAW;
} else if (tok == "array") {
- wpi::StringRef array_tok;
- std::tie(array_tok, m_line) = m_line.split(' ');
- if (array_tok == "boolean")
+ std::string_view array_tok;
+ std::tie(array_tok, m_line) = wpi::split(m_line, ' ');
+ if (array_tok == "boolean") {
return NT_BOOLEAN_ARRAY;
- else if (array_tok == "double")
+ } else if (array_tok == "double") {
return NT_DOUBLE_ARRAY;
- else if (array_tok == "string")
+ } else if (array_tok == "string") {
return NT_STRING_ARRAY;
+ }
}
return NT_UNASSIGNED;
}
-wpi::StringRef LoadPersistentImpl::ReadName(wpi::SmallVectorImpl<char>& buf) {
- wpi::StringRef tok;
+std::string_view LoadPersistentImpl::ReadName(wpi::SmallVectorImpl<char>& buf) {
+ std::string_view tok;
std::tie(tok, m_line) = ReadStringToken(m_line);
if (tok.empty()) {
Warn("missing name");
- return wpi::StringRef{};
+ return {};
}
if (tok.back() != '"') {
Warn("unterminated name string");
- return wpi::StringRef{};
+ return {};
}
return UnescapeString(tok, buf);
}
@@ -250,15 +262,19 @@
std::shared_ptr<Value> LoadPersistentImpl::ReadBooleanValue() {
// only true or false is accepted
- if (m_line == "true") return Value::MakeBoolean(true);
- if (m_line == "false") return Value::MakeBoolean(false);
+ if (m_line == "true") {
+ return Value::MakeBoolean(true);
+ }
+ if (m_line == "false") {
+ return Value::MakeBoolean(false);
+ }
Warn("unrecognized boolean value, not 'true' or 'false'");
return nullptr;
}
std::shared_ptr<Value> LoadPersistentImpl::ReadDoubleValue() {
// need to convert to null-terminated string for std::strtod()
- wpi::SmallString<64> buf = m_line;
+ wpi::SmallString<64> buf{m_line};
char* end;
double v = std::strtod(buf.c_str(), &end);
if (*end != '\0') {
@@ -269,7 +285,7 @@
}
std::shared_ptr<Value> LoadPersistentImpl::ReadStringValue() {
- wpi::StringRef tok;
+ std::string_view tok;
std::tie(tok, m_line) = ReadStringToken(m_line);
if (tok.empty()) {
Warn("missing string value");
@@ -292,9 +308,9 @@
std::shared_ptr<Value> LoadPersistentImpl::ReadBooleanArrayValue() {
m_buf_boolean_array.clear();
while (!m_line.empty()) {
- wpi::StringRef tok;
- std::tie(tok, m_line) = m_line.split(',');
- tok = tok.trim(" \t");
+ std::string_view tok;
+ std::tie(tok, m_line) = wpi::split(m_line, ',');
+ tok = wpi::trim(tok, " \t");
if (tok == "true") {
m_buf_boolean_array.push_back(1);
} else if (tok == "false") {
@@ -310,11 +326,11 @@
std::shared_ptr<Value> LoadPersistentImpl::ReadDoubleArrayValue() {
m_buf_double_array.clear();
while (!m_line.empty()) {
- wpi::StringRef tok;
- std::tie(tok, m_line) = m_line.split(',');
- tok = tok.trim(" \t");
+ std::string_view tok;
+ std::tie(tok, m_line) = wpi::split(m_line, ',');
+ tok = wpi::trim(tok, " \t");
// need to convert to null-terminated string for std::strtod()
- wpi::SmallString<64> buf = tok;
+ wpi::SmallString<64> buf{tok};
char* end;
double v = std::strtod(buf.c_str(), &end);
if (*end != '\0') {
@@ -330,7 +346,7 @@
std::shared_ptr<Value> LoadPersistentImpl::ReadStringArrayValue() {
m_buf_string_array.clear();
while (!m_line.empty()) {
- wpi::StringRef tok;
+ std::string_view tok;
std::tie(tok, m_line) = ReadStringToken(m_line);
if (tok.empty()) {
Warn("missing string value");
@@ -342,31 +358,33 @@
}
wpi::SmallString<128> buf;
- m_buf_string_array.push_back(UnescapeString(tok, buf));
+ m_buf_string_array.emplace_back(UnescapeString(tok, buf));
- m_line = m_line.ltrim(" \t");
- if (m_line.empty()) break;
+ m_line = wpi::ltrim(m_line, " \t");
+ if (m_line.empty()) {
+ break;
+ }
if (m_line.front() != ',') {
Warn("expected comma between strings");
return nullptr;
}
- m_line = m_line.drop_front().ltrim(" \t");
+ m_line.remove_prefix(1);
+ m_line = wpi::ltrim(m_line, " \t");
}
return Value::MakeStringArray(std::move(m_buf_string_array));
}
bool Storage::LoadEntries(
- wpi::raw_istream& is, const Twine& prefix, bool persistent,
+ wpi::raw_istream& is, std::string_view prefix, bool persistent,
std::function<void(size_t line, const char* msg)> warn) {
- wpi::SmallString<128> prefixBuf;
- StringRef prefixStr = prefix.toStringRef(prefixBuf);
-
// entries to add
std::vector<LoadPersistentImpl::Entry> entries;
// load file
- if (!LoadPersistentImpl(is, warn).Load(prefixStr, &entries)) return false;
+ if (!LoadPersistentImpl(is, warn).Load(prefix, &entries)) {
+ return false;
+ }
// copy values into storage as quickly as possible so lock isn't held
std::vector<std::shared_ptr<Message>> msgs;
@@ -376,7 +394,9 @@
auto old_value = entry->value;
entry->value = i.second;
bool was_persist = entry->IsPersistent();
- if (!was_persist && persistent) entry->flags |= NT_PERSISTENT;
+ if (!was_persist && persistent) {
+ entry->flags |= NT_PERSISTENT;
+ }
// if we're the server, assign an id if it doesn't have one
if (m_server && entry->id == 0xffff) {
@@ -392,7 +412,9 @@
NT_NOTIFY_NEW | NT_NOTIFY_LOCAL);
} else if (*old_value != *i.second) {
unsigned int notify_flags = NT_NOTIFY_UPDATE | NT_NOTIFY_LOCAL;
- if (!was_persist && persistent) notify_flags |= NT_NOTIFY_FLAGS;
+ if (!was_persist && persistent) {
+ notify_flags |= NT_NOTIFY_FLAGS;
+ }
m_notifier.NotifyEntry(entry->local_id, i.first, i.second,
notify_flags);
} else if (!was_persist && persistent) {
@@ -401,7 +423,9 @@
}
}
- if (!m_dispatcher) continue; // shortcut
+ if (!m_dispatcher) {
+ continue; // shortcut
+ }
++entry->seq_num;
// put on update queue
@@ -410,40 +434,51 @@
i.first, entry->id, 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)
+ if (*old_value != *i.second) {
msgs.emplace_back(
Message::EntryUpdate(entry->id, entry->seq_num.value(), i.second));
- if (!was_persist)
+ }
+ if (!was_persist) {
msgs.emplace_back(Message::FlagsUpdate(entry->id, entry->flags));
+ }
}
}
if (m_dispatcher) {
auto dispatcher = m_dispatcher;
lock.unlock();
- for (auto& msg : msgs)
+ for (auto& msg : msgs) {
dispatcher->QueueOutgoing(std::move(msg), nullptr, nullptr);
+ }
}
return true;
}
const char* Storage::LoadPersistent(
- const Twine& filename,
+ std::string_view filename,
std::function<void(size_t line, const char* msg)> warn) {
std::error_code ec;
wpi::raw_fd_istream is(filename, ec);
- if (ec.value() != 0) return "could not open file";
- if (!LoadEntries(is, "", true, warn)) return "error reading file";
+ if (ec.value() != 0) {
+ return "could not open file";
+ }
+ if (!LoadEntries(is, "", true, warn)) {
+ return "error reading file";
+ }
return nullptr;
}
const char* Storage::LoadEntries(
- const Twine& filename, const Twine& prefix,
+ std::string_view filename, std::string_view prefix,
std::function<void(size_t line, const char* msg)> warn) {
std::error_code ec;
wpi::raw_fd_istream is(filename, ec);
- if (ec.value() != 0) return "could not open file";
- if (!LoadEntries(is, prefix, false, warn)) return "error reading file";
+ if (ec.value() != 0) {
+ return "could not open file";
+ }
+ if (!LoadEntries(is, prefix, false, warn)) {
+ return "error reading file";
+ }
return nullptr;
}
diff --git a/ntcore/src/main/native/cpp/Storage_save.cpp b/ntcore/src/main/native/cpp/Storage_save.cpp
index 797eb8b..6d5db9f 100644
--- a/ntcore/src/main/native/cpp/Storage_save.cpp
+++ b/ntcore/src/main/native/cpp/Storage_save.cpp
@@ -1,18 +1,15 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <cctype>
#include <string>
+#include <fmt/format.h>
#include <wpi/Base64.h>
-#include <wpi/FileSystem.h>
-#include <wpi/Format.h>
#include <wpi/SmallString.h>
#include <wpi/StringExtras.h>
+#include <wpi/fs.h>
#include <wpi/raw_ostream.h>
#include "Log.h"
@@ -24,17 +21,17 @@
class SavePersistentImpl {
public:
- typedef std::pair<std::string, std::shared_ptr<Value>> Entry;
+ using Entry = std::pair<std::string, std::shared_ptr<Value>>;
explicit SavePersistentImpl(wpi::raw_ostream& os) : m_os(os) {}
- void Save(wpi::ArrayRef<Entry> entries);
+ void Save(wpi::span<const Entry> entries);
private:
- void WriteString(wpi::StringRef str);
+ void WriteString(std::string_view str);
void WriteHeader();
- void WriteEntries(wpi::ArrayRef<Entry> entries);
- void WriteEntry(wpi::StringRef name, const Value& value);
+ void WriteEntries(wpi::span<const Entry> entries);
+ void WriteEntry(std::string_view name, const Value& value);
bool WriteType(NT_Type type);
void WriteValue(const Value& value);
@@ -44,7 +41,7 @@
} // namespace
/* Escapes and writes a string, including start and end double quotes */
-void SavePersistentImpl::WriteString(wpi::StringRef str) {
+void SavePersistentImpl::WriteString(std::string_view str) {
m_os << '"';
for (auto c : str) {
switch (c) {
@@ -75,7 +72,7 @@
m_os << '"';
}
-void SavePersistentImpl::Save(wpi::ArrayRef<Entry> entries) {
+void SavePersistentImpl::Save(wpi::span<const Entry> entries) {
WriteHeader();
WriteEntries(entries);
}
@@ -84,19 +81,23 @@
m_os << "[NetworkTables Storage 3.0]\n";
}
-void SavePersistentImpl::WriteEntries(wpi::ArrayRef<Entry> entries) {
+void SavePersistentImpl::WriteEntries(wpi::span<const Entry> entries) {
for (auto& i : entries) {
- if (!i.second) continue;
+ if (!i.second) {
+ continue;
+ }
WriteEntry(i.first, *i.second);
}
}
-void SavePersistentImpl::WriteEntry(wpi::StringRef name, const Value& value) {
- if (!WriteType(value.type())) return; // type
- WriteString(name); // name
- m_os << '='; // '='
- WriteValue(value); // value
- m_os << '\n'; // eol
+void SavePersistentImpl::WriteEntry(std::string_view name, const Value& value) {
+ if (!WriteType(value.type())) {
+ return; // type
+ }
+ WriteString(name); // name
+ m_os << '='; // '='
+ WriteValue(value); // value
+ m_os << '\n'; // eol
}
bool SavePersistentImpl::WriteType(NT_Type type) {
@@ -134,7 +135,7 @@
m_os << (value.GetBoolean() ? "true" : "false");
break;
case NT_DOUBLE:
- m_os << wpi::format("%g", value.GetDouble());
+ m_os << fmt::format("{:g}", value.GetDouble());
break;
case NT_STRING:
WriteString(value.GetString());
@@ -146,7 +147,9 @@
case NT_BOOLEAN_ARRAY: {
bool first = true;
for (auto elem : value.GetBooleanArray()) {
- if (!first) m_os << ',';
+ if (!first) {
+ m_os << ',';
+ }
first = false;
m_os << (elem ? "true" : "false");
}
@@ -155,16 +158,20 @@
case NT_DOUBLE_ARRAY: {
bool first = true;
for (auto elem : value.GetDoubleArray()) {
- if (!first) m_os << ',';
+ if (!first) {
+ m_os << ',';
+ }
first = false;
- m_os << wpi::format("%g", elem);
+ m_os << fmt::format("{:g}", elem);
}
break;
}
case NT_STRING_ARRAY: {
bool first = true;
for (auto& elem : value.GetStringArray()) {
- if (!first) m_os << ',';
+ if (!first) {
+ m_os << ',';
+ }
first = false;
WriteString(elem);
}
@@ -177,33 +184,34 @@
void Storage::SavePersistent(wpi::raw_ostream& os, bool periodic) const {
std::vector<SavePersistentImpl::Entry> entries;
- if (!GetPersistentEntries(periodic, &entries)) return;
+ if (!GetPersistentEntries(periodic, &entries)) {
+ return;
+ }
SavePersistentImpl(os).Save(entries);
}
-const char* Storage::SavePersistent(const Twine& filename,
+const char* Storage::SavePersistent(std::string_view filename,
bool periodic) const {
- wpi::SmallString<128> fn;
- filename.toVector(fn);
- wpi::SmallString<128> tmp = fn;
- tmp += ".tmp";
- wpi::SmallString<128> bak = fn;
- bak += ".bak";
+ std::string fn{filename};
+ auto tmp = fmt::format("{}.tmp", filename);
+ auto bak = fmt::format("{}.bak", filename);
// Get entries before creating file
std::vector<SavePersistentImpl::Entry> entries;
- if (!GetPersistentEntries(periodic, &entries)) return nullptr;
+ if (!GetPersistentEntries(periodic, &entries)) {
+ return nullptr;
+ }
const char* err = nullptr;
// start by writing to temporary file
std::error_code ec;
- wpi::raw_fd_ostream os(tmp, ec, wpi::sys::fs::F_Text);
+ wpi::raw_fd_ostream os(tmp, ec, fs::F_Text);
if (ec.value() != 0) {
err = "could not open file";
goto done;
}
- DEBUG0("saving persistent file '" << filename << "'");
+ DEBUG0("saving persistent file '{}'", filename);
SavePersistentImpl(os).Save(entries);
os.close();
if (os.has_error()) {
@@ -223,36 +231,39 @@
done:
// try again if there was an error
- if (err && periodic) m_persistent_dirty = true;
+ if (err && periodic) {
+ m_persistent_dirty = true;
+ }
return err;
}
-void Storage::SaveEntries(wpi::raw_ostream& os, const Twine& prefix) const {
+void Storage::SaveEntries(wpi::raw_ostream& os, std::string_view prefix) const {
std::vector<SavePersistentImpl::Entry> entries;
- if (!GetEntries(prefix, &entries)) return;
+ if (!GetEntries(prefix, &entries)) {
+ return;
+ }
SavePersistentImpl(os).Save(entries);
}
-const char* Storage::SaveEntries(const Twine& filename,
- const Twine& prefix) const {
- wpi::SmallString<128> fn;
- filename.toVector(fn);
- wpi::SmallString<128> tmp = fn;
- tmp += ".tmp";
- wpi::SmallString<128> bak = fn;
- bak += ".bak";
+const char* Storage::SaveEntries(std::string_view filename,
+ std::string_view prefix) const {
+ std::string fn{filename};
+ auto tmp = fmt::format("{}.tmp", filename);
+ auto bak = fmt::format("{}.bak", filename);
// Get entries before creating file
std::vector<SavePersistentImpl::Entry> entries;
- if (!GetEntries(prefix, &entries)) return nullptr;
+ if (!GetEntries(prefix, &entries)) {
+ return nullptr;
+ }
// start by writing to temporary file
std::error_code ec;
- wpi::raw_fd_ostream os(tmp, ec, wpi::sys::fs::F_Text);
+ wpi::raw_fd_ostream os(tmp, ec, fs::F_Text);
if (ec.value() != 0) {
return "could not open file";
}
- DEBUG0("saving file '" << filename << "'");
+ DEBUG0("saving file '{}'", filename);
SavePersistentImpl(os).Save(entries);
os.close();
if (os.has_error()) {
diff --git a/ntcore/src/main/native/cpp/Value.cpp b/ntcore/src/main/native/cpp/Value.cpp
index 3f58b73..e0d65c9 100644
--- a/ntcore/src/main/native/cpp/Value.cpp
+++ b/ntcore/src/main/native/cpp/Value.cpp
@@ -1,12 +1,11 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <stdint.h>
+#include <cstring>
+
#include <wpi/MemAlloc.h>
#include <wpi/timestamp.h>
@@ -22,28 +21,31 @@
Value::Value(NT_Type type, uint64_t time, const private_init&) {
m_val.type = type;
- if (time == 0)
+ if (time == 0) {
m_val.last_change = wpi::Now();
- else
+ } else {
m_val.last_change = time;
- if (m_val.type == NT_BOOLEAN_ARRAY)
+ }
+ if (m_val.type == NT_BOOLEAN_ARRAY) {
m_val.data.arr_boolean.arr = nullptr;
- else if (m_val.type == NT_DOUBLE_ARRAY)
+ } else if (m_val.type == NT_DOUBLE_ARRAY) {
m_val.data.arr_double.arr = nullptr;
- else if (m_val.type == NT_STRING_ARRAY)
+ } else if (m_val.type == NT_STRING_ARRAY) {
m_val.data.arr_string.arr = nullptr;
+ }
}
Value::~Value() {
- if (m_val.type == NT_BOOLEAN_ARRAY)
+ if (m_val.type == NT_BOOLEAN_ARRAY) {
delete[] m_val.data.arr_boolean.arr;
- else if (m_val.type == NT_DOUBLE_ARRAY)
+ } else if (m_val.type == NT_DOUBLE_ARRAY) {
delete[] m_val.data.arr_double.arr;
- else if (m_val.type == NT_STRING_ARRAY)
+ } else if (m_val.type == NT_STRING_ARRAY) {
delete[] m_val.data.arr_string.arr;
+ }
}
-std::shared_ptr<Value> Value::MakeBooleanArray(wpi::ArrayRef<bool> value,
+std::shared_ptr<Value> Value::MakeBooleanArray(wpi::span<const bool> value,
uint64_t time) {
auto val = std::make_shared<Value>(NT_BOOLEAN_ARRAY, time, private_init());
val->m_val.data.arr_boolean.arr = new int[value.size()];
@@ -52,7 +54,7 @@
return val;
}
-std::shared_ptr<Value> Value::MakeBooleanArray(wpi::ArrayRef<int> value,
+std::shared_ptr<Value> Value::MakeBooleanArray(wpi::span<const int> value,
uint64_t time) {
auto val = std::make_shared<Value>(NT_BOOLEAN_ARRAY, time, private_init());
val->m_val.data.arr_boolean.arr = new int[value.size()];
@@ -61,7 +63,7 @@
return val;
}
-std::shared_ptr<Value> Value::MakeDoubleArray(wpi::ArrayRef<double> value,
+std::shared_ptr<Value> Value::MakeDoubleArray(wpi::span<const double> value,
uint64_t time) {
auto val = std::make_shared<Value>(NT_DOUBLE_ARRAY, time, private_init());
val->m_val.data.arr_double.arr = new double[value.size()];
@@ -70,10 +72,10 @@
return val;
}
-std::shared_ptr<Value> Value::MakeStringArray(wpi::ArrayRef<std::string> value,
- uint64_t time) {
+std::shared_ptr<Value> Value::MakeStringArray(
+ wpi::span<const std::string> value, uint64_t time) {
auto val = std::make_shared<Value>(NT_STRING_ARRAY, time, private_init());
- val->m_string_array = value;
+ val->m_string_array.assign(value.begin(), value.end());
// 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();
@@ -140,8 +142,9 @@
auto v = in.GetStringArray();
out->data.arr_string.arr = static_cast<NT_String*>(
wpi::safe_malloc(v.size() * sizeof(NT_String)));
- for (size_t i = 0; i < v.size(); ++i)
+ 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();
break;
}
@@ -152,7 +155,7 @@
out->type = in.type();
}
-void nt::ConvertToC(wpi::StringRef in, NT_String* out) {
+void nt::ConvertToC(std::string_view in, NT_String* out) {
out->len = in.size();
out->str = static_cast<char*>(wpi::safe_malloc(in.size() + 1));
std::memcpy(out->str, in.data(), in.size());
@@ -174,16 +177,17 @@
case NT_RPC:
return Value::MakeRpc(ConvertFromC(value.data.v_raw));
case NT_BOOLEAN_ARRAY:
- return Value::MakeBooleanArray(wpi::ArrayRef<int>(
- value.data.arr_boolean.arr, value.data.arr_boolean.size));
+ return Value::MakeBooleanArray(
+ wpi::span(value.data.arr_boolean.arr, value.data.arr_boolean.size));
case NT_DOUBLE_ARRAY:
- return Value::MakeDoubleArray(wpi::ArrayRef<double>(
- value.data.arr_double.arr, value.data.arr_double.size));
+ return Value::MakeDoubleArray(
+ wpi::span(value.data.arr_double.arr, value.data.arr_double.size));
case NT_STRING_ARRAY: {
std::vector<std::string> v;
v.reserve(value.data.arr_string.size);
- for (size_t i = 0; i < value.data.arr_string.size; ++i)
- v.push_back(ConvertFromC(value.data.arr_string.arr[i]));
+ for (size_t i = 0; i < value.data.arr_string.size; ++i) {
+ v.emplace_back(ConvertFromC(value.data.arr_string.arr[i]));
+ }
return Value::MakeStringArray(std::move(v));
}
default:
@@ -193,7 +197,9 @@
}
bool nt::operator==(const Value& lhs, const Value& rhs) {
- if (lhs.type() != rhs.type()) return false;
+ if (lhs.type() != rhs.type()) {
+ return false;
+ }
switch (lhs.type()) {
case NT_UNASSIGNED:
return true; // XXX: is this better being false instead?
@@ -206,15 +212,17 @@
case NT_RPC:
return lhs.m_string == rhs.m_string;
case NT_BOOLEAN_ARRAY:
- if (lhs.m_val.data.arr_boolean.size != rhs.m_val.data.arr_boolean.size)
+ if (lhs.m_val.data.arr_boolean.size != rhs.m_val.data.arr_boolean.size) {
return false;
+ }
return std::memcmp(lhs.m_val.data.arr_boolean.arr,
rhs.m_val.data.arr_boolean.arr,
lhs.m_val.data.arr_boolean.size *
sizeof(lhs.m_val.data.arr_boolean.arr[0])) == 0;
case NT_DOUBLE_ARRAY:
- if (lhs.m_val.data.arr_double.size != rhs.m_val.data.arr_double.size)
+ if (lhs.m_val.data.arr_double.size != rhs.m_val.data.arr_double.size) {
return false;
+ }
return std::memcmp(lhs.m_val.data.arr_double.arr,
rhs.m_val.data.arr_double.arr,
lhs.m_val.data.arr_double.size *
diff --git a/ntcore/src/main/native/cpp/Value_internal.h b/ntcore/src/main/native/cpp/Value_internal.h
index ea25777..54850ab 100644
--- a/ntcore/src/main/native/cpp/Value_internal.h
+++ b/ntcore/src/main/native/cpp/Value_internal.h
@@ -1,17 +1,13 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_VALUE_INTERNAL_H_
#define NTCORE_VALUE_INTERNAL_H_
#include <memory>
#include <string>
-
-#include <wpi/StringRef.h>
+#include <string_view>
#include "ntcore_c.h"
@@ -21,9 +17,9 @@
void ConvertToC(const Value& in, NT_Value* out);
std::shared_ptr<Value> ConvertFromC(const NT_Value& value);
-void ConvertToC(wpi::StringRef in, NT_String* out);
-inline wpi::StringRef ConvertFromC(const NT_String& str) {
- return wpi::StringRef(str.str, str.len);
+void ConvertToC(std::string_view in, NT_String* out);
+inline std::string_view ConvertFromC(const NT_String& str) {
+ return {str.str, str.len};
}
} // namespace nt
diff --git a/ntcore/src/main/native/cpp/WireDecoder.cpp b/ntcore/src/main/native/cpp/WireDecoder.cpp
index 07c85d2..c0647dc 100644
--- a/ntcore/src/main/native/cpp/WireDecoder.cpp
+++ b/ntcore/src/main/native/cpp/WireDecoder.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "WireDecoder.h"
@@ -58,27 +55,37 @@
m_error = nullptr;
}
-WireDecoder::~WireDecoder() { std::free(m_buf); }
+WireDecoder::~WireDecoder() {
+ std::free(m_buf);
+}
bool WireDecoder::ReadDouble(double* val) {
const char* buf;
- if (!Read(&buf, 8)) return false;
+ if (!Read(&buf, 8)) {
+ return false;
+ }
*val = ::ReadDouble(buf);
return true;
}
void WireDecoder::Realloc(size_t len) {
// Double current buffer size until we have enough space.
- if (m_allocated >= len) return;
+ if (m_allocated >= len) {
+ return;
+ }
size_t newlen = m_allocated * 2;
- while (newlen < len) newlen *= 2;
+ while (newlen < len) {
+ newlen *= 2;
+ }
m_buf = static_cast<char*>(wpi::safe_realloc(m_buf, newlen));
m_allocated = newlen;
}
bool WireDecoder::ReadType(NT_Type* type) {
unsigned int itype;
- if (!Read8(&itype)) return false;
+ if (!Read8(&itype)) {
+ return false;
+ }
// Convert from byte value to enum
switch (itype) {
case 0x00:
@@ -117,17 +124,23 @@
switch (type) {
case NT_BOOLEAN: {
unsigned int v;
- if (!Read8(&v)) return nullptr;
+ if (!Read8(&v)) {
+ return nullptr;
+ }
return Value::MakeBoolean(v != 0);
}
case NT_DOUBLE: {
double v;
- if (!ReadDouble(&v)) return nullptr;
+ if (!ReadDouble(&v)) {
+ return nullptr;
+ }
return Value::MakeDouble(v);
}
case NT_STRING: {
std::string v;
- if (!ReadString(&v)) return nullptr;
+ if (!ReadString(&v)) {
+ return nullptr;
+ }
return Value::MakeString(std::move(v));
}
case NT_RAW: {
@@ -136,7 +149,9 @@
return nullptr;
}
std::string v;
- if (!ReadString(&v)) return nullptr;
+ if (!ReadString(&v)) {
+ return nullptr;
+ }
return Value::MakeRaw(std::move(v));
}
case NT_RPC: {
@@ -145,42 +160,60 @@
return nullptr;
}
std::string v;
- if (!ReadString(&v)) return nullptr;
+ if (!ReadString(&v)) {
+ return nullptr;
+ }
return Value::MakeRpc(std::move(v));
}
case NT_BOOLEAN_ARRAY: {
// size
unsigned int size;
- if (!Read8(&size)) return nullptr;
+ if (!Read8(&size)) {
+ return nullptr;
+ }
// array values
const char* buf;
- if (!Read(&buf, size)) return nullptr;
+ if (!Read(&buf, size)) {
+ return nullptr;
+ }
std::vector<int> 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: {
// size
unsigned int size;
- if (!Read8(&size)) return nullptr;
+ if (!Read8(&size)) {
+ return nullptr;
+ }
// array values
const char* buf;
- if (!Read(&buf, size * 8)) return nullptr;
+ if (!Read(&buf, size * 8)) {
+ return nullptr;
+ }
std::vector<double> 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: {
// size
unsigned int size;
- if (!Read8(&size)) return nullptr;
+ if (!Read8(&size)) {
+ return nullptr;
+ }
// array values
std::vector<std::string> v(size);
for (unsigned int i = 0; i < size; ++i) {
- if (!ReadString(&v[i])) return nullptr;
+ if (!ReadString(&v[i])) {
+ return nullptr;
+ }
}
return Value::MakeStringArray(std::move(v));
}
@@ -194,15 +227,21 @@
size_t len;
if (m_proto_rev < 0x0300u) {
unsigned int v;
- if (!Read16(&v)) return false;
+ if (!Read16(&v)) {
+ return false;
+ }
len = v;
} else {
uint64_t v;
- if (!ReadUleb128(&v)) return false;
+ if (!ReadUleb128(&v)) {
+ return false;
+ }
len = v;
}
const char* buf;
- if (!Read(&buf, len)) return false;
- *str = wpi::StringRef(buf, len);
+ if (!Read(&buf, len)) {
+ return false;
+ }
+ str->assign(buf, len);
return true;
}
diff --git a/ntcore/src/main/native/cpp/WireDecoder.h b/ntcore/src/main/native/cpp/WireDecoder.h
index 6b4483b..972be57 100644
--- a/ntcore/src/main/native/cpp/WireDecoder.h
+++ b/ntcore/src/main/native/cpp/WireDecoder.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_WIREDECODER_H_
#define NTCORE_WIREDECODER_H_
@@ -59,7 +56,9 @@
* Caution: the buffer is only temporarily valid.
*/
bool Read(const char** buf, size_t len) {
- if (len > m_allocated) Realloc(len);
+ if (len > m_allocated) {
+ Realloc(len);
+ }
*buf = m_buf;
m_is.read(m_buf, len);
#if 0
@@ -81,7 +80,9 @@
/* Reads a single byte. */
bool Read8(unsigned int* val) {
const char* buf;
- if (!Read(&buf, 1)) return false;
+ if (!Read(&buf, 1)) {
+ return false;
+ }
*val = (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
return true;
}
@@ -89,7 +90,9 @@
/* Reads a 16-bit word. */
bool Read16(unsigned int* val) {
const char* buf;
- if (!Read(&buf, 2)) return false;
+ if (!Read(&buf, 2)) {
+ return false;
+ }
unsigned int v = (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
++buf;
v <<= 8;
@@ -101,7 +104,9 @@
/* Reads a 32-bit word. */
bool Read32(uint32_t* val) {
const char* buf;
- if (!Read(&buf, 4)) return false;
+ if (!Read(&buf, 4)) {
+ return false;
+ }
unsigned int v = (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
++buf;
v <<= 8;
diff --git a/ntcore/src/main/native/cpp/WireEncoder.cpp b/ntcore/src/main/native/cpp/WireEncoder.cpp
index 6538349..b35b780 100644
--- a/ntcore/src/main/native/cpp/WireEncoder.cpp
+++ b/ntcore/src/main/native/cpp/WireEncoder.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "WireEncoder.h"
@@ -33,7 +30,9 @@
static_cast<char>((v >> 8) & 0xff), static_cast<char>(v & 0xff)});
}
-void WireEncoder::WriteUleb128(uint32_t val) { wpi::WriteUleb128(m_data, val); }
+void WireEncoder::WriteUleb128(uint32_t val) {
+ wpi::WriteUleb128(m_data, val);
+}
void WireEncoder::WriteType(NT_Type type) {
char ch;
@@ -87,29 +86,41 @@
case NT_STRING:
return GetStringSize(value.GetString());
case NT_RAW:
- if (m_proto_rev < 0x0300u) return 0;
+ if (m_proto_rev < 0x0300u) {
+ return 0;
+ }
return GetStringSize(value.GetRaw());
case NT_RPC:
- if (m_proto_rev < 0x0300u) return 0;
+ if (m_proto_rev < 0x0300u) {
+ return 0;
+ }
return GetStringSize(value.GetRpc());
case NT_BOOLEAN_ARRAY: {
// 1-byte size, 1 byte per element
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
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();
size_t size = v.size();
- if (size > 0xff) size = 0xff; // size is only 1 byte, truncate
- size_t len = 1; // 1-byte size
- for (size_t i = 0; i < size; ++i) len += GetStringSize(v[i]);
+ if (size > 0xff) {
+ size = 0xff; // size is only 1 byte, truncate
+ }
+ size_t len = 1; // 1-byte size
+ for (size_t i = 0; i < size; ++i) {
+ len += GetStringSize(v[i]);
+ }
return len;
}
default:
@@ -145,28 +156,40 @@
case NT_BOOLEAN_ARRAY: {
auto v = value.GetBooleanArray();
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 (size_t i = 0; i < size; ++i) Write8(v[i] ? 1 : 0);
+ for (size_t i = 0; i < size; ++i) {
+ Write8(v[i] ? 1 : 0);
+ }
break;
}
case NT_DOUBLE_ARRAY: {
auto v = value.GetDoubleArray();
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 (size_t i = 0; i < size; ++i) WriteDouble(v[i]);
+ for (size_t i = 0; i < size; ++i) {
+ WriteDouble(v[i]);
+ }
break;
}
case NT_STRING_ARRAY: {
auto v = value.GetStringArray();
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 (size_t i = 0; i < size; ++i) WriteString(v[i]);
+ for (size_t i = 0; i < size; ++i) {
+ WriteString(v[i]);
+ }
break;
}
default:
@@ -175,20 +198,24 @@
}
}
-size_t WireEncoder::GetStringSize(wpi::StringRef str) const {
+size_t WireEncoder::GetStringSize(std::string_view str) const {
if (m_proto_rev < 0x0300u) {
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();
}
-void WireEncoder::WriteString(wpi::StringRef str) {
+void WireEncoder::WriteString(std::string_view str) {
// length
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/ntcore/src/main/native/cpp/WireEncoder.h b/ntcore/src/main/native/cpp/WireEncoder.h
index c4f769c..84edc39 100644
--- a/ntcore/src/main/native/cpp/WireEncoder.h
+++ b/ntcore/src/main/native/cpp/WireEncoder.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_WIREENCODER_H_
#define NTCORE_WIREENCODER_H_
@@ -12,9 +9,9 @@
#include <cassert>
#include <cstddef>
+#include <string_view>
#include <wpi/SmallVector.h>
-#include <wpi/StringRef.h>
#include "networktables/NetworkTableValue.h"
@@ -52,8 +49,8 @@
/* Returns number of bytes written to memory buffer. */
size_t size() const { return m_data.size(); }
- wpi::StringRef ToStringRef() const {
- return wpi::StringRef(m_data.data(), m_data.size());
+ std::string_view ToStringView() const {
+ return {m_data.data(), m_data.size()};
}
/* Writes a single byte. */
@@ -83,7 +80,7 @@
void WriteType(NT_Type type);
void WriteValue(const Value& value);
- void WriteString(wpi::StringRef str);
+ void WriteString(std::string_view str);
/* Utility function to get the written size of a value (without actually
* writing it).
@@ -93,7 +90,7 @@
/* Utility function to get the written size of a string (without actually
* writing it).
*/
- size_t GetStringSize(wpi::StringRef str) const;
+ size_t GetStringSize(std::string_view str) const;
protected:
/* The protocol revision. E.g. 0x0200 for version 2.0. */
diff --git a/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp b/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp
index ac00a3c..6c7ed82 100644
--- a/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp
+++ b/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp
@@ -1,18 +1,14 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <jni.h>
#include <cassert>
+#include <fmt/format.h>
#include <wpi/ConvertUTF.h>
-#include <wpi/SmallString.h>
#include <wpi/jni_util.h>
-#include <wpi/raw_ostream.h>
#include "edu_wpi_first_networktables_NetworkTablesJNI.h"
#include "ntcore.h"
@@ -67,18 +63,23 @@
jvm = vm;
JNIEnv* env;
- if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
+ if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
return JNI_ERR;
+ }
// Cache references to classes
for (auto& c : classes) {
*c.cls = JClass(env, c.name);
- if (!*c.cls) return JNI_ERR;
+ if (!*c.cls) {
+ return JNI_ERR;
+ }
}
for (auto& c : exceptions) {
*c.cls = JException(env, c.name);
- if (!*c.cls) return JNI_ERR;
+ if (!*c.cls) {
+ return JNI_ERR;
+ }
}
return JNI_VERSION_1_6;
@@ -86,8 +87,9 @@
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved) {
JNIEnv* env;
- if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
+ if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
return;
+ }
// Delete global references
for (auto& c : classes) {
c.cls->free(env);
@@ -107,40 +109,52 @@
inline std::shared_ptr<nt::Value> FromJavaRaw(JNIEnv* env, jbyteArray jarr,
jlong time) {
CriticalJByteArrayRef ref{env, jarr};
- if (!ref) return nullptr;
- return nt::Value::MakeRaw(ref, time);
+ if (!ref) {
+ return nullptr;
+ }
+ return nt::Value::MakeRaw(ref.str(), time);
}
inline std::shared_ptr<nt::Value> FromJavaRawBB(JNIEnv* env, jobject jbb,
int len, jlong time) {
JByteArrayRef ref{env, jbb, len};
- if (!ref) return nullptr;
+ if (!ref) {
+ return nullptr;
+ }
return nt::Value::MakeRaw(ref.str(), time);
}
inline std::shared_ptr<nt::Value> FromJavaRpc(JNIEnv* env, jbyteArray jarr,
jlong time) {
CriticalJByteArrayRef ref{env, jarr};
- if (!ref) return nullptr;
+ if (!ref) {
+ return nullptr;
+ }
return nt::Value::MakeRpc(ref.str(), time);
}
std::shared_ptr<nt::Value> FromJavaBooleanArray(JNIEnv* env, jbooleanArray jarr,
jlong time) {
CriticalJBooleanArrayRef ref{env, jarr};
- if (!ref) return nullptr;
- wpi::ArrayRef<jboolean> elements{ref};
+ if (!ref) {
+ return nullptr;
+ }
+ wpi::span<const jboolean> elements{ref};
size_t len = elements.size();
std::vector<int> arr;
arr.reserve(len);
- for (size_t i = 0; i < len; ++i) arr.push_back(elements[i]);
+ for (size_t i = 0; i < len; ++i) {
+ arr.push_back(elements[i]);
+ }
return nt::Value::MakeBooleanArray(arr, time);
}
std::shared_ptr<nt::Value> FromJavaDoubleArray(JNIEnv* env, jdoubleArray jarr,
jlong time) {
CriticalJDoubleArrayRef ref{env, jarr};
- if (!ref) return nullptr;
+ if (!ref) {
+ return nullptr;
+ }
return nt::Value::MakeDoubleArray(ref, time);
}
@@ -152,8 +166,10 @@
for (size_t i = 0; i < len; ++i) {
JLocal<jstring> elem{
env, static_cast<jstring>(env->GetObjectArrayElement(jarr, i))};
- if (!elem) return nullptr;
- arr.push_back(JStringRef{env, elem}.str());
+ if (!elem) {
+ return nullptr;
+ }
+ arr.emplace_back(JStringRef{env, elem}.str());
}
return nt::Value::MakeStringArray(std::move(arr), time);
}
@@ -165,18 +181,20 @@
static jobject MakeJObject(JNIEnv* env, const nt::Value& value) {
static jmethodID booleanConstructor = nullptr;
static jmethodID doubleConstructor = nullptr;
- if (!booleanConstructor)
+ if (!booleanConstructor) {
booleanConstructor = env->GetMethodID(booleanCls, "<init>", "(Z)V");
- if (!doubleConstructor)
+ }
+ if (!doubleConstructor) {
doubleConstructor = env->GetMethodID(doubleCls, "<init>", "(D)V");
+ }
switch (value.type()) {
case NT_BOOLEAN:
return env->NewObject(booleanCls, booleanConstructor,
- (jboolean)(value.GetBoolean() ? 1 : 0));
+ static_cast<jboolean>(value.GetBoolean() ? 1 : 0));
case NT_DOUBLE:
return env->NewObject(doubleCls, doubleConstructor,
- (jdouble)value.GetDouble());
+ static_cast<jdouble>(value.GetDouble()));
case NT_STRING:
return MakeJString(env, value.GetString());
case NT_RAW:
@@ -197,11 +215,14 @@
static jobject MakeJValue(JNIEnv* env, const nt::Value* value) {
static jmethodID constructor =
env->GetMethodID(valueCls, "<init>", "(ILjava/lang/Object;J)V");
- if (!value)
- return env->NewObject(valueCls, constructor, (jint)NT_UNASSIGNED, nullptr,
- (jlong)0);
- return env->NewObject(valueCls, constructor, (jint)value->type(),
- MakeJObject(env, *value), (jlong)value->time());
+ if (!value) {
+ return env->NewObject(valueCls, constructor,
+ static_cast<jint>(NT_UNASSIGNED), nullptr,
+ static_cast<jlong>(0));
+ }
+ return env->NewObject(valueCls, constructor, static_cast<jint>(value->type()),
+ MakeJObject(env, *value),
+ static_cast<jlong>(value->time()));
}
static jobject MakeJObject(JNIEnv* env, const nt::ConnectionInfo& info) {
@@ -211,8 +232,9 @@
JLocal<jstring> remote_id{env, MakeJString(env, info.remote_id)};
JLocal<jstring> remote_ip{env, MakeJString(env, info.remote_ip)};
return env->NewObject(connectionInfoCls, constructor, remote_id.obj(),
- remote_ip.obj(), (jint)info.remote_port,
- (jlong)info.last_update, (jint)info.protocol_version);
+ remote_ip.obj(), static_cast<jint>(info.remote_port),
+ static_cast<jlong>(info.last_update),
+ static_cast<jint>(info.protocol_version));
}
static jobject MakeJObject(JNIEnv* env, jobject inst,
@@ -223,8 +245,9 @@
"networktables/ConnectionInfo;)V");
JLocal<jobject> conn{env, MakeJObject(env, notification.conn)};
return env->NewObject(connectionNotificationCls, constructor, inst,
- (jint)notification.listener,
- (jboolean)notification.connected, conn.obj());
+ static_cast<jint>(notification.listener),
+ static_cast<jboolean>(notification.connected),
+ conn.obj());
}
static jobject MakeJObject(JNIEnv* env, jobject inst,
@@ -234,9 +257,10 @@
"(Ledu/wpi/first/networktables/"
"NetworkTableInstance;ILjava/lang/String;IIJ)V");
JLocal<jstring> name{env, MakeJString(env, info.name)};
- return env->NewObject(entryInfoCls, constructor, inst, (jint)info.entry,
- name.obj(), (jint)info.type, (jint)info.flags,
- (jlong)info.last_change);
+ return env->NewObject(
+ entryInfoCls, constructor, inst, static_cast<jint>(info.entry),
+ name.obj(), static_cast<jint>(info.type), static_cast<jint>(info.flags),
+ static_cast<jlong>(info.last_change));
}
static jobject MakeJObject(JNIEnv* env, jobject inst,
@@ -248,8 +272,9 @@
JLocal<jstring> name{env, MakeJString(env, notification.name)};
JLocal<jobject> value{env, MakeJValue(env, notification.value.get())};
return env->NewObject(entryNotificationCls, constructor, inst,
- (jint)notification.listener, (jint)notification.entry,
- name.obj(), value.obj(), (jint)notification.flags);
+ static_cast<jint>(notification.listener),
+ static_cast<jint>(notification.entry), name.obj(),
+ value.obj(), static_cast<jint>(notification.flags));
}
static jobject MakeJObject(JNIEnv* env, jobject inst,
@@ -260,9 +285,10 @@
"String;ILjava/lang/String;)V");
JLocal<jstring> filename{env, MakeJString(env, msg.filename)};
JLocal<jstring> message{env, MakeJString(env, msg.message)};
- return env->NewObject(logMessageCls, constructor, inst, (jint)msg.logger,
- (jint)msg.level, filename.obj(), (jint)msg.line,
- message.obj());
+ return env->NewObject(logMessageCls, constructor, inst,
+ static_cast<jint>(msg.logger),
+ static_cast<jint>(msg.level), filename.obj(),
+ static_cast<jint>(msg.line), message.obj());
}
static jobject MakeJObject(JNIEnv* env, jobject inst,
@@ -275,16 +301,19 @@
JLocal<jstring> name{env, MakeJString(env, answer.name)};
JLocal<jbyteArray> params{env, MakeJByteArray(env, answer.params)};
JLocal<jobject> conn{env, MakeJObject(env, answer.conn)};
- return env->NewObject(rpcAnswerCls, constructor, inst, (jint)answer.entry,
- (jint)answer.call, name.obj(), params.obj(),
- conn.obj());
+ return env->NewObject(
+ rpcAnswerCls, constructor, inst, static_cast<jint>(answer.entry),
+ static_cast<jint>(answer.call), name.obj(), params.obj(), conn.obj());
}
-static jobjectArray MakeJObject(JNIEnv* env, jobject inst,
- wpi::ArrayRef<nt::ConnectionNotification> arr) {
+static jobjectArray MakeJObject(
+ JNIEnv* env, jobject inst,
+ wpi::span<const nt::ConnectionNotification> arr) {
jobjectArray jarr =
env->NewObjectArray(arr.size(), connectionNotificationCls, nullptr);
- if (!jarr) return nullptr;
+ if (!jarr) {
+ return nullptr;
+ }
for (size_t i = 0; i < arr.size(); ++i) {
JLocal<jobject> elem{env, MakeJObject(env, inst, arr[i])};
env->SetObjectArrayElement(jarr, i, elem.obj());
@@ -293,10 +322,12 @@
}
static jobjectArray MakeJObject(JNIEnv* env, jobject inst,
- wpi::ArrayRef<nt::EntryNotification> arr) {
+ wpi::span<const nt::EntryNotification> arr) {
jobjectArray jarr =
env->NewObjectArray(arr.size(), entryNotificationCls, nullptr);
- if (!jarr) return nullptr;
+ if (!jarr) {
+ return nullptr;
+ }
for (size_t i = 0; i < arr.size(); ++i) {
JLocal<jobject> elem{env, MakeJObject(env, inst, arr[i])};
env->SetObjectArrayElement(jarr, i, elem.obj());
@@ -305,9 +336,11 @@
}
static jobjectArray MakeJObject(JNIEnv* env, jobject inst,
- wpi::ArrayRef<nt::LogMessage> arr) {
+ wpi::span<const nt::LogMessage> arr) {
jobjectArray jarr = env->NewObjectArray(arr.size(), logMessageCls, nullptr);
- if (!jarr) return nullptr;
+ if (!jarr) {
+ return nullptr;
+ }
for (size_t i = 0; i < arr.size(); ++i) {
JLocal<jobject> elem{env, MakeJObject(env, inst, arr[i])};
env->SetObjectArrayElement(jarr, i, elem.obj());
@@ -316,9 +349,11 @@
}
static jobjectArray MakeJObject(JNIEnv* env, jobject inst,
- wpi::ArrayRef<nt::RpcAnswer> arr) {
+ wpi::span<const nt::RpcAnswer> arr) {
jobjectArray jarr = env->NewObjectArray(arr.size(), rpcAnswerCls, nullptr);
- if (!jarr) return nullptr;
+ if (!jarr) {
+ return nullptr;
+ }
for (size_t i = 0; i < arr.size(); ++i) {
JLocal<jobject> elem{env, MakeJObject(env, inst, arr[i])};
env->SetObjectArrayElement(jarr, i, elem.obj());
@@ -516,7 +551,9 @@
return false;
}
auto v = FromJavaRaw(env, value, time);
- if (!v) return false;
+ if (!v) {
+ return false;
+ }
if (force) {
nt::SetEntryTypeValue(entry, v);
return JNI_TRUE;
@@ -539,7 +576,9 @@
return false;
}
auto v = FromJavaRawBB(env, value, len, time);
- if (!v) return false;
+ if (!v) {
+ return false;
+ }
if (force) {
nt::SetEntryTypeValue(entry, v);
return JNI_TRUE;
@@ -562,7 +601,9 @@
return false;
}
auto v = FromJavaBooleanArray(env, value, time);
- if (!v) return false;
+ if (!v) {
+ return false;
+ }
if (force) {
nt::SetEntryTypeValue(entry, v);
return JNI_TRUE;
@@ -585,7 +626,9 @@
return false;
}
auto v = FromJavaDoubleArray(env, value, time);
- if (!v) return false;
+ if (!v) {
+ return false;
+ }
if (force) {
nt::SetEntryTypeValue(entry, v);
return JNI_TRUE;
@@ -608,7 +651,9 @@
return false;
}
auto v = FromJavaStringArray(env, value, time);
- if (!v) return false;
+ if (!v) {
+ return false;
+ }
if (force) {
nt::SetEntryTypeValue(entry, v);
return JNI_TRUE;
@@ -639,7 +684,9 @@
(JNIEnv*, jclass, jint entry, jboolean defaultValue)
{
auto val = nt::GetEntryValue(entry);
- if (!val || !val->IsBoolean()) return defaultValue;
+ if (!val || !val->IsBoolean()) {
+ return defaultValue;
+ }
return val->GetBoolean();
}
@@ -653,7 +700,9 @@
(JNIEnv*, jclass, jint entry, jdouble defaultValue)
{
auto val = nt::GetEntryValue(entry);
- if (!val || !val->IsDouble()) return defaultValue;
+ if (!val || !val->IsDouble()) {
+ return defaultValue;
+ }
return val->GetDouble();
}
@@ -667,7 +716,9 @@
(JNIEnv* env, jclass, jint entry, jstring defaultValue)
{
auto val = nt::GetEntryValue(entry);
- if (!val || !val->IsString()) return defaultValue;
+ if (!val || !val->IsString()) {
+ return defaultValue;
+ }
return MakeJString(env, val->GetString());
}
@@ -681,7 +732,9 @@
(JNIEnv* env, jclass, jint entry, jbyteArray defaultValue)
{
auto val = nt::GetEntryValue(entry);
- if (!val || !val->IsRaw()) return defaultValue;
+ if (!val || !val->IsRaw()) {
+ return defaultValue;
+ }
return MakeJByteArray(env, val->GetRaw());
}
@@ -695,7 +748,9 @@
(JNIEnv* env, jclass, jint entry, jbooleanArray defaultValue)
{
auto val = nt::GetEntryValue(entry);
- if (!val || !val->IsBooleanArray()) return defaultValue;
+ if (!val || !val->IsBooleanArray()) {
+ return defaultValue;
+ }
return MakeJBooleanArray(env, val->GetBooleanArray());
}
@@ -709,7 +764,9 @@
(JNIEnv* env, jclass, jint entry, jdoubleArray defaultValue)
{
auto val = nt::GetEntryValue(entry);
- if (!val || !val->IsDoubleArray()) return defaultValue;
+ if (!val || !val->IsDoubleArray()) {
+ return defaultValue;
+ }
return MakeJDoubleArray(env, val->GetDoubleArray());
}
@@ -723,7 +780,9 @@
(JNIEnv* env, jclass, jint entry, jobjectArray defaultValue)
{
auto val = nt::GetEntryValue(entry);
- if (!val || !val->IsStringArray()) return defaultValue;
+ if (!val || !val->IsStringArray()) {
+ return defaultValue;
+ }
return MakeJStringArray(env, val->GetStringArray());
}
@@ -914,7 +973,9 @@
}
auto arr = nt::GetEntryInfo(inst, JStringRef{env, prefix}.str(), types);
jobjectArray jarr = env->NewObjectArray(arr.size(), entryInfoCls, nullptr);
- if (!jarr) return nullptr;
+ if (!jarr) {
+ return nullptr;
+ }
for (size_t i = 0; i < arr.size(); ++i) {
JLocal<jobject> jelem{env, MakeJObject(env, instObject, arr[i])};
env->SetObjectArrayElement(jarr, i, jelem);
@@ -1190,7 +1251,7 @@
nullPointerEx.Throw(env, "def cannot be null");
return;
}
- nt::CreatePolledRpc(entry, JByteArrayRef{env, def}, poller);
+ nt::CreatePolledRpc(entry, JByteArrayRef{env, def}.str(), poller);
}
/*
@@ -1265,7 +1326,7 @@
nullPointerEx.Throw(env, "result cannot be null");
return false;
}
- return nt::PostRpcResponse(entry, call, JByteArrayRef{env, result});
+ return nt::PostRpcResponse(entry, call, JByteArrayRef{env, result}.str());
}
/*
@@ -1281,7 +1342,7 @@
nullPointerEx.Throw(env, "params cannot be null");
return 0;
}
- return nt::CallRpc(entry, JByteArrayRef{env, params});
+ return nt::CallRpc(entry, JByteArrayRef{env, params}.str());
}
/*
@@ -1294,7 +1355,9 @@
(JNIEnv* env, jclass, jint entry, jint call)
{
std::string result;
- if (!nt::GetRpcResult(entry, call, &result)) return nullptr;
+ if (!nt::GetRpcResult(entry, call, &result)) {
+ return nullptr;
+ }
return MakeJByteArray(env, result);
}
@@ -1309,8 +1372,9 @@
{
std::string result;
bool timed_out = false;
- if (!nt::GetRpcResult(entry, call, &result, timeout, &timed_out))
+ if (!nt::GetRpcResult(entry, call, &result, timeout, &timed_out)) {
return nullptr;
+ }
return MakeJByteArray(env, result);
}
@@ -1336,7 +1400,9 @@
(JNIEnv* env, jclass, jint entry, jbyteArray defaultValue)
{
auto val = nt::GetEntryValue(entry);
- if (!val || !val->IsRpc()) return defaultValue;
+ if (!val || !val->IsRpc()) {
+ return defaultValue;
+ }
return MakeJByteArray(env, val->GetRpc());
}
@@ -1478,10 +1544,12 @@
return;
}
jint* portInts = env->GetIntArrayElements(ports, nullptr);
- if (!portInts) return;
+ if (!portInts) {
+ return;
+ }
std::vector<std::string> names;
- std::vector<std::pair<nt::StringRef, unsigned int>> servers;
+ std::vector<std::pair<std::string_view, unsigned int>> servers;
names.reserve(len);
servers.reserve(len);
for (int i = 0; i < len; ++i) {
@@ -1493,7 +1561,7 @@
}
names.emplace_back(JStringRef{env, elem}.str());
servers.emplace_back(
- std::make_pair(nt::StringRef(names.back()), portInts[i]));
+ std::make_pair(std::string_view{names.back()}, portInts[i]));
}
env->ReleaseIntArrayElements(ports, portInts, JNI_ABORT);
nt::StartClient(inst, servers);
@@ -1563,10 +1631,12 @@
return;
}
jint* portInts = env->GetIntArrayElements(ports, nullptr);
- if (!portInts) return;
+ if (!portInts) {
+ return;
+ }
std::vector<std::string> names;
- std::vector<std::pair<nt::StringRef, unsigned int>> servers;
+ std::vector<std::pair<std::string_view, unsigned int>> servers;
names.reserve(len);
servers.reserve(len);
for (int i = 0; i < len; ++i) {
@@ -1578,7 +1648,7 @@
}
names.emplace_back(JStringRef{env, elem}.str());
servers.emplace_back(
- std::make_pair(nt::StringRef(names.back()), portInts[i]));
+ std::make_pair(std::string_view{names.back()}, portInts[i]));
}
env->ReleaseIntArrayElements(ports, portInts, JNI_ABORT);
nt::SetServer(inst, servers);
@@ -1656,7 +1726,9 @@
auto arr = nt::GetConnections(inst);
jobjectArray jarr =
env->NewObjectArray(arr.size(), connectionInfoCls, nullptr);
- if (!jarr) return nullptr;
+ if (!jarr) {
+ return nullptr;
+ }
for (size_t i = 0; i < arr.size(); ++i) {
JLocal<jobject> jelem{env, MakeJObject(env, arr[i])};
env->SetObjectArrayElement(jarr, i, jelem);
@@ -1690,7 +1762,9 @@
return;
}
const char* err = nt::SavePersistent(inst, JStringRef{env, filename}.str());
- if (err) persistentEx.Throw(env, err);
+ if (err) {
+ persistentEx.Throw(env, err);
+ }
}
/*
@@ -1707,13 +1781,10 @@
return nullptr;
}
std::vector<std::string> warns;
- const char* err = nt::LoadPersistent(inst, JStringRef{env, filename}.str(),
- [&](size_t line, const char* msg) {
- wpi::SmallString<128> warn;
- wpi::raw_svector_ostream oss(warn);
- oss << line << ": " << msg;
- warns.emplace_back(oss.str());
- });
+ const char* err = nt::LoadPersistent(
+ inst, JStringRef{env, filename}.str(), [&](size_t line, const char* msg) {
+ warns.emplace_back(fmt::format("{}: {}", line, msg));
+ });
if (err) {
persistentEx.Throw(env, err);
return nullptr;
@@ -1740,7 +1811,9 @@
}
const char* err = nt::SaveEntries(inst, JStringRef{env, filename}.str(),
JStringRef{env, prefix}.str());
- if (err) persistentEx.Throw(env, err);
+ if (err) {
+ persistentEx.Throw(env, err);
+ }
}
/*
@@ -1761,14 +1834,11 @@
return nullptr;
}
std::vector<std::string> warns;
- const char* err = nt::LoadEntries(inst, JStringRef{env, filename}.str(),
- JStringRef{env, prefix}.str(),
- [&](size_t line, const char* msg) {
- wpi::SmallString<128> warn;
- wpi::raw_svector_ostream oss(warn);
- oss << line << ": " << msg;
- warns.emplace_back(oss.str());
- });
+ const char* err = nt::LoadEntries(
+ inst, JStringRef{env, filename}.str(), JStringRef{env, prefix}.str(),
+ [&](size_t line, const char* msg) {
+ warns.emplace_back(fmt::format("{}: {}", line, msg));
+ });
if (err) {
persistentEx.Throw(env, err);
return nullptr;
diff --git a/ntcore/src/main/native/cpp/networktables/NTSendable.cpp b/ntcore/src/main/native/cpp/networktables/NTSendable.cpp
new file mode 100644
index 0000000..211fcc5
--- /dev/null
+++ b/ntcore/src/main/native/cpp/networktables/NTSendable.cpp
@@ -0,0 +1,17 @@
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
+
+#include "networktables/NTSendable.h"
+
+#include <wpi/sendable/SendableBuilder.h>
+
+#include "networktables/NTSendableBuilder.h"
+
+using namespace nt;
+
+void NTSendable::InitSendable(wpi::SendableBuilder& builder) {
+ if (builder.GetBackendKind() == wpi::SendableBuilder::kNetworkTables) {
+ InitSendable(static_cast<NTSendableBuilder&>(builder));
+ }
+}
diff --git a/ntcore/src/main/native/cpp/networktables/NTSendableBuilder.cpp b/ntcore/src/main/native/cpp/networktables/NTSendableBuilder.cpp
new file mode 100644
index 0000000..df9fc47
--- /dev/null
+++ b/ntcore/src/main/native/cpp/networktables/NTSendableBuilder.cpp
@@ -0,0 +1,11 @@
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
+
+#include "networktables/NTSendableBuilder.h"
+
+using namespace nt;
+
+NTSendableBuilder::BackendKind NTSendableBuilder::GetBackendKind() const {
+ return kNetworkTables;
+}
diff --git a/ntcore/src/main/native/cpp/networktables/NetworkTable.cpp b/ntcore/src/main/native/cpp/networktables/NetworkTable.cpp
index ca6e8a6..cc519f5 100644
--- a/ntcore/src/main/native/cpp/networktables/NetworkTable.cpp
+++ b/ntcore/src/main/native/cpp/networktables/NetworkTable.cpp
@@ -1,77 +1,64 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "networktables/NetworkTable.h"
#include <algorithm>
+#include <fmt/core.h>
+#include <fmt/format.h>
#include <wpi/SmallString.h>
+#include <wpi/StringExtras.h>
#include <wpi/StringMap.h>
-#include <wpi/raw_ostream.h>
#include "networktables/NetworkTableInstance.h"
#include "ntcore.h"
-#include "tables/ITableListener.h"
using namespace nt;
-#ifdef __GNUC__
-#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
-#elif _WIN32
-#pragma warning(disable : 4996)
-#endif
-
-const char NetworkTable::PATH_SEPARATOR_CHAR = '/';
-std::string NetworkTable::s_persistent_filename = "networktables.ini";
-bool NetworkTable::s_client = false;
-bool NetworkTable::s_enable_ds = true;
-bool NetworkTable::s_running = false;
-unsigned int NetworkTable::s_port = NT_DEFAULT_PORT;
-
-StringRef NetworkTable::BasenameKey(StringRef key) {
+std::string_view NetworkTable::BasenameKey(std::string_view key) {
size_t slash = key.rfind(PATH_SEPARATOR_CHAR);
- if (slash == StringRef::npos) return key;
+ if (slash == std::string_view::npos) {
+ return key;
+ }
return key.substr(slash + 1);
}
-std::string NetworkTable::NormalizeKey(const Twine& key,
+std::string NetworkTable::NormalizeKey(std::string_view key,
bool withLeadingSlash) {
wpi::SmallString<128> buf;
- return NormalizeKey(key, buf, withLeadingSlash);
+ return std::string{NormalizeKey(key, buf, withLeadingSlash)};
}
-StringRef NetworkTable::NormalizeKey(const Twine& key,
- wpi::SmallVectorImpl<char>& buf,
- bool withLeadingSlash) {
+std::string_view NetworkTable::NormalizeKey(std::string_view key,
+ wpi::SmallVectorImpl<char>& buf,
+ bool withLeadingSlash) {
buf.clear();
- if (withLeadingSlash) buf.push_back(PATH_SEPARATOR_CHAR);
+ if (withLeadingSlash) {
+ buf.push_back(PATH_SEPARATOR_CHAR);
+ }
// for each path element, add it with a slash following
- wpi::SmallString<128> keyBuf;
- StringRef keyStr = key.toStringRef(keyBuf);
- wpi::SmallVector<StringRef, 16> parts;
- keyStr.split(parts, PATH_SEPARATOR_CHAR, -1, false);
+ wpi::SmallVector<std::string_view, 16> parts;
+ wpi::split(key, parts, PATH_SEPARATOR_CHAR, -1, false);
for (auto i = parts.begin(); i != parts.end(); ++i) {
buf.append(i->begin(), i->end());
buf.push_back(PATH_SEPARATOR_CHAR);
}
// remove trailing slash if the input key didn't have one
- if (!keyStr.empty() && keyStr.back() != PATH_SEPARATOR_CHAR) buf.pop_back();
- return StringRef(buf.data(), buf.size());
+ if (!key.empty() && key.back() != PATH_SEPARATOR_CHAR) {
+ buf.pop_back();
+ }
+ return {buf.data(), buf.size()};
}
-std::vector<std::string> NetworkTable::GetHierarchy(const Twine& key) {
+std::vector<std::string> NetworkTable::GetHierarchy(std::string_view key) {
std::vector<std::string> hierarchy;
hierarchy.emplace_back(1, PATH_SEPARATOR_CHAR);
// for each path element, add it to the end of what we built previously
- wpi::SmallString<128> keyBuf;
- StringRef keyStr = key.toStringRef(keyBuf);
wpi::SmallString<128> path;
- wpi::SmallVector<StringRef, 16> parts;
- keyStr.split(parts, PATH_SEPARATOR_CHAR, -1, false);
+ wpi::SmallVector<std::string_view, 16> parts;
+ wpi::split(key, parts, PATH_SEPARATOR_CHAR, -1, false);
if (!parts.empty()) {
for (auto i = parts.begin(); i != parts.end(); ++i) {
path += PATH_SEPARATOR_CHAR;
@@ -79,7 +66,7 @@
hierarchy.emplace_back(path.str());
}
// handle trailing slash
- if (keyStr.back() == PATH_SEPARATOR_CHAR) {
+ if (key.back() == PATH_SEPARATOR_CHAR) {
path += PATH_SEPARATOR_CHAR;
hierarchy.emplace_back(path.str());
}
@@ -87,129 +74,27 @@
return hierarchy;
}
-void NetworkTable::Initialize() {
- if (s_running) Shutdown();
- auto inst = NetworkTableInstance::GetDefault();
- if (s_client) {
- inst.StartClient();
- if (s_enable_ds) inst.StartDSClient(s_port);
- } else {
- inst.StartServer(s_persistent_filename, "", s_port);
- }
- s_running = true;
-}
-
-void NetworkTable::Shutdown() {
- if (!s_running) return;
- auto inst = NetworkTableInstance::GetDefault();
- if (s_client) {
- inst.StopDSClient();
- inst.StopClient();
- } else {
- inst.StopServer();
- }
- s_running = false;
-}
-
-void NetworkTable::SetClientMode() { s_client = true; }
-
-void NetworkTable::SetServerMode() { s_client = false; }
-
-void NetworkTable::SetTeam(int team) {
- auto inst = NetworkTableInstance::GetDefault();
- inst.SetServerTeam(team, s_port);
- if (s_enable_ds) inst.StartDSClient(s_port);
-}
-
-void NetworkTable::SetIPAddress(StringRef address) {
- auto inst = NetworkTableInstance::GetDefault();
- wpi::SmallString<32> addr_copy{address};
- inst.SetServer(addr_copy.c_str(), s_port);
-
- // Stop the DS client if we're explicitly connecting to localhost
- if (address == "localhost" || address == "127.0.0.1")
- inst.StopDSClient();
- else if (s_enable_ds)
- inst.StartDSClient(s_port);
-}
-
-void NetworkTable::SetIPAddress(ArrayRef<std::string> addresses) {
- auto inst = NetworkTableInstance::GetDefault();
- wpi::SmallVector<StringRef, 8> servers;
- for (const auto& ip_address : addresses) servers.emplace_back(ip_address);
- inst.SetServer(servers, s_port);
-
- // Stop the DS client if we're explicitly connecting to localhost
- if (!addresses.empty() &&
- (addresses[0] == "localhost" || addresses[0] == "127.0.0.1"))
- inst.StopDSClient();
- else if (s_enable_ds)
- inst.StartDSClient(s_port);
-}
-
-void NetworkTable::SetPort(unsigned int port) { s_port = port; }
-
-void NetworkTable::SetDSClientEnabled(bool enabled) {
- auto inst = NetworkTableInstance::GetDefault();
- s_enable_ds = enabled;
- if (s_enable_ds)
- inst.StartDSClient(s_port);
- else
- inst.StopDSClient();
-}
-
-void NetworkTable::SetPersistentFilename(StringRef filename) {
- s_persistent_filename = filename;
-}
-
-void NetworkTable::SetNetworkIdentity(StringRef name) {
- NetworkTableInstance::GetDefault().SetNetworkIdentity(name);
-}
-
-void NetworkTable::GlobalDeleteAll() {
- NetworkTableInstance::GetDefault().DeleteAllEntries();
-}
-
-void NetworkTable::Flush() { NetworkTableInstance::GetDefault().Flush(); }
-
-void NetworkTable::SetUpdateRate(double interval) {
- NetworkTableInstance::GetDefault().SetUpdateRate(interval);
-}
-
-const char* NetworkTable::SavePersistent(StringRef filename) {
- return NetworkTableInstance::GetDefault().SavePersistent(filename);
-}
-
-const char* NetworkTable::LoadPersistent(
- StringRef filename,
- std::function<void(size_t line, const char* msg)> warn) {
- return NetworkTableInstance::GetDefault().LoadPersistent(filename, warn);
-}
-
-std::shared_ptr<NetworkTable> NetworkTable::GetTable(StringRef key) {
- if (!s_running) Initialize();
- return NetworkTableInstance::GetDefault().GetTable(key);
-}
-
-NetworkTable::NetworkTable(NT_Inst inst, const Twine& path, const private_init&)
- : m_inst(inst), m_path(path.str()) {}
+NetworkTable::NetworkTable(NT_Inst inst, std::string_view path,
+ const private_init&)
+ : m_inst(inst), m_path(path) {}
NetworkTable::~NetworkTable() {
- for (auto& i : m_listeners) RemoveEntryListener(i.second);
- for (auto i : m_lambdaListeners) RemoveEntryListener(i);
+ for (auto i : m_listeners) {
+ RemoveEntryListener(i);
+ }
}
NetworkTableInstance NetworkTable::GetInstance() const {
return NetworkTableInstance{m_inst};
}
-NetworkTableEntry NetworkTable::GetEntry(const Twine& key) const {
- wpi::SmallString<128> keyBuf;
- StringRef keyStr = key.toStringRef(keyBuf);
+NetworkTableEntry NetworkTable::GetEntry(std::string_view key) const {
std::scoped_lock lock(m_mutex);
- NT_Entry& entry = m_entries[keyStr];
+ NT_Entry& entry = m_entries[key];
if (entry == 0) {
- entry = nt::GetEntry(m_inst, m_path + Twine(PATH_SEPARATOR_CHAR) + keyStr);
+ fmt::memory_buffer buf;
+ fmt::format_to(fmt::appender{buf}, "{}/{}", m_path, key);
+ entry = nt::GetEntry(m_inst, {buf.data(), buf.size()});
}
return NetworkTableEntry{entry};
}
@@ -218,17 +103,19 @@
unsigned int flags) const {
size_t prefix_len = m_path.size() + 1;
return nt::AddEntryListener(
- m_inst, m_path + Twine(PATH_SEPARATOR_CHAR),
+ m_inst, fmt::format("{}/", m_path),
[=](const EntryNotification& event) {
- StringRef relative_key = event.name.substr(prefix_len);
- if (relative_key.find(PATH_SEPARATOR_CHAR) != StringRef::npos) return;
+ auto relative_key = std::string_view{event.name}.substr(prefix_len);
+ if (relative_key.find(PATH_SEPARATOR_CHAR) != std::string_view::npos) {
+ return;
+ }
listener(const_cast<NetworkTable*>(this), relative_key,
NetworkTableEntry{event.entry}, event.value, event.flags);
},
flags);
}
-NT_EntryListener NetworkTable::AddEntryListener(const Twine& key,
+NT_EntryListener NetworkTable::AddEntryListener(std::string_view key,
TableEntryListener listener,
unsigned int flags) const {
size_t prefix_len = m_path.size() + 1;
@@ -236,8 +123,9 @@
return nt::AddEntryListener(
entry.GetHandle(),
[=](const EntryNotification& event) {
- listener(const_cast<NetworkTable*>(this), event.name.substr(prefix_len),
- entry, event.value, event.flags);
+ listener(const_cast<NetworkTable*>(this),
+ std::string_view{event.name}.substr(prefix_len), entry,
+ event.value, event.flags);
},
flags);
}
@@ -246,60 +134,6 @@
nt::RemoveEntryListener(listener);
}
-void NetworkTable::AddTableListener(ITableListener* listener) {
- AddTableListenerEx(listener, NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
-}
-
-void NetworkTable::AddTableListener(ITableListener* listener,
- bool immediateNotify) {
- unsigned int flags = NT_NOTIFY_NEW | NT_NOTIFY_UPDATE;
- if (immediateNotify) flags |= NT_NOTIFY_IMMEDIATE;
- AddTableListenerEx(listener, flags);
-}
-
-void NetworkTable::AddTableListenerEx(ITableListener* listener,
- unsigned int flags) {
- std::scoped_lock lock(m_mutex);
- wpi::SmallString<128> path(m_path);
- path += PATH_SEPARATOR_CHAR;
- size_t prefix_len = path.size();
- NT_EntryListener id = nt::AddEntryListener(
- m_inst, path,
- [=](const EntryNotification& event) {
- StringRef relative_key = event.name.substr(prefix_len);
- if (relative_key.find(PATH_SEPARATOR_CHAR) != StringRef::npos) return;
- listener->ValueChangedEx(this, relative_key, event.value, event.flags);
- },
- flags);
- m_listeners.emplace_back(listener, id);
-}
-
-void NetworkTable::AddTableListener(StringRef key, ITableListener* listener,
- bool immediateNotify) {
- unsigned int flags = NT_NOTIFY_NEW | NT_NOTIFY_UPDATE;
- if (immediateNotify) flags |= NT_NOTIFY_IMMEDIATE;
- AddTableListenerEx(key, listener, flags);
-}
-
-void NetworkTable::AddTableListenerEx(StringRef key, ITableListener* listener,
- unsigned int flags) {
- std::scoped_lock lock(m_mutex);
- size_t prefix_len = m_path.size() + 1;
- auto entry = GetEntry(key);
- NT_EntryListener id = nt::AddEntryListener(
- entry.GetHandle(),
- [=](const EntryNotification& event) {
- listener->ValueChangedEx(this, event.name.substr(prefix_len),
- event.value, event.flags);
- },
- flags);
- m_listeners.emplace_back(listener, id);
-}
-
-void NetworkTable::AddSubTableListener(ITableListener* listener) {
- AddSubTableListener(listener, false);
-}
-
NT_EntryListener NetworkTable::AddSubTableListener(TableListener listener,
bool localNotify) {
size_t prefix_len = m_path.size() + 1;
@@ -309,99 +143,64 @@
auto notified_tables = std::make_shared<wpi::StringMap<char>>();
unsigned int flags = NT_NOTIFY_NEW | NT_NOTIFY_IMMEDIATE;
- if (localNotify) flags |= NT_NOTIFY_LOCAL;
+ if (localNotify) {
+ flags |= NT_NOTIFY_LOCAL;
+ }
NT_EntryListener id = nt::AddEntryListener(
- m_inst, m_path + Twine(PATH_SEPARATOR_CHAR),
+ m_inst, fmt::format("{}/", m_path),
[=](const EntryNotification& event) {
- StringRef relative_key = event.name.substr(prefix_len);
+ auto relative_key = std::string_view{event.name}.substr(prefix_len);
auto end_sub_table = relative_key.find(PATH_SEPARATOR_CHAR);
- if (end_sub_table == StringRef::npos) return;
- StringRef sub_table_key = relative_key.substr(0, end_sub_table);
- if (notified_tables->find(sub_table_key) == notified_tables->end())
+ if (end_sub_table == std::string_view::npos) {
return;
+ }
+ auto sub_table_key = relative_key.substr(0, end_sub_table);
+ if (notified_tables->find(sub_table_key) == notified_tables->end()) {
+ return;
+ }
notified_tables->insert(std::make_pair(sub_table_key, '\0'));
listener(this, sub_table_key, this->GetSubTable(sub_table_key));
},
flags);
- m_lambdaListeners.emplace_back(id);
+ m_listeners.emplace_back(id);
return id;
}
void NetworkTable::RemoveTableListener(NT_EntryListener listener) {
nt::RemoveEntryListener(listener);
auto matches_begin =
- std::remove(m_lambdaListeners.begin(), m_lambdaListeners.end(), listener);
- m_lambdaListeners.erase(matches_begin, m_lambdaListeners.end());
-}
-
-void NetworkTable::AddSubTableListener(ITableListener* listener,
- bool localNotify) {
- std::scoped_lock lock(m_mutex);
- size_t prefix_len = m_path.size() + 1;
-
- // The lambda needs to be copyable, but StringMap is not, so use
- // a shared_ptr to it.
- auto notified_tables = std::make_shared<wpi::StringMap<char>>();
-
- unsigned int flags = NT_NOTIFY_NEW | NT_NOTIFY_IMMEDIATE;
- if (localNotify) flags |= NT_NOTIFY_LOCAL;
- NT_EntryListener id = nt::AddEntryListener(
- m_inst, m_path + Twine(PATH_SEPARATOR_CHAR),
- [=](const EntryNotification& event) {
- StringRef relative_key = event.name.substr(prefix_len);
- auto end_sub_table = relative_key.find(PATH_SEPARATOR_CHAR);
- if (end_sub_table == StringRef::npos) return;
- StringRef sub_table_key = relative_key.substr(0, end_sub_table);
- if (notified_tables->find(sub_table_key) == notified_tables->end())
- return;
- notified_tables->insert(std::make_pair(sub_table_key, '\0'));
- listener->ValueChangedEx(this, sub_table_key, nullptr, event.flags);
- },
- flags);
- m_listeners.emplace_back(listener, id);
-}
-
-void NetworkTable::RemoveTableListener(ITableListener* listener) {
- std::scoped_lock lock(m_mutex);
- auto matches_begin =
- std::remove_if(m_listeners.begin(), m_listeners.end(),
- [=](const Listener& x) { return x.first == listener; });
-
- for (auto i = matches_begin; i != m_listeners.end(); ++i)
- RemoveEntryListener(i->second);
+ std::remove(m_listeners.begin(), m_listeners.end(), listener);
m_listeners.erase(matches_begin, m_listeners.end());
}
std::shared_ptr<NetworkTable> NetworkTable::GetSubTable(
- const Twine& key) const {
+ std::string_view key) const {
return std::make_shared<NetworkTable>(
- m_inst, m_path + Twine(PATH_SEPARATOR_CHAR) + key, private_init{});
+ m_inst, fmt::format("{}/{}", m_path, key), private_init{});
}
-bool NetworkTable::ContainsKey(const Twine& key) const {
- if (key.isTriviallyEmpty() ||
- (key.isSingleStringRef() && key.getSingleStringRef().empty()))
+bool NetworkTable::ContainsKey(std::string_view key) const {
+ if (key.empty()) {
return false;
+ }
return GetEntry(key).Exists();
}
-bool NetworkTable::ContainsSubTable(const Twine& key) const {
- return !GetEntryInfo(m_inst,
- m_path + Twine(PATH_SEPARATOR_CHAR) + key +
- Twine(PATH_SEPARATOR_CHAR),
- 0)
- .empty();
+bool NetworkTable::ContainsSubTable(std::string_view key) const {
+ return !GetEntryInfo(m_inst, fmt::format("{}/{}/", m_path, key), 0).empty();
}
std::vector<std::string> NetworkTable::GetKeys(int types) const {
std::vector<std::string> keys;
size_t prefix_len = m_path.size() + 1;
- auto infos = GetEntryInfo(m_inst, m_path + Twine(PATH_SEPARATOR_CHAR), types);
+ auto infos = GetEntryInfo(m_inst, fmt::format("{}/", m_path), types);
std::scoped_lock lock(m_mutex);
for (auto& info : infos) {
- auto relative_key = StringRef(info.name).substr(prefix_len);
- if (relative_key.find(PATH_SEPARATOR_CHAR) != StringRef::npos) continue;
- keys.push_back(relative_key);
+ auto relative_key = std::string_view{info.name}.substr(prefix_len);
+ if (relative_key.find(PATH_SEPARATOR_CHAR) != std::string_view::npos) {
+ continue;
+ }
+ keys.emplace_back(relative_key);
m_entries[relative_key] = info.entry;
}
return keys;
@@ -410,155 +209,167 @@
std::vector<std::string> NetworkTable::GetSubTables() const {
std::vector<std::string> keys;
size_t prefix_len = m_path.size() + 1;
- for (auto& entry :
- GetEntryInfo(m_inst, m_path + Twine(PATH_SEPARATOR_CHAR), 0)) {
- auto relative_key = StringRef(entry.name).substr(prefix_len);
+ for (auto& entry : GetEntryInfo(m_inst, fmt::format("{}/", m_path), 0)) {
+ auto relative_key = std::string_view{entry.name}.substr(prefix_len);
size_t end_subtable = relative_key.find(PATH_SEPARATOR_CHAR);
- if (end_subtable == StringRef::npos) continue;
- keys.push_back(relative_key.substr(0, end_subtable));
+ if (end_subtable == std::string_view::npos) {
+ continue;
+ }
+ keys.emplace_back(relative_key.substr(0, end_subtable));
}
return keys;
}
-void NetworkTable::SetPersistent(StringRef key) {
+void NetworkTable::SetPersistent(std::string_view key) {
GetEntry(key).SetPersistent();
}
-void NetworkTable::ClearPersistent(StringRef key) {
+void NetworkTable::ClearPersistent(std::string_view key) {
GetEntry(key).ClearPersistent();
}
-bool NetworkTable::IsPersistent(StringRef key) const {
+bool NetworkTable::IsPersistent(std::string_view key) const {
return GetEntry(key).IsPersistent();
}
-void NetworkTable::SetFlags(StringRef key, unsigned int flags) {
+void NetworkTable::SetFlags(std::string_view key, unsigned int flags) {
GetEntry(key).SetFlags(flags);
}
-void NetworkTable::ClearFlags(StringRef key, unsigned int flags) {
+void NetworkTable::ClearFlags(std::string_view key, unsigned int flags) {
GetEntry(key).ClearFlags(flags);
}
-unsigned int NetworkTable::GetFlags(StringRef key) const {
+unsigned int NetworkTable::GetFlags(std::string_view key) const {
return GetEntry(key).GetFlags();
}
-void NetworkTable::Delete(const Twine& key) { GetEntry(key).Delete(); }
+void NetworkTable::Delete(std::string_view key) {
+ GetEntry(key).Delete();
+}
-bool NetworkTable::PutNumber(StringRef key, double value) {
+bool NetworkTable::PutNumber(std::string_view key, double value) {
return GetEntry(key).SetDouble(value);
}
-bool NetworkTable::SetDefaultNumber(StringRef key, double defaultValue) {
+bool NetworkTable::SetDefaultNumber(std::string_view key, double defaultValue) {
return GetEntry(key).SetDefaultDouble(defaultValue);
}
-double NetworkTable::GetNumber(StringRef key, double defaultValue) const {
+double NetworkTable::GetNumber(std::string_view key,
+ double defaultValue) const {
return GetEntry(key).GetDouble(defaultValue);
}
-bool NetworkTable::PutString(StringRef key, StringRef value) {
+bool NetworkTable::PutString(std::string_view key, std::string_view value) {
return GetEntry(key).SetString(value);
}
-bool NetworkTable::SetDefaultString(StringRef key, StringRef defaultValue) {
+bool NetworkTable::SetDefaultString(std::string_view key,
+ std::string_view defaultValue) {
return GetEntry(key).SetDefaultString(defaultValue);
}
-std::string NetworkTable::GetString(StringRef key,
- StringRef defaultValue) const {
+std::string NetworkTable::GetString(std::string_view key,
+ std::string_view defaultValue) const {
return GetEntry(key).GetString(defaultValue);
}
-bool NetworkTable::PutBoolean(StringRef key, bool value) {
+bool NetworkTable::PutBoolean(std::string_view key, bool value) {
return GetEntry(key).SetBoolean(value);
}
-bool NetworkTable::SetDefaultBoolean(StringRef key, bool defaultValue) {
+bool NetworkTable::SetDefaultBoolean(std::string_view key, bool defaultValue) {
return GetEntry(key).SetDefaultBoolean(defaultValue);
}
-bool NetworkTable::GetBoolean(StringRef key, bool defaultValue) const {
+bool NetworkTable::GetBoolean(std::string_view key, bool defaultValue) const {
return GetEntry(key).GetBoolean(defaultValue);
}
-bool NetworkTable::PutBooleanArray(StringRef key, ArrayRef<int> value) {
+bool NetworkTable::PutBooleanArray(std::string_view key,
+ wpi::span<const int> value) {
return GetEntry(key).SetBooleanArray(value);
}
-bool NetworkTable::SetDefaultBooleanArray(StringRef key,
- ArrayRef<int> defaultValue) {
+bool NetworkTable::SetDefaultBooleanArray(std::string_view key,
+ wpi::span<const int> defaultValue) {
return GetEntry(key).SetDefaultBooleanArray(defaultValue);
}
std::vector<int> NetworkTable::GetBooleanArray(
- StringRef key, ArrayRef<int> defaultValue) const {
+ std::string_view key, wpi::span<const int> defaultValue) const {
return GetEntry(key).GetBooleanArray(defaultValue);
}
-bool NetworkTable::PutNumberArray(StringRef key, ArrayRef<double> value) {
+bool NetworkTable::PutNumberArray(std::string_view key,
+ wpi::span<const double> value) {
return GetEntry(key).SetDoubleArray(value);
}
-bool NetworkTable::SetDefaultNumberArray(StringRef key,
- ArrayRef<double> defaultValue) {
+bool NetworkTable::SetDefaultNumberArray(std::string_view key,
+ wpi::span<const double> defaultValue) {
return GetEntry(key).SetDefaultDoubleArray(defaultValue);
}
std::vector<double> NetworkTable::GetNumberArray(
- StringRef key, ArrayRef<double> defaultValue) const {
+ std::string_view key, wpi::span<const double> defaultValue) const {
return GetEntry(key).GetDoubleArray(defaultValue);
}
-bool NetworkTable::PutStringArray(StringRef key, ArrayRef<std::string> value) {
+bool NetworkTable::PutStringArray(std::string_view key,
+ wpi::span<const std::string> value) {
return GetEntry(key).SetStringArray(value);
}
-bool NetworkTable::SetDefaultStringArray(StringRef key,
- ArrayRef<std::string> defaultValue) {
+bool NetworkTable::SetDefaultStringArray(
+ std::string_view key, wpi::span<const std::string> defaultValue) {
return GetEntry(key).SetDefaultStringArray(defaultValue);
}
std::vector<std::string> NetworkTable::GetStringArray(
- StringRef key, ArrayRef<std::string> defaultValue) const {
+ std::string_view key, wpi::span<const std::string> defaultValue) const {
return GetEntry(key).GetStringArray(defaultValue);
}
-bool NetworkTable::PutRaw(StringRef key, StringRef value) {
+bool NetworkTable::PutRaw(std::string_view key, std::string_view value) {
return GetEntry(key).SetRaw(value);
}
-bool NetworkTable::SetDefaultRaw(StringRef key, StringRef defaultValue) {
+bool NetworkTable::SetDefaultRaw(std::string_view key,
+ std::string_view defaultValue) {
return GetEntry(key).SetDefaultRaw(defaultValue);
}
-std::string NetworkTable::GetRaw(StringRef key, StringRef defaultValue) const {
+std::string NetworkTable::GetRaw(std::string_view key,
+ std::string_view defaultValue) const {
return GetEntry(key).GetRaw(defaultValue);
}
-bool NetworkTable::PutValue(const Twine& key, std::shared_ptr<Value> value) {
+bool NetworkTable::PutValue(std::string_view key,
+ std::shared_ptr<Value> value) {
return GetEntry(key).SetValue(value);
}
-bool NetworkTable::SetDefaultValue(const Twine& key,
+bool NetworkTable::SetDefaultValue(std::string_view key,
std::shared_ptr<Value> defaultValue) {
return GetEntry(key).SetDefaultValue(defaultValue);
}
-std::shared_ptr<Value> NetworkTable::GetValue(const Twine& key) const {
+std::shared_ptr<Value> NetworkTable::GetValue(std::string_view key) const {
return GetEntry(key).GetValue();
}
-StringRef NetworkTable::GetPath() const { return m_path; }
+std::string_view NetworkTable::GetPath() const {
+ return m_path;
+}
-const char* NetworkTable::SaveEntries(const Twine& filename) const {
- return nt::SaveEntries(m_inst, filename, m_path + Twine(PATH_SEPARATOR_CHAR));
+const char* NetworkTable::SaveEntries(std::string_view filename) const {
+ return nt::SaveEntries(m_inst, filename, fmt::format("{}/", m_path));
}
const char* NetworkTable::LoadEntries(
- const Twine& filename,
+ std::string_view filename,
std::function<void(size_t line, const char* msg)> warn) {
- return nt::LoadEntries(m_inst, filename, m_path + Twine(PATH_SEPARATOR_CHAR),
- warn);
+ return nt::LoadEntries(m_inst, filename, fmt::format("{}/", m_path), warn);
}
diff --git a/ntcore/src/main/native/cpp/networktables/NetworkTableEntry.cpp b/ntcore/src/main/native/cpp/networktables/NetworkTableEntry.cpp
index 5507ac0..abe64e5 100644
--- a/ntcore/src/main/native/cpp/networktables/NetworkTableEntry.cpp
+++ b/ntcore/src/main/native/cpp/networktables/NetworkTableEntry.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "networktables/NetworkTableEntry.h"
diff --git a/ntcore/src/main/native/cpp/networktables/NetworkTableInstance.cpp b/ntcore/src/main/native/cpp/networktables/NetworkTableInstance.cpp
index 018572e..4566b30 100644
--- a/ntcore/src/main/native/cpp/networktables/NetworkTableInstance.cpp
+++ b/ntcore/src/main/native/cpp/networktables/NetworkTableInstance.cpp
@@ -1,52 +1,48 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "networktables/NetworkTableInstance.h"
-#include <wpi/SmallString.h>
+#include <fmt/format.h>
+#include <wpi/SmallVector.h>
using namespace nt;
std::shared_ptr<NetworkTable> NetworkTableInstance::GetTable(
- const Twine& key) const {
- StringRef simple;
- bool isSimple = key.isSingleStringRef();
- if (isSimple) simple = key.getSingleStringRef();
- if (isSimple && (simple.empty() || simple == "/")) {
+ std::string_view key) const {
+ if (key.empty() || key == "/") {
return std::make_shared<NetworkTable>(m_handle, "",
NetworkTable::private_init{});
- } else if (isSimple && simple[0] == NetworkTable::PATH_SEPARATOR_CHAR) {
+ } else if (key.front() == NetworkTable::PATH_SEPARATOR_CHAR) {
return std::make_shared<NetworkTable>(m_handle, key,
NetworkTable::private_init{});
} else {
- return std::make_shared<NetworkTable>(
- m_handle, Twine(NetworkTable::PATH_SEPARATOR_CHAR) + key,
- NetworkTable::private_init{});
+ return std::make_shared<NetworkTable>(m_handle, fmt::format("/{}", key),
+ NetworkTable::private_init{});
}
}
-void NetworkTableInstance::StartClient(ArrayRef<StringRef> servers,
- unsigned int port) {
- wpi::SmallVector<std::pair<StringRef, unsigned int>, 8> server_ports;
- for (const auto& server : servers)
+void NetworkTableInstance::StartClient(
+ wpi::span<const std::string_view> servers, unsigned int port) {
+ wpi::SmallVector<std::pair<std::string_view, unsigned int>, 8> server_ports;
+ for (const auto& server : servers) {
server_ports.emplace_back(std::make_pair(server, port));
+ }
StartClient(server_ports);
}
-void NetworkTableInstance::SetServer(ArrayRef<StringRef> servers,
+void NetworkTableInstance::SetServer(wpi::span<const std::string_view> servers,
unsigned int port) {
- wpi::SmallVector<std::pair<StringRef, unsigned int>, 8> server_ports;
- for (const auto& server : servers)
+ wpi::SmallVector<std::pair<std::string_view, unsigned int>, 8> server_ports;
+ for (const auto& server : servers) {
server_ports.emplace_back(std::make_pair(server, port));
+ }
SetServer(server_ports);
}
NT_EntryListener NetworkTableInstance::AddEntryListener(
- const Twine& prefix,
+ std::string_view prefix,
std::function<void(const EntryNotification& event)> callback,
unsigned int flags) const {
return ::nt::AddEntryListener(m_handle, prefix, callback, flags);
diff --git a/ntcore/src/main/native/cpp/networktables/RpcCall.cpp b/ntcore/src/main/native/cpp/networktables/RpcCall.cpp
index 1149681..2192a82 100644
--- a/ntcore/src/main/native/cpp/networktables/RpcCall.cpp
+++ b/ntcore/src/main/native/cpp/networktables/RpcCall.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "networktables/RpcCall.h"
diff --git a/ntcore/src/main/native/cpp/ntcore_c.cpp b/ntcore/src/main/native/cpp/ntcore_c.cpp
index ee8eabc..d910892 100644
--- a/ntcore/src/main/native/cpp/ntcore_c.cpp
+++ b/ntcore/src/main/native/cpp/ntcore_c.cpp
@@ -1,14 +1,13 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <stdint.h>
#include <cassert>
#include <cstdlib>
+#include <cstring>
+#include <string_view>
#include <wpi/MemAlloc.h>
#include <wpi/timestamp.h>
@@ -20,9 +19,9 @@
// Conversion helpers
-static void ConvertToC(wpi::StringRef in, char** out) {
+static void ConvertToC(std::string_view in, char** out) {
*out = static_cast<char*>(wpi::safe_malloc(in.size() + 1));
- std::memmove(*out, in.data(), in.size());
+ std::memmove(*out, in.data(), in.size()); // NOLINT
(*out)[in.size()] = '\0';
}
@@ -59,14 +58,16 @@
out->num_params = in.params.size();
out->params = static_cast<NT_RpcParamDef*>(
wpi::safe_malloc(in.params.size() * sizeof(NT_RpcParamDef)));
- for (size_t i = 0; i < in.params.size(); ++i)
+ for (size_t i = 0; i < in.params.size(); ++i) {
ConvertToC(in.params[i], &out->params[i]);
+ }
out->num_results = in.results.size();
out->results = static_cast<NT_RpcResultDef*>(
wpi::safe_malloc(in.results.size() * sizeof(NT_RpcResultDef)));
- for (size_t i = 0; i < in.results.size(); ++i)
+ for (size_t i = 0; i < in.results.size(); ++i) {
ConvertToC(in.results[i], &out->results[i]);
+ }
}
static void ConvertToC(const RpcAnswer& in, NT_RpcAnswer* out) {
@@ -95,18 +96,24 @@
static void ConvertToC(const LogMessage& in, NT_LogMessage* out) {
out->logger = in.logger;
out->level = in.level;
- out->filename = in.filename;
+ ConvertToC(in.filename, &out->filename);
out->line = in.line;
ConvertToC(in.message, &out->message);
}
template <typename O, typename I>
static O* ConvertToC(const std::vector<I>& in, size_t* out_len) {
- if (!out_len) return nullptr;
+ if (!out_len) {
+ return nullptr;
+ }
*out_len = in.size();
- if (in.empty()) return nullptr;
+ if (in.empty()) {
+ return nullptr;
+ }
O* out = static_cast<O*>(wpi::safe_malloc(sizeof(O) * in.size()));
- for (size_t i = 0; i < in.size(); ++i) ConvertToC(in[i], &out[i]);
+ for (size_t i = 0; i < in.size(); ++i) {
+ ConvertToC(in[i], &out[i]);
+ }
return out;
}
@@ -115,7 +122,9 @@
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 void DisposeEntryNotification(NT_EntryNotification* info) {
std::free(info->name.str);
@@ -146,12 +155,14 @@
out.name = ConvertFromC(in.name);
out.params.reserve(in.num_params);
- for (size_t i = 0; i < in.num_params; ++i)
+ for (size_t i = 0; i < in.num_params; ++i) {
out.params.push_back(ConvertFromC(in.params[i]));
+ }
out.results.reserve(in.num_results);
- for (size_t i = 0; i < in.num_results; ++i)
+ for (size_t i = 0; i < in.num_results; ++i) {
out.results.push_back(ConvertFromC(in.results[i]));
+ }
return out;
}
@@ -162,11 +173,17 @@
* Instance Functions
*/
-NT_Inst NT_GetDefaultInstance(void) { return nt::GetDefaultInstance(); }
+NT_Inst NT_GetDefaultInstance(void) {
+ return nt::GetDefaultInstance();
+}
-NT_Inst NT_CreateInstance(void) { return nt::CreateInstance(); }
+NT_Inst NT_CreateInstance(void) {
+ return nt::CreateInstance();
+}
-void NT_DestroyInstance(NT_Inst inst) { return nt::DestroyInstance(inst); }
+void NT_DestroyInstance(NT_Inst inst) {
+ return nt::DestroyInstance(inst);
+}
NT_Inst NT_GetInstanceFromHandle(NT_Handle handle) {
return nt::GetInstanceFromHandle(handle);
@@ -177,14 +194,16 @@
*/
NT_Entry NT_GetEntry(NT_Inst inst, const char* name, size_t name_len) {
- return nt::GetEntry(inst, StringRef(name, name_len));
+ return nt::GetEntry(inst, {name, name_len});
}
NT_Entry* NT_GetEntries(NT_Inst inst, const char* prefix, size_t prefix_len,
unsigned int types, size_t* count) {
- auto info_v = nt::GetEntries(inst, StringRef(prefix, prefix_len), types);
+ auto info_v = nt::GetEntries(inst, {prefix, prefix_len}, types);
*count = info_v.size();
- if (info_v.size() == 0) return nullptr;
+ if (info_v.size() == 0) {
+ return nullptr;
+ }
// create array and copy into it
NT_Entry* info = static_cast<NT_Entry*>(
@@ -200,7 +219,9 @@
return v_name.str;
}
-enum NT_Type NT_GetEntryType(NT_Entry entry) { return nt::GetEntryType(entry); }
+enum NT_Type NT_GetEntryType(NT_Entry entry) {
+ return nt::GetEntryType(entry);
+}
uint64_t NT_GetEntryLastChange(NT_Entry entry) {
return nt::GetEntryLastChange(entry);
@@ -209,7 +230,9 @@
void NT_GetEntryValue(NT_Entry entry, struct NT_Value* value) {
NT_InitValue(value);
auto v = nt::GetEntryValue(entry);
- if (!v) return;
+ if (!v) {
+ return;
+ }
ConvertToC(*v, value);
}
@@ -234,20 +257,26 @@
return nt::GetEntryFlags(entry);
}
-void NT_DeleteEntry(NT_Entry entry) { nt::DeleteEntry(entry); }
+void NT_DeleteEntry(NT_Entry entry) {
+ nt::DeleteEntry(entry);
+}
-void NT_DeleteAllEntries(NT_Inst inst) { nt::DeleteAllEntries(inst); }
+void NT_DeleteAllEntries(NT_Inst inst) {
+ nt::DeleteAllEntries(inst);
+}
struct NT_EntryInfo* NT_GetEntryInfo(NT_Inst inst, const char* prefix,
size_t prefix_len, unsigned int types,
size_t* count) {
- auto info_v = nt::GetEntryInfo(inst, StringRef(prefix, prefix_len), types);
+ auto info_v = nt::GetEntryInfo(inst, {prefix, prefix_len}, types);
return ConvertToC<NT_EntryInfo>(info_v, count);
}
NT_Bool NT_GetEntryInfoHandle(NT_Entry entry, struct NT_EntryInfo* info) {
auto info_v = nt::GetEntryInfo(entry);
- if (info_v.name.empty()) return false;
+ if (info_v.name.empty()) {
+ return false;
+ }
ConvertToC(info_v, info);
return true;
}
@@ -261,7 +290,7 @@
NT_EntryListenerCallback callback,
unsigned int flags) {
return nt::AddEntryListener(
- inst, StringRef(prefix, prefix_len),
+ inst, {prefix, prefix_len},
[=](const EntryNotification& event) {
NT_EntryNotification c_event;
ConvertToC(event, &c_event);
@@ -297,8 +326,7 @@
const char* prefix,
size_t prefix_len,
unsigned int flags) {
- return nt::AddPolledEntryListener(poller, StringRef(prefix, prefix_len),
- flags);
+ return nt::AddPolledEntryListener(poller, {prefix, prefix_len}, flags);
}
NT_EntryListener NT_AddPolledEntryListenerSingle(NT_EntryListenerPoller poller,
@@ -394,7 +422,7 @@
void NT_CreateRpc(NT_Entry entry, const char* def, size_t def_len, void* data,
NT_RpcCallback callback) {
- nt::CreateRpc(entry, StringRef(def, def_len), [=](const RpcAnswer& answer) {
+ nt::CreateRpc(entry, {def, def_len}, [=](const RpcAnswer& answer) {
NT_RpcAnswer answer_c;
ConvertToC(answer, &answer_c);
callback(data, &answer_c);
@@ -412,7 +440,7 @@
void NT_CreatePolledRpc(NT_Entry entry, const char* def, size_t def_len,
NT_RpcCallPoller poller) {
- nt::CreatePolledRpc(entry, StringRef(def, def_len), poller);
+ nt::CreatePolledRpc(entry, {def, def_len}, poller);
}
NT_RpcAnswer* NT_PollRpc(NT_RpcCallPoller poller, size_t* len) {
@@ -428,7 +456,9 @@
return ConvertToC<NT_RpcAnswer>(arr_cpp, len);
}
-void NT_CancelPollRpc(NT_RpcCallPoller poller) { nt::CancelPollRpc(poller); }
+void NT_CancelPollRpc(NT_RpcCallPoller poller) {
+ nt::CancelPollRpc(poller);
+}
NT_Bool NT_WaitForRpcCallQueue(NT_Inst inst, double timeout) {
return nt::WaitForRpcCallQueue(inst, timeout);
@@ -436,16 +466,18 @@
NT_Bool NT_PostRpcResponse(NT_Entry entry, NT_RpcCall call, const char* result,
size_t result_len) {
- return nt::PostRpcResponse(entry, call, StringRef(result, result_len));
+ return nt::PostRpcResponse(entry, call, {result, result_len});
}
NT_RpcCall NT_CallRpc(NT_Entry entry, const char* params, size_t params_len) {
- return nt::CallRpc(entry, StringRef(params, params_len));
+ return nt::CallRpc(entry, {params, params_len});
}
char* NT_GetRpcResult(NT_Entry entry, NT_RpcCall call, size_t* result_len) {
std::string result;
- if (!nt::GetRpcResult(entry, call, &result)) return nullptr;
+ if (!nt::GetRpcResult(entry, call, &result)) {
+ return nullptr;
+ }
// convert result
*result_len = result.size();
@@ -489,7 +521,9 @@
NT_Bool 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({packed, packed_len}, &def_v)) {
+ return 0;
+ }
// convert result
ConvertToC(def_v, def);
@@ -501,8 +535,9 @@
// create input vector
std::vector<std::shared_ptr<Value>> values_v;
values_v.reserve(values_len);
- for (size_t i = 0; i < values_len; ++i)
+ for (size_t i = 0; i < values_len; ++i) {
values_v.push_back(ConvertFromC(*values[i]));
+ }
// make the call
auto packed = nt::PackRpcValues(values_v);
@@ -516,13 +551,14 @@
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<NT_Type>(types, types_len));
- if (values_v.size() == 0) return nullptr;
+ auto values_v = nt::UnpackRpcValues({packed, packed_len}, {types, types_len});
+ if (values_v.size() == 0) {
+ return nullptr;
+ }
// create array and copy into it
NT_Value** values = static_cast<NT_Value**>(
- wpi::safe_malloc(values_v.size() * sizeof(NT_Value*)));
+ wpi::safe_malloc(values_v.size() * sizeof(NT_Value*))); // NOLINT
for (size_t i = 0; i < values_v.size(); ++i) {
values[i] = static_cast<NT_Value*>(wpi::safe_malloc(sizeof(NT_Value)));
ConvertToC(*values_v[i], values[i]);
@@ -535,25 +571,33 @@
*/
void NT_SetNetworkIdentity(NT_Inst inst, const char* name, size_t name_len) {
- nt::SetNetworkIdentity(inst, StringRef(name, name_len));
+ nt::SetNetworkIdentity(inst, {name, name_len});
}
unsigned int NT_GetNetworkMode(NT_Inst inst) {
return nt::GetNetworkMode(inst);
}
-void NT_StartLocal(NT_Inst inst) { nt::StartLocal(inst); }
+void NT_StartLocal(NT_Inst inst) {
+ nt::StartLocal(inst);
+}
-void NT_StopLocal(NT_Inst inst) { nt::StopLocal(inst); }
+void NT_StopLocal(NT_Inst inst) {
+ nt::StopLocal(inst);
+}
void NT_StartServer(NT_Inst inst, const char* persist_filename,
const char* listen_address, unsigned int port) {
nt::StartServer(inst, persist_filename, listen_address, port);
}
-void NT_StopServer(NT_Inst inst) { nt::StopServer(inst); }
+void NT_StopServer(NT_Inst inst) {
+ nt::StopServer(inst);
+}
-void NT_StartClientNone(NT_Inst inst) { nt::StartClient(inst); }
+void NT_StartClientNone(NT_Inst inst) {
+ nt::StartClient(inst);
+}
void NT_StartClient(NT_Inst inst, const char* server_name, unsigned int port) {
nt::StartClient(inst, server_name, port);
@@ -561,10 +605,11 @@
void NT_StartClientMulti(NT_Inst inst, size_t count, const char** server_names,
const unsigned int* ports) {
- std::vector<std::pair<StringRef, unsigned int>> servers;
+ std::vector<std::pair<std::string_view, unsigned int>> servers;
servers.reserve(count);
- for (size_t i = 0; i < count; ++i)
+ for (size_t i = 0; i < count; ++i) {
servers.emplace_back(std::make_pair(server_names[i], ports[i]));
+ }
nt::StartClient(inst, servers);
}
@@ -572,7 +617,9 @@
nt::StartClientTeam(inst, team, port);
}
-void NT_StopClient(NT_Inst inst) { nt::StopClient(inst); }
+void NT_StopClient(NT_Inst inst) {
+ nt::StopClient(inst);
+}
void NT_SetServer(NT_Inst inst, const char* server_name, unsigned int port) {
nt::SetServer(inst, server_name, port);
@@ -580,10 +627,11 @@
void NT_SetServerMulti(NT_Inst inst, size_t count, const char** server_names,
const unsigned int* ports) {
- std::vector<std::pair<StringRef, unsigned int>> servers;
+ std::vector<std::pair<std::string_view, unsigned int>> servers;
servers.reserve(count);
- for (size_t i = 0; i < count; ++i)
+ for (size_t i = 0; i < count; ++i) {
servers.emplace_back(std::make_pair(server_names[i], ports[i]));
+ }
nt::SetServer(inst, servers);
}
@@ -595,15 +643,21 @@
nt::StartDSClient(inst, port);
}
-void NT_StopDSClient(NT_Inst inst) { nt::StopDSClient(inst); }
+void NT_StopDSClient(NT_Inst inst) {
+ nt::StopDSClient(inst);
+}
void NT_SetUpdateRate(NT_Inst inst, double interval) {
nt::SetUpdateRate(inst, interval);
}
-void NT_Flush(NT_Inst inst) { nt::Flush(inst); }
+void NT_Flush(NT_Inst inst) {
+ nt::Flush(inst);
+}
-NT_Bool NT_IsConnected(NT_Inst inst) { return nt::IsConnected(inst); }
+NT_Bool NT_IsConnected(NT_Inst inst) {
+ return nt::IsConnected(inst);
+}
struct NT_ConnectionInfo* NT_GetConnections(NT_Inst inst, size_t* count) {
auto conn_v = nt::GetConnections(inst);
@@ -625,20 +679,22 @@
const char* NT_SaveEntries(NT_Inst inst, const char* filename,
const char* prefix, size_t prefix_len) {
- return nt::SaveEntries(inst, filename, StringRef(prefix, prefix_len));
+ return nt::SaveEntries(inst, filename, {prefix, prefix_len});
}
const char* NT_LoadEntries(NT_Inst inst, const char* filename,
const char* prefix, size_t prefix_len,
void (*warn)(size_t line, const char* msg)) {
- return nt::LoadEntries(inst, filename, StringRef(prefix, prefix_len), warn);
+ return nt::LoadEntries(inst, filename, {prefix, prefix_len}, warn);
}
/*
* Utility Functions
*/
-uint64_t NT_Now(void) { return wpi::Now(); }
+uint64_t NT_Now(void) {
+ return wpi::Now();
+}
NT_Logger NT_AddLogger(NT_Inst inst, void* data, NT_LogFunc func,
unsigned int min_level, unsigned int max_level) {
@@ -683,7 +739,9 @@
nt::CancelPollLogger(poller);
}
-void NT_RemoveLogger(NT_Logger logger) { nt::RemoveLogger(logger); }
+void NT_RemoveLogger(NT_Logger logger) {
+ nt::RemoveLogger(logger);
+}
NT_Bool NT_WaitForLoggerQueue(NT_Inst inst, double timeout) {
return nt::WaitForLoggerQueue(inst, timeout);
@@ -707,8 +765,9 @@
std::free(value->data.arr_double.arr);
break;
case NT_STRING_ARRAY: {
- for (size_t i = 0; i < value->data.arr_string.size; i++)
+ for (size_t i = 0; i < value->data.arr_string.size; i++) {
std::free(value->data.arr_string.arr[i].str);
+ }
std::free(value->data.arr_string.arr);
break;
}
@@ -735,22 +794,32 @@
str->len = 0;
}
-void NT_DisposeEntryArray(NT_Entry* arr, size_t /*count*/) { std::free(arr); }
+void NT_DisposeEntryArray(NT_Entry* arr, size_t /*count*/) {
+ std::free(arr);
+}
void NT_DisposeConnectionInfoArray(NT_ConnectionInfo* arr, size_t count) {
- for (size_t i = 0; i < count; i++) DisposeConnectionInfo(&arr[i]);
+ for (size_t i = 0; i < count; i++) {
+ DisposeConnectionInfo(&arr[i]);
+ }
std::free(arr);
}
void NT_DisposeEntryInfoArray(NT_EntryInfo* arr, size_t count) {
- for (size_t i = 0; i < count; i++) DisposeEntryInfo(&arr[i]);
+ for (size_t i = 0; i < count; i++) {
+ DisposeEntryInfo(&arr[i]);
+ }
std::free(arr);
}
-void NT_DisposeEntryInfo(NT_EntryInfo* info) { DisposeEntryInfo(info); }
+void NT_DisposeEntryInfo(NT_EntryInfo* info) {
+ DisposeEntryInfo(info);
+}
void NT_DisposeEntryNotificationArray(NT_EntryNotification* arr, size_t count) {
- for (size_t i = 0; i < count; i++) DisposeEntryNotification(&arr[i]);
+ for (size_t i = 0; i < count; i++) {
+ DisposeEntryNotification(&arr[i]);
+ }
std::free(arr);
}
@@ -760,7 +829,9 @@
void NT_DisposeConnectionNotificationArray(NT_ConnectionNotification* arr,
size_t count) {
- for (size_t i = 0; i < count; i++) DisposeConnectionNotification(&arr[i]);
+ for (size_t i = 0; i < count; i++) {
+ DisposeConnectionNotification(&arr[i]);
+ }
std::free(arr);
}
@@ -769,11 +840,16 @@
}
void NT_DisposeLogMessageArray(NT_LogMessage* arr, size_t count) {
- for (size_t i = 0; i < count; i++) NT_DisposeLogMessage(&arr[i]);
+ for (size_t i = 0; i < count; i++) {
+ NT_DisposeLogMessage(&arr[i]);
+ }
std::free(arr);
}
-void NT_DisposeLogMessage(NT_LogMessage* info) { std::free(info->message); }
+void NT_DisposeLogMessage(NT_LogMessage* info) {
+ std::free(info->filename);
+ std::free(info->message);
+}
void NT_DisposeRpcDefinition(NT_RpcDefinition* def) {
NT_DisposeString(&def->name);
@@ -786,15 +862,18 @@
def->params = nullptr;
def->num_params = 0;
- for (size_t i = 0; i < def->num_results; ++i)
+ for (size_t i = 0; i < def->num_results; ++i) {
NT_DisposeString(&def->results[i].name);
+ }
std::free(def->results);
def->results = nullptr;
def->num_results = 0;
}
void NT_DisposeRpcAnswerArray(NT_RpcAnswer* arr, size_t count) {
- for (size_t i = 0; i < count; i++) NT_DisposeRpcAnswer(&arr[i]);
+ for (size_t i = 0; i < count; i++) {
+ NT_DisposeRpcAnswer(&arr[i]);
+ }
std::free(arr);
}
@@ -834,11 +913,19 @@
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_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);
+ for (size_t i = 0; i < arr_size; i++) {
+ std::free(v_string[i].str);
+ }
std::free(v_string);
}
@@ -865,23 +952,20 @@
NT_Bool NT_SetEntryString(NT_Entry entry, uint64_t time, const char* str,
size_t str_len, NT_Bool force) {
if (force != 0) {
- nt::SetEntryTypeValue(entry,
- Value::MakeString(StringRef(str, str_len), time));
+ nt::SetEntryTypeValue(entry, Value::MakeString({str, str_len}, time));
return 1;
} else {
- return nt::SetEntryValue(entry,
- Value::MakeString(StringRef(str, str_len), time));
+ return nt::SetEntryValue(entry, Value::MakeString({str, str_len}, time));
}
}
NT_Bool NT_SetEntryRaw(NT_Entry entry, uint64_t time, const char* raw,
size_t raw_len, NT_Bool force) {
if (force != 0) {
- nt::SetEntryTypeValue(entry, Value::MakeRaw(StringRef(raw, raw_len), time));
+ nt::SetEntryTypeValue(entry, Value::MakeRaw({raw, raw_len}, time));
return 1;
} else {
- return nt::SetEntryValue(entry,
- Value::MakeRaw(StringRef(raw, raw_len), time));
+ return nt::SetEntryValue(entry, Value::MakeRaw({raw, raw_len}, time));
}
}
@@ -889,24 +973,22 @@
const NT_Bool* arr, size_t size,
NT_Bool force) {
if (force != 0) {
- nt::SetEntryTypeValue(
- entry, Value::MakeBooleanArray(wpi::makeArrayRef(arr, size), time));
+ nt::SetEntryTypeValue(entry,
+ Value::MakeBooleanArray(wpi::span(arr, size), time));
return 1;
} else {
return nt::SetEntryValue(
- entry, Value::MakeBooleanArray(wpi::makeArrayRef(arr, size), time));
+ entry, Value::MakeBooleanArray(wpi::span(arr, size), time));
}
}
NT_Bool NT_SetEntryDoubleArray(NT_Entry entry, uint64_t time, const double* arr,
size_t size, NT_Bool force) {
if (force != 0) {
- nt::SetEntryTypeValue(
- entry, Value::MakeDoubleArray(wpi::makeArrayRef(arr, size), time));
+ nt::SetEntryTypeValue(entry, Value::MakeDoubleArray({arr, size}, time));
return 1;
} else {
- return nt::SetEntryValue(
- entry, Value::MakeDoubleArray(wpi::makeArrayRef(arr, size), time));
+ return nt::SetEntryValue(entry, Value::MakeDoubleArray({arr, size}, time));
}
}
@@ -915,7 +997,9 @@
NT_Bool force) {
std::vector<std::string> v;
v.reserve(size);
- for (size_t i = 0; i < size; ++i) v.push_back(ConvertFromC(arr[i]));
+ for (size_t i = 0; i < size; ++i) {
+ v.emplace_back(ConvertFromC(arr[i]));
+ }
if (force != 0) {
nt::SetEntryTypeValue(entry, Value::MakeStringArray(std::move(v), time));
@@ -926,13 +1010,17 @@
}
enum NT_Type NT_GetValueType(const struct NT_Value* value) {
- if (!value) return NT_Type::NT_UNASSIGNED;
+ if (!value) {
+ return NT_Type::NT_UNASSIGNED;
+ }
return value->type;
}
NT_Bool NT_GetValueBoolean(const struct NT_Value* value, uint64_t* last_change,
NT_Bool* v_boolean) {
- if (!value || value->type != NT_Type::NT_BOOLEAN) return 0;
+ if (!value || value->type != NT_Type::NT_BOOLEAN) {
+ return 0;
+ }
*v_boolean = value->data.v_boolean;
*last_change = value->last_change;
return 1;
@@ -940,7 +1028,9 @@
NT_Bool NT_GetValueDouble(const struct NT_Value* value, uint64_t* last_change,
double* v_double) {
- if (!value || value->type != NT_Type::NT_DOUBLE) return 0;
+ if (!value || value->type != NT_Type::NT_DOUBLE) {
+ return 0;
+ }
*last_change = value->last_change;
*v_double = value->data.v_double;
return 1;
@@ -948,7 +1038,9 @@
char* NT_GetValueString(const struct NT_Value* value, uint64_t* last_change,
size_t* str_len) {
- if (!value || value->type != NT_Type::NT_STRING) return nullptr;
+ if (!value || value->type != NT_Type::NT_STRING) {
+ return nullptr;
+ }
*last_change = value->last_change;
*str_len = value->data.v_string.len;
char* str =
@@ -959,7 +1051,9 @@
char* NT_GetValueRaw(const struct NT_Value* value, uint64_t* last_change,
size_t* raw_len) {
- if (!value || value->type != NT_Type::NT_RAW) return nullptr;
+ if (!value || value->type != NT_Type::NT_RAW) {
+ return nullptr;
+ }
*last_change = value->last_change;
*raw_len = value->data.v_string.len;
char* raw =
@@ -970,7 +1064,9 @@
NT_Bool* NT_GetValueBooleanArray(const struct NT_Value* value,
uint64_t* last_change, size_t* arr_size) {
- if (!value || value->type != NT_Type::NT_BOOLEAN_ARRAY) return nullptr;
+ if (!value || value->type != NT_Type::NT_BOOLEAN_ARRAY) {
+ return nullptr;
+ }
*last_change = value->last_change;
*arr_size = value->data.arr_boolean.size;
NT_Bool* arr = static_cast<int*>(
@@ -982,7 +1078,9 @@
double* NT_GetValueDoubleArray(const struct NT_Value* value,
uint64_t* last_change, size_t* arr_size) {
- if (!value || value->type != NT_Type::NT_DOUBLE_ARRAY) return nullptr;
+ 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 = static_cast<double*>(
@@ -994,7 +1092,9 @@
NT_String* NT_GetValueStringArray(const struct NT_Value* value,
uint64_t* last_change, size_t* arr_size) {
- if (!value || value->type != NT_Type::NT_STRING_ARRAY) return nullptr;
+ 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*>(
@@ -1024,29 +1124,28 @@
const char* default_value,
size_t default_len) {
return nt::SetDefaultEntryValue(
- entry, Value::MakeString(StringRef(default_value, default_len), time));
+ entry, Value::MakeString({default_value, default_len}, time));
}
NT_Bool NT_SetDefaultEntryRaw(NT_Entry entry, uint64_t time,
const char* default_value, size_t default_len) {
return nt::SetDefaultEntryValue(
- entry, Value::MakeRaw(StringRef(default_value, default_len), time));
+ entry, Value::MakeRaw({default_value, default_len}, time));
}
NT_Bool NT_SetDefaultEntryBooleanArray(NT_Entry entry, uint64_t time,
const NT_Bool* default_value,
size_t default_size) {
return nt::SetDefaultEntryValue(
- entry, Value::MakeBooleanArray(
- wpi::makeArrayRef(default_value, default_size), time));
+ entry,
+ Value::MakeBooleanArray(wpi::span(default_value, default_size), time));
}
NT_Bool NT_SetDefaultEntryDoubleArray(NT_Entry entry, uint64_t time,
const double* default_value,
size_t default_size) {
return nt::SetDefaultEntryValue(
- entry, Value::MakeDoubleArray(
- wpi::makeArrayRef(default_value, default_size), time));
+ entry, Value::MakeDoubleArray({default_value, default_size}, time));
}
NT_Bool NT_SetDefaultEntryStringArray(NT_Entry entry, uint64_t time,
@@ -1054,8 +1153,9 @@
size_t default_size) {
std::vector<std::string> vec;
vec.reserve(default_size);
- for (size_t i = 0; i < default_size; ++i)
- vec.push_back(ConvertFromC(default_value[i]));
+ for (size_t i = 0; i < default_size; ++i) {
+ vec.emplace_back(ConvertFromC(default_value[i]));
+ }
return nt::SetDefaultEntryValue(entry,
Value::MakeStringArray(std::move(vec), time));
@@ -1064,7 +1164,9 @@
NT_Bool NT_GetEntryBoolean(NT_Entry entry, uint64_t* last_change,
NT_Bool* v_boolean) {
auto v = nt::GetEntryValue(entry);
- if (!v || !v->IsBoolean()) return 0;
+ if (!v || !v->IsBoolean()) {
+ return 0;
+ }
*v_boolean = v->GetBoolean();
*last_change = v->last_change();
return 1;
@@ -1073,7 +1175,9 @@
NT_Bool NT_GetEntryDouble(NT_Entry entry, uint64_t* last_change,
double* v_double) {
auto v = nt::GetEntryValue(entry);
- if (!v || !v->IsDouble()) return 0;
+ if (!v || !v->IsDouble()) {
+ return 0;
+ }
*last_change = v->last_change();
*v_double = v->GetDouble();
return 1;
@@ -1082,7 +1186,9 @@
char* NT_GetEntryString(NT_Entry entry, uint64_t* last_change,
size_t* str_len) {
auto v = nt::GetEntryValue(entry);
- if (!v || !v->IsString()) return nullptr;
+ if (!v || !v->IsString()) {
+ return nullptr;
+ }
*last_change = v->last_change();
struct NT_String v_string;
nt::ConvertToC(v->GetString(), &v_string);
@@ -1092,7 +1198,9 @@
char* NT_GetEntryRaw(NT_Entry entry, uint64_t* last_change, size_t* raw_len) {
auto v = nt::GetEntryValue(entry);
- if (!v || !v->IsRaw()) return nullptr;
+ if (!v || !v->IsRaw()) {
+ return nullptr;
+ }
*last_change = v->last_change();
struct NT_String v_raw;
nt::ConvertToC(v->GetRaw(), &v_raw);
@@ -1103,7 +1211,9 @@
NT_Bool* NT_GetEntryBooleanArray(NT_Entry entry, uint64_t* last_change,
size_t* arr_size) {
auto v = nt::GetEntryValue(entry);
- if (!v || !v->IsBooleanArray()) return nullptr;
+ if (!v || !v->IsBooleanArray()) {
+ return nullptr;
+ }
*last_change = v->last_change();
auto vArr = v->GetBooleanArray();
NT_Bool* arr =
@@ -1116,7 +1226,9 @@
double* NT_GetEntryDoubleArray(NT_Entry entry, uint64_t* last_change,
size_t* arr_size) {
auto v = nt::GetEntryValue(entry);
- if (!v || !v->IsDoubleArray()) return nullptr;
+ if (!v || !v->IsDoubleArray()) {
+ return nullptr;
+ }
*last_change = v->last_change();
auto vArr = v->GetDoubleArray();
double* arr =
@@ -1129,7 +1241,9 @@
NT_String* NT_GetEntryStringArray(NT_Entry entry, uint64_t* last_change,
size_t* arr_size) {
auto v = nt::GetEntryValue(entry);
- if (!v || !v->IsStringArray()) return nullptr;
+ if (!v || !v->IsStringArray()) {
+ return nullptr;
+ }
*last_change = v->last_change();
auto vArr = v->GetStringArray();
NT_String* arr = static_cast<NT_String*>(
diff --git a/ntcore/src/main/native/cpp/ntcore_cpp.cpp b/ntcore/src/main/native/cpp/ntcore_cpp.cpp
index ad8f68b..8616aaf 100644
--- a/ntcore/src/main/native/cpp/ntcore_cpp.cpp
+++ b/ntcore/src/main/native/cpp/ntcore_cpp.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <stdint.h>
@@ -36,15 +33,18 @@
void DestroyInstance(NT_Inst inst) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
- if (i < 0) return;
+ if (i < 0) {
+ return;
+ }
InstanceImpl::Destroy(i);
}
NT_Inst GetInstanceFromHandle(NT_Handle handle) {
Handle h{handle};
auto type = h.GetType();
- if (type >= Handle::kConnectionListener && type <= Handle::kRpcCallPoller)
+ if (type >= Handle::kConnectionListener && type <= Handle::kRpcCallPoller) {
return Handle(h.GetInst(), 0, Handle::kInstance);
+ }
return 0;
}
@@ -53,25 +53,33 @@
* Table Functions
*/
-NT_Entry GetEntry(NT_Inst inst, const Twine& name) {
+NT_Entry GetEntry(NT_Inst inst, std::string_view name) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
auto ii = InstanceImpl::Get(i);
- if (!ii) return 0;
+ if (!ii) {
+ return 0;
+ }
unsigned int id = ii->storage.GetEntry(name);
- if (id == UINT_MAX) return 0;
+ if (id == UINT_MAX) {
+ return 0;
+ }
return Handle(i, id, Handle::kEntry);
}
-std::vector<NT_Entry> GetEntries(NT_Inst inst, const Twine& prefix,
+std::vector<NT_Entry> GetEntries(NT_Inst inst, std::string_view prefix,
unsigned int types) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
auto ii = InstanceImpl::Get(i);
- if (!ii) return std::vector<NT_Entry>{};
+ if (!ii) {
+ return {};
+ }
auto arr = ii->storage.GetEntries(prefix, types);
// convert indices to handles
- for (auto& val : arr) val = Handle(i, val, Handle::kEntry);
+ for (auto& val : arr) {
+ val = Handle(i, val, Handle::kEntry);
+ }
return arr;
}
@@ -79,7 +87,9 @@
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return std::string{};
+ if (id < 0 || !ii) {
+ return {};
+ }
return ii->storage.GetEntryName(id);
}
@@ -88,7 +98,9 @@
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return NT_UNASSIGNED;
+ if (id < 0 || !ii) {
+ return NT_UNASSIGNED;
+ }
return ii->storage.GetEntryType(id);
}
@@ -97,123 +109,107 @@
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return 0;
+ if (id < 0 || !ii) {
+ return 0;
+ }
return ii->storage.GetEntryLastChange(id);
}
-std::shared_ptr<Value> GetEntryValue(StringRef name) {
- return InstanceImpl::GetDefault()->storage.GetEntryValue(name);
-}
-
std::shared_ptr<Value> GetEntryValue(NT_Entry entry) {
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return nullptr;
+ if (id < 0 || !ii) {
+ return nullptr;
+ }
return ii->storage.GetEntryValue(id);
}
-bool SetDefaultEntryValue(StringRef name, std::shared_ptr<Value> value) {
- return InstanceImpl::GetDefault()->storage.SetDefaultEntryValue(name, value);
-}
-
bool SetDefaultEntryValue(NT_Entry entry, std::shared_ptr<Value> value) {
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return false;
+ if (id < 0 || !ii) {
+ return false;
+ }
return ii->storage.SetDefaultEntryValue(id, value);
}
-bool SetEntryValue(StringRef name, std::shared_ptr<Value> value) {
- return InstanceImpl::GetDefault()->storage.SetEntryValue(name, value);
-}
-
bool SetEntryValue(NT_Entry entry, std::shared_ptr<Value> value) {
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return false;
+ if (id < 0 || !ii) {
+ return false;
+ }
return ii->storage.SetEntryValue(id, value);
}
-void SetEntryTypeValue(StringRef name, std::shared_ptr<Value> value) {
- InstanceImpl::GetDefault()->storage.SetEntryTypeValue(name, value);
-}
-
void SetEntryTypeValue(NT_Entry entry, std::shared_ptr<Value> value) {
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return;
+ if (id < 0 || !ii) {
+ return;
+ }
ii->storage.SetEntryTypeValue(id, value);
}
-void SetEntryFlags(StringRef name, unsigned int flags) {
- InstanceImpl::GetDefault()->storage.SetEntryFlags(name, flags);
-}
-
void SetEntryFlags(NT_Entry entry, unsigned int flags) {
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return;
+ if (id < 0 || !ii) {
+ return;
+ }
ii->storage.SetEntryFlags(id, flags);
}
-unsigned int GetEntryFlags(StringRef name) {
- return InstanceImpl::GetDefault()->storage.GetEntryFlags(name);
-}
-
unsigned int GetEntryFlags(NT_Entry entry) {
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return 0;
+ if (id < 0 || !ii) {
+ return 0;
+ }
return ii->storage.GetEntryFlags(id);
}
-void DeleteEntry(StringRef name) {
- InstanceImpl::GetDefault()->storage.DeleteEntry(name);
-}
-
void DeleteEntry(NT_Entry entry) {
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return;
+ if (id < 0 || !ii) {
+ return;
+ }
ii->storage.DeleteEntry(id);
}
-void DeleteAllEntries() {
- InstanceImpl::GetDefault()->storage.DeleteAllEntries();
-}
-
void DeleteAllEntries(NT_Inst inst) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
auto ii = InstanceImpl::Get(i);
- if (i < 0 || !ii) return;
+ if (i < 0 || !ii) {
+ return;
+ }
ii->storage.DeleteAllEntries();
}
-std::vector<EntryInfo> GetEntryInfo(StringRef prefix, unsigned int types) {
- return InstanceImpl::GetDefault()->storage.GetEntryInfo(0, prefix, types);
-}
-
-std::vector<EntryInfo> GetEntryInfo(NT_Inst inst, const Twine& prefix,
+std::vector<EntryInfo> GetEntryInfo(NT_Inst inst, std::string_view prefix,
unsigned int types) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
auto ii = InstanceImpl::Get(i);
- if (!ii) return std::vector<EntryInfo>{};
+ if (!ii) {
+ return {};
+ }
return ii->storage.GetEntryInfo(i, prefix, types);
}
@@ -239,24 +235,15 @@
* Callback Creation Functions
*/
-NT_EntryListener AddEntryListener(StringRef prefix,
- EntryListenerCallback callback,
- unsigned int flags) {
- return AddEntryListener(
- Handle(InstanceImpl::GetDefaultIndex(), 0, Handle::kInstance), prefix,
- [=](const EntryNotification& event) {
- callback(event.listener, event.name, event.value, event.flags);
- },
- flags);
-}
-
NT_EntryListener AddEntryListener(
- NT_Inst inst, const Twine& prefix,
+ NT_Inst inst, std::string_view prefix,
std::function<void(const EntryNotification& event)> callback,
unsigned int flags) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
auto ii = InstanceImpl::Get(i);
- if (i < 0 || !ii) return 0;
+ if (i < 0 || !ii) {
+ return 0;
+ }
unsigned int uid = ii->storage.AddListener(prefix, callback, flags);
return Handle(i, uid, Handle::kEntryListener);
@@ -270,7 +257,9 @@
int id = handle.GetTypedIndex(Handle::kEntry);
int i = handle.GetInst();
auto ii = InstanceImpl::Get(i);
- if (id < 0 || !ii) return 0;
+ if (id < 0 || !ii) {
+ return 0;
+ }
unsigned int uid = ii->storage.AddListener(id, callback, flags);
return Handle(i, uid, Handle::kEntryListener);
@@ -279,7 +268,9 @@
NT_EntryListenerPoller CreateEntryListenerPoller(NT_Inst inst) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
auto ii = InstanceImpl::Get(i);
- if (!ii) return 0;
+ if (!ii) {
+ return 0;
+ }
return Handle(i, ii->entry_notifier.CreatePoller(),
Handle::kEntryListenerPoller);
@@ -289,19 +280,23 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kEntryListenerPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return;
+ if (id < 0 || !ii) {
+ return;
+ }
ii->entry_notifier.RemovePoller(id);
}
NT_EntryListener AddPolledEntryListener(NT_EntryListenerPoller poller,
- const Twine& prefix,
+ std::string_view prefix,
unsigned int flags) {
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kEntryListenerPoller);
int i = handle.GetInst();
auto ii = InstanceImpl::Get(i);
- if (id < 0 || !ii) return 0;
+ if (id < 0 || !ii) {
+ return 0;
+ }
unsigned int uid = ii->storage.AddPolledListener(id, prefix, flags);
return Handle(i, uid, Handle::kEntryListener);
@@ -313,12 +308,18 @@
int id = handle.GetTypedIndex(Handle::kEntry);
int i = handle.GetInst();
auto ii = InstanceImpl::Get(i);
- if (id < 0 || !ii) return 0;
+ if (id < 0 || !ii) {
+ return 0;
+ }
Handle phandle{poller};
int p_id = phandle.GetTypedIndex(Handle::kEntryListenerPoller);
- if (p_id < 0) return 0;
- if (handle.GetInst() != phandle.GetInst()) return 0;
+ if (p_id < 0) {
+ return 0;
+ }
+ if (handle.GetInst() != phandle.GetInst()) {
+ return 0;
+ }
unsigned int uid = ii->storage.AddPolledListener(p_id, id, flags);
return Handle(i, uid, Handle::kEntryListener);
@@ -329,7 +330,9 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kEntryListenerPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return std::vector<EntryNotification>{};
+ if (id < 0 || !ii) {
+ return {};
+ }
return ii->entry_notifier.Poll(static_cast<unsigned int>(id));
}
@@ -341,7 +344,9 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kEntryListenerPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return std::vector<EntryNotification>{};
+ if (id < 0 || !ii) {
+ return {};
+ }
return ii->entry_notifier.Poll(static_cast<unsigned int>(id), timeout,
timed_out);
@@ -351,7 +356,9 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kEntryListenerPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return;
+ if (id < 0 || !ii) {
+ return;
+ }
ii->entry_notifier.CancelPoll(id);
}
@@ -360,7 +367,9 @@
Handle handle{entry_listener};
int uid = handle.GetTypedIndex(Handle::kEntryListener);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (uid < 0 || !ii) return;
+ if (uid < 0 || !ii) {
+ return;
+ }
ii->entry_notifier.Remove(uid);
}
@@ -368,27 +377,21 @@
bool WaitForEntryListenerQueue(NT_Inst inst, double timeout) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
auto ii = InstanceImpl::Get(i);
- if (!ii) return true;
+ if (!ii) {
+ return true;
+ }
return ii->entry_notifier.WaitForQueue(timeout);
}
-NT_ConnectionListener AddConnectionListener(ConnectionListenerCallback callback,
- bool immediate_notify) {
- return AddConnectionListener(
- Handle(InstanceImpl::GetDefaultIndex(), 0, Handle::kInstance),
- [=](const ConnectionNotification& event) {
- callback(event.listener, event.connected, event.conn);
- },
- immediate_notify);
-}
-
NT_ConnectionListener AddConnectionListener(
NT_Inst inst,
std::function<void(const ConnectionNotification& event)> callback,
bool immediate_notify) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
auto ii = InstanceImpl::Get(i);
- if (!ii) return 0;
+ if (!ii) {
+ return 0;
+ }
unsigned int uid = ii->dispatcher.AddListener(callback, immediate_notify);
return Handle(i, uid, Handle::kConnectionListener);
@@ -397,7 +400,9 @@
NT_ConnectionListenerPoller CreateConnectionListenerPoller(NT_Inst inst) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
auto ii = InstanceImpl::Get(i);
- if (!ii) return 0;
+ if (!ii) {
+ return 0;
+ }
return Handle(i, ii->connection_notifier.CreatePoller(),
Handle::kConnectionListenerPoller);
@@ -407,7 +412,9 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kConnectionListenerPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return;
+ if (id < 0 || !ii) {
+ return;
+ }
ii->connection_notifier.RemovePoller(id);
}
@@ -418,7 +425,9 @@
int id = handle.GetTypedIndex(Handle::kConnectionListenerPoller);
int i = handle.GetInst();
auto ii = InstanceImpl::Get(i);
- if (id < 0 || !ii) return 0;
+ if (id < 0 || !ii) {
+ return 0;
+ }
unsigned int uid = ii->dispatcher.AddPolledListener(id, immediate_notify);
return Handle(i, uid, Handle::kConnectionListener);
@@ -429,7 +438,9 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kConnectionListenerPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return std::vector<ConnectionNotification>{};
+ if (id < 0 || !ii) {
+ return {};
+ }
return ii->connection_notifier.Poll(static_cast<unsigned int>(id));
}
@@ -440,7 +451,9 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kConnectionListenerPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return std::vector<ConnectionNotification>{};
+ if (id < 0 || !ii) {
+ return {};
+ }
return ii->connection_notifier.Poll(static_cast<unsigned int>(id), timeout,
timed_out);
@@ -450,7 +463,9 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kConnectionListenerPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return;
+ if (id < 0 || !ii) {
+ return;
+ }
ii->connection_notifier.CancelPoll(id);
}
@@ -459,7 +474,9 @@
Handle handle{conn_listener};
int uid = handle.GetTypedIndex(Handle::kConnectionListener);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (uid < 0 || !ii) return;
+ if (uid < 0 || !ii) {
+ return;
+ }
ii->connection_notifier.Remove(uid);
}
@@ -467,7 +484,9 @@
bool WaitForConnectionListenerQueue(NT_Inst inst, double timeout) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
auto ii = InstanceImpl::Get(i);
- if (!ii) return true;
+ if (!ii) {
+ return true;
+ }
return ii->connection_notifier.WaitForQueue(timeout);
}
@@ -475,16 +494,22 @@
* Remote Procedure Call Functions
*/
-void CreateRpc(NT_Entry entry, StringRef def,
+void CreateRpc(NT_Entry entry, std::string_view def,
std::function<void(const RpcAnswer& answer)> callback) {
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return;
+ if (id < 0 || !ii) {
+ return;
+ }
// only server can create RPCs
- if ((ii->dispatcher.GetNetworkMode() & NT_NET_MODE_SERVER) == 0) return;
- if (def.empty() || !callback) return;
+ if ((ii->dispatcher.GetNetworkMode() & NT_NET_MODE_SERVER) == 0) {
+ return;
+ }
+ if (def.empty() || !callback) {
+ return;
+ }
ii->storage.CreateRpc(id, def, ii->rpc_server.Add(callback));
}
@@ -492,7 +517,9 @@
NT_RpcCallPoller CreateRpcCallPoller(NT_Inst inst) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
auto ii = InstanceImpl::Get(i);
- if (!ii) return 0;
+ if (!ii) {
+ return 0;
+ }
return Handle(i, ii->rpc_server.CreatePoller(), Handle::kRpcCallPoller);
}
@@ -501,25 +528,38 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kRpcCallPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return;
+ if (id < 0 || !ii) {
+ return;
+ }
ii->rpc_server.RemovePoller(id);
}
-void CreatePolledRpc(NT_Entry entry, StringRef def, NT_RpcCallPoller poller) {
+void CreatePolledRpc(NT_Entry entry, std::string_view def,
+ NT_RpcCallPoller poller) {
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return;
+ if (id < 0 || !ii) {
+ return;
+ }
Handle phandle{poller};
int p_id = phandle.GetTypedIndex(Handle::kRpcCallPoller);
- if (p_id < 0) return;
- if (handle.GetInst() != phandle.GetInst()) return;
+ if (p_id < 0) {
+ return;
+ }
+ if (handle.GetInst() != phandle.GetInst()) {
+ return;
+ }
// only server can create RPCs
- if ((ii->dispatcher.GetNetworkMode() & NT_NET_MODE_SERVER) == 0) return;
- if (def.empty()) return;
+ if ((ii->dispatcher.GetNetworkMode() & NT_NET_MODE_SERVER) == 0) {
+ return;
+ }
+ if (def.empty()) {
+ return;
+ }
ii->storage.CreateRpc(id, def, ii->rpc_server.AddPolled(p_id));
}
@@ -528,7 +568,9 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kRpcCallPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return std::vector<RpcAnswer>{};
+ if (id < 0 || !ii) {
+ return {};
+ }
return ii->rpc_server.Poll(static_cast<unsigned int>(id));
}
@@ -539,7 +581,9 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kRpcCallPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return std::vector<RpcAnswer>{};
+ if (id < 0 || !ii) {
+ return {};
+ }
return ii->rpc_server.Poll(static_cast<unsigned int>(id), timeout, timed_out);
}
@@ -548,7 +592,9 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kRpcCallPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return;
+ if (id < 0 || !ii) {
+ return;
+ }
ii->rpc_server.CancelPoll(id);
}
@@ -556,33 +602,45 @@
bool WaitForRpcCallQueue(NT_Inst inst, double timeout) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
auto ii = InstanceImpl::Get(i);
- if (!ii) return true;
+ if (!ii) {
+ return true;
+ }
return ii->rpc_server.WaitForQueue(timeout);
}
-bool PostRpcResponse(NT_Entry entry, NT_RpcCall call, StringRef result) {
+bool PostRpcResponse(NT_Entry entry, NT_RpcCall call, std::string_view result) {
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return false;
+ if (id < 0 || !ii) {
+ return false;
+ }
Handle chandle{call};
int call_uid = chandle.GetTypedIndex(Handle::kRpcCall);
- if (call_uid < 0) return false;
- if (handle.GetInst() != chandle.GetInst()) return false;
+ if (call_uid < 0) {
+ return false;
+ }
+ if (handle.GetInst() != chandle.GetInst()) {
+ return false;
+ }
return ii->rpc_server.PostRpcResponse(id, call_uid, result);
}
-NT_RpcCall CallRpc(NT_Entry entry, StringRef params) {
+NT_RpcCall CallRpc(NT_Entry entry, std::string_view params) {
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
int i = handle.GetInst();
auto ii = InstanceImpl::Get(i);
- if (id < 0 || !ii) return 0;
+ if (id < 0 || !ii) {
+ return 0;
+ }
unsigned int call_uid = ii->storage.CallRpc(id, params);
- if (call_uid == 0) return 0;
+ if (call_uid == 0) {
+ return 0;
+ }
return Handle(i, call_uid, Handle::kRpcCall);
}
@@ -590,12 +648,18 @@
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return false;
+ if (id < 0 || !ii) {
+ return false;
+ }
Handle chandle{call};
int call_uid = chandle.GetTypedIndex(Handle::kRpcCall);
- if (call_uid < 0) return false;
- if (handle.GetInst() != chandle.GetInst()) return false;
+ if (call_uid < 0) {
+ return false;
+ }
+ if (handle.GetInst() != chandle.GetInst()) {
+ return false;
+ }
return ii->storage.GetRpcResult(id, call_uid, result);
}
@@ -606,12 +670,18 @@
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return false;
+ if (id < 0 || !ii) {
+ return false;
+ }
Handle chandle{call};
int call_uid = chandle.GetTypedIndex(Handle::kRpcCall);
- if (call_uid < 0) return false;
- if (handle.GetInst() != chandle.GetInst()) return false;
+ if (call_uid < 0) {
+ return false;
+ }
+ if (handle.GetInst() != chandle.GetInst()) {
+ return false;
+ }
return ii->storage.GetRpcResult(id, call_uid, result, timeout, timed_out);
}
@@ -620,12 +690,18 @@
Handle handle{entry};
int id = handle.GetTypedIndex(Handle::kEntry);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return;
+ if (id < 0 || !ii) {
+ return;
+ }
Handle chandle{call};
int call_uid = chandle.GetTypedIndex(Handle::kRpcCall);
- if (call_uid < 0) return;
- if (handle.GetInst() != chandle.GetInst()) return;
+ if (call_uid < 0) {
+ return;
+ }
+ if (handle.GetInst() != chandle.GetInst()) {
+ return;
+ }
ii->storage.CancelRpcResult(id, call_uid);
}
@@ -637,7 +713,9 @@
// parameters
unsigned int params_size = def.params.size();
- if (params_size > 0xff) params_size = 0xff;
+ if (params_size > 0xff) {
+ params_size = 0xff;
+ }
enc.Write8(params_size);
for (size_t i = 0; i < params_size; ++i) {
enc.WriteType(def.params[i].def_value->type());
@@ -647,171 +725,186 @@
// results
unsigned int results_size = def.results.size();
- if (results_size > 0xff) results_size = 0xff;
+ if (results_size > 0xff) {
+ results_size = 0xff;
+ }
enc.Write8(results_size);
for (size_t i = 0; i < results_size; ++i) {
enc.WriteType(def.results[i].type);
enc.WriteString(def.results[i].name);
}
- return enc.ToStringRef();
+ return std::string{enc.ToStringView()};
}
-bool UnpackRpcDefinition(StringRef packed, RpcDefinition* def) {
+bool UnpackRpcDefinition(std::string_view packed, RpcDefinition* def) {
wpi::raw_mem_istream is(packed.data(), packed.size());
wpi::Logger logger;
WireDecoder dec(is, 0x0300, logger);
- if (!dec.Read8(&def->version)) return false;
- if (!dec.ReadString(&def->name)) return false;
+ if (!dec.Read8(&def->version)) {
+ return false;
+ }
+ if (!dec.ReadString(&def->name)) {
+ return false;
+ }
// parameters
unsigned int params_size;
- if (!dec.Read8(¶ms_size)) return false;
+ if (!dec.Read8(¶ms_size)) {
+ return false;
+ }
def->params.resize(0);
def->params.reserve(params_size);
for (size_t i = 0; i < params_size; ++i) {
RpcParamDef pdef;
NT_Type type;
- if (!dec.ReadType(&type)) return false;
- if (!dec.ReadString(&pdef.name)) return false;
+ if (!dec.ReadType(&type)) {
+ return false;
+ }
+ if (!dec.ReadString(&pdef.name)) {
+ return false;
+ }
pdef.def_value = dec.ReadValue(type);
- if (!pdef.def_value) return false;
+ if (!pdef.def_value) {
+ return false;
+ }
def->params.emplace_back(std::move(pdef));
}
// results
unsigned int results_size;
- if (!dec.Read8(&results_size)) return false;
+ if (!dec.Read8(&results_size)) {
+ return false;
+ }
def->results.resize(0);
def->results.reserve(results_size);
for (size_t i = 0; i < results_size; ++i) {
RpcResultDef rdef;
- if (!dec.ReadType(&rdef.type)) return false;
- if (!dec.ReadString(&rdef.name)) return false;
+ if (!dec.ReadType(&rdef.type)) {
+ return false;
+ }
+ if (!dec.ReadString(&rdef.name)) {
+ return false;
+ }
def->results.emplace_back(std::move(rdef));
}
return true;
}
-std::string PackRpcValues(ArrayRef<std::shared_ptr<Value>> values) {
+std::string PackRpcValues(wpi::span<const std::shared_ptr<Value>> values) {
WireEncoder enc(0x0300);
- for (auto& value : values) enc.WriteValue(*value);
- return enc.ToStringRef();
+ for (auto& value : values) {
+ enc.WriteValue(*value);
+ }
+ return std::string{enc.ToStringView()};
}
-std::vector<std::shared_ptr<Value>> UnpackRpcValues(StringRef packed,
- ArrayRef<NT_Type> types) {
+std::vector<std::shared_ptr<Value>> UnpackRpcValues(
+ std::string_view packed, wpi::span<const NT_Type> types) {
wpi::raw_mem_istream is(packed.data(), packed.size());
wpi::Logger logger;
WireDecoder dec(is, 0x0300, logger);
std::vector<std::shared_ptr<Value>> vec;
for (auto type : types) {
auto item = dec.ReadValue(type);
- if (!item) return std::vector<std::shared_ptr<Value>>();
+ if (!item) {
+ return std::vector<std::shared_ptr<Value>>();
+ }
vec.emplace_back(std::move(item));
}
return vec;
}
-uint64_t Now() { return wpi::Now(); }
+uint64_t Now() {
+ return wpi::Now();
+}
/*
* Client/Server Functions
*/
-void SetNetworkIdentity(StringRef name) {
- InstanceImpl::GetDefault()->dispatcher.SetIdentity(name);
-}
-
-void SetNetworkIdentity(NT_Inst inst, const Twine& name) {
+void SetNetworkIdentity(NT_Inst inst, std::string_view name) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->dispatcher.SetIdentity(name);
}
-unsigned int GetNetworkMode() {
- return InstanceImpl::GetDefault()->dispatcher.GetNetworkMode();
-}
-
unsigned int GetNetworkMode(NT_Inst inst) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return 0;
+ if (!ii) {
+ return 0;
+ }
return ii->dispatcher.GetNetworkMode();
}
void StartLocal(NT_Inst inst) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->dispatcher.StartLocal();
}
void StopLocal(NT_Inst inst) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->dispatcher.Stop();
}
-void StartServer(StringRef persist_filename, const char* listen_address,
- unsigned int port) {
- auto ii = InstanceImpl::GetDefault();
- ii->dispatcher.StartServer(persist_filename, listen_address, port);
-}
-
-void StartServer(NT_Inst inst, const Twine& persist_filename,
+void StartServer(NT_Inst inst, std::string_view persist_filename,
const char* listen_address, unsigned int port) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->dispatcher.StartServer(persist_filename, listen_address, port);
}
-void StopServer() { InstanceImpl::GetDefault()->dispatcher.Stop(); }
-
void StopServer(NT_Inst inst) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->dispatcher.Stop();
}
-void StartClient() { InstanceImpl::GetDefault()->dispatcher.StartClient(); }
-
void StartClient(NT_Inst inst) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->dispatcher.StartClient();
}
-void StartClient(const char* server_name, unsigned int port) {
- auto ii = InstanceImpl::GetDefault();
- ii->dispatcher.SetServer(server_name, port);
- ii->dispatcher.StartClient();
-}
-
void StartClient(NT_Inst inst, const char* server_name, unsigned int port) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->dispatcher.SetServer(server_name, port);
ii->dispatcher.StartClient();
}
-void StartClient(ArrayRef<std::pair<StringRef, unsigned int>> servers) {
- auto ii = InstanceImpl::GetDefault();
- ii->dispatcher.SetServer(servers);
- ii->dispatcher.StartClient();
-}
-
-void StartClient(NT_Inst inst,
- ArrayRef<std::pair<StringRef, unsigned int>> servers) {
+void StartClient(
+ NT_Inst inst,
+ wpi::span<const std::pair<std::string_view, unsigned int>> servers) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->dispatcher.SetServer(servers);
ii->dispatcher.StartClient();
@@ -819,105 +912,102 @@
void StartClientTeam(NT_Inst inst, unsigned int team, unsigned int port) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->dispatcher.SetServerTeam(team, port);
ii->dispatcher.StartClient();
}
-void StopClient() { InstanceImpl::GetDefault()->dispatcher.Stop(); }
-
void StopClient(NT_Inst inst) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->dispatcher.Stop();
}
-void SetServer(const char* server_name, unsigned int port) {
- InstanceImpl::GetDefault()->dispatcher.SetServer(server_name, port);
-}
-
void SetServer(NT_Inst inst, const char* server_name, unsigned int port) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->dispatcher.SetServer(server_name, port);
}
-void SetServer(ArrayRef<std::pair<StringRef, unsigned int>> servers) {
- InstanceImpl::GetDefault()->dispatcher.SetServer(servers);
-}
-
-void SetServer(NT_Inst inst,
- ArrayRef<std::pair<StringRef, unsigned int>> servers) {
+void SetServer(
+ NT_Inst inst,
+ wpi::span<const std::pair<std::string_view, unsigned int>> servers) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->dispatcher.SetServer(servers);
}
void SetServerTeam(NT_Inst inst, unsigned int team, unsigned int port) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->dispatcher.SetServerTeam(team, port);
}
-void StartDSClient(unsigned int port) {
- InstanceImpl::GetDefault()->ds_client.Start(port);
-}
-
void StartDSClient(NT_Inst inst, unsigned int port) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->ds_client.Start(port);
}
-void StopDSClient() { InstanceImpl::GetDefault()->ds_client.Stop(); }
-
void StopDSClient(NT_Inst inst) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->ds_client.Stop();
}
-void SetUpdateRate(double interval) {
- InstanceImpl::GetDefault()->dispatcher.SetUpdateRate(interval);
-}
-
void SetUpdateRate(NT_Inst inst, double interval) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->dispatcher.SetUpdateRate(interval);
}
-void Flush() { InstanceImpl::GetDefault()->dispatcher.Flush(); }
-
void Flush(NT_Inst inst) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return;
+ if (!ii) {
+ return;
+ }
ii->dispatcher.Flush();
}
-std::vector<ConnectionInfo> GetConnections() {
- return InstanceImpl::GetDefault()->dispatcher.GetConnections();
-}
-
std::vector<ConnectionInfo> GetConnections(NT_Inst inst) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return std::vector<ConnectionInfo>{};
+ if (!ii) {
+ return {};
+ }
return ii->dispatcher.GetConnections();
}
bool IsConnected(NT_Inst inst) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return false;
+ if (!ii) {
+ return false;
+ }
return ii->dispatcher.IsConnected();
}
@@ -926,79 +1016,70 @@
* Persistent Functions
*/
-const char* SavePersistent(StringRef filename) {
- return InstanceImpl::GetDefault()->storage.SavePersistent(filename, false);
-}
-
-const char* SavePersistent(NT_Inst inst, const Twine& filename) {
+const char* SavePersistent(NT_Inst inst, std::string_view filename) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return "invalid instance handle";
+ if (!ii) {
+ return "invalid instance handle";
+ }
return ii->storage.SavePersistent(filename, false);
}
const char* LoadPersistent(
- StringRef filename,
- std::function<void(size_t line, const char* msg)> warn) {
- return InstanceImpl::GetDefault()->storage.LoadPersistent(filename, warn);
-}
-
-const char* LoadPersistent(
- NT_Inst inst, const Twine& filename,
+ NT_Inst inst, std::string_view filename,
std::function<void(size_t line, const char* msg)> warn) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return "invalid instance handle";
+ if (!ii) {
+ return "invalid instance handle";
+ }
return ii->storage.LoadPersistent(filename, warn);
}
-const char* SaveEntries(NT_Inst inst, const Twine& filename,
- const Twine& prefix) {
+const char* SaveEntries(NT_Inst inst, std::string_view filename,
+ std::string_view prefix) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return "invalid instance handle";
+ if (!ii) {
+ return "invalid instance handle";
+ }
return ii->storage.SaveEntries(filename, prefix);
}
const char* LoadEntries(
- NT_Inst inst, const Twine& filename, const Twine& prefix,
+ NT_Inst inst, std::string_view filename, std::string_view prefix,
std::function<void(size_t line, const char* msg)> warn) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
- if (!ii) return "invalid instance handle";
+ if (!ii) {
+ return "invalid instance handle";
+ }
return ii->storage.LoadEntries(filename, prefix, warn);
}
-void SetLogger(LogFunc func, unsigned int min_level) {
- auto ii = InstanceImpl::GetDefault();
- static wpi::mutex mutex;
- static unsigned int logger = 0;
- std::scoped_lock lock(mutex);
- if (logger != 0) ii->logger_impl.Remove(logger);
- logger = ii->logger_impl.Add(
- [=](const LogMessage& msg) {
- func(msg.level, msg.filename, msg.line, msg.message.c_str());
- },
- min_level, UINT_MAX);
-}
-
NT_Logger AddLogger(NT_Inst inst,
std::function<void(const LogMessage& msg)> func,
- unsigned int min_level, unsigned int max_level) {
+ unsigned int minLevel, unsigned int maxLevel) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
auto ii = InstanceImpl::Get(i);
- if (!ii) return 0;
+ if (!ii) {
+ return 0;
+ }
- if (min_level < ii->logger.min_level()) ii->logger.set_min_level(min_level);
+ if (minLevel < ii->logger.min_level()) {
+ ii->logger.set_min_level(minLevel);
+ }
- return Handle(i, ii->logger_impl.Add(func, min_level, max_level),
+ return Handle(i, ii->logger_impl.Add(func, minLevel, maxLevel),
Handle::kLogger);
}
NT_LoggerPoller CreateLoggerPoller(NT_Inst inst) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
auto ii = InstanceImpl::Get(i);
- if (!ii) return 0;
+ if (!ii) {
+ return 0;
+ }
return Handle(i, ii->logger_impl.CreatePoller(), Handle::kLoggerPoller);
}
@@ -1007,7 +1088,9 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kLoggerPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return;
+ if (id < 0 || !ii) {
+ return;
+ }
ii->logger_impl.RemovePoller(id);
}
@@ -1018,9 +1101,13 @@
int id = handle.GetTypedIndex(Handle::kLoggerPoller);
int i = handle.GetInst();
auto ii = InstanceImpl::Get(i);
- if (id < 0 || !ii) return 0;
+ if (id < 0 || !ii) {
+ return 0;
+ }
- if (min_level < ii->logger.min_level()) ii->logger.set_min_level(min_level);
+ if (min_level < ii->logger.min_level()) {
+ ii->logger.set_min_level(min_level);
+ }
return Handle(i, ii->logger_impl.AddPolled(id, min_level, max_level),
Handle::kLogger);
@@ -1030,7 +1117,9 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kLoggerPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return std::vector<LogMessage>{};
+ if (id < 0 || !ii) {
+ return {};
+ }
return ii->logger_impl.Poll(static_cast<unsigned int>(id));
}
@@ -1041,7 +1130,9 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kLoggerPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return std::vector<LogMessage>{};
+ if (id < 0 || !ii) {
+ return {};
+ }
return ii->logger_impl.Poll(static_cast<unsigned int>(id), timeout,
timed_out);
@@ -1051,7 +1142,9 @@
Handle handle{poller};
int id = handle.GetTypedIndex(Handle::kLoggerPoller);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (id < 0 || !ii) return;
+ if (id < 0 || !ii) {
+ return;
+ }
ii->logger_impl.CancelPoll(id);
}
@@ -1060,7 +1153,9 @@
Handle handle{logger};
int uid = handle.GetTypedIndex(Handle::kLogger);
auto ii = InstanceImpl::Get(handle.GetInst());
- if (uid < 0 || !ii) return;
+ if (uid < 0 || !ii) {
+ return;
+ }
ii->logger_impl.Remove(uid);
ii->logger.set_min_level(ii->logger_impl.GetMinLevel());
@@ -1069,7 +1164,9 @@
bool WaitForLoggerQueue(NT_Inst inst, double timeout) {
int i = Handle{inst}.GetTypedInst(Handle::kInstance);
auto ii = InstanceImpl::Get(i);
- if (!ii) return true;
+ if (!ii) {
+ return true;
+ }
return ii->logger_impl.WaitForQueue(timeout);
}
diff --git a/ntcore/src/main/native/cpp/ntcore_test.cpp b/ntcore/src/main/native/cpp/ntcore_test.cpp
index d633fc5..64bde68 100644
--- a/ntcore/src/main/native/cpp/ntcore_test.cpp
+++ b/ntcore/src/main/native/cpp/ntcore_test.cpp
@@ -1,12 +1,11 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "ntcore_test.h"
+#include <cstring>
+
#include <wpi/MemAlloc.h>
#include "Value_internal.h"
@@ -15,7 +14,7 @@
struct NT_String* NT_GetStringForTesting(const char* string, int* struct_size) {
struct NT_String* str =
static_cast<NT_String*>(wpi::safe_calloc(1, sizeof(NT_String)));
- nt::ConvertToC(wpi::StringRef(string), str);
+ nt::ConvertToC(string, str);
*struct_size = sizeof(NT_String);
return str;
}
@@ -27,7 +26,7 @@
int* struct_size) {
struct NT_EntryInfo* entry_info =
static_cast<NT_EntryInfo*>(wpi::safe_calloc(1, sizeof(NT_EntryInfo)));
- nt::ConvertToC(wpi::StringRef(name), &entry_info->name);
+ nt::ConvertToC(name, &entry_info->name);
entry_info->type = type;
entry_info->flags = flags;
entry_info->last_change = last_change;
@@ -45,8 +44,8 @@
uint64_t last_update, unsigned int protocol_version, int* struct_size) {
struct NT_ConnectionInfo* conn_info = static_cast<NT_ConnectionInfo*>(
wpi::safe_calloc(1, sizeof(NT_ConnectionInfo)));
- nt::ConvertToC(wpi::StringRef(remote_id), &conn_info->remote_id);
- nt::ConvertToC(wpi::StringRef(remote_ip), &conn_info->remote_ip);
+ nt::ConvertToC(remote_id, &conn_info->remote_id);
+ nt::ConvertToC(remote_ip, &conn_info->remote_ip);
conn_info->remote_port = remote_port;
conn_info->last_update = last_update;
conn_info->protocol_version = protocol_version;
@@ -89,7 +88,7 @@
static_cast<NT_Value*>(wpi::safe_calloc(1, sizeof(NT_Value)));
value->type = NT_STRING;
value->last_change = last_change;
- nt::ConvertToC(wpi::StringRef(str), &value->data.v_string);
+ nt::ConvertToC(str, &value->data.v_string);
*struct_size = sizeof(NT_Value);
return value;
}
@@ -100,7 +99,7 @@
static_cast<NT_Value*>(wpi::safe_calloc(1, sizeof(NT_Value)));
value->type = NT_RAW;
value->last_change = last_change;
- nt::ConvertToC(wpi::StringRef(raw, raw_len), &value->data.v_string);
+ nt::ConvertToC(std::string_view(raw, raw_len), &value->data.v_string);
*struct_size = sizeof(NT_Value);
return value;
}
@@ -167,7 +166,7 @@
static void CopyNtString(const struct NT_String* copy_from,
struct NT_String* copy_to) {
- nt::ConvertToC(wpi::StringRef(copy_from->str, copy_from->len), copy_to);
+ nt::ConvertToC({copy_from->str, copy_from->len}, copy_to);
}
struct NT_RpcParamDef* NT_GetRpcParamDefForTesting(const char* name,
@@ -175,7 +174,7 @@
int* struct_size) {
struct NT_RpcParamDef* def =
static_cast<NT_RpcParamDef*>(wpi::safe_calloc(1, sizeof(NT_RpcParamDef)));
- nt::ConvertToC(wpi::StringRef(name), &def->name);
+ nt::ConvertToC(name, &def->name);
CopyNtValue(val, &def->def_value);
*struct_size = sizeof(NT_RpcParamDef);
return def;
@@ -192,7 +191,7 @@
int* struct_size) {
struct NT_RpcResultDef* def = static_cast<NT_RpcResultDef*>(
wpi::safe_calloc(1, sizeof(NT_RpcResultDef)));
- nt::ConvertToC(wpi::StringRef(name), &def->name);
+ nt::ConvertToC(name, &def->name);
def->type = type;
*struct_size = sizeof(NT_RpcResultDef);
return def;
@@ -210,7 +209,7 @@
struct NT_RpcDefinition* def = static_cast<NT_RpcDefinition*>(
wpi::safe_calloc(1, sizeof(NT_RpcDefinition)));
def->version = version;
- nt::ConvertToC(wpi::StringRef(name), &def->name);
+ nt::ConvertToC(name, &def->name);
def->num_params = num_params;
def->params = static_cast<NT_RpcParamDef*>(
wpi::safe_malloc(num_params * sizeof(NT_RpcParamDef)));
@@ -237,8 +236,8 @@
static_cast<NT_RpcAnswer*>(wpi::safe_calloc(1, sizeof(NT_RpcAnswer)));
info->entry = rpc_id;
info->call = call_uid;
- nt::ConvertToC(wpi::StringRef(name), &info->name);
- nt::ConvertToC(wpi::StringRef(params, params_len), &info->params);
+ nt::ConvertToC(name, &info->name);
+ nt::ConvertToC({params, params_len}, &info->params);
*struct_size = sizeof(NT_RpcAnswer);
return info;
}
diff --git a/ntcore/src/main/native/cpp/tables/ITableListener.cpp b/ntcore/src/main/native/cpp/tables/ITableListener.cpp
deleted file mode 100644
index 6abd3bb..0000000
--- a/ntcore/src/main/native/cpp/tables/ITableListener.cpp
+++ /dev/null
@@ -1,16 +0,0 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
-
-#include "tables/ITableListener.h"
-
-#include "ntcore_c.h"
-
-void ITableListener::ValueChangedEx(ITable* source, wpi::StringRef key,
- std::shared_ptr<nt::Value> value,
- unsigned int flags) {
- ValueChanged(source, key, value, (flags & NT_NOTIFY_NEW) != 0);
-}
diff --git a/ntcore/src/main/native/include/networktables/EntryListenerFlags.h b/ntcore/src/main/native/include/networktables/EntryListenerFlags.h
index c5cc620..bbf5e42 100644
--- a/ntcore/src/main/native/include/networktables/EntryListenerFlags.h
+++ b/ntcore/src/main/native/include/networktables/EntryListenerFlags.h
@@ -1,19 +1,14 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_NETWORKTABLES_ENTRYLISTENERFLAGS_H_
#define NTCORE_NETWORKTABLES_ENTRYLISTENERFLAGS_H_
#include "ntcore_c.h"
-namespace nt {
-
/** Entry listener flags */
-namespace EntryListenerFlags {
+namespace nt::EntryListenerFlags {
/**
* Flag values for use with entry listeners.
@@ -75,8 +70,6 @@
kFlags = NT_NOTIFY_FLAGS
};
-} // namespace EntryListenerFlags
-
-} // namespace nt
+} // namespace nt::EntryListenerFlags
#endif // NTCORE_NETWORKTABLES_ENTRYLISTENERFLAGS_H_
diff --git a/ntcore/src/main/native/include/networktables/NTSendable.h b/ntcore/src/main/native/include/networktables/NTSendable.h
new file mode 100644
index 0000000..88924f8
--- /dev/null
+++ b/ntcore/src/main/native/include/networktables/NTSendable.h
@@ -0,0 +1,28 @@
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
+
+#pragma once
+
+#include <wpi/sendable/Sendable.h>
+
+namespace nt {
+
+class NTSendableBuilder;
+
+/**
+ * Interface for NetworkTable Sendable objects.
+ */
+class NTSendable : public wpi::Sendable {
+ public:
+ /**
+ * Initializes this Sendable object.
+ *
+ * @param builder sendable builder
+ */
+ virtual void InitSendable(NTSendableBuilder& builder) = 0;
+
+ void InitSendable(wpi::SendableBuilder& builder) override;
+};
+
+} // namespace nt
diff --git a/ntcore/src/main/native/include/networktables/NTSendableBuilder.h b/ntcore/src/main/native/include/networktables/NTSendableBuilder.h
new file mode 100644
index 0000000..8ff265b
--- /dev/null
+++ b/ntcore/src/main/native/include/networktables/NTSendableBuilder.h
@@ -0,0 +1,65 @@
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
+
+#pragma once
+
+#include <functional>
+#include <memory>
+#include <string_view>
+
+#include <wpi/sendable/SendableBuilder.h>
+
+#include "networktables/NetworkTable.h"
+#include "networktables/NetworkTableEntry.h"
+#include "networktables/NetworkTableValue.h"
+
+namespace nt {
+
+class NTSendableBuilder : public wpi::SendableBuilder {
+ public:
+ /**
+ * Set the function that should be called to update the network table
+ * for things other than properties. Note this function is not passed
+ * the network table object; instead it should use the entry handles
+ * returned by GetEntry().
+ *
+ * @param func function
+ */
+ virtual void SetUpdateTable(std::function<void()> func) = 0;
+
+ /**
+ * Add a property without getters or setters. This can be used to get
+ * entry handles for the function called by SetUpdateTable().
+ *
+ * @param key property name
+ * @return Network table entry
+ */
+ virtual NetworkTableEntry GetEntry(std::string_view key) = 0;
+
+ /**
+ * Add a NetworkTableValue property.
+ *
+ * @param key property name
+ * @param getter getter function (returns current value)
+ * @param setter setter function (sets new value)
+ */
+ virtual void AddValueProperty(
+ std::string_view key, std::function<std::shared_ptr<Value>()> getter,
+ std::function<void(std::shared_ptr<Value>)> setter) = 0;
+
+ /**
+ * Get the network table.
+ * @return The network table
+ */
+ virtual std::shared_ptr<NetworkTable> GetTable() = 0;
+
+ /**
+ * Gets the kind of backend being used.
+ *
+ * @return Backend kind
+ */
+ BackendKind GetBackendKind() const override;
+};
+
+} // namespace nt
diff --git a/ntcore/src/main/native/include/networktables/NetworkTable.h b/ntcore/src/main/native/include/networktables/NetworkTable.h
index 3bfadbd..3a04e45 100644
--- a/ntcore/src/main/native/include/networktables/NetworkTable.h
+++ b/ntcore/src/main/native/include/networktables/NetworkTable.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_NETWORKTABLES_NETWORKTABLE_H_
#define NTCORE_NETWORKTABLES_NETWORKTABLE_H_
@@ -11,36 +8,23 @@
#include <functional>
#include <memory>
#include <string>
+#include <string_view>
#include <utility>
#include <vector>
-#include <wpi/ArrayRef.h>
#include <wpi/StringMap.h>
-#include <wpi/Twine.h>
#include <wpi/mutex.h>
+#include <wpi/span.h>
#include "networktables/NetworkTableEntry.h"
#include "networktables/TableEntryListener.h"
#include "networktables/TableListener.h"
#include "ntcore_c.h"
-#include "tables/ITable.h"
namespace nt {
-using wpi::ArrayRef;
-using wpi::StringRef;
-using wpi::Twine;
-
class NetworkTableInstance;
-#ifdef __GNUC__
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
-#elif _WIN32
-#pragma warning(push)
-#pragma warning(disable : 4996)
-#endif
-
/**
* @defgroup ntcore_cpp_api ntcore C++ object-oriented API
*
@@ -51,22 +35,13 @@
* A network table that knows its subtable path.
* @ingroup ntcore_cpp_api
*/
-class NetworkTable final : public ITable {
+class NetworkTable final {
private:
NT_Inst m_inst;
std::string m_path;
mutable wpi::mutex m_mutex;
mutable wpi::StringMap<NT_Entry> m_entries;
- typedef std::pair<ITableListener*, NT_EntryListener> Listener;
- std::vector<Listener> m_listeners;
- std::vector<NT_EntryListener> m_lambdaListeners;
-
- static std::vector<std::string> s_ip_addresses;
- static std::string s_persistent_filename;
- static bool s_client;
- static bool s_enable_ds;
- static bool s_running;
- static unsigned int s_port;
+ std::vector<NT_EntryListener> m_listeners;
struct private_init {};
friend class NetworkTableInstance;
@@ -79,7 +54,7 @@
* @param key key
* @return base name
*/
- static StringRef BasenameKey(StringRef key);
+ static std::string_view BasenameKey(std::string_view key);
/**
* Normalizes an network table key to contain no consecutive slashes and
@@ -97,12 +72,12 @@
* with a leading slash
* @return normalized key
*/
- static std::string NormalizeKey(const Twine& key,
+ static std::string NormalizeKey(std::string_view key,
bool withLeadingSlash = true);
- static StringRef NormalizeKey(const Twine& key,
- wpi::SmallVectorImpl<char>& buf,
- bool withLeadingSlash = true);
+ static std::string_view NormalizeKey(std::string_view key,
+ wpi::SmallVectorImpl<char>& buf,
+ bool withLeadingSlash = true);
/**
* Gets a list of the names of all the super tables of a given key. For
@@ -112,13 +87,13 @@
* @param key the key
* @return List of super tables
*/
- static std::vector<std::string> GetHierarchy(const Twine& key);
+ static std::vector<std::string> GetHierarchy(std::string_view key);
/**
* Constructor. Use NetworkTableInstance::GetTable() or GetSubTable()
* instead.
*/
- NetworkTable(NT_Inst inst, const Twine& path, const private_init&);
+ NetworkTable(NT_Inst inst, std::string_view path, const private_init&);
virtual ~NetworkTable();
/**
@@ -131,169 +106,7 @@
/**
* The path separator for sub-tables and keys
*/
- static const char PATH_SEPARATOR_CHAR;
-
- /**
- * Initializes network tables
- */
- WPI_DEPRECATED(
- "use NetworkTableInstance::StartServer() or "
- "NetworkTableInstance::StartClient() instead")
- static void Initialize();
-
- /**
- * Shuts down network tables
- */
- WPI_DEPRECATED(
- "use NetworkTableInstance::StopServer() or "
- "NetworkTableInstance::StopClient() instead")
- static void Shutdown();
-
- /**
- * set that network tables should be a client
- * This must be called before initialize or GetTable
- */
- WPI_DEPRECATED("use NetworkTableInstance::StartClient() instead")
- static void SetClientMode();
-
- /**
- * set that network tables should be a server
- * This must be called before initialize or GetTable
- */
- WPI_DEPRECATED("use NetworkTableInstance::StartServer() instead")
- static void SetServerMode();
-
- /**
- * set the team the robot is configured for (this will set the mdns address
- * that network tables will connect to in client mode)
- * This must be called before initialize or GetTable
- *
- * @param team the team number
- */
- WPI_DEPRECATED(
- "use NetworkTableInstance::SetServerTeam() or "
- "NetworkTableInstance::StartClientTeam() instead")
- static void SetTeam(int team);
-
- /**
- * @param address the address that network tables will connect to in client
- * mode
- */
- WPI_DEPRECATED(
- "use NetworkTableInstance::SetServer() or "
- "NetworkTableInstance::StartClient() instead")
- static void SetIPAddress(StringRef address);
-
- /**
- * @param addresses the addresses that network tables will connect to in
- * client mode (in round robin order)
- */
- WPI_DEPRECATED(
- "use NetworkTableInstance::SetServer() or "
- "NetworkTableInstance::StartClient() instead")
- static void SetIPAddress(ArrayRef<std::string> addresses);
-
- /**
- * Set the port number that network tables will connect to in client
- * mode or listen to in server mode.
- *
- * @param port the port number
- */
- WPI_DEPRECATED(
- "use the appropriate parameters to NetworkTableInstance::SetServer(), "
- "NetworkTableInstance::StartClient(), "
- "NetworkTableInstance::StartServer(), and "
- "NetworkTableInstance::StartDSClient() instead")
- static void SetPort(unsigned int port);
-
- /**
- * Enable requesting the server address from the Driver Station.
- *
- * @param enabled whether to enable the connection to the local DS
- */
- WPI_DEPRECATED(
- "use NetworkTableInstance::StartDSClient() and "
- "NetworkTableInstance::StopDSClient() instead")
- static void SetDSClientEnabled(bool enabled);
-
- /**
- * Sets the persistent filename.
- *
- * @param filename the filename that the network tables server uses for
- * automatic loading and saving of persistent values
- */
- WPI_DEPRECATED(
- "use the appropriate parameter to NetworkTableInstance::StartServer() "
- "instead")
- static void SetPersistentFilename(StringRef filename);
-
- /**
- * Sets the network identity.
- * This is provided in the connection info on the remote end.
- *
- * @param name identity
- */
- WPI_DEPRECATED("use NetworkTableInstance::SetNetworkIdentity() instead")
- static void SetNetworkIdentity(StringRef name);
-
- /**
- * Deletes ALL keys in ALL subtables. Use with caution!
- */
- WPI_DEPRECATED("use NetworkTableInstance::DeleteAllEntries() instead")
- static void GlobalDeleteAll();
-
- /**
- * Flushes all updated values immediately to the network.
- * Note: This is rate-limited to protect the network from flooding.
- * This is primarily useful for synchronizing network updates with
- * user code.
- */
- WPI_DEPRECATED("use NetworkTableInstance::Flush() instead")
- static void Flush();
-
- /**
- * Set the periodic update rate.
- * Sets how frequently updates are sent to other nodes over the network.
- *
- * @param interval update interval in seconds (range 0.01 to 1.0)
- */
- WPI_DEPRECATED("use NetworkTableInstance::SetUpdateRate() instead")
- static void SetUpdateRate(double interval);
-
- /**
- * Saves persistent keys to a file. The server does this automatically.
- *
- * @param filename file name
- * @return Error (or nullptr).
- */
- WPI_DEPRECATED("use NetworkTableInstance::SavePersistent() instead")
- static const char* SavePersistent(StringRef filename);
-
- /**
- * Loads persistent keys from a file. The server does this automatically.
- *
- * @param filename file name
- * @param warn callback function called for warnings
- * @return Error (or nullptr).
- */
- WPI_DEPRECATED("use NetworkTableInstance::LoadPersistent() instead")
- static const char* LoadPersistent(
- StringRef filename,
- std::function<void(size_t line, const char* msg)> warn);
-
- /**
- * Gets the table with the specified key. If the table does not exist, a new
- * table will be created.<br>
- * This will automatically initialize network tables if it has not been
- * already.
- *
- * @param key the key name
- * @return the network table requested
- */
- WPI_DEPRECATED(
- "use NetworkTableInstance::GetTable() or "
- "NetworkTableInstance::GetEntry() instead")
- static std::shared_ptr<NetworkTable> GetTable(StringRef key);
+ static constexpr char PATH_SEPARATOR_CHAR = '/';
/**
* Gets the entry for a subkey.
@@ -301,7 +114,7 @@
* @param key the key name
* @return Network table entry.
*/
- NetworkTableEntry GetEntry(const Twine& key) const;
+ NetworkTableEntry GetEntry(std::string_view key) const;
/**
* Listen to keys only within this table.
@@ -321,7 +134,7 @@
* @param flags EntryListenerFlags bitmask
* @return Listener handle
*/
- NT_EntryListener AddEntryListener(const Twine& key,
+ NT_EntryListener AddEntryListener(std::string_view key,
TableEntryListener listener,
unsigned int flags) const;
@@ -351,38 +164,6 @@
*/
void RemoveTableListener(NT_EntryListener listener);
- WPI_DEPRECATED(
- "use AddEntryListener() instead with flags value of NT_NOTIFY_NEW | "
- "NT_NOTIFY_UPDATE")
- void AddTableListener(ITableListener* listener) override;
-
- WPI_DEPRECATED(
- "use AddEntryListener() instead with flags value of NT_NOTIFY_NEW | "
- "NT_NOTIFY_UPDATE | NT_NOTIFY_IMMEDIATE")
- void AddTableListener(ITableListener* listener,
- bool immediateNotify) override;
-
- WPI_DEPRECATED("use AddEntryListener() instead")
- void AddTableListenerEx(ITableListener* listener,
- unsigned int flags) override;
-
- WPI_DEPRECATED("use AddEntryListener() instead")
- void AddTableListener(StringRef key, ITableListener* listener,
- bool immediateNotify) override;
-
- WPI_DEPRECATED("use AddEntryListener() instead")
- void AddTableListenerEx(StringRef key, ITableListener* listener,
- unsigned int flags) override;
-
- WPI_DEPRECATED("use AddSubTableListener(TableListener, bool) instead")
- void AddSubTableListener(ITableListener* listener) override;
-
- WPI_DEPRECATED("use AddSubTableListener(TableListener, bool) instead")
- void AddSubTableListener(ITableListener* listener, bool localNotify) override;
-
- WPI_DEPRECATED("use RemoveTableListener(NT_EntryListener) instead")
- void RemoveTableListener(ITableListener* listener) override;
-
/**
* Returns the table at the specified key. If there is no table at the
* specified key, it will create a new table
@@ -390,7 +171,7 @@
* @param key the key name
* @return the networktable to be returned
*/
- std::shared_ptr<NetworkTable> GetSubTable(const Twine& key) const override;
+ std::shared_ptr<NetworkTable> GetSubTable(std::string_view key) const;
/**
* Determines whether the given key is in this table.
@@ -398,7 +179,7 @@
* @param key the key to search for
* @return true if the table as a value assigned to the given key
*/
- bool ContainsKey(const Twine& key) const override;
+ bool ContainsKey(std::string_view key) const;
/**
* Determines whether there exists a non-empty subtable for this key
@@ -408,7 +189,7 @@
* @return true if there is a subtable with the key which contains at least
* one key/subtable of its own
*/
- bool ContainsSubTable(const Twine& key) const override;
+ bool ContainsSubTable(std::string_view key) const;
/**
* Gets all keys in the table (not including sub-tables).
@@ -416,21 +197,21 @@
* @param types bitmask of types; 0 is treated as a "don't care".
* @return keys currently in the table
*/
- std::vector<std::string> GetKeys(int types = 0) const override;
+ std::vector<std::string> GetKeys(int types = 0) const;
/**
* Gets the names of all subtables in the table.
*
* @return subtables currently in the table
*/
- std::vector<std::string> GetSubTables() const override;
+ std::vector<std::string> GetSubTables() const;
/**
* Makes a key's value persistent through program restarts.
*
* @param key the key to make persistent
*/
- void SetPersistent(StringRef key) override;
+ void SetPersistent(std::string_view key);
/**
* Stop making a key's value persistent through program restarts.
@@ -438,7 +219,7 @@
*
* @param key the key name
*/
- void ClearPersistent(StringRef key) override;
+ void ClearPersistent(std::string_view key);
/**
* Returns whether the value is persistent through program restarts.
@@ -446,7 +227,7 @@
*
* @param key the key name
*/
- bool IsPersistent(StringRef key) const override;
+ bool IsPersistent(std::string_view key) const;
/**
* Sets flags on the specified key in this table. The key can
@@ -455,7 +236,7 @@
* @param key the key name
* @param flags the flags to set (bitmask)
*/
- void SetFlags(StringRef key, unsigned int flags) override;
+ void SetFlags(std::string_view key, unsigned int flags);
/**
* Clears flags on the specified key in this table. The key can
@@ -464,7 +245,7 @@
* @param key the key name
* @param flags the flags to clear (bitmask)
*/
- void ClearFlags(StringRef key, unsigned int flags) override;
+ void ClearFlags(std::string_view key, unsigned int flags);
/**
* Returns the flags for the specified key.
@@ -472,14 +253,14 @@
* @param key the key name
* @return the flags, or 0 if the key is not defined
*/
- unsigned int GetFlags(StringRef key) const override;
+ unsigned int GetFlags(std::string_view key) const;
/**
* Deletes the specified key in this table.
*
* @param key the key name
*/
- void Delete(const Twine& key) override;
+ void Delete(std::string_view key);
/**
* Put a number in the table
@@ -488,7 +269,7 @@
* @param value the value that will be assigned
* @return False if the table key already exists with a different type
*/
- bool PutNumber(StringRef key, double value) override;
+ bool PutNumber(std::string_view key, double value);
/**
* Gets the current value in the table, setting it if it does not exist.
@@ -497,7 +278,7 @@
* @param defaultValue the default value to set if key doesn't exist.
* @returns False if the table key exists with a different type
*/
- bool SetDefaultNumber(StringRef key, double defaultValue) override;
+ bool SetDefaultNumber(std::string_view key, double defaultValue);
/**
* Gets the number associated with the given name.
@@ -507,7 +288,7 @@
* @return the value associated with the given key or the given default value
* if there is no value associated with the key
*/
- double GetNumber(StringRef key, double defaultValue) const override;
+ double GetNumber(std::string_view key, double defaultValue) const;
/**
* Put a string in the table
@@ -516,7 +297,7 @@
* @param value the value that will be assigned
* @return False if the table key already exists with a different type
*/
- bool PutString(StringRef key, StringRef value) override;
+ bool PutString(std::string_view key, std::string_view value);
/**
* Gets the current value in the table, setting it if it does not exist.
@@ -525,7 +306,7 @@
* @param defaultValue the default value to set if key doesn't exist.
* @returns False if the table key exists with a different type
*/
- bool SetDefaultString(StringRef key, StringRef defaultValue) override;
+ bool SetDefaultString(std::string_view key, std::string_view defaultValue);
/**
* Gets the string associated with the given name. If the key does not
@@ -536,7 +317,8 @@
* @return the value associated with the given key or the given default value
* if there is no value associated with the key
*/
- std::string GetString(StringRef key, StringRef defaultValue) const override;
+ std::string GetString(std::string_view key,
+ std::string_view defaultValue) const;
/**
* Put a boolean in the table
@@ -545,7 +327,7 @@
* @param value the value that will be assigned
* @return False if the table key already exists with a different type
*/
- bool PutBoolean(StringRef key, bool value) override;
+ bool PutBoolean(std::string_view key, bool value);
/**
* Gets the current value in the table, setting it if it does not exist.
@@ -554,7 +336,7 @@
* @param defaultValue the default value to set if key doesn't exist.
* @returns False if the table key exists with a different type
*/
- bool SetDefaultBoolean(StringRef key, bool defaultValue) override;
+ bool SetDefaultBoolean(std::string_view key, bool defaultValue);
/**
* Gets the boolean associated with the given name. If the key does not
@@ -565,7 +347,7 @@
* @return the value associated with the given key or the given default value
* if there is no value associated with the key
*/
- bool GetBoolean(StringRef key, bool defaultValue) const override;
+ bool GetBoolean(std::string_view key, bool defaultValue) const;
/**
* Put a boolean array in the table
@@ -578,7 +360,7 @@
* std::vector<bool> is special-cased in C++. 0 is false, any
* non-zero value is true.
*/
- bool PutBooleanArray(StringRef key, ArrayRef<int> value) override;
+ bool PutBooleanArray(std::string_view key, wpi::span<const int> value);
/**
* Gets the current value in the table, setting it if it does not exist.
@@ -587,8 +369,8 @@
* @param defaultValue the default value to set if key doesn't exist.
* @return False if the table key exists with a different type
*/
- bool SetDefaultBooleanArray(StringRef key,
- ArrayRef<int> defaultValue) override;
+ bool SetDefaultBooleanArray(std::string_view key,
+ wpi::span<const int> defaultValue);
/**
* Returns the boolean array the key maps to. If the key does not exist or is
@@ -606,8 +388,8 @@
* because std::vector<bool> is special-cased in C++. 0 is false, any
* non-zero value is true.
*/
- std::vector<int> GetBooleanArray(StringRef key,
- ArrayRef<int> defaultValue) const override;
+ std::vector<int> GetBooleanArray(std::string_view key,
+ wpi::span<const int> defaultValue) const;
/**
* Put a number array in the table
@@ -616,7 +398,7 @@
* @param value the value that will be assigned
* @return False if the table key already exists with a different type
*/
- bool PutNumberArray(StringRef key, ArrayRef<double> value) override;
+ bool PutNumberArray(std::string_view key, wpi::span<const double> value);
/**
* Gets the current value in the table, setting it if it does not exist.
@@ -625,8 +407,8 @@
* @param defaultValue the default value to set if key doesn't exist.
* @returns False if the table key exists with a different type
*/
- bool SetDefaultNumberArray(StringRef key,
- ArrayRef<double> defaultValue) override;
+ bool SetDefaultNumberArray(std::string_view key,
+ wpi::span<const double> defaultValue);
/**
* Returns the number array the key maps to. If the key does not exist or is
@@ -641,7 +423,7 @@
* concern, use GetValue() instead.
*/
std::vector<double> GetNumberArray(
- StringRef key, ArrayRef<double> defaultValue) const override;
+ std::string_view key, wpi::span<const double> defaultValue) const;
/**
* Put a string array in the table
@@ -650,7 +432,7 @@
* @param value the value that will be assigned
* @return False if the table key already exists with a different type
*/
- bool PutStringArray(StringRef key, ArrayRef<std::string> value) override;
+ bool PutStringArray(std::string_view key, wpi::span<const std::string> value);
/**
* Gets the current value in the table, setting it if it does not exist.
@@ -659,8 +441,8 @@
* @param defaultValue the default value to set if key doesn't exist.
* @returns False if the table key exists with a different type
*/
- bool SetDefaultStringArray(StringRef key,
- ArrayRef<std::string> defaultValue) override;
+ bool SetDefaultStringArray(std::string_view key,
+ wpi::span<const std::string> defaultValue);
/**
* Returns the string array the key maps to. If the key does not exist or is
@@ -675,7 +457,7 @@
* concern, use GetValue() instead.
*/
std::vector<std::string> GetStringArray(
- StringRef key, ArrayRef<std::string> defaultValue) const override;
+ std::string_view key, wpi::span<const std::string> defaultValue) const;
/**
* Put a raw value (byte array) in the table
@@ -684,7 +466,7 @@
* @param value the value that will be assigned
* @return False if the table key already exists with a different type
*/
- bool PutRaw(StringRef key, StringRef value) override;
+ bool PutRaw(std::string_view key, std::string_view value);
/**
* Gets the current value in the table, setting it if it does not exist.
@@ -693,7 +475,7 @@
* @param defaultValue the default value to set if key doesn't exist.
* @return False if the table key exists with a different type
*/
- bool SetDefaultRaw(StringRef key, StringRef defaultValue) override;
+ bool SetDefaultRaw(std::string_view key, std::string_view defaultValue);
/**
* Returns the raw value (byte array) the key maps to. If the key does not
@@ -707,7 +489,7 @@
* @note This makes a copy of the raw contents. If the overhead of this is a
* concern, use GetValue() instead.
*/
- std::string GetRaw(StringRef key, StringRef defaultValue) const override;
+ std::string GetRaw(std::string_view key, std::string_view defaultValue) const;
/**
* Put a value in the table
@@ -716,7 +498,7 @@
* @param value the value that will be assigned
* @return False if the table key already exists with a different type
*/
- bool PutValue(const Twine& key, std::shared_ptr<Value> value) override;
+ bool PutValue(std::string_view key, std::shared_ptr<Value> value);
/**
* Gets the current value in the table, setting it if it does not exist.
@@ -725,8 +507,8 @@
* @param defaultValue the default value to set if key doesn't exist.
* @return False if the table key exists with a different type
*/
- bool SetDefaultValue(const Twine& key,
- std::shared_ptr<Value> defaultValue) override;
+ bool SetDefaultValue(std::string_view key,
+ std::shared_ptr<Value> defaultValue);
/**
* Gets the value associated with a key as an object
@@ -735,14 +517,14 @@
* @return the value associated with the given key, or nullptr if the key
* does not exist
*/
- std::shared_ptr<Value> GetValue(const Twine& key) const override;
+ std::shared_ptr<Value> GetValue(std::string_view key) const;
/**
* Gets the full path of this table. Does not include the trailing "/".
*
* @return The path (e.g "", "/foo").
*/
- StringRef GetPath() const override;
+ std::string_view GetPath() const;
/**
* Save table values to a file. The file format used is identical to
@@ -751,7 +533,7 @@
* @param filename filename
* @return error string, or nullptr if successful
*/
- const char* SaveEntries(const Twine& filename) const;
+ const char* SaveEntries(std::string_view filename) const;
/**
* Load table values from a file. The file format used is identical to
@@ -762,21 +544,10 @@
* @return error string, or nullptr if successful
*/
const char* LoadEntries(
- const Twine& filename,
+ std::string_view filename,
std::function<void(size_t line, const char* msg)> warn);
};
-#ifdef __GNUC__
-#pragma GCC diagnostic pop
-#elif _WIN32
-#pragma warning(pop)
-#endif
-
} // namespace nt
-// For backwards compatibility
-#ifndef NAMESPACED_NT
-using nt::NetworkTable; // NOLINT
-#endif
-
#endif // NTCORE_NETWORKTABLES_NETWORKTABLE_H_
diff --git a/ntcore/src/main/native/include/networktables/NetworkTableEntry.h b/ntcore/src/main/native/include/networktables/NetworkTableEntry.h
index 8fdedc6..c6c9c86 100644
--- a/ntcore/src/main/native/include/networktables/NetworkTableEntry.h
+++ b/ntcore/src/main/native/include/networktables/NetworkTableEntry.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_NETWORKTABLES_NETWORKTABLEENTRY_H_
#define NTCORE_NETWORKTABLES_NETWORKTABLEENTRY_H_
@@ -13,10 +10,10 @@
#include <initializer_list>
#include <memory>
#include <string>
+#include <string_view>
#include <vector>
-#include <wpi/StringRef.h>
-#include <wpi/Twine.h>
+#include <wpi/span.h>
#include "networktables/NetworkTableType.h"
#include "networktables/NetworkTableValue.h"
@@ -26,10 +23,6 @@
namespace nt {
-using wpi::ArrayRef;
-using wpi::StringRef;
-using wpi::Twine;
-
class NetworkTableInstance;
/**
@@ -150,7 +143,7 @@
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
- std::string GetString(StringRef defaultValue) const;
+ std::string GetString(std::string_view defaultValue) const;
/**
* Gets the entry's value as a raw. If the entry does not exist or is of
@@ -159,7 +152,7 @@
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
- std::string GetRaw(StringRef defaultValue) const;
+ std::string GetRaw(std::string_view defaultValue) const;
/**
* Gets the entry's value as a boolean array. If the entry does not exist
@@ -175,7 +168,7 @@
* because std::vector<bool> is special-cased in C++. 0 is false, any
* non-zero value is true.
*/
- std::vector<int> GetBooleanArray(ArrayRef<int> defaultValue) const;
+ std::vector<int> GetBooleanArray(wpi::span<const int> defaultValue) const;
/**
* Gets the entry's value as a boolean array. If the entry does not exist
@@ -204,7 +197,8 @@
* @note This makes a copy of the array. If the overhead of this is a
* concern, use GetValue() instead.
*/
- std::vector<double> GetDoubleArray(ArrayRef<double> defaultValue) const;
+ std::vector<double> GetDoubleArray(
+ wpi::span<const double> defaultValue) const;
/**
* Gets the entry's value as a double array. If the entry does not exist
@@ -230,7 +224,7 @@
* concern, use GetValue() instead.
*/
std::vector<std::string> GetStringArray(
- ArrayRef<std::string> defaultValue) const;
+ wpi::span<const std::string> defaultValue) const;
/**
* Gets the entry's value as a string array. If the entry does not exist
@@ -248,7 +242,7 @@
/**
* Sets the entry's value if it does not exist.
*
- * @param defaultValue the default value to set
+ * @param value the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultValue(std::shared_ptr<Value> value);
@@ -275,7 +269,7 @@
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
- bool SetDefaultString(const Twine& defaultValue);
+ bool SetDefaultString(std::string_view defaultValue);
/**
* Sets the entry's value if it does not exist.
@@ -283,7 +277,7 @@
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
- bool SetDefaultRaw(StringRef defaultValue);
+ bool SetDefaultRaw(std::string_view defaultValue);
/**
* Sets the entry's value if it does not exist.
@@ -291,7 +285,7 @@
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
- bool SetDefaultBooleanArray(ArrayRef<int> defaultValue);
+ bool SetDefaultBooleanArray(wpi::span<const int> defaultValue);
/**
* Sets the entry's value if it does not exist.
@@ -307,7 +301,7 @@
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
- bool SetDefaultDoubleArray(ArrayRef<double> defaultValue);
+ bool SetDefaultDoubleArray(wpi::span<const double> defaultValue);
/**
* Sets the entry's value if it does not exist.
@@ -323,7 +317,7 @@
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
- bool SetDefaultStringArray(ArrayRef<std::string> defaultValue);
+ bool SetDefaultStringArray(wpi::span<const std::string> defaultValue);
/**
* Sets the entry's value if it does not exist.
@@ -363,7 +357,7 @@
* @param value the value to set
* @return False if the entry exists with a different type
*/
- bool SetString(const Twine& value);
+ bool SetString(std::string_view value);
/**
* Sets the entry's value.
@@ -371,7 +365,7 @@
* @param value the value to set
* @return False if the entry exists with a different type
*/
- bool SetRaw(StringRef value);
+ bool SetRaw(std::string_view value);
/**
* Sets the entry's value.
@@ -379,7 +373,7 @@
* @param value the value to set
* @return False if the entry exists with a different type
*/
- bool SetBooleanArray(ArrayRef<bool> value);
+ bool SetBooleanArray(wpi::span<const bool> value);
/**
* Sets the entry's value.
@@ -395,7 +389,7 @@
* @param value the value to set
* @return False if the entry exists with a different type
*/
- bool SetBooleanArray(ArrayRef<int> value);
+ bool SetBooleanArray(wpi::span<const int> value);
/**
* Sets the entry's value.
@@ -411,7 +405,7 @@
* @param value the value to set
* @return False if the entry exists with a different type
*/
- bool SetDoubleArray(ArrayRef<double> value);
+ bool SetDoubleArray(wpi::span<const double> value);
/**
* Sets the entry's value.
@@ -427,7 +421,7 @@
* @param value the value to set
* @return False if the entry exists with a different type
*/
- bool SetStringArray(ArrayRef<std::string> value);
+ bool SetStringArray(wpi::span<const std::string> value);
/**
* Sets the entry's value.
@@ -467,7 +461,7 @@
*
* @param value the value to set
*/
- void ForceSetString(const Twine& value);
+ void ForceSetString(std::string_view value);
/**
* Sets the entry's value. If the value is of different type, the type is
@@ -475,7 +469,7 @@
*
* @param value the value to set
*/
- void ForceSetRaw(StringRef value);
+ void ForceSetRaw(std::string_view value);
/**
* Sets the entry's value. If the value is of different type, the type is
@@ -483,7 +477,7 @@
*
* @param value the value to set
*/
- void ForceSetBooleanArray(ArrayRef<bool> value);
+ void ForceSetBooleanArray(wpi::span<const bool> value);
/**
* Sets the entry's value. If the value is of different type, the type is
@@ -499,7 +493,7 @@
*
* @param value the value to set
*/
- void ForceSetBooleanArray(ArrayRef<int> value);
+ void ForceSetBooleanArray(wpi::span<const int> value);
/**
* Sets the entry's value. If the value is of different type, the type is
@@ -515,7 +509,7 @@
*
* @param value the value to set
*/
- void ForceSetDoubleArray(ArrayRef<double> value);
+ void ForceSetDoubleArray(wpi::span<const double> value);
/**
* Sets the entry's value. If the value is of different type, the type is
@@ -531,7 +525,7 @@
*
* @param value the value to set
*/
- void ForceSetStringArray(ArrayRef<std::string> value);
+ void ForceSetStringArray(wpi::span<const std::string> value);
/**
* Sets the entry's value. If the value is of different type, the type is
@@ -603,7 +597,7 @@
* @param params parameter
* @return RPC call object.
*/
- RpcCall CallRpc(StringRef params);
+ RpcCall CallRpc(std::string_view params);
/**
* Add a listener for changes to this entry.
@@ -638,11 +632,11 @@
protected:
/* Native handle */
- NT_Entry m_handle;
+ NT_Entry m_handle{0};
};
} // namespace nt
-#include "networktables/NetworkTableEntry.inl"
+#include "networktables/NetworkTableEntry.inc"
#endif // NTCORE_NETWORKTABLES_NETWORKTABLEENTRY_H_
diff --git a/ntcore/src/main/native/include/networktables/NetworkTableEntry.inl b/ntcore/src/main/native/include/networktables/NetworkTableEntry.inc
similarity index 61%
rename from ntcore/src/main/native/include/networktables/NetworkTableEntry.inl
rename to ntcore/src/main/native/include/networktables/NetworkTableEntry.inc
index 5bf1dbf..e7deb51 100644
--- a/ntcore/src/main/native/include/networktables/NetworkTableEntry.inl
+++ b/ntcore/src/main/native/include/networktables/NetworkTableEntry.inc
@@ -1,25 +1,27 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
-#ifndef NTCORE_NETWORKTABLES_NETWORKTABLEENTRY_INL_
-#define NTCORE_NETWORKTABLES_NETWORKTABLEENTRY_INL_
+#ifndef NTCORE_NETWORKTABLES_NETWORKTABLEENTRY_INC_
+#define NTCORE_NETWORKTABLES_NETWORKTABLEENTRY_INC_
#include <memory>
#include <string>
+#include <string_view>
#include <vector>
+#include "networktables/NetworkTableEntry.h"
+
namespace nt {
-inline NetworkTableEntry::NetworkTableEntry() : m_handle{0} {}
+inline NetworkTableEntry::NetworkTableEntry() {}
inline NetworkTableEntry::NetworkTableEntry(NT_Entry handle)
: m_handle{handle} {}
-inline NT_Entry NetworkTableEntry::GetHandle() const { return m_handle; }
+inline NT_Entry NetworkTableEntry::GetHandle() const {
+ return m_handle;
+}
inline bool NetworkTableEntry::Exists() const {
return GetEntryType(m_handle) != NT_UNASSIGNED;
@@ -51,65 +53,81 @@
inline bool NetworkTableEntry::GetBoolean(bool defaultValue) const {
auto value = GetEntryValue(m_handle);
- if (!value || value->type() != NT_BOOLEAN) return defaultValue;
+ if (!value || value->type() != NT_BOOLEAN) {
+ return defaultValue;
+ }
return value->GetBoolean();
}
inline double NetworkTableEntry::GetDouble(double defaultValue) const {
auto value = GetEntryValue(m_handle);
- if (!value || value->type() != NT_DOUBLE) return defaultValue;
+ if (!value || value->type() != NT_DOUBLE) {
+ return defaultValue;
+ }
return value->GetDouble();
}
-inline std::string NetworkTableEntry::GetString(StringRef defaultValue) const {
+inline std::string NetworkTableEntry::GetString(
+ std::string_view defaultValue) const {
auto value = GetEntryValue(m_handle);
- if (!value || value->type() != NT_STRING) return defaultValue;
- return value->GetString();
+ if (!value || value->type() != NT_STRING) {
+ return std::string{defaultValue};
+ }
+ return std::string{value->GetString()};
}
-inline std::string NetworkTableEntry::GetRaw(StringRef defaultValue) const {
+inline std::string NetworkTableEntry::GetRaw(
+ std::string_view defaultValue) const {
auto value = GetEntryValue(m_handle);
- if (!value || value->type() != NT_RAW) return defaultValue;
- return value->GetString();
+ if (!value || value->type() != NT_RAW) {
+ return std::string{defaultValue};
+ }
+ return std::string{value->GetRaw()};
}
inline std::vector<int> NetworkTableEntry::GetBooleanArray(
- ArrayRef<int> defaultValue) const {
+ wpi::span<const int> defaultValue) const {
auto value = GetEntryValue(m_handle);
- if (!value || value->type() != NT_BOOLEAN_ARRAY) return defaultValue;
- return value->GetBooleanArray();
+ if (!value || value->type() != NT_BOOLEAN_ARRAY) {
+ return {defaultValue.begin(), defaultValue.end()};
+ }
+ auto arr = value->GetBooleanArray();
+ return {arr.begin(), arr.end()};
}
inline std::vector<int> NetworkTableEntry::GetBooleanArray(
std::initializer_list<int> defaultValue) const {
- return GetBooleanArray(
- wpi::makeArrayRef(defaultValue.begin(), defaultValue.end()));
+ return GetBooleanArray({defaultValue.begin(), defaultValue.end()});
}
inline std::vector<double> NetworkTableEntry::GetDoubleArray(
- ArrayRef<double> defaultValue) const {
+ wpi::span<const double> defaultValue) const {
auto value = GetEntryValue(m_handle);
- if (!value || value->type() != NT_DOUBLE_ARRAY) return defaultValue;
- return value->GetDoubleArray();
+ if (!value || value->type() != NT_DOUBLE_ARRAY) {
+ return {defaultValue.begin(), defaultValue.end()};
+ }
+ auto arr = value->GetDoubleArray();
+ return {arr.begin(), arr.end()};
}
inline std::vector<double> NetworkTableEntry::GetDoubleArray(
std::initializer_list<double> defaultValue) const {
- return GetDoubleArray(
- wpi::makeArrayRef(defaultValue.begin(), defaultValue.end()));
+ return GetDoubleArray({defaultValue.begin(), defaultValue.end()});
}
inline std::vector<std::string> NetworkTableEntry::GetStringArray(
- ArrayRef<std::string> defaultValue) const {
+ wpi::span<const std::string> defaultValue) const {
auto value = GetEntryValue(m_handle);
- if (!value || value->type() != NT_STRING_ARRAY) return defaultValue;
- return value->GetStringArray();
+ if (!value || value->type() != NT_STRING_ARRAY) {
+ return {defaultValue.begin(), defaultValue.end()};
+ }
+ auto arr = value->GetStringArray();
+ return {arr.begin(), arr.end()};
}
inline std::vector<std::string> NetworkTableEntry::GetStringArray(
std::initializer_list<std::string> defaultValue) const {
- return GetStringArray(
- wpi::makeArrayRef(defaultValue.begin(), defaultValue.end()));
+ return GetStringArray({defaultValue.begin(), defaultValue.end()});
}
inline bool NetworkTableEntry::SetDefaultValue(std::shared_ptr<Value> value) {
@@ -124,26 +142,41 @@
return SetDefaultEntryValue(m_handle, Value::MakeDouble(defaultValue));
}
-inline bool NetworkTableEntry::SetDefaultString(const Twine& defaultValue) {
+inline bool NetworkTableEntry::SetDefaultString(std::string_view defaultValue) {
return SetDefaultEntryValue(m_handle, Value::MakeString(defaultValue));
}
-inline bool NetworkTableEntry::SetDefaultRaw(StringRef defaultValue) {
+inline bool NetworkTableEntry::SetDefaultRaw(std::string_view defaultValue) {
return SetDefaultEntryValue(m_handle, Value::MakeRaw(defaultValue));
}
inline bool NetworkTableEntry::SetDefaultBooleanArray(
- ArrayRef<int> defaultValue) {
+ wpi::span<const int> defaultValue) {
+ return SetDefaultEntryValue(m_handle, Value::MakeBooleanArray(defaultValue));
+}
+
+inline bool NetworkTableEntry::SetDefaultBooleanArray(
+ std::initializer_list<int> defaultValue) {
return SetDefaultEntryValue(m_handle, Value::MakeBooleanArray(defaultValue));
}
inline bool NetworkTableEntry::SetDefaultDoubleArray(
- ArrayRef<double> defaultValue) {
+ wpi::span<const double> defaultValue) {
return SetDefaultEntryValue(m_handle, Value::MakeDoubleArray(defaultValue));
}
+inline bool NetworkTableEntry::SetDefaultDoubleArray(
+ std::initializer_list<double> value) {
+ return SetDefaultEntryValue(m_handle, Value::MakeDoubleArray(value));
+}
+
inline bool NetworkTableEntry::SetDefaultStringArray(
- ArrayRef<std::string> defaultValue) {
+ wpi::span<const std::string> defaultValue) {
+ return SetDefaultEntryValue(m_handle, Value::MakeStringArray(defaultValue));
+}
+
+inline bool NetworkTableEntry::SetDefaultStringArray(
+ std::initializer_list<std::string> defaultValue) {
return SetDefaultEntryValue(m_handle, Value::MakeStringArray(defaultValue));
}
@@ -159,15 +192,15 @@
return SetEntryValue(m_handle, Value::MakeDouble(value));
}
-inline bool NetworkTableEntry::SetString(const Twine& value) {
+inline bool NetworkTableEntry::SetString(std::string_view value) {
return SetEntryValue(m_handle, Value::MakeString(value));
}
-inline bool NetworkTableEntry::SetRaw(StringRef value) {
+inline bool NetworkTableEntry::SetRaw(std::string_view value) {
return SetEntryValue(m_handle, Value::MakeRaw(value));
}
-inline bool NetworkTableEntry::SetBooleanArray(ArrayRef<bool> value) {
+inline bool NetworkTableEntry::SetBooleanArray(wpi::span<const bool> value) {
return SetEntryValue(m_handle, Value::MakeBooleanArray(value));
}
@@ -176,7 +209,7 @@
return SetEntryValue(m_handle, Value::MakeBooleanArray(value));
}
-inline bool NetworkTableEntry::SetBooleanArray(ArrayRef<int> value) {
+inline bool NetworkTableEntry::SetBooleanArray(wpi::span<const int> value) {
return SetEntryValue(m_handle, Value::MakeBooleanArray(value));
}
@@ -185,7 +218,7 @@
return SetEntryValue(m_handle, Value::MakeBooleanArray(value));
}
-inline bool NetworkTableEntry::SetDoubleArray(ArrayRef<double> value) {
+inline bool NetworkTableEntry::SetDoubleArray(wpi::span<const double> value) {
return SetEntryValue(m_handle, Value::MakeDoubleArray(value));
}
@@ -194,7 +227,8 @@
return SetEntryValue(m_handle, Value::MakeDoubleArray(value));
}
-inline bool NetworkTableEntry::SetStringArray(ArrayRef<std::string> value) {
+inline bool NetworkTableEntry::SetStringArray(
+ wpi::span<const std::string> value) {
return SetEntryValue(m_handle, Value::MakeStringArray(value));
}
@@ -215,15 +249,16 @@
SetEntryTypeValue(m_handle, Value::MakeDouble(value));
}
-inline void NetworkTableEntry::ForceSetString(const Twine& value) {
+inline void NetworkTableEntry::ForceSetString(std::string_view value) {
SetEntryTypeValue(m_handle, Value::MakeString(value));
}
-inline void NetworkTableEntry::ForceSetRaw(StringRef value) {
+inline void NetworkTableEntry::ForceSetRaw(std::string_view value) {
SetEntryTypeValue(m_handle, Value::MakeRaw(value));
}
-inline void NetworkTableEntry::ForceSetBooleanArray(ArrayRef<bool> value) {
+inline void NetworkTableEntry::ForceSetBooleanArray(
+ wpi::span<const bool> value) {
SetEntryTypeValue(m_handle, Value::MakeBooleanArray(value));
}
@@ -232,7 +267,8 @@
SetEntryTypeValue(m_handle, Value::MakeBooleanArray(value));
}
-inline void NetworkTableEntry::ForceSetBooleanArray(ArrayRef<int> value) {
+inline void NetworkTableEntry::ForceSetBooleanArray(
+ wpi::span<const int> value) {
SetEntryTypeValue(m_handle, Value::MakeBooleanArray(value));
}
@@ -241,7 +277,8 @@
SetEntryTypeValue(m_handle, Value::MakeBooleanArray(value));
}
-inline void NetworkTableEntry::ForceSetDoubleArray(ArrayRef<double> value) {
+inline void NetworkTableEntry::ForceSetDoubleArray(
+ wpi::span<const double> value) {
SetEntryTypeValue(m_handle, Value::MakeDoubleArray(value));
}
@@ -251,7 +288,7 @@
}
inline void NetworkTableEntry::ForceSetStringArray(
- ArrayRef<std::string> value) {
+ wpi::span<const std::string> value) {
SetEntryTypeValue(m_handle, Value::MakeStringArray(value));
}
@@ -268,22 +305,28 @@
SetEntryFlags(m_handle, GetFlags() & ~flags);
}
-inline void NetworkTableEntry::SetPersistent() { SetFlags(kPersistent); }
+inline void NetworkTableEntry::SetPersistent() {
+ SetFlags(kPersistent);
+}
-inline void NetworkTableEntry::ClearPersistent() { ClearFlags(kPersistent); }
+inline void NetworkTableEntry::ClearPersistent() {
+ ClearFlags(kPersistent);
+}
inline bool NetworkTableEntry::IsPersistent() const {
return (GetFlags() & kPersistent) != 0;
}
-inline void NetworkTableEntry::Delete() { DeleteEntry(m_handle); }
+inline void NetworkTableEntry::Delete() {
+ DeleteEntry(m_handle);
+}
inline void NetworkTableEntry::CreateRpc(
std::function<void(const RpcAnswer& answer)> callback) {
- ::nt::CreateRpc(m_handle, StringRef("\0", 1), callback);
+ ::nt::CreateRpc(m_handle, std::string_view("\0", 1), callback);
}
-inline RpcCall NetworkTableEntry::CallRpc(StringRef params) {
+inline RpcCall NetworkTableEntry::CallRpc(std::string_view params) {
return RpcCall{m_handle, ::nt::CallRpc(m_handle, params)};
}
@@ -299,4 +342,4 @@
} // namespace nt
-#endif // NTCORE_NETWORKTABLES_NETWORKTABLEENTRY_INL_
+#endif // NTCORE_NETWORKTABLES_NETWORKTABLEENTRY_INC_
diff --git a/ntcore/src/main/native/include/networktables/NetworkTableInstance.h b/ntcore/src/main/native/include/networktables/NetworkTableInstance.h
index 16fc5b2..54b5324 100644
--- a/ntcore/src/main/native/include/networktables/NetworkTableInstance.h
+++ b/ntcore/src/main/native/include/networktables/NetworkTableInstance.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_NETWORKTABLES_NETWORKTABLEINSTANCE_H_
#define NTCORE_NETWORKTABLES_NETWORKTABLEINSTANCE_H_
@@ -11,12 +8,11 @@
#include <functional>
#include <memory>
#include <string>
+#include <string_view>
#include <utility>
#include <vector>
-#include <wpi/ArrayRef.h>
-#include <wpi/StringRef.h>
-#include <wpi/Twine.h>
+#include <wpi/span.h>
#include "networktables/NetworkTable.h"
#include "networktables/NetworkTableEntry.h"
@@ -25,10 +21,6 @@
namespace nt {
-using wpi::ArrayRef;
-using wpi::StringRef;
-using wpi::Twine;
-
/**
* NetworkTables Instance.
*
@@ -93,7 +85,7 @@
/**
* Construct from native handle.
*
- * @param handle Native handle
+ * @param inst Native handle
*/
explicit NetworkTableInstance(NT_Inst inst) noexcept;
@@ -138,7 +130,7 @@
* @param name Key
* @return Network table entry.
*/
- NetworkTableEntry GetEntry(const Twine& name);
+ NetworkTableEntry GetEntry(std::string_view name);
/**
* Get entries starting with the given prefix.
@@ -151,7 +143,7 @@
* @param types bitmask of types; 0 is treated as a "don't care"
* @return Array of entries.
*/
- std::vector<NetworkTableEntry> GetEntries(const Twine& prefix,
+ std::vector<NetworkTableEntry> GetEntries(std::string_view prefix,
unsigned int types);
/**
@@ -165,7 +157,7 @@
* @param types bitmask of types; 0 is treated as a "don't care"
* @return Array of entry information.
*/
- std::vector<EntryInfo> GetEntryInfo(const Twine& prefix,
+ std::vector<EntryInfo> GetEntryInfo(std::string_view prefix,
unsigned int types) const;
/**
@@ -174,7 +166,7 @@
* @param key the key name
* @return The network table
*/
- std::shared_ptr<NetworkTable> GetTable(const Twine& key) const;
+ std::shared_ptr<NetworkTable> GetTable(std::string_view key) const;
/**
* Deletes ALL keys in ALL subtables (except persistent values).
@@ -196,7 +188,7 @@
* @return Listener handle
*/
NT_EntryListener AddEntryListener(
- const Twine& prefix,
+ std::string_view prefix,
std::function<void(const EntryNotification& event)> callback,
unsigned int flags) const;
@@ -290,7 +282,7 @@
*
* @param name identity to advertise
*/
- void SetNetworkIdentity(const Twine& name);
+ void SetNetworkIdentity(std::string_view name);
/**
* Get the current network mode.
@@ -321,7 +313,7 @@
* address (UTF-8 string, null terminated)
* @param port port to communicate over
*/
- void StartServer(const Twine& persist_filename = "networktables.ini",
+ void StartServer(std::string_view persist_filename = "networktables.ini",
const char* listen_address = "",
unsigned int port = kDefaultPort);
@@ -349,7 +341,8 @@
*
* @param servers array of server name and port pairs
*/
- void StartClient(ArrayRef<std::pair<StringRef, unsigned int>> servers);
+ void StartClient(
+ wpi::span<const std::pair<std::string_view, unsigned int>> servers);
/**
* Starts a client using the specified servers and port. The
@@ -358,7 +351,7 @@
* @param servers array of server names
* @param port port to communicate over
*/
- void StartClient(ArrayRef<StringRef> servers,
+ void StartClient(wpi::span<const std::string_view> servers,
unsigned int port = kDefaultPort);
/**
@@ -389,7 +382,8 @@
*
* @param servers array of server name and port pairs
*/
- void SetServer(ArrayRef<std::pair<StringRef, unsigned int>> servers);
+ void SetServer(
+ wpi::span<const std::pair<std::string_view, unsigned int>> servers);
/**
* Sets server addresses and port for client (without restarting client).
@@ -398,7 +392,8 @@
* @param servers array of server names
* @param port port to communicate over
*/
- void SetServer(ArrayRef<StringRef> servers, unsigned int port = kDefaultPort);
+ void SetServer(wpi::span<const std::string_view> servers,
+ unsigned int port = kDefaultPort);
/**
* Sets server addresses and port for client (without restarting client).
@@ -469,7 +464,7 @@
* @param filename filename
* @return error string, or nullptr if successful
*/
- const char* SavePersistent(const Twine& filename) const;
+ const char* SavePersistent(std::string_view filename) const;
/**
* Load persistent values from a file. The server automatically does this
@@ -481,7 +476,7 @@
* @return error string, or nullptr if successful
*/
const char* LoadPersistent(
- const Twine& filename,
+ std::string_view filename,
std::function<void(size_t line, const char* msg)> warn);
/**
@@ -492,7 +487,8 @@
* @param prefix save only keys starting with this prefix
* @return error string, or nullptr if successful
*/
- const char* SaveEntries(const Twine& filename, const Twine& prefix) const;
+ const char* SaveEntries(std::string_view filename,
+ std::string_view prefix) const;
/**
* Load table values from a file. The file format used is identical to
@@ -504,7 +500,7 @@
* @return error string, or nullptr if successful
*/
const char* LoadEntries(
- const Twine& filename, const Twine& prefix,
+ std::string_view filename, std::string_view prefix,
std::function<void(size_t line, const char* msg)> warn);
/** @} */
@@ -527,7 +523,7 @@
* @return Logger handle
*/
NT_Logger AddLogger(std::function<void(const LogMessage& msg)> func,
- unsigned int min_level, unsigned int max_level);
+ unsigned int minLevel, unsigned int maxLevel);
/**
* Remove a logger.
@@ -565,11 +561,11 @@
private:
/* Native handle */
- NT_Inst m_handle;
+ NT_Inst m_handle{0};
};
} // namespace nt
-#include "networktables/NetworkTableInstance.inl"
+#include "networktables/NetworkTableInstance.inc"
#endif // NTCORE_NETWORKTABLES_NETWORKTABLEINSTANCE_H_
diff --git a/ntcore/src/main/native/include/networktables/NetworkTableInstance.inl b/ntcore/src/main/native/include/networktables/NetworkTableInstance.inc
similarity index 69%
rename from ntcore/src/main/native/include/networktables/NetworkTableInstance.inl
rename to ntcore/src/main/native/include/networktables/NetworkTableInstance.inc
index 5321e83..5cb7be0 100644
--- a/ntcore/src/main/native/include/networktables/NetworkTableInstance.inl
+++ b/ntcore/src/main/native/include/networktables/NetworkTableInstance.inc
@@ -1,19 +1,19 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
-#ifndef NTCORE_NETWORKTABLES_NETWORKTABLEINSTANCE_INL_
-#define NTCORE_NETWORKTABLES_NETWORKTABLEINSTANCE_INL_
+#ifndef NTCORE_NETWORKTABLES_NETWORKTABLEINSTANCE_INC_
+#define NTCORE_NETWORKTABLES_NETWORKTABLEINSTANCE_INC_
+#include <string_view>
#include <utility>
#include <vector>
+#include "networktables/NetworkTableInstance.h"
+
namespace nt {
-inline NetworkTableInstance::NetworkTableInstance() noexcept : m_handle{0} {}
+inline NetworkTableInstance::NetworkTableInstance() noexcept {}
inline NetworkTableInstance::NetworkTableInstance(NT_Inst handle) noexcept
: m_handle{handle} {}
@@ -27,25 +27,30 @@
}
inline void NetworkTableInstance::Destroy(NetworkTableInstance inst) {
- if (inst.m_handle != 0) DestroyInstance(inst.m_handle);
+ if (inst.m_handle != 0) {
+ DestroyInstance(inst.m_handle);
+ }
}
-inline NT_Inst NetworkTableInstance::GetHandle() const { return m_handle; }
+inline NT_Inst NetworkTableInstance::GetHandle() const {
+ return m_handle;
+}
-inline NetworkTableEntry NetworkTableInstance::GetEntry(const Twine& name) {
+inline NetworkTableEntry NetworkTableInstance::GetEntry(std::string_view name) {
return NetworkTableEntry{::nt::GetEntry(m_handle, name)};
}
inline std::vector<NetworkTableEntry> NetworkTableInstance::GetEntries(
- const Twine& prefix, unsigned int types) {
+ std::string_view prefix, unsigned int types) {
std::vector<NetworkTableEntry> entries;
- for (auto entry : ::nt::GetEntries(m_handle, prefix, types))
+ for (auto entry : ::nt::GetEntries(m_handle, prefix, types)) {
entries.emplace_back(entry);
+ }
return entries;
}
inline std::vector<EntryInfo> NetworkTableInstance::GetEntryInfo(
- const Twine& prefix, unsigned int types) const {
+ std::string_view prefix, unsigned int types) const {
return ::nt::GetEntryInfo(m_handle, prefix, types);
}
@@ -76,7 +81,7 @@
return ::nt::WaitForRpcCallQueue(m_handle, timeout);
}
-inline void NetworkTableInstance::SetNetworkIdentity(const Twine& name) {
+inline void NetworkTableInstance::SetNetworkIdentity(std::string_view name) {
::nt::SetNetworkIdentity(m_handle, name);
}
@@ -84,19 +89,27 @@
return ::nt::GetNetworkMode(m_handle);
}
-inline void NetworkTableInstance::StartLocal() { ::nt::StartLocal(m_handle); }
+inline void NetworkTableInstance::StartLocal() {
+ ::nt::StartLocal(m_handle);
+}
-inline void NetworkTableInstance::StopLocal() { ::nt::StopLocal(m_handle); }
+inline void NetworkTableInstance::StopLocal() {
+ ::nt::StopLocal(m_handle);
+}
-inline void NetworkTableInstance::StartServer(const Twine& persist_filename,
+inline void NetworkTableInstance::StartServer(std::string_view persist_filename,
const char* listen_address,
unsigned int port) {
::nt::StartServer(m_handle, persist_filename, listen_address, port);
}
-inline void NetworkTableInstance::StopServer() { ::nt::StopServer(m_handle); }
+inline void NetworkTableInstance::StopServer() {
+ ::nt::StopServer(m_handle);
+}
-inline void NetworkTableInstance::StartClient() { ::nt::StartClient(m_handle); }
+inline void NetworkTableInstance::StartClient() {
+ ::nt::StartClient(m_handle);
+}
inline void NetworkTableInstance::StartClient(const char* server_name,
unsigned int port) {
@@ -104,7 +117,7 @@
}
inline void NetworkTableInstance::StartClient(
- ArrayRef<std::pair<StringRef, unsigned int>> servers) {
+ wpi::span<const std::pair<std::string_view, unsigned int>> servers) {
::nt::StartClient(m_handle, servers);
}
@@ -113,7 +126,9 @@
::nt::StartClientTeam(m_handle, team, port);
}
-inline void NetworkTableInstance::StopClient() { ::nt::StopClient(m_handle); }
+inline void NetworkTableInstance::StopClient() {
+ ::nt::StopClient(m_handle);
+}
inline void NetworkTableInstance::SetServer(const char* server_name,
unsigned int port) {
@@ -121,7 +136,7 @@
}
inline void NetworkTableInstance::SetServer(
- ArrayRef<std::pair<StringRef, unsigned int>> servers) {
+ wpi::span<const std::pair<std::string_view, unsigned int>> servers) {
::nt::SetServer(m_handle, servers);
}
@@ -142,7 +157,9 @@
::nt::SetUpdateRate(m_handle, interval);
}
-inline void NetworkTableInstance::Flush() const { ::nt::Flush(m_handle); }
+inline void NetworkTableInstance::Flush() const {
+ ::nt::Flush(m_handle);
+}
inline std::vector<ConnectionInfo> NetworkTableInstance::GetConnections()
const {
@@ -154,23 +171,23 @@
}
inline const char* NetworkTableInstance::SavePersistent(
- const Twine& filename) const {
+ std::string_view filename) const {
return ::nt::SavePersistent(m_handle, filename);
}
inline const char* NetworkTableInstance::LoadPersistent(
- const Twine& filename,
+ std::string_view filename,
std::function<void(size_t line, const char* msg)> warn) {
return ::nt::LoadPersistent(m_handle, filename, warn);
}
inline const char* NetworkTableInstance::SaveEntries(
- const Twine& filename, const Twine& prefix) const {
+ std::string_view filename, std::string_view prefix) const {
return ::nt::SaveEntries(m_handle, filename, prefix);
}
inline const char* NetworkTableInstance::LoadEntries(
- const Twine& filename, const Twine& prefix,
+ std::string_view filename, std::string_view prefix,
std::function<void(size_t line, const char* msg)> warn) {
return ::nt::LoadEntries(m_handle, filename, prefix, warn);
}
@@ -191,4 +208,4 @@
} // namespace nt
-#endif // NTCORE_NETWORKTABLES_NETWORKTABLEINSTANCE_INL_
+#endif // NTCORE_NETWORKTABLES_NETWORKTABLEINSTANCE_INC_
diff --git a/ntcore/src/main/native/include/networktables/NetworkTableType.h b/ntcore/src/main/native/include/networktables/NetworkTableType.h
index 7ac3d9a..d7681f2 100644
--- a/ntcore/src/main/native/include/networktables/NetworkTableType.h
+++ b/ntcore/src/main/native/include/networktables/NetworkTableType.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_NETWORKTABLES_NETWORKTABLETYPE_H_
#define NTCORE_NETWORKTABLES_NETWORKTABLETYPE_H_
diff --git a/ntcore/src/main/native/include/networktables/NetworkTableValue.h b/ntcore/src/main/native/include/networktables/NetworkTableValue.h
index 1b8aabe..fcb9cb5 100644
--- a/ntcore/src/main/native/include/networktables/NetworkTableValue.h
+++ b/ntcore/src/main/native/include/networktables/NetworkTableValue.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_NETWORKTABLES_NETWORKTABLEVALUE_H_
#define NTCORE_NETWORKTABLES_NETWORKTABLEVALUE_H_
@@ -14,22 +11,17 @@
#include <initializer_list>
#include <memory>
#include <string>
+#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
-#include <wpi/ArrayRef.h>
-#include <wpi/StringRef.h>
-#include <wpi/Twine.h>
+#include <wpi/span.h>
#include "ntcore_c.h"
namespace nt {
-using wpi::ArrayRef;
-using wpi::StringRef;
-using wpi::Twine;
-
/**
* A network table entry value.
* @ingroup ntcore_cpp_api
@@ -170,7 +162,7 @@
*
* @return The string value.
*/
- StringRef GetString() const {
+ std::string_view GetString() const {
assert(m_val.type == NT_STRING);
return m_string;
}
@@ -180,7 +172,7 @@
*
* @return The raw value.
*/
- StringRef GetRaw() const {
+ std::string_view GetRaw() const {
assert(m_val.type == NT_RAW);
return m_string;
}
@@ -190,7 +182,7 @@
*
* @return The rpc definition value.
*/
- StringRef GetRpc() const {
+ std::string_view GetRpc() const {
assert(m_val.type == NT_RPC);
return m_string;
}
@@ -200,10 +192,9 @@
*
* @return The boolean array value.
*/
- ArrayRef<int> GetBooleanArray() const {
+ wpi::span<const int> GetBooleanArray() const {
assert(m_val.type == NT_BOOLEAN_ARRAY);
- return ArrayRef<int>(m_val.data.arr_boolean.arr,
- m_val.data.arr_boolean.size);
+ return {m_val.data.arr_boolean.arr, m_val.data.arr_boolean.size};
}
/**
@@ -211,10 +202,9 @@
*
* @return The double array value.
*/
- ArrayRef<double> GetDoubleArray() const {
+ wpi::span<const double> GetDoubleArray() const {
assert(m_val.type == NT_DOUBLE_ARRAY);
- return ArrayRef<double>(m_val.data.arr_double.arr,
- m_val.data.arr_double.size);
+ return {m_val.data.arr_double.arr, m_val.data.arr_double.size};
}
/**
@@ -222,7 +212,7 @@
*
* @return The string array value.
*/
- ArrayRef<std::string> GetStringArray() const {
+ wpi::span<const std::string> GetStringArray() const {
assert(m_val.type == NT_STRING_ARRAY);
return m_string_array;
}
@@ -270,10 +260,10 @@
* time)
* @return The entry value
*/
- static std::shared_ptr<Value> MakeString(const Twine& value,
+ static std::shared_ptr<Value> MakeString(std::string_view value,
uint64_t time = 0) {
auto val = std::make_shared<Value>(NT_STRING, time, private_init());
- val->m_string = value.str();
+ val->m_string = value;
val->m_val.data.v_string.str = const_cast<char*>(val->m_string.c_str());
val->m_val.data.v_string.len = val->m_string.size();
return val;
@@ -291,7 +281,7 @@
typename std::enable_if<std::is_same<T, std::string>::value>::type>
static std::shared_ptr<Value> MakeString(T&& value, uint64_t time = 0) {
auto val = std::make_shared<Value>(NT_STRING, time, private_init());
- val->m_string = std::move(value);
+ val->m_string = std::forward<T>(value);
val->m_val.data.v_string.str = const_cast<char*>(val->m_string.c_str());
val->m_val.data.v_string.len = val->m_string.size();
return val;
@@ -305,7 +295,8 @@
* time)
* @return The entry value
*/
- static std::shared_ptr<Value> MakeRaw(StringRef value, uint64_t time = 0) {
+ static std::shared_ptr<Value> MakeRaw(std::string_view value,
+ uint64_t time = 0) {
auto val = std::make_shared<Value>(NT_RAW, time, private_init());
val->m_string = value;
val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
@@ -325,7 +316,7 @@
typename std::enable_if<std::is_same<T, std::string>::value>::type>
static std::shared_ptr<Value> MakeRaw(T&& value, uint64_t time = 0) {
auto val = std::make_shared<Value>(NT_RAW, time, private_init());
- val->m_string = std::move(value);
+ val->m_string = std::forward<T>(value);
val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
val->m_val.data.v_raw.len = val->m_string.size();
return val;
@@ -339,7 +330,8 @@
* time)
* @return The entry value
*/
- static std::shared_ptr<Value> MakeRpc(StringRef value, uint64_t time = 0) {
+ static std::shared_ptr<Value> MakeRpc(std::string_view value,
+ uint64_t time = 0) {
auto val = std::make_shared<Value>(NT_RPC, time, private_init());
val->m_string = value;
val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
@@ -358,7 +350,7 @@
template <typename T>
static std::shared_ptr<Value> MakeRpc(T&& value, uint64_t time = 0) {
auto val = std::make_shared<Value>(NT_RPC, time, private_init());
- val->m_string = std::move(value);
+ val->m_string = std::forward<T>(value);
val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
val->m_val.data.v_raw.len = val->m_string.size();
return val;
@@ -372,7 +364,7 @@
* time)
* @return The entry value
*/
- static std::shared_ptr<Value> MakeBooleanArray(ArrayRef<bool> value,
+ static std::shared_ptr<Value> MakeBooleanArray(wpi::span<const bool> value,
uint64_t time = 0);
/**
@@ -385,8 +377,7 @@
*/
static std::shared_ptr<Value> MakeBooleanArray(
std::initializer_list<bool> value, uint64_t time = 0) {
- return MakeBooleanArray(wpi::makeArrayRef(value.begin(), value.end()),
- time);
+ return MakeBooleanArray(wpi::span(value.begin(), value.end()), time);
}
/**
@@ -397,7 +388,7 @@
* time)
* @return The entry value
*/
- static std::shared_ptr<Value> MakeBooleanArray(ArrayRef<int> value,
+ static std::shared_ptr<Value> MakeBooleanArray(wpi::span<const int> value,
uint64_t time = 0);
/**
@@ -410,8 +401,7 @@
*/
static std::shared_ptr<Value> MakeBooleanArray(
std::initializer_list<int> value, uint64_t time = 0) {
- return MakeBooleanArray(wpi::makeArrayRef(value.begin(), value.end()),
- time);
+ return MakeBooleanArray(wpi::span(value.begin(), value.end()), time);
}
/**
@@ -422,7 +412,7 @@
* time)
* @return The entry value
*/
- static std::shared_ptr<Value> MakeDoubleArray(ArrayRef<double> value,
+ static std::shared_ptr<Value> MakeDoubleArray(wpi::span<const double> value,
uint64_t time = 0);
/**
@@ -435,7 +425,7 @@
*/
static std::shared_ptr<Value> MakeDoubleArray(
std::initializer_list<double> value, uint64_t time = 0) {
- return MakeDoubleArray(wpi::makeArrayRef(value.begin(), value.end()), time);
+ return MakeDoubleArray(wpi::span(value.begin(), value.end()), time);
}
/**
@@ -446,8 +436,8 @@
* time)
* @return The entry value
*/
- static std::shared_ptr<Value> MakeStringArray(ArrayRef<std::string> value,
- uint64_t time = 0);
+ static std::shared_ptr<Value> MakeStringArray(
+ wpi::span<const std::string> value, uint64_t time = 0);
/**
* Creates a string array entry value.
@@ -459,7 +449,7 @@
*/
static std::shared_ptr<Value> MakeStringArray(
std::initializer_list<std::string> value, uint64_t time = 0) {
- return MakeStringArray(wpi::makeArrayRef(value.begin(), value.end()), time);
+ return MakeStringArray(wpi::span(value.begin(), value.end()), time);
}
/**
@@ -496,7 +486,7 @@
* NetworkTable Value alias for similarity with Java.
* @ingroup ntcore_cpp_api
*/
-typedef Value NetworkTableValue;
+using NetworkTableValue = Value;
} // namespace nt
diff --git a/ntcore/src/main/native/include/networktables/RpcCall.h b/ntcore/src/main/native/include/networktables/RpcCall.h
index fc2e0bf..9c6e9f9 100644
--- a/ntcore/src/main/native/include/networktables/RpcCall.h
+++ b/ntcore/src/main/native/include/networktables/RpcCall.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_NETWORKTABLES_RPCCALL_H_
#define NTCORE_NETWORKTABLES_RPCCALL_H_
@@ -26,7 +23,7 @@
/**
* Construct invalid instance.
*/
- RpcCall() : m_entry(0), m_call(0) {}
+ RpcCall() = default;
/**
* Construct from native handles.
@@ -98,12 +95,12 @@
}
private:
- NT_Entry m_entry;
- NT_RpcCall m_call;
+ NT_Entry m_entry{0};
+ NT_RpcCall m_call{0};
};
} // namespace nt
-#include "networktables/RpcCall.inl"
+#include "networktables/RpcCall.inc"
#endif // NTCORE_NETWORKTABLES_RPCCALL_H_
diff --git a/ntcore/src/main/native/include/networktables/RpcCall.inc b/ntcore/src/main/native/include/networktables/RpcCall.inc
new file mode 100644
index 0000000..5e25b04
--- /dev/null
+++ b/ntcore/src/main/native/include/networktables/RpcCall.inc
@@ -0,0 +1,51 @@
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
+
+#ifndef NTCORE_NETWORKTABLES_RPCCALL_INC_
+#define NTCORE_NETWORKTABLES_RPCCALL_INC_
+
+#include <string>
+#include <utility>
+
+#include "networktables/RpcCall.h"
+#include "ntcore_cpp.h"
+
+namespace nt {
+
+inline RpcCall::RpcCall(RpcCall&& other) noexcept : RpcCall() {
+ swap(*this, other);
+}
+
+inline RpcCall::~RpcCall() {
+ // automatically cancel result if user didn't request it
+ if (m_call != 0) {
+ CancelResult();
+ }
+}
+
+inline bool RpcCall::GetResult(std::string* result) {
+ if (GetRpcResult(m_entry, m_call, result)) {
+ m_call = 0;
+ return true;
+ }
+ return false;
+}
+
+inline bool RpcCall::GetResult(std::string* result, double timeout,
+ bool* timed_out) {
+ if (GetRpcResult(m_entry, m_call, result, timeout, timed_out)) {
+ m_call = 0;
+ return true;
+ }
+ return false;
+}
+
+inline void RpcCall::CancelResult() {
+ CancelRpcResult(m_entry, m_call);
+ m_call = 0;
+}
+
+} // namespace nt
+
+#endif // NTCORE_NETWORKTABLES_RPCCALL_INC_
diff --git a/ntcore/src/main/native/include/networktables/RpcCall.inl b/ntcore/src/main/native/include/networktables/RpcCall.inl
deleted file mode 100644
index faf305d..0000000
--- a/ntcore/src/main/native/include/networktables/RpcCall.inl
+++ /dev/null
@@ -1,51 +0,0 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
-
-#ifndef NTCORE_NETWORKTABLES_RPCCALL_INL_
-#define NTCORE_NETWORKTABLES_RPCCALL_INL_
-
-#include <string>
-#include <utility>
-
-#include "ntcore_cpp.h"
-
-namespace nt {
-
-inline RpcCall::RpcCall(RpcCall&& other) noexcept : RpcCall() {
- swap(*this, other);
-}
-
-inline RpcCall::~RpcCall() {
- // automatically cancel result if user didn't request it
- if (m_call != 0) CancelResult();
-}
-
-inline bool RpcCall::GetResult(std::string* result) {
- if (GetRpcResult(m_entry, m_call, result)) {
- m_call = 0;
- return true;
- }
- return false;
-}
-
-inline bool RpcCall::GetResult(std::string* result, double timeout,
- bool* timed_out) {
- if (GetRpcResult(m_entry, m_call, result, timeout, timed_out)) {
- m_call = 0;
- return true;
- }
- return false;
-}
-
-inline void RpcCall::CancelResult() {
- CancelRpcResult(m_entry, m_call);
- m_call = 0;
-}
-
-} // namespace nt
-
-#endif // NTCORE_NETWORKTABLES_RPCCALL_INL_
diff --git a/ntcore/src/main/native/include/networktables/TableEntryListener.h b/ntcore/src/main/native/include/networktables/TableEntryListener.h
index c4552678..180234f 100644
--- a/ntcore/src/main/native/include/networktables/TableEntryListener.h
+++ b/ntcore/src/main/native/include/networktables/TableEntryListener.h
@@ -1,17 +1,13 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_NETWORKTABLES_TABLEENTRYLISTENER_H_
#define NTCORE_NETWORKTABLES_TABLEENTRYLISTENER_H_
#include <functional>
#include <memory>
-
-#include <wpi/StringRef.h>
+#include <string_view>
namespace nt {
@@ -19,8 +15,6 @@
class NetworkTableEntry;
class Value;
-using wpi::StringRef;
-
/**
* A listener that listens to changes in values in a NetworkTable.
*
@@ -35,10 +29,9 @@
*
* @ingroup ntcore_cpp_api
*/
-typedef std::function<void(NetworkTable* table, StringRef name,
- NetworkTableEntry entry,
- std::shared_ptr<Value> value, int flags)>
- TableEntryListener;
+using TableEntryListener = std::function<void(
+ NetworkTable* table, std::string_view name, NetworkTableEntry entry,
+ std::shared_ptr<Value> value, int flags)>;
} // namespace nt
diff --git a/ntcore/src/main/native/include/networktables/TableListener.h b/ntcore/src/main/native/include/networktables/TableListener.h
index 9940bad..cc1113e 100644
--- a/ntcore/src/main/native/include/networktables/TableListener.h
+++ b/ntcore/src/main/native/include/networktables/TableListener.h
@@ -1,24 +1,18 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_NETWORKTABLES_TABLELISTENER_H_
#define NTCORE_NETWORKTABLES_TABLELISTENER_H_
#include <functional>
#include <memory>
-
-#include <wpi/StringRef.h>
+#include <string_view>
namespace nt {
class NetworkTable;
-using wpi::StringRef;
-
/**
* A listener that listens to new sub-tables in a NetworkTable.
*
@@ -30,9 +24,9 @@
*
* @ingroup ntcore_cpp_api
*/
-typedef std::function<void(NetworkTable* parent, StringRef name,
- std::shared_ptr<NetworkTable> table)>
- TableListener;
+using TableListener =
+ std::function<void(NetworkTable* parent, std::string_view name,
+ std::shared_ptr<NetworkTable> table)>;
} // namespace nt
diff --git a/ntcore/src/main/native/include/ntcore.h b/ntcore/src/main/native/include/ntcore.h
index ff0511a..5cdd473 100644
--- a/ntcore/src/main/native/include/ntcore.h
+++ b/ntcore/src/main/native/include/ntcore.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_NTCORE_H_
#define NTCORE_NTCORE_H_
diff --git a/ntcore/src/main/native/include/ntcore_c.h b/ntcore/src/main/native/include/ntcore_c.h
index 980752f..3ee6d51 100644
--- a/ntcore/src/main/native/include/ntcore_c.h
+++ b/ntcore/src/main/native/include/ntcore_c.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_NTCORE_C_H_
#define NTCORE_NTCORE_C_H_
@@ -16,8 +13,6 @@
#include <stddef.h>
#endif
-#include <wpi/deprecated.h>
-
#ifdef __cplusplus
extern "C" {
#endif
@@ -261,7 +256,7 @@
unsigned int level;
/** The filename of the source file that generated the message. */
- const char* filename;
+ char* filename;
/** The line number in the source file that generated the message. */
unsigned int line;
@@ -330,11 +325,13 @@
* filtered by string prefix and entry type to only return a subset of all
* entries.
*
- * @param prefix entry name required prefix; only entries whose name
- * starts with this string are returned
- * @param prefix_len length of prefix in bytes
- * @param types bitmask of NT_Type values; 0 is treated specially
- * as a "don't care"
+ * @param inst NetworkTable instance
+ * @param prefix entry name required prefix; only entries whose name starts
+ * with this string are returned
+ * @param prefix_len length of prefix in bytes
+ * @param types bitmask of NT_Type values; 0 is treated specially as a
+ * "don't care"
+ * @param count stores number of entry handles returned
* @return Array of entry handles.
*/
NT_Entry* NT_GetEntries(NT_Inst inst, const char* prefix, size_t prefix_len,
@@ -575,6 +572,7 @@
*
* @param poller poller handle
* @param prefix UTF-8 string prefix
+ * @param prefix_len Length of UTF-8 string prefix
* @param flags NT_NotifyKind bitmask
* @return Listener handle
*/
@@ -588,7 +586,7 @@
* The caller is responsible for calling NT_PollEntryListener() to poll.
*
* @param poller poller handle
- * @param prefix UTF-8 string prefix
+ * @param entry entry handle
* @param flags NT_NotifyKind bitmask
* @return Listener handle
*/
@@ -1597,21 +1595,21 @@
/**
* Frees an array of chars.
*
- * @param v_boolean pointer to the char array to free
+ * @param v_char pointer to the char array to free
*/
void NT_FreeCharArray(char* v_char);
/**
* Frees an array of doubles.
*
- * @param v_boolean pointer to the double array to free
+ * @param v_double pointer to the double array to free
*/
void NT_FreeDoubleArray(double* v_double);
/**
* Frees an array of booleans.
*
- * @param v_boolean pointer to the boolean array to free
+ * @param v_boolean pointer to the boolean array to free
*/
void NT_FreeBooleanArray(NT_Bool* v_boolean);
diff --git a/ntcore/src/main/native/include/ntcore_cpp.h b/ntcore/src/main/native/include/ntcore_cpp.h
index af1ea12..2d91fe5 100644
--- a/ntcore/src/main/native/include/ntcore_cpp.h
+++ b/ntcore/src/main/native/include/ntcore_cpp.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_NTCORE_CPP_H_
#define NTCORE_NTCORE_CPP_H_
@@ -14,14 +11,12 @@
#include <functional>
#include <memory>
#include <string>
+#include <string_view>
#include <thread>
#include <utility>
#include <vector>
-#include <wpi/ArrayRef.h>
-#include <wpi/StringRef.h>
-#include <wpi/Twine.h>
-#include <wpi/deprecated.h>
+#include <wpi/span.h>
#include "networktables/NetworkTableValue.h"
@@ -36,10 +31,6 @@
* @{
*/
-using wpi::ArrayRef;
-using wpi::StringRef;
-using wpi::Twine;
-
/** NetworkTables Entry Information */
struct EntryInfo {
/** Entry handle */
@@ -106,8 +97,8 @@
/** NetworkTables RPC Version 1 Definition Parameter */
struct RpcParamDef {
RpcParamDef() = default;
- RpcParamDef(StringRef name_, std::shared_ptr<Value> def_value_)
- : name(name_), def_value(def_value_) {}
+ RpcParamDef(std::string_view name_, std::shared_ptr<Value> def_value_)
+ : name(name_), def_value(std::move(def_value_)) {}
std::string name;
std::shared_ptr<Value> def_value;
@@ -116,7 +107,8 @@
/** NetworkTables RPC Version 1 Definition Result */
struct RpcResultDef {
RpcResultDef() = default;
- RpcResultDef(StringRef name_, NT_Type type_) : name(name_), type(type_) {}
+ RpcResultDef(std::string_view name_, NT_Type type_)
+ : name(name_), type(type_) {}
std::string name;
NT_Type type;
@@ -133,16 +125,20 @@
/** NetworkTables Remote Procedure Call (Server Side) */
class RpcAnswer {
public:
- RpcAnswer() : entry(0), call(0) {}
- RpcAnswer(NT_Entry entry_, NT_RpcCall call_, StringRef name_,
- StringRef params_, const ConnectionInfo& conn_)
- : entry(entry_), call(call_), name(name_), params(params_), conn(conn_) {}
+ RpcAnswer() = default;
+ RpcAnswer(NT_Entry entry_, NT_RpcCall call_, std::string_view name_,
+ std::string_view params_, ConnectionInfo conn_)
+ : entry(entry_),
+ call(call_),
+ name(name_),
+ params(params_),
+ conn(std::move(conn_)) {}
/** Entry handle. */
- NT_Entry entry;
+ NT_Entry entry{0};
/** Call handle. */
- mutable NT_RpcCall call;
+ mutable NT_RpcCall call{0};
/** Entry name. */
std::string name;
@@ -164,7 +160,7 @@
* @param result result raw data that will be provided to remote caller
* @return True if posting the response is valid, otherwise false
*/
- bool PostResponse(StringRef result) const;
+ bool PostResponse(std::string_view result) const;
friend void swap(RpcAnswer& first, RpcAnswer& second) {
using std::swap;
@@ -179,21 +175,21 @@
/** NetworkTables Entry Notification */
class EntryNotification {
public:
- EntryNotification() : listener(0), entry(0), flags(0) {}
+ EntryNotification() = default;
EntryNotification(NT_EntryListener listener_, NT_Entry entry_,
- StringRef name_, std::shared_ptr<Value> value_,
+ std::string_view name_, std::shared_ptr<Value> value_,
unsigned int flags_)
: listener(listener_),
entry(entry_),
name(name_),
- value(value_),
+ value(std::move(value_)),
flags(flags_) {}
/** Listener that was triggered. */
- NT_EntryListener listener;
+ NT_EntryListener listener{0};
/** Entry handle. */
- NT_Entry entry;
+ NT_Entry entry{0};
/** Entry name. */
std::string name;
@@ -205,7 +201,7 @@
* Update flags. For example, NT_NOTIFY_NEW if the key did not previously
* exist.
*/
- unsigned int flags;
+ unsigned int flags{0};
friend void swap(EntryNotification& first, EntryNotification& second) {
using std::swap;
@@ -220,13 +216,13 @@
/** NetworkTables Connection Notification */
class ConnectionNotification {
public:
- ConnectionNotification() : listener(0), connected(false) {}
+ ConnectionNotification() = default;
ConnectionNotification(NT_ConnectionListener listener_, bool connected_,
- const ConnectionInfo& conn_)
- : listener(listener_), connected(connected_), conn(conn_) {}
+ ConnectionInfo conn_)
+ : listener(listener_), connected(connected_), conn(std::move(conn_)) {}
/** Listener that was triggered. */
- NT_ConnectionListener listener;
+ NT_ConnectionListener listener{0};
/** True if event is due to connection being established. */
bool connected = false;
@@ -246,9 +242,9 @@
/** NetworkTables log message. */
class LogMessage {
public:
- LogMessage() : logger(0), level(0), filename(""), line(0) {}
- LogMessage(NT_Logger logger_, unsigned int level_, const char* filename_,
- unsigned int line_, StringRef message_)
+ LogMessage() = default;
+ LogMessage(NT_Logger logger_, unsigned int level_, std::string_view filename_,
+ unsigned int line_, std::string_view message_)
: logger(logger_),
level(level_),
filename(filename_),
@@ -256,16 +252,16 @@
message(message_) {}
/** The logger that generated the message. */
- NT_Logger logger;
+ NT_Logger logger{0};
/** Log level of the message. See NT_LogLevel. */
- unsigned int level;
+ unsigned int level{0};
/** The filename of the source file that generated the message. */
- const char* filename;
+ std::string filename;
/** The line number in the source file that generated the message. */
- unsigned int line;
+ unsigned int line{0};
/** The message. */
std::string message;
@@ -330,7 +326,7 @@
* @param name entry name (UTF-8 string)
* @return entry handle
*/
-NT_Entry GetEntry(NT_Inst inst, const Twine& name);
+NT_Entry GetEntry(NT_Inst inst, std::string_view name);
/**
* Get Entry Handles.
@@ -346,7 +342,7 @@
* as a "don't care"
* @return Array of entry handles.
*/
-std::vector<NT_Entry> GetEntries(NT_Inst inst, const Twine& prefix,
+std::vector<NT_Entry> GetEntries(NT_Inst inst, std::string_view prefix,
unsigned int types);
/**
@@ -381,18 +377,6 @@
* Returns copy of current entry value.
* Note that one of the type options is "unassigned".
*
- * @param name entry name (UTF-8 string)
- * @return entry value
- */
-WPI_DEPRECATED("use NT_Entry function instead")
-std::shared_ptr<Value> GetEntryValue(StringRef name);
-
-/**
- * Get Entry Value.
- *
- * Returns copy of current entry value.
- * Note that one of the type options is "unassigned".
- *
* @param entry entry handle
* @return entry value
*/
@@ -405,20 +389,6 @@
* 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
- */
-WPI_DEPRECATED("use NT_Entry function instead")
-bool SetDefaultEntryValue(StringRef name, std::shared_ptr<Value> value);
-
-/**
- * Set Default Entry Value
- *
- * 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 entry entry handle
* @param value value to be set if name does not exist
* @return False on error (value not set), True on success
@@ -431,19 +401,6 @@
* Sets new entry value. If type of new value differs from the type of the
* currently stored entry, returns error and does not update value.
*
- * @param name entry name (UTF-8 string)
- * @param value new entry value
- * @return False on error (type mismatch), True on success
- */
-WPI_DEPRECATED("use NT_Entry function instead")
-bool SetEntryValue(StringRef name, std::shared_ptr<Value> value);
-
-/**
- * Set Entry Value.
- *
- * Sets new entry value. If type of new value differs from the type of the
- * currently stored entry, returns error and does not update value.
- *
* @param entry entry handle
* @param value new entry value
* @return False on error (type mismatch), True on success
@@ -460,22 +417,6 @@
* This is NOT the preferred method to update a value; generally
* SetEntryValue() should be used instead, with appropriate error handling.
*
- * @param name entry name (UTF-8 string)
- * @param value new entry value
- */
-WPI_DEPRECATED("use NT_Entry function instead")
-void SetEntryTypeValue(StringRef name, std::shared_ptr<Value> value);
-
-/**
- * Set Entry Type and Value.
- *
- * Sets new entry value. If type of new value differs from the type of the
- * currently stored entry, the currently stored entry type is overridden
- * (generally this will generate an Entry Assignment message).
- *
- * This is NOT the preferred method to update a value; generally
- * SetEntryValue() should be used instead, with appropriate error handling.
- *
* @param entry entry handle
* @param value new entry value
*/
@@ -484,15 +425,6 @@
/**
* Set Entry Flags.
*
- * @param name entry name (UTF-8 string)
- * @param flags flags value (bitmask of NT_EntryFlags)
- */
-WPI_DEPRECATED("use NT_Entry function instead")
-void SetEntryFlags(StringRef name, unsigned int flags);
-
-/**
- * Set Entry Flags.
- *
* @param entry entry handle
* @param flags flags value (bitmask of NT_EntryFlags)
*/
@@ -501,15 +433,6 @@
/**
* Get Entry Flags.
*
- * @param name entry name (UTF-8 string)
- * @return Flags value (bitmask of NT_EntryFlags)
- */
-WPI_DEPRECATED("use NT_Entry function instead")
-unsigned int GetEntryFlags(StringRef name);
-
-/**
- * Get Entry Flags.
- *
* @param entry entry handle
* @return Flags value (bitmask of NT_EntryFlags)
*/
@@ -526,22 +449,6 @@
* of direct remote connection(s), but this is not sufficient to determine
* if all nodes in the network are version 3.0 or newer.
*
- * @param name entry name (UTF-8 string)
- */
-WPI_DEPRECATED("use NT_Entry function instead")
-void DeleteEntry(StringRef name);
-
-/**
- * Delete Entry.
- *
- * Deletes an entry. This is a new feature in version 3.0 of the protocol,
- * so this may not have an effect if any other node in the network is not
- * version 3.0 or newer.
- *
- * Note: GetConnections() can be used to determine the protocol version
- * of direct remote connection(s), but this is not sufficient to determine
- * if all nodes in the network are version 3.0 or newer.
- *
* @param entry entry handle
*/
void DeleteEntry(NT_Entry entry);
@@ -556,12 +463,6 @@
* Note: GetConnections() can be used to determine the protocol version
* of direct remote connection(s), but this is not sufficient to determine
* if all nodes in the network are version 3.0 or newer.
- */
-WPI_DEPRECATED("use NT_Inst function instead")
-void DeleteAllEntries();
-
-/**
- * @copydoc DeleteAllEntries()
*
* @param inst instance handle
*/
@@ -575,21 +476,14 @@
* filtered by string prefix and entry type to only return a subset of all
* entries.
*
+ * @param inst instance handle
* @param prefix entry name required prefix; only entries whose name
* starts with this string are returned
* @param types bitmask of NT_Type values; 0 is treated specially
* as a "don't care"
* @return Array of entry information.
*/
-WPI_DEPRECATED("use NT_Inst function instead")
-std::vector<EntryInfo> GetEntryInfo(StringRef prefix, unsigned int types);
-
-/**
- * @copydoc GetEntryInfo(StringRef, unsigned int)
- *
- * @param inst instance handle
- */
-std::vector<EntryInfo> GetEntryInfo(NT_Inst inst, const Twine& prefix,
+std::vector<EntryInfo> GetEntryInfo(NT_Inst inst, std::string_view prefix,
unsigned int types);
/**
@@ -621,30 +515,21 @@
* @param flags update flags; for example, NT_NOTIFY_NEW if the key
* did not previously exist
*/
-typedef std::function<void(NT_EntryListener entry_listener, StringRef name,
- std::shared_ptr<Value> value, unsigned int flags)>
- EntryListenerCallback;
+using EntryListenerCallback =
+ std::function<void(NT_EntryListener entry_listener, std::string_view name,
+ std::shared_ptr<Value> value, unsigned int flags)>;
/**
* Add a listener for all entries starting with a certain prefix.
*
+ * @param inst instance handle
* @param prefix UTF-8 string prefix
* @param callback listener to add
* @param flags NotifyKind bitmask
* @return Listener handle
*/
-WPI_DEPRECATED("use NT_Inst function instead")
-NT_EntryListener AddEntryListener(StringRef prefix,
- EntryListenerCallback callback,
- unsigned int flags);
-
-/**
- * @copydoc AddEntryListener(StringRef, EntryListenerCallback, unsigned int)
- *
- * @param inst instance handle
- */
NT_EntryListener AddEntryListener(
- NT_Inst inst, const Twine& prefix,
+ NT_Inst inst, std::string_view prefix,
std::function<void(const EntryNotification& event)> callback,
unsigned int flags);
@@ -692,7 +577,7 @@
* @return Listener handle
*/
NT_EntryListener AddPolledEntryListener(NT_EntryListenerPoller poller,
- const Twine& prefix,
+ std::string_view prefix,
unsigned int flags);
/**
@@ -700,7 +585,7 @@
* The caller is responsible for calling PollEntryListener() to poll.
*
* @param poller poller handle
- * @param prefix UTF-8 string prefix
+ * @param entry entry handle
* @param flags NotifyKind bitmask
* @return Listener handle
*/
@@ -780,26 +665,17 @@
* @param connected true if event is due to connection being established
* @param conn connection info
*/
-typedef std::function<void(NT_ConnectionListener conn_listener, bool connected,
- const ConnectionInfo& conn)>
- ConnectionListenerCallback;
+using ConnectionListenerCallback =
+ std::function<void(NT_ConnectionListener, bool, const ConnectionInfo&)>;
/**
* Add a connection listener.
*
+ * @param inst instance handle
* @param callback listener to add
* @param immediate_notify notify listener of all existing connections
* @return Listener handle
*/
-WPI_DEPRECATED("use NT_Inst function instead")
-NT_ConnectionListener AddConnectionListener(ConnectionListenerCallback callback,
- bool immediate_notify);
-
-/**
- * @copydoc AddConnectionListener(ConnectionListenerCallback, bool)
- *
- * @param inst instance handle
- */
NT_ConnectionListener AddConnectionListener(
NT_Inst inst,
std::function<void(const ConnectionNotification& event)> callback,
@@ -910,7 +786,7 @@
* @param callback callback function; note the callback function must call
* PostRpcResponse() to provide a response to the call
*/
-void CreateRpc(NT_Entry entry, StringRef def,
+void CreateRpc(NT_Entry entry, std::string_view def,
std::function<void(const RpcAnswer& answer)> callback);
/**
@@ -943,7 +819,8 @@
* @param def RPC definition
* @param poller poller handle
*/
-void CreatePolledRpc(NT_Entry entry, StringRef def, NT_RpcCallPoller poller);
+void CreatePolledRpc(NT_Entry entry, std::string_view def,
+ NT_RpcCallPoller poller);
/**
* Get the next incoming RPC call. This blocks until the next incoming RPC
@@ -1006,7 +883,7 @@
* @param result result raw data that will be provided to remote caller
* @return true if the response was posted, otherwise false
*/
-bool PostRpcResponse(NT_Entry entry, NT_RpcCall call, StringRef result);
+bool PostRpcResponse(NT_Entry entry, NT_RpcCall call, std::string_view result);
/**
* Call a RPC function. May be used on either the client or server.
@@ -1019,7 +896,7 @@
* @return RPC call handle (for use with GetRpcResult() or
* CancelRpcResult()).
*/
-NT_RpcCall CallRpc(NT_Entry entry, StringRef params);
+NT_RpcCall CallRpc(NT_Entry entry, std::string_view params);
/**
* Get the result (return value) of a RPC call. This function blocks until
@@ -1070,7 +947,7 @@
* @param def RPC version 1 definition (output)
* @return True if successfully unpacked, false otherwise.
*/
-bool UnpackRpcDefinition(StringRef packed, RpcDefinition* def);
+bool UnpackRpcDefinition(std::string_view packed, RpcDefinition* def);
/**
* Pack RPC values as required for RPC version 1 definition messages.
@@ -1078,7 +955,7 @@
* @param values array of values to pack
* @return Raw packed bytes.
*/
-std::string PackRpcValues(ArrayRef<std::shared_ptr<Value>> values);
+std::string PackRpcValues(wpi::span<const std::shared_ptr<Value>> values);
/**
* Unpack RPC values as required for RPC version 1 definition messages.
@@ -1087,8 +964,8 @@
* @param types array of data types (as provided in the RPC definition)
* @return Array of values.
*/
-std::vector<std::shared_ptr<Value>> UnpackRpcValues(StringRef packed,
- ArrayRef<NT_Type> types);
+std::vector<std::shared_ptr<Value>> UnpackRpcValues(
+ std::string_view packed, wpi::span<const NT_Type> types);
/** @} */
@@ -1102,25 +979,10 @@
* This is the name used during the initial connection handshake, and is
* visible through ConnectionInfo on the remote node.
*
+ * @param inst instance handle
* @param name identity to advertise
*/
-WPI_DEPRECATED("use NT_Inst function instead")
-void SetNetworkIdentity(StringRef name);
-
-/**
- * @copydoc SetNetworkIdentity(StringRef)
- *
- * @param inst instance handle
- */
-void SetNetworkIdentity(NT_Inst inst, const Twine& name);
-
-/**
- * Get the current network mode.
- *
- * @return Bitmask of NT_NetworkMode.
- */
-WPI_DEPRECATED("use NT_Inst function instead")
-unsigned int GetNetworkMode();
+void SetNetworkIdentity(NT_Inst inst, std::string_view name);
/**
* Get the current network mode.
@@ -1146,32 +1008,18 @@
/**
* Starts a server using the specified filename, listening address, and port.
*
+ * @param inst instance handle
* @param persist_filename the name of the persist file to use (UTF-8 string,
* null terminated)
* @param listen_address the address to listen on, or null to listen on any
* address. (UTF-8 string, null terminated)
* @param port port to communicate over.
*/
-WPI_DEPRECATED("use NT_Inst function instead")
-void StartServer(StringRef persist_filename, const char* listen_address,
- unsigned int port);
-
-/**
- * @copydoc StartServer(StringRef, const char*, unsigned int)
- *
- * @param inst instance handle
- */
-void StartServer(NT_Inst inst, const Twine& persist_filename,
+void StartServer(NT_Inst inst, std::string_view persist_filename,
const char* listen_address, unsigned int port);
/**
* Stops the server if it is running.
- */
-WPI_DEPRECATED("use NT_Inst function instead")
-void StopServer();
-
-/**
- * @copydoc StopServer()
*
* @param inst instance handle
*/
@@ -1179,49 +1027,30 @@
/**
* Starts a client. Use SetServer to set the server name and port.
- */
-WPI_DEPRECATED("use NT_Inst function instead")
-void StartClient();
-
-/**
- * Starts a client using the specified server and port
- *
- * @param server_name server name (UTF-8 string, null terminated)
- * @param port port to communicate over
- */
-WPI_DEPRECATED("use NT_Inst function instead")
-void StartClient(const char* server_name, unsigned int port);
-
-/**
- * Starts a client using the specified (server, port) combinations. The
- * client will attempt to connect to each server in round robin fashion.
- *
- * @param servers array of server name and port pairs
- */
-WPI_DEPRECATED("use NT_Inst function instead")
-void StartClient(ArrayRef<std::pair<StringRef, unsigned int>> servers);
-
-/**
- * @copydoc StartClient()
*
* @param inst instance handle
*/
void StartClient(NT_Inst inst);
/**
- * @copydoc StartClient(const char*, unsigned int)
+ * Starts a client using the specified server and port
*
* @param inst instance handle
+ * @param server_name server name (UTF-8 string, null terminated)
+ * @param port port to communicate over
*/
void StartClient(NT_Inst inst, const char* server_name, unsigned int port);
/**
- * @copydoc StartClient(ArrayRef<std::pair<StringRef, unsigned int>>)
+ * Starts a client using the specified (server, port) combinations. The
+ * client will attempt to connect to each server in round robin fashion.
*
* @param inst instance handle
+ * @param servers array of server name and port pairs
*/
-void StartClient(NT_Inst inst,
- ArrayRef<std::pair<StringRef, unsigned int>> servers);
+void StartClient(
+ NT_Inst inst,
+ wpi::span<const std::pair<std::string_view, unsigned int>> servers);
/**
* Starts a client using commonly known robot addresses for the specified
@@ -1235,12 +1064,7 @@
/**
* Stops the client if it is running.
- */
-WPI_DEPRECATED("use NT_Inst function instead")
-void StopClient();
-
-/**
- * @copydoc StopClient()
+ *
* @param inst instance handle
*/
void StopClient(NT_Inst inst);
@@ -1248,35 +1072,22 @@
/**
* Sets server address and port for client (without restarting client).
*
+ * @param inst instance handle
* @param server_name server name (UTF-8 string, null terminated)
* @param port port to communicate over
*/
-WPI_DEPRECATED("use NT_Inst function instead")
-void SetServer(const char* server_name, unsigned int port);
+void SetServer(NT_Inst inst, const char* server_name, unsigned int port);
/**
* Sets server addresses for client (without restarting client).
* The client will attempt to connect to each server in round robin fashion.
*
+ * @param inst instance handle
* @param servers array of server name and port pairs
*/
-WPI_DEPRECATED("use NT_Inst function instead")
-void SetServer(ArrayRef<std::pair<StringRef, unsigned int>> servers);
-
-/**
- * @copydoc SetServer(const char*, unsigned int)
- *
- * @param inst instance handle
- */
-void SetServer(NT_Inst inst, const char* server_name, unsigned int port);
-
-/**
- * @copydoc SetServer(ArrayRef<std::pair<StringRef, unsigned int>>)
- *
- * @param inst instance handle
- */
-void SetServer(NT_Inst inst,
- ArrayRef<std::pair<StringRef, unsigned int>> servers);
+void SetServer(
+ NT_Inst inst,
+ wpi::span<const std::pair<std::string_view, unsigned int>> servers);
/**
* Sets server addresses and port for client (without restarting client).
@@ -1293,45 +1104,24 @@
* This connects to the Driver Station running on localhost to obtain the
* server IP address.
*
- * @param port server port to use in combination with IP from DS
- */
-WPI_DEPRECATED("use NT_Inst function instead")
-void StartDSClient(unsigned int port);
-
-/**
- * @copydoc StartDSClient(unsigned int)
* @param inst instance handle
+ * @param port server port to use in combination with IP from DS
*/
void StartDSClient(NT_Inst inst, unsigned int port);
-/** Stops requesting server address from Driver Station. */
-WPI_DEPRECATED("use NT_Inst function instead")
-void StopDSClient();
-
/**
- * @copydoc StopDSClient()
+ * Stops requesting server address from Driver Station.
*
* @param inst instance handle
*/
void StopDSClient(NT_Inst inst);
-/** Stops the RPC server if it is running. */
-WPI_DEPRECATED("use NT_Inst function instead")
-void StopRpcServer();
-
/**
* Set the periodic update rate.
* Sets how frequently updates are sent to other nodes over the network.
*
- * @param interval update interval in seconds (range 0.01 to 1.0)
- */
-WPI_DEPRECATED("use NT_Inst function instead")
-void SetUpdateRate(double interval);
-
-/**
- * @copydoc SetUpdateRate(double)
- *
* @param inst instance handle
+ * @param interval update interval in seconds (range 0.01 to 1.0)
*/
void SetUpdateRate(NT_Inst inst, double interval);
@@ -1345,12 +1135,6 @@
* Note: flushes are rate limited to avoid excessive network traffic. If
* the time between calls is too short, the flush will occur after the minimum
* time elapses (rather than immediately).
- */
-WPI_DEPRECATED("use NT_Inst function instead")
-void Flush();
-
-/**
- * @copydoc Flush()
*
* @param inst instance handle
*/
@@ -1360,15 +1144,8 @@
* Get information on the currently established network connections.
* If operating as a client, this will return either zero or one values.
*
- * @return array of connection information
- */
-WPI_DEPRECATED("use NT_Inst function instead")
-std::vector<ConnectionInfo> GetConnections();
-
-/**
- * @copydoc GetConnections()
- *
* @param inst instance handle
+ * @return array of connection information
*/
std::vector<ConnectionInfo> GetConnections(NT_Inst inst);
@@ -1392,39 +1169,24 @@
* but this function provides a way to save persistent values in the same
* format to a file on either a client or a server.
*
+ * @param inst instance handle
* @param filename filename
* @return error string, or nullptr if successful
*/
-WPI_DEPRECATED("use NT_Inst function instead")
-const char* SavePersistent(StringRef filename);
-
-/**
- * @copydoc SavePersistent(StringRef)
- * @param inst instance handle
- */
-const char* SavePersistent(NT_Inst inst, const Twine& filename);
+const char* SavePersistent(NT_Inst inst, std::string_view filename);
/**
* Load persistent values from a file. The server automatically does this
* at startup, but this function provides a way to restore persistent values
* in the same format from a file at any time on either a client or a server.
*
+ * @param inst instance handle
* @param filename filename
* @param warn callback function for warnings
* @return error string, or nullptr if successful
*/
-WPI_DEPRECATED("use NT_Inst function instead")
const char* LoadPersistent(
- StringRef filename, std::function<void(size_t line, const char* msg)> warn);
-
-/**
- * @copydoc LoadPersistent(StringRef, std::function<void(size_t, const
- * char*)>)
- *
- * @param inst instance handle
- */
-const char* LoadPersistent(
- NT_Inst inst, const Twine& filename,
+ NT_Inst inst, std::string_view filename,
std::function<void(size_t line, const char* msg)> warn);
/**
@@ -1436,8 +1198,8 @@
* @param prefix save only keys starting with this prefix
* @return error string, or nullptr if successful
*/
-const char* SaveEntries(NT_Inst inst, const Twine& filename,
- const Twine& prefix);
+const char* SaveEntries(NT_Inst inst, std::string_view filename,
+ std::string_view prefix);
/**
* Load table values from a file. The file format used is identical to
@@ -1449,8 +1211,8 @@
* @param warn callback function for warnings
* @return error string, or nullptr if successful
*/
-const char* LoadEntries(NT_Inst inst, const Twine& filename,
- const Twine& prefix,
+const char* LoadEntries(NT_Inst inst, std::string_view filename,
+ std::string_view prefix,
std::function<void(size_t line, const char* msg)> warn);
/** @} */
@@ -1477,31 +1239,6 @@
*/
/**
- * Log function.
- *
- * @param level log level of the message (see NT_LogLevel)
- * @param file origin source filename
- * @param line origin source line number
- * @param msg message
- */
-typedef std::function<void(unsigned int level, const char* file,
- unsigned int line, const char* msg)>
- LogFunc;
-
-/**
- * Set logger callback function. By default, log messages are sent to stderr;
- * this function changes the log level and sends log messages to the provided
- * callback function instead. The callback function will only be called for
- * log messages with level greater than or equal to min_level; messages lower
- * than this level will be silently ignored.
- *
- * @param func log callback function
- * @param min_level minimum log level
- */
-WPI_DEPRECATED("use NT_Inst function instead")
-void SetLogger(LogFunc func, unsigned int min_level);
-
-/**
* Add logger callback function. By default, log messages are sent to stderr;
* this function sends log messages to the provided callback function instead.
* The callback function will only be called for log messages with level
@@ -1602,7 +1339,7 @@
/** @} */
/** @} */
-inline bool RpcAnswer::PostResponse(StringRef result) const {
+inline bool RpcAnswer::PostResponse(std::string_view result) const {
auto ret = PostRpcResponse(entry, call, result);
call = 0;
return ret;
diff --git a/ntcore/src/main/native/include/ntcore_test.h b/ntcore/src/main/native/include/ntcore_test.h
index 920fd68..65d1243 100644
--- a/ntcore/src/main/native/include/ntcore_test.h
+++ b/ntcore/src/main/native/include/ntcore_test.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_NTCORE_TEST_H_
#define NTCORE_NTCORE_TEST_H_
diff --git a/ntcore/src/main/native/include/tables/ITable.h b/ntcore/src/main/native/include/tables/ITable.h
deleted file mode 100644
index d03aaa7..0000000
--- a/ntcore/src/main/native/include/tables/ITable.h
+++ /dev/null
@@ -1,456 +0,0 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
-
-#ifndef NTCORE_TABLES_ITABLE_H_
-#define NTCORE_TABLES_ITABLE_H_
-
-#include <memory>
-#include <string>
-#include <vector>
-
-#include <wpi/StringRef.h>
-#include <wpi/Twine.h>
-#include <wpi/deprecated.h>
-
-#include "networktables/NetworkTableValue.h"
-
-namespace nt {
-class NetworkTable;
-} // namespace nt
-
-class ITableListener;
-
-/**
- * A table whose values can be read and written to
- */
-class WPI_DEPRECATED("Use NetworkTable directly") ITable {
- public:
- /**
- * Determines whether the given key is in this table.
- *
- * @param key the key to search for
- * @return true if the table as a value assigned to the given key
- */
- virtual bool ContainsKey(const wpi::Twine& key) const = 0;
-
- /**
- * Determines whether there exists a non-empty subtable for this key
- * in this table.
- *
- * @param key the key to search for
- * @return true if there is a subtable with the key which contains at least
- * one key/subtable of its own
- */
- virtual bool ContainsSubTable(const wpi::Twine& key) const = 0;
-
- /**
- * Gets the subtable in this table for the given name.
- *
- * @param key the name of the table relative to this one
- * @return a sub table relative to this one
- */
- virtual std::shared_ptr<nt::NetworkTable> GetSubTable(
- const wpi::Twine& key) const = 0;
-
- /**
- * @param types bitmask of types; 0 is treated as a "don't care".
- * @return keys currently in the table
- */
- virtual std::vector<std::string> GetKeys(int types = 0) const = 0;
-
- /**
- * @return subtables currently in the table
- */
- virtual std::vector<std::string> GetSubTables() const = 0;
-
- /**
- * Makes a key's value persistent through program restarts.
- *
- * @param key the key to make persistent
- */
- virtual void SetPersistent(wpi::StringRef key) = 0;
-
- /**
- * Stop making a key's value persistent through program restarts.
- * The key cannot be null.
- *
- * @param key the key name
- */
- virtual void ClearPersistent(wpi::StringRef key) = 0;
-
- /**
- * Returns whether the value is persistent through program restarts.
- * The key cannot be null.
- *
- * @param key the key name
- */
- virtual bool IsPersistent(wpi::StringRef key) const = 0;
-
- /**
- * Sets flags on the specified key in this table. The key can
- * not be null.
- *
- * @param key the key name
- * @param flags the flags to set (bitmask)
- */
- virtual void SetFlags(wpi::StringRef key, unsigned int flags) = 0;
-
- /**
- * Clears flags on the specified key in this table. The key can
- * not be null.
- *
- * @param key the key name
- * @param flags the flags to clear (bitmask)
- */
- virtual void ClearFlags(wpi::StringRef key, unsigned int flags) = 0;
-
- /**
- * Returns the flags for the specified key.
- *
- * @param key the key name
- * @return the flags, or 0 if the key is not defined
- */
- virtual unsigned int GetFlags(wpi::StringRef key) const = 0;
-
- /**
- * Deletes the specified key in this table.
- *
- * @param key the key name
- */
- virtual void Delete(const wpi::Twine& key) = 0;
-
- /**
- * Gets the value associated with a key as an object
- *
- * @param key the key of the value to look up
- * @return the value associated with the given key, or nullptr if the key
- * does not exist
- */
- virtual std::shared_ptr<nt::Value> GetValue(const wpi::Twine& 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.
- * @returns False if the table key exists with a different type
- */
- virtual bool SetDefaultValue(const wpi::Twine& key,
- std::shared_ptr<nt::Value> defaultValue) = 0;
-
- /**
- * Put a value in the table
- *
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- virtual bool PutValue(const wpi::Twine& key,
- std::shared_ptr<nt::Value> value) = 0;
-
- /**
- * Put a number in the table
- *
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- virtual bool PutNumber(wpi::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.
- * @returns False if the table key exists with a different type
- */
- virtual bool SetDefaultNumber(wpi::StringRef key, double defaultValue) = 0;
-
- /**
- * Gets the number associated with the given name.
- *
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- */
- virtual double GetNumber(wpi::StringRef key, double defaultValue) const = 0;
-
- /**
- * Put a string in the table
- *
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- virtual bool PutString(wpi::StringRef key, wpi::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(wpi::StringRef key,
- wpi::StringRef defaultValue) = 0;
-
- /**
- * Gets the string associated with the given name. If the key does not
- * exist or is of different type, it will return the default value.
- *
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- *
- * @note This makes a copy of the string. If the overhead of this is a
- * concern, use GetValue() instead.
- */
- virtual std::string GetString(wpi::StringRef key,
- wpi::StringRef defaultValue) const = 0;
-
- /**
- * Put a boolean in the table
- *
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- virtual bool PutBoolean(wpi::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.
- * @returns False if the table key exists with a different type
- */
- virtual bool SetDefaultBoolean(wpi::StringRef key, bool defaultValue) = 0;
-
- /**
- * Gets the boolean associated with the given name. If the key does not
- * exist or is of different type, it will return the default value.
- *
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- */
- virtual bool GetBoolean(wpi::StringRef key, bool defaultValue) const = 0;
-
- /**
- * Put a boolean array in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- *
- * @note The array must be of int's rather than of bool's because
- * std::vector<bool> is special-cased in C++. 0 is false, any
- * non-zero value is true.
- */
- virtual bool PutBooleanArray(wpi::StringRef key,
- wpi::ArrayRef<int> 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(wpi::StringRef key,
- wpi::ArrayRef<int> defaultValue) = 0;
-
- /**
- * Returns the boolean array the key maps to. If the key does not exist or is
- * of different type, it will return the default value.
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- *
- * @note This makes a copy of the array. If the overhead of this is a
- * concern, use GetValue() instead.
- *
- * @note The returned array is std::vector<int> instead of std::vector<bool>
- * because std::vector<bool> is special-cased in C++. 0 is false, any
- * non-zero value is true.
- */
- virtual std::vector<int> GetBooleanArray(
- wpi::StringRef key, wpi::ArrayRef<int> defaultValue) const = 0;
-
- /**
- * Put a number array in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- virtual bool PutNumberArray(wpi::StringRef key,
- wpi::ArrayRef<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.
- * @returns False if the table key exists with a different type
- */
- virtual bool SetDefaultNumberArray(wpi::StringRef key,
- wpi::ArrayRef<double> defaultValue) = 0;
-
- /**
- * Returns the number array the key maps to. If the key does not exist or is
- * of different type, it will return the default value.
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- *
- * @note This makes a copy of the array. If the overhead of this is a
- * concern, use GetValue() instead.
- */
- virtual std::vector<double> GetNumberArray(
- wpi::StringRef key, wpi::ArrayRef<double> defaultValue) const = 0;
-
- /**
- * Put a string array in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- virtual bool PutStringArray(wpi::StringRef key,
- wpi::ArrayRef<std::string> 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(
- wpi::StringRef key, wpi::ArrayRef<std::string> defaultValue) = 0;
-
- /**
- * Returns the string array the key maps to. If the key does not exist or is
- * of different type, it will return the default value.
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- *
- * @note This makes a copy of the array. If the overhead of this is a
- * concern, use GetValue() instead.
- */
- virtual std::vector<std::string> GetStringArray(
- wpi::StringRef key, wpi::ArrayRef<std::string> defaultValue) const = 0;
-
- /**
- * Put a raw value (byte array) in the table
- * @param key the key to be assigned to
- * @param value the value that will be assigned
- * @return False if the table key already exists with a different type
- */
- virtual bool PutRaw(wpi::StringRef key, wpi::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(wpi::StringRef key,
- wpi::StringRef defaultValue) = 0;
-
- /**
- * Returns the raw value (byte array) the key maps to. If the key does not
- * exist or is of different type, it will return the default value.
- * @param key the key to look up
- * @param defaultValue the value to be returned if no value is found
- * @return the value associated with the given key or the given default value
- * if there is no value associated with the key
- *
- * @note This makes a copy of the raw contents. If the overhead of this is a
- * concern, use GetValue() instead.
- */
- virtual std::string GetRaw(wpi::StringRef key,
- wpi::StringRef defaultValue) const = 0;
-
- /**
- * Add a listener for changes to the table
- *
- * @param listener the listener to add
- */
- virtual void AddTableListener(ITableListener* listener) = 0;
-
- /**
- * Add a listener for changes to the table
- *
- * @param listener the listener to add
- * @param immediateNotify if true then this listener will be notified of all
- * current entries (marked as new)
- */
- virtual void AddTableListener(ITableListener* listener,
- bool immediateNotify) = 0;
-
- /**
- * Add a listener for changes to the table
- *
- * @param listener the listener to add
- * @param immediateNotify if true then this listener will be notified of all
- * current entries (marked as new)
- * @param flags bitmask of NT_NotifyKind specifying desired notifications
- */
- virtual void AddTableListenerEx(ITableListener* listener,
- unsigned int flags) = 0;
-
- /**
- * Add a listener for changes to a specific key the table
- *
- * @param key the key to listen for
- * @param listener the listener to add
- * @param immediateNotify if true then this listener will be notified of all
- * current entries (marked as new)
- */
- virtual void AddTableListener(wpi::StringRef key, ITableListener* listener,
- bool immediateNotify) = 0;
-
- /**
- * Add a listener for changes to a specific key the table
- *
- * @param key the key to listen for
- * @param listener the listener to add
- * @param immediateNotify if true then this listener will be notified of all
- * current entries (marked as new)
- * @param flags bitmask of NT_NotifyKind specifying desired notifications
- */
- virtual void AddTableListenerEx(wpi::StringRef key, ITableListener* listener,
- unsigned int flags) = 0;
-
- /**
- * This will immediately notify the listener of all current sub tables
- * @param listener the listener to add
- */
- virtual void AddSubTableListener(ITableListener* listener) = 0;
-
- /**
- * This will immediately notify the listener of all current sub tables
- * @param listener the listener to add
- * @param localNotify if true then this listener will be notified of all
- * local changes in addition to all remote changes
- */
- virtual void AddSubTableListener(ITableListener* listener,
- bool localNotify) = 0;
-
- /**
- * Remove a listener from receiving table events
- *
- * @param listener the listener to be removed
- */
- virtual void RemoveTableListener(ITableListener* listener) = 0;
-
- /**
- * Gets the full path of this table.
- */
- virtual wpi::StringRef GetPath() const = 0;
-};
-
-#endif // NTCORE_TABLES_ITABLE_H_
diff --git a/ntcore/src/main/native/include/tables/ITableListener.h b/ntcore/src/main/native/include/tables/ITableListener.h
deleted file mode 100644
index dae6f85..0000000
--- a/ntcore/src/main/native/include/tables/ITableListener.h
+++ /dev/null
@@ -1,68 +0,0 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
-
-#ifndef NTCORE_TABLES_ITABLELISTENER_H_
-#define NTCORE_TABLES_ITABLELISTENER_H_
-
-#include <memory>
-
-#include <wpi/StringRef.h>
-#include <wpi/deprecated.h>
-
-#include "networktables/NetworkTableValue.h"
-
-#ifdef __GNUC__
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
-#elif _WIN32
-#pragma warning(push)
-#pragma warning(disable : 4996)
-#endif
-
-class ITable;
-
-/**
- * A listener that listens to changes in values in a {@link ITable}
- */
-class WPI_DEPRECATED(
- "Use EntryListener, TableEntryListener, or TableListener as appropriate")
- ITableListener {
- public:
- virtual ~ITableListener() = default;
- /**
- * Called when a key-value pair is changed in a {@link ITable}
- * @param source the table the key-value pair exists in
- * @param key the key associated with the value that changed
- * @param value the new value
- * @param isNew true if the key did not previously exist in the table,
- * otherwise it is false
- */
- virtual void ValueChanged(ITable* source, wpi::StringRef key,
- std::shared_ptr<nt::Value> value, bool isNew) = 0;
-
- /**
- * Extended version of ValueChanged. Called when a key-value pair is
- * changed in a {@link ITable}. The default implementation simply calls
- * ValueChanged(). If this is overridden, ValueChanged() will not be called.
- * @param source the table the key-value pair exists in
- * @param key the key associated with the value that changed
- * @param value the new value
- * @param flags update flags; for example, NT_NOTIFY_NEW if the key did not
- * previously exist in the table
- */
- virtual void ValueChangedEx(ITable* source, wpi::StringRef key,
- std::shared_ptr<nt::Value> value,
- unsigned int flags);
-};
-
-#ifdef __GNUC__
-#pragma GCC diagnostic pop
-#elif _WIN32
-#pragma warning(pop)
-#endif
-
-#endif // NTCORE_TABLES_ITABLELISTENER_H_
diff --git a/ntcore/src/test/java/edu/wpi/first/networktables/ConnectionListenerTest.java b/ntcore/src/test/java/edu/wpi/first/networktables/ConnectionListenerTest.java
index 9628a0e..3c38853 100644
--- a/ntcore/src/test/java/edu/wpi/first/networktables/ConnectionListenerTest.java
+++ b/ntcore/src/test/java/edu/wpi/first/networktables/ConnectionListenerTest.java
@@ -1,23 +1,9 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.condition.DisabledOnOs;
-import org.junit.jupiter.api.condition.OS;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.ValueSource;
-
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -25,6 +11,16 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledOnOs;
+import org.junit.jupiter.api.condition.OS;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
class ConnectionListenerTest {
private NetworkTableInstance m_serverInst;
private NetworkTableInstance m_clientInst;
@@ -44,13 +40,10 @@
m_serverInst.close();
}
- /**
- * Connect to the server.
- */
- @SuppressWarnings("PMD.AvoidUsingHardCodedIP")
- private void connect() {
- m_serverInst.startServer("connectionlistenertest.ini", "127.0.0.1", 10000);
- m_clientInst.startClient("127.0.0.1", 10000);
+ /** Connect to the server. */
+ private void connect(int port) {
+ m_serverInst.startServer("connectionlistenertest.ini", "127.0.0.1", port);
+ m_clientInst.startClient("127.0.0.1", port);
// wait for client to report it's started, then wait another 0.1 sec
try {
@@ -73,7 +66,7 @@
assertNotSame(handle, 0, "bad listener handle");
// trigger a connect event
- connect();
+ connect(10020);
// get the event
assertTrue(m_serverInst.waitForConnectionListenerQueue(1.0));
@@ -111,20 +104,21 @@
assertEquals(1, events.length);
assertEquals(handle, events[0].listener);
assertFalse(events[0].connected);
-
}
+ private static int threadedPort = 10001;
+
@ParameterizedTest
@DisabledOnOs(OS.WINDOWS)
- @SuppressWarnings("PMD.AvoidUsingHardCodedIP")
- @ValueSource(strings = { "127.0.0.1", "127.0.0.1 ", " 127.0.0.1 " })
+ @ValueSource(strings = {"127.0.0.1", "127.0.0.1 ", " 127.0.0.1 "})
void testThreaded(String address) {
- m_serverInst.startServer("connectionlistenertest.ini", address, 10000);
+ m_serverInst.startServer("connectionlistenertest.ini", address, threadedPort);
List<ConnectionNotification> events = new ArrayList<>();
final int handle = m_serverInst.addConnectionListener(events::add, false);
// trigger a connect event
- m_clientInst.startClient(address, 10000);
+ m_clientInst.startClient(address, threadedPort);
+ threadedPort++;
// wait for client to report it's started, then wait another 0.1 sec
try {
diff --git a/ntcore/src/test/java/edu/wpi/first/networktables/EntryListenerTest.java b/ntcore/src/test/java/edu/wpi/first/networktables/EntryListenerTest.java
index 80627a4..7b31c26 100644
--- a/ntcore/src/test/java/edu/wpi/first/networktables/EntryListenerTest.java
+++ b/ntcore/src/test/java/edu/wpi/first/networktables/EntryListenerTest.java
@@ -1,24 +1,20 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
class EntryListenerTest {
private NetworkTableInstance m_serverInst;
private NetworkTableInstance m_clientInst;
@@ -38,10 +34,9 @@
m_serverInst.close();
}
- @SuppressWarnings("PMD.AvoidUsingHardCodedIP")
private void connect() {
- m_serverInst.startServer("connectionlistenertest.ini", "127.0.0.1", 10000);
- m_clientInst.startClient("127.0.0.1", 10000);
+ m_serverInst.startServer("connectionlistenertest.ini", "127.0.0.1", 10010);
+ m_clientInst.startClient("127.0.0.1", 10010);
// Use connection listener to ensure we've connected
int poller = NetworkTablesJNI.createConnectionListenerPoller(m_clientInst.getHandle());
@@ -56,15 +51,12 @@
}
}
- /**
- * Test prefix with a new remote.
- */
+ /** Test prefix with a new remote. */
@Test
void testPrefixNewRemote() {
connect();
List<EntryNotification> events = new ArrayList<>();
- final int handle = m_serverInst.addEntryListener("/foo", events::add,
- EntryListenerFlags.kNew);
+ final int handle = m_serverInst.addEntryListener("/foo", events::add, EntryListenerFlags.kNew);
// Trigger an event
m_clientInst.getEntry("/foo/bar").setDouble(1.0);
@@ -79,13 +71,13 @@
assertTrue(m_serverInst.waitForEntryListenerQueue(1.0));
// Check the event
- assertAll("Event",
+ assertAll(
+ "Event",
() -> assertEquals(1, events.size()),
() -> assertEquals(handle, events.get(0).listener),
() -> assertEquals(m_serverInst.getEntry("/foo/bar"), events.get(0).getEntry()),
() -> assertEquals("/foo/bar", events.get(0).name),
() -> assertEquals(NetworkTableValue.makeDouble(1.0), events.get(0).value),
- () -> assertEquals(EntryListenerFlags.kNew, events.get(0).flags)
- );
+ () -> assertEquals(EntryListenerFlags.kNew, events.get(0).flags));
}
}
diff --git a/ntcore/src/test/java/edu/wpi/first/networktables/JNITest.java b/ntcore/src/test/java/edu/wpi/first/networktables/JNITest.java
index ef2b42b..3eb522b 100644
--- a/ntcore/src/test/java/edu/wpi/first/networktables/JNITest.java
+++ b/ntcore/src/test/java/edu/wpi/first/networktables/JNITest.java
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
diff --git a/ntcore/src/test/java/edu/wpi/first/networktables/LoggerTest.java b/ntcore/src/test/java/edu/wpi/first/networktables/LoggerTest.java
index 420f9dc..1dc26ab 100644
--- a/ntcore/src/test/java/edu/wpi/first/networktables/LoggerTest.java
+++ b/ntcore/src/test/java/edu/wpi/first/networktables/LoggerTest.java
@@ -1,22 +1,18 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.fail;
+
import java.util.ArrayList;
import java.util.List;
-
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.fail;
-
class LoggerTest {
private NetworkTableInstance m_clientInst;
@@ -31,7 +27,6 @@
}
@Test
- @SuppressWarnings("PMD.AvoidUsingHardCodedIP")
void addMessageTest() {
List<LogMessage> msgs = new ArrayList<>();
m_clientInst.addLogger(msgs::add, LogMessage.kInfo, 100);
diff --git a/ntcore/src/test/java/edu/wpi/first/networktables/NetworkTableTest.java b/ntcore/src/test/java/edu/wpi/first/networktables/NetworkTableTest.java
index 71ce0ae..4c431af 100644
--- a/ntcore/src/test/java/edu/wpi/first/networktables/NetworkTableTest.java
+++ b/ntcore/src/test/java/edu/wpi/first/networktables/NetworkTableTest.java
@@ -1,30 +1,25 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.networktables;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
-
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
class NetworkTableTest {
private static Stream<Arguments> basenameKeyArguments() {
return Stream.of(
Arguments.of("simple", "simple"),
Arguments.of("simple", "one/two/many/simple"),
- Arguments.of("simple", "//////an/////awful/key////simple")
- );
+ Arguments.of("simple", "//////an/////awful/key////simple"));
}
@ParameterizedTest
@@ -38,8 +33,7 @@
Arguments.of("/", "///"),
Arguments.of("/no/normal/req", "/no/normal/req"),
Arguments.of("/no/leading/slash", "no/leading/slash"),
- Arguments.of("/what/an/awful/key/", "//////what////an/awful/////key///")
- );
+ Arguments.of("/what/an/awful/key/", "//////what////an/awful/////key///"));
}
@ParameterizedTest
@@ -54,8 +48,7 @@
Arguments.of("a", "///a"),
Arguments.of("leading/slash", "/leading/slash"),
Arguments.of("no/leading/slash", "no/leading/slash"),
- Arguments.of("what/an/awful/key/", "//////what////an/awful/////key///")
- );
+ Arguments.of("what/an/awful/key/", "//////what////an/awful/////key///"));
}
@ParameterizedTest
@@ -69,8 +62,7 @@
Arguments.of(Collections.singletonList("/"), ""),
Arguments.of(Collections.singletonList("/"), "/"),
Arguments.of(Arrays.asList("/", "/foo", "/foo/bar", "/foo/bar/baz"), "/foo/bar/baz"),
- Arguments.of(Arrays.asList("/", "/foo", "/foo/bar", "/foo/bar/"), "/foo/bar/")
- );
+ Arguments.of(Arrays.asList("/", "/foo", "/foo/bar", "/foo/bar/"), "/foo/bar/"));
}
@ParameterizedTest
diff --git a/ntcore/src/test/native/cpp/ConnectionListenerTest.cpp b/ntcore/src/test/native/cpp/ConnectionListenerTest.cpp
index a56e45c..14c327c 100644
--- a/ntcore/src/test/native/cpp/ConnectionListenerTest.cpp
+++ b/ntcore/src/test/native/cpp/ConnectionListenerTest.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <chrono>
#include <thread>
@@ -25,21 +22,21 @@
nt::DestroyInstance(client_inst);
}
- void Connect();
+ void Connect(unsigned int port);
protected:
NT_Inst server_inst;
NT_Inst client_inst;
};
-void ConnectionListenerTest::Connect() {
- nt::StartServer(server_inst, "connectionlistenertest.ini", "127.0.0.1",
- 10000);
- nt::StartClient(client_inst, "127.0.0.1", 10000);
+void ConnectionListenerTest::Connect(unsigned int port) {
+ nt::StartServer(server_inst, "connectionlistenertest.ini", "127.0.0.1", port);
+ nt::StartClient(client_inst, "127.0.0.1", port);
// wait for client to report it's started, then wait another 0.1 sec
- while ((nt::GetNetworkMode(client_inst) & NT_NET_MODE_STARTING) != 0)
+ while ((nt::GetNetworkMode(client_inst) & NT_NET_MODE_STARTING) != 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
+ }
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
@@ -52,7 +49,7 @@
ASSERT_NE(handle, 0u);
// trigger a connect event
- Connect();
+ Connect(10000);
// get the event
ASSERT_TRUE(nt::WaitForConnectionListenerQueue(server_inst, 1.0));
@@ -87,7 +84,7 @@
false);
// trigger a connect event
- Connect();
+ Connect(10001);
ASSERT_TRUE(nt::WaitForConnectionListenerQueue(server_inst, 1.0));
diff --git a/ntcore/src/test/native/cpp/EntryListenerTest.cpp b/ntcore/src/test/native/cpp/EntryListenerTest.cpp
index b7bf2f6..8349484 100644
--- a/ntcore/src/test/native/cpp/EntryListenerTest.cpp
+++ b/ntcore/src/test/native/cpp/EntryListenerTest.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <chrono>
#include <thread>
@@ -38,16 +35,16 @@
nt::DestroyInstance(client_inst);
}
- void Connect();
+ void Connect(unsigned int port);
protected:
NT_Inst server_inst;
NT_Inst client_inst;
};
-void EntryListenerTest::Connect() {
- nt::StartServer(server_inst, "entrylistenertest.ini", "127.0.0.1", 10000);
- nt::StartClient(client_inst, "127.0.0.1", 10000);
+void EntryListenerTest::Connect(unsigned int port) {
+ nt::StartServer(server_inst, "entrylistenertest.ini", "127.0.0.1", port);
+ nt::StartClient(client_inst, "127.0.0.1", port);
// Use connection listener to ensure we've connected
NT_ConnectionListenerPoller poller =
@@ -84,8 +81,10 @@
}
TEST_F(EntryListenerTest, DISABLED_EntryNewRemote) {
- Connect();
- if (HasFatalFailure()) return;
+ Connect(10010);
+ if (HasFatalFailure()) {
+ return;
+ }
std::vector<nt::EntryNotification> events;
auto handle = nt::AddEntryListener(
nt::GetEntry(server_inst, "/foo"),
@@ -136,8 +135,10 @@
}
TEST_F(EntryListenerTest, DISABLED_PrefixNewRemote) {
- Connect();
- if (HasFatalFailure()) return;
+ Connect(10011);
+ if (HasFatalFailure()) {
+ return;
+ }
std::vector<nt::EntryNotification> events;
auto handle = nt::AddEntryListener(
server_inst, "/foo",
diff --git a/ntcore/src/test/native/cpp/EntryNotifierTest.cpp b/ntcore/src/test/native/cpp/EntryNotifierTest.cpp
index 604db3d..e781b49 100644
--- a/ntcore/src/test/native/cpp/EntryNotifierTest.cpp
+++ b/ntcore/src/test/native/cpp/EntryNotifierTest.cpp
@@ -1,11 +1,9 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <wpi/Logger.h>
+#include <wpi/StringExtras.h>
#include "EntryNotifier.h"
#include "TestPrinters.h"
@@ -248,7 +246,7 @@
int g4count = 0;
for (const auto& result : results) {
SCOPED_TRACE(::testing::PrintToString(result));
- EXPECT_TRUE(StringRef(result.name).startswith("/foo"));
+ EXPECT_TRUE(wpi::starts_with(result.name, "/foo"));
EXPECT_THAT(result.value, ValueEq(Value::MakeDouble(1)));
EXPECT_EQ(Handle{result.entry}.GetType(), Handle::kEntry);
EXPECT_EQ(Handle{result.entry}.GetInst(), 1);
diff --git a/ntcore/src/test/native/cpp/MessageMatcher.cpp b/ntcore/src/test/native/cpp/MessageMatcher.cpp
index 35d4f8b..69c87a0 100644
--- a/ntcore/src/test/native/cpp/MessageMatcher.cpp
+++ b/ntcore/src/test/native/cpp/MessageMatcher.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "MessageMatcher.h"
@@ -13,7 +10,9 @@
std::shared_ptr<Message> msg,
::testing::MatchResultListener* listener) const {
bool match = true;
- if (!msg) return false;
+ if (!msg) {
+ return false;
+ }
if (msg->str() != goodmsg->str()) {
*listener << "str mismatch ";
match = false;
diff --git a/ntcore/src/test/native/cpp/MessageMatcher.h b/ntcore/src/test/native/cpp/MessageMatcher.h
index 5b14334..7afeeef 100644
--- a/ntcore/src/test/native/cpp/MessageMatcher.h
+++ b/ntcore/src/test/native/cpp/MessageMatcher.h
@@ -1,15 +1,13 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_MESSAGEMATCHER_H_
#define NTCORE_MESSAGEMATCHER_H_
#include <memory>
#include <ostream>
+#include <utility>
#include "Message.h"
#include "TestPrinters.h"
@@ -21,7 +19,7 @@
: public ::testing::MatcherInterface<std::shared_ptr<Message>> {
public:
explicit MessageMatcher(std::shared_ptr<Message> goodmsg_)
- : goodmsg(goodmsg_) {}
+ : goodmsg(std::move(goodmsg_)) {}
bool MatchAndExplain(std::shared_ptr<Message> msg,
::testing::MatchResultListener* listener) const override;
diff --git a/ntcore/src/test/native/cpp/MockConnectionNotifier.h b/ntcore/src/test/native/cpp/MockConnectionNotifier.h
index ddf8b2e..d632d5c 100644
--- a/ntcore/src/test/native/cpp/MockConnectionNotifier.h
+++ b/ntcore/src/test/native/cpp/MockConnectionNotifier.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_MOCKCONNECTIONNOTIFIER_H_
#define NTCORE_MOCKCONNECTIONNOTIFIER_H_
diff --git a/ntcore/src/test/native/cpp/MockDispatcher.h b/ntcore/src/test/native/cpp/MockDispatcher.h
index 10af839..22b0fba 100644
--- a/ntcore/src/test/native/cpp/MockDispatcher.h
+++ b/ntcore/src/test/native/cpp/MockDispatcher.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_MOCKDISPATCHER_H_
#define NTCORE_MOCKDISPATCHER_H_
diff --git a/ntcore/src/test/native/cpp/MockEntryNotifier.h b/ntcore/src/test/native/cpp/MockEntryNotifier.h
index 2f078cb..58518c6 100644
--- a/ntcore/src/test/native/cpp/MockEntryNotifier.h
+++ b/ntcore/src/test/native/cpp/MockEntryNotifier.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_MOCKENTRYNOTIFIER_H_
#define NTCORE_MOCKENTRYNOTIFIER_H_
@@ -21,19 +18,19 @@
MOCK_METHOD3(
Add,
unsigned int(std::function<void(const EntryNotification& event)> callback,
- wpi::StringRef prefix, unsigned int flags));
+ std::string_view prefix, unsigned int flags));
MOCK_METHOD3(
Add,
unsigned int(std::function<void(const EntryNotification& event)> callback,
unsigned int local_id, unsigned int flags));
MOCK_METHOD3(AddPolled,
- unsigned int(unsigned int poller_uid, wpi::StringRef prefix,
+ unsigned int(unsigned int poller_uid, std::string_view prefix,
unsigned int flags));
MOCK_METHOD3(AddPolled,
unsigned int(unsigned int poller_uid, unsigned int local_id,
unsigned int flags));
MOCK_METHOD5(NotifyEntry,
- void(unsigned int local_id, StringRef name,
+ void(unsigned int local_id, std::string_view name,
std::shared_ptr<Value> value, unsigned int flags,
unsigned int only_listener));
};
diff --git a/ntcore/src/test/native/cpp/MockNetworkConnection.h b/ntcore/src/test/native/cpp/MockNetworkConnection.h
index 52c917d..be9c2c6 100644
--- a/ntcore/src/test/native/cpp/MockNetworkConnection.h
+++ b/ntcore/src/test/native/cpp/MockNetworkConnection.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_MOCKNETWORKCONNECTION_H_
#define NTCORE_MOCKNETWORKCONNECTION_H_
diff --git a/ntcore/src/test/native/cpp/MockRpcServer.h b/ntcore/src/test/native/cpp/MockRpcServer.h
index 6e9d970..be9e512 100644
--- a/ntcore/src/test/native/cpp/MockRpcServer.h
+++ b/ntcore/src/test/native/cpp/MockRpcServer.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_MOCKRPCSERVER_H_
#define NTCORE_MOCKRPCSERVER_H_
@@ -19,7 +16,7 @@
MOCK_METHOD1(RemoveRpc, void(unsigned int rpc_uid));
MOCK_METHOD7(ProcessRpc,
void(unsigned int local_id, unsigned int call_uid,
- StringRef name, StringRef params,
+ std::string_view name, std::string_view params,
const ConnectionInfo& conn, SendResponseFunc send_response,
unsigned int rpc_uid));
};
diff --git a/ntcore/src/test/native/cpp/NetworkTableTest.cpp b/ntcore/src/test/native/cpp/NetworkTableTest.cpp
index d9a2743..73c4786 100644
--- a/ntcore/src/test/native/cpp/NetworkTableTest.cpp
+++ b/ntcore/src/test/native/cpp/NetworkTableTest.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "TestPrinters.h"
#include "gtest/gtest.h"
@@ -13,51 +10,51 @@
class NetworkTableTest : public ::testing::Test {};
TEST_F(NetworkTableTest, BasenameKey) {
- EXPECT_EQ("simple", NetworkTable::BasenameKey("simple"));
- EXPECT_EQ("simple", NetworkTable::BasenameKey("one/two/many/simple"));
+ EXPECT_EQ("simple", nt::NetworkTable::BasenameKey("simple"));
+ EXPECT_EQ("simple", nt::NetworkTable::BasenameKey("one/two/many/simple"));
EXPECT_EQ("simple",
- NetworkTable::BasenameKey("//////an/////awful/key////simple"));
+ nt::NetworkTable::BasenameKey("//////an/////awful/key////simple"));
}
TEST_F(NetworkTableTest, NormalizeKeySlash) {
- EXPECT_EQ("/", NetworkTable::NormalizeKey("///"));
- EXPECT_EQ("/no/normal/req", NetworkTable::NormalizeKey("/no/normal/req"));
+ EXPECT_EQ("/", nt::NetworkTable::NormalizeKey("///"));
+ EXPECT_EQ("/no/normal/req", nt::NetworkTable::NormalizeKey("/no/normal/req"));
EXPECT_EQ("/no/leading/slash",
- NetworkTable::NormalizeKey("no/leading/slash"));
- EXPECT_EQ("/what/an/awful/key/",
- NetworkTable::NormalizeKey("//////what////an/awful/////key///"));
+ nt::NetworkTable::NormalizeKey("no/leading/slash"));
+ EXPECT_EQ("/what/an/awful/key/", nt::NetworkTable::NormalizeKey(
+ "//////what////an/awful/////key///"));
}
TEST_F(NetworkTableTest, NormalizeKeyNoSlash) {
- EXPECT_EQ("a", NetworkTable::NormalizeKey("a", false));
- EXPECT_EQ("a", NetworkTable::NormalizeKey("///a", false));
+ EXPECT_EQ("a", nt::NetworkTable::NormalizeKey("a", false));
+ EXPECT_EQ("a", nt::NetworkTable::NormalizeKey("///a", false));
EXPECT_EQ("leading/slash",
- NetworkTable::NormalizeKey("/leading/slash", false));
+ nt::NetworkTable::NormalizeKey("/leading/slash", false));
EXPECT_EQ("no/leading/slash",
- NetworkTable::NormalizeKey("no/leading/slash", false));
- EXPECT_EQ(
- "what/an/awful/key/",
- NetworkTable::NormalizeKey("//////what////an/awful/////key///", false));
+ nt::NetworkTable::NormalizeKey("no/leading/slash", false));
+ EXPECT_EQ("what/an/awful/key/",
+ nt::NetworkTable::NormalizeKey("//////what////an/awful/////key///",
+ false));
}
TEST_F(NetworkTableTest, GetHierarchyEmpty) {
std::vector<std::string> expected{"/"};
- ASSERT_EQ(expected, NetworkTable::GetHierarchy(""));
+ ASSERT_EQ(expected, nt::NetworkTable::GetHierarchy(""));
}
TEST_F(NetworkTableTest, GetHierarchyRoot) {
std::vector<std::string> expected{"/"};
- ASSERT_EQ(expected, NetworkTable::GetHierarchy("/"));
+ ASSERT_EQ(expected, nt::NetworkTable::GetHierarchy("/"));
}
TEST_F(NetworkTableTest, GetHierarchyNormal) {
std::vector<std::string> expected{"/", "/foo", "/foo/bar", "/foo/bar/baz"};
- ASSERT_EQ(expected, NetworkTable::GetHierarchy("/foo/bar/baz"));
+ ASSERT_EQ(expected, nt::NetworkTable::GetHierarchy("/foo/bar/baz"));
}
TEST_F(NetworkTableTest, GetHierarchyTrailingSlash) {
std::vector<std::string> expected{"/", "/foo", "/foo/bar", "/foo/bar/"};
- ASSERT_EQ(expected, NetworkTable::GetHierarchy("/foo/bar/"));
+ ASSERT_EQ(expected, nt::NetworkTable::GetHierarchy("/foo/bar/"));
}
TEST_F(NetworkTableTest, ContainsKey) {
@@ -68,6 +65,7 @@
ASSERT_TRUE(nt->ContainsKey("testkey"));
ASSERT_TRUE(inst.GetEntry("/containskey/testkey").Exists());
ASSERT_FALSE(inst.GetEntry("containskey/testkey").Exists());
+ nt::NetworkTableInstance::Destroy(inst);
}
TEST_F(NetworkTableTest, LeadingSlash) {
@@ -78,6 +76,7 @@
nt2->PutNumber("testkey", 5);
ASSERT_TRUE(nt->ContainsKey("testkey"));
ASSERT_TRUE(inst.GetEntry("/leadingslash/testkey").Exists());
+ nt::NetworkTableInstance::Destroy(inst);
}
TEST_F(NetworkTableTest, EmptyOrNoSlash) {
@@ -88,4 +87,5 @@
nt2->PutNumber("testkey", 5);
ASSERT_TRUE(nt->ContainsKey("testkey"));
ASSERT_TRUE(inst.GetEntry("/testkey").Exists());
+ nt::NetworkTableInstance::Destroy(inst);
}
diff --git a/ntcore/src/test/native/cpp/StorageTest.cpp b/ntcore/src/test/native/cpp/StorageTest.cpp
index f271123..e1ee1c7 100644
--- a/ntcore/src/test/native/cpp/StorageTest.cpp
+++ b/ntcore/src/test/native/cpp/StorageTest.cpp
@@ -1,12 +1,11 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "StorageTest.h"
+#include <wpi/SmallString.h>
+#include <wpi/StringExtras.h>
#include <wpi/raw_istream.h>
#include <wpi/raw_ostream.h>
@@ -25,10 +24,10 @@
namespace nt {
-class StorageTestEmpty : public StorageTest,
+class StorageEmptyTest : public StorageTest,
public ::testing::TestWithParam<bool> {
public:
- StorageTestEmpty() {
+ StorageEmptyTest() {
HookOutgoing(GetParam());
EXPECT_CALL(notifier, local_notifiers())
.Times(AnyNumber())
@@ -36,9 +35,9 @@
}
};
-class StorageTestPopulateOne : public StorageTestEmpty {
+class StoragePopulateOneTest : public StorageEmptyTest {
public:
- StorageTestPopulateOne() {
+ StoragePopulateOneTest() {
EXPECT_CALL(dispatcher, QueueOutgoing(_, _, _)).Times(AnyNumber());
EXPECT_CALL(notifier, NotifyEntry(_, _, _, _, _)).Times(AnyNumber());
EXPECT_CALL(notifier, local_notifiers())
@@ -53,9 +52,9 @@
}
};
-class StorageTestPopulated : public StorageTestEmpty {
+class StoragePopulatedTest : public StorageEmptyTest {
public:
- StorageTestPopulated() {
+ StoragePopulatedTest() {
EXPECT_CALL(dispatcher, QueueOutgoing(_, _, _)).Times(AnyNumber());
EXPECT_CALL(notifier, NotifyEntry(_, _, _, _, _)).Times(AnyNumber());
EXPECT_CALL(notifier, local_notifiers())
@@ -73,9 +72,9 @@
}
};
-class StorageTestPersistent : public StorageTestEmpty {
+class StoragePersistentTest : public StorageEmptyTest {
public:
- StorageTestPersistent() {
+ StoragePersistentTest() {
EXPECT_CALL(dispatcher, QueueOutgoing(_, _, _)).Times(AnyNumber());
EXPECT_CALL(notifier, NotifyEntry(_, _, _, _, _)).Times(AnyNumber());
EXPECT_CALL(notifier, local_notifiers())
@@ -88,13 +87,13 @@
storage.SetEntryTypeValue("double/big", Value::MakeDouble(1.3e8));
storage.SetEntryTypeValue("string/empty", Value::MakeString(""));
storage.SetEntryTypeValue("string/normal", Value::MakeString("hello"));
- storage.SetEntryTypeValue("string/special",
- Value::MakeString(StringRef("\0\3\5\n", 4)));
+ storage.SetEntryTypeValue(
+ "string/special", Value::MakeString(std::string_view("\0\3\5\n", 4)));
storage.SetEntryTypeValue("string/quoted", Value::MakeString("\"a\""));
storage.SetEntryTypeValue("raw/empty", Value::MakeRaw(""));
storage.SetEntryTypeValue("raw/normal", Value::MakeRaw("hello"));
storage.SetEntryTypeValue("raw/special",
- Value::MakeRaw(StringRef("\0\3\5\n", 4)));
+ Value::MakeRaw(std::string_view("\0\3\5\n", 4)));
storage.SetEntryTypeValue("booleanarr/empty",
Value::MakeBooleanArray(std::vector<int>{}));
storage.SetEntryTypeValue("booleanarr/one",
@@ -116,7 +115,7 @@
storage.SetEntryTypeValue(
"stringarr/two",
Value::MakeStringArray(std::vector<std::string>{"hello", "world\n"}));
- storage.SetEntryTypeValue(StringRef("\0\3\5\n", 4),
+ storage.SetEntryTypeValue(std::string_view("\0\3\5\n", 4),
Value::MakeBoolean(true));
storage.SetEntryTypeValue("=", Value::MakeBoolean(true));
::testing::Mock::VerifyAndClearExpectations(&dispatcher);
@@ -129,15 +128,15 @@
class MockLoadWarn {
public:
- MOCK_METHOD2(Warn, void(size_t line, wpi::StringRef msg));
+ MOCK_METHOD2(Warn, void(size_t line, std::string_view msg));
};
-TEST_P(StorageTestEmpty, Construct) {
+TEST_P(StorageEmptyTest, Construct) {
EXPECT_TRUE(entries().empty());
EXPECT_TRUE(idmap().empty());
}
-TEST_P(StorageTestEmpty, StorageEntryInit) {
+TEST_P(StorageEmptyTest, StorageEntryInit) {
auto entry = GetEntry("foo");
EXPECT_FALSE(entry->value);
EXPECT_EQ(0u, entry->flags);
@@ -146,13 +145,13 @@
EXPECT_EQ(SequenceNumber(), entry->seq_num);
}
-TEST_P(StorageTestEmpty, GetEntryValueNotExist) {
+TEST_P(StorageEmptyTest, GetEntryValueNotExist) {
EXPECT_FALSE(storage.GetEntryValue("foo"));
EXPECT_TRUE(entries().empty());
EXPECT_TRUE(idmap().empty());
}
-TEST_P(StorageTestEmpty, GetEntryValueExist) {
+TEST_P(StorageEmptyTest, GetEntryValueExist) {
auto value = Value::MakeBoolean(true);
EXPECT_CALL(dispatcher, QueueOutgoing(_, IsNull(), IsNull()));
EXPECT_CALL(notifier, NotifyEntry(_, _, _, _, _));
@@ -160,7 +159,7 @@
EXPECT_EQ(value, storage.GetEntryValue("foo"));
}
-TEST_P(StorageTestEmpty, SetEntryTypeValueAssignNew) {
+TEST_P(StorageEmptyTest, SetEntryTypeValueAssignNew) {
// brand new entry
auto value = Value::MakeBoolean(true);
// id assigned if server
@@ -168,7 +167,7 @@
QueueOutgoing(MessageEq(Message::EntryAssign(
"foo", GetParam() ? 0 : 0xffff, 1, value, 0)),
IsNull(), IsNull()));
- EXPECT_CALL(notifier, NotifyEntry(0, StringRef("foo"), value,
+ EXPECT_CALL(notifier, NotifyEntry(0, std::string_view("foo"), value,
NT_NOTIFY_NEW | NT_NOTIFY_LOCAL, UINT_MAX));
storage.SetEntryTypeValue("foo", value);
@@ -181,7 +180,7 @@
}
}
-TEST_P(StorageTestPopulateOne, SetEntryTypeValueAssignTypeChange) {
+TEST_P(StoragePopulateOneTest, SetEntryTypeValueAssignTypeChange) {
// update with different type results in assignment message
auto value = Value::MakeDouble(0.0);
@@ -191,14 +190,14 @@
"foo", GetParam() ? 0 : 0xffff, 2, value, 0)),
IsNull(), IsNull()));
EXPECT_CALL(notifier,
- NotifyEntry(0, StringRef("foo"), value,
+ NotifyEntry(0, std::string_view("foo"), value,
NT_NOTIFY_UPDATE | NT_NOTIFY_LOCAL, UINT_MAX));
storage.SetEntryTypeValue("foo", value);
EXPECT_EQ(value, GetEntry("foo")->value);
}
-TEST_P(StorageTestPopulateOne, SetEntryTypeValueEqualValue) {
+TEST_P(StoragePopulateOneTest, SetEntryTypeValueEqualValue) {
// update with same type and same value: change value contents but no update
// message is issued (minimizing bandwidth usage)
auto value = Value::MakeBoolean(true);
@@ -206,7 +205,7 @@
EXPECT_EQ(value, GetEntry("foo")->value);
}
-TEST_P(StorageTestPopulated, SetEntryTypeValueDifferentValue) {
+TEST_P(StoragePopulatedTest, SetEntryTypeValueDifferentValue) {
// update with same type and different value results in value update message
auto value = Value::MakeDouble(1.0);
@@ -218,7 +217,7 @@
IsNull(), IsNull()));
}
EXPECT_CALL(notifier,
- NotifyEntry(1, StringRef("foo2"), value,
+ NotifyEntry(1, std::string_view("foo2"), value,
NT_NOTIFY_UPDATE | NT_NOTIFY_LOCAL, UINT_MAX));
storage.SetEntryTypeValue("foo2", value);
EXPECT_EQ(value, GetEntry("foo2")->value);
@@ -229,20 +228,20 @@
}
}
-TEST_P(StorageTestEmpty, SetEntryTypeValueEmptyName) {
+TEST_P(StorageEmptyTest, SetEntryTypeValueEmptyName) {
auto value = Value::MakeBoolean(true);
storage.SetEntryTypeValue("", value);
EXPECT_TRUE(entries().empty());
EXPECT_TRUE(idmap().empty());
}
-TEST_P(StorageTestEmpty, SetEntryTypeValueEmptyValue) {
+TEST_P(StorageEmptyTest, SetEntryTypeValueEmptyValue) {
storage.SetEntryTypeValue("foo", nullptr);
EXPECT_TRUE(entries().empty());
EXPECT_TRUE(idmap().empty());
}
-TEST_P(StorageTestEmpty, SetEntryValueAssignNew) {
+TEST_P(StorageEmptyTest, SetEntryValueAssignNew) {
// brand new entry
auto value = Value::MakeBoolean(true);
@@ -251,14 +250,14 @@
QueueOutgoing(MessageEq(Message::EntryAssign(
"foo", GetParam() ? 0 : 0xffff, 1, value, 0)),
IsNull(), IsNull()));
- EXPECT_CALL(notifier, NotifyEntry(0, StringRef("foo"), value,
+ EXPECT_CALL(notifier, NotifyEntry(0, std::string_view("foo"), value,
NT_NOTIFY_NEW | NT_NOTIFY_LOCAL, UINT_MAX));
EXPECT_TRUE(storage.SetEntryValue("foo", value));
EXPECT_EQ(value, GetEntry("foo")->value);
}
-TEST_P(StorageTestPopulateOne, SetEntryValueAssignTypeChange) {
+TEST_P(StoragePopulateOneTest, SetEntryValueAssignTypeChange) {
// update with different type results in error and no message or notification
auto value = Value::MakeDouble(0.0);
EXPECT_FALSE(storage.SetEntryValue("foo", value));
@@ -266,7 +265,7 @@
EXPECT_NE(value, entry->value);
}
-TEST_P(StorageTestPopulateOne, SetEntryValueEqualValue) {
+TEST_P(StoragePopulateOneTest, SetEntryValueEqualValue) {
// update with same type and same value: change value contents but no update
// message is issued (minimizing bandwidth usage)
auto value = Value::MakeBoolean(true);
@@ -275,7 +274,7 @@
EXPECT_EQ(value, entry->value);
}
-TEST_P(StorageTestPopulated, SetEntryValueDifferentValue) {
+TEST_P(StoragePopulatedTest, SetEntryValueDifferentValue) {
// update with same type and different value results in value update message
auto value = Value::MakeDouble(1.0);
@@ -287,7 +286,7 @@
IsNull(), IsNull()));
}
EXPECT_CALL(notifier,
- NotifyEntry(1, StringRef("foo2"), value,
+ NotifyEntry(1, std::string_view("foo2"), value,
NT_NOTIFY_UPDATE | NT_NOTIFY_LOCAL, UINT_MAX));
EXPECT_TRUE(storage.SetEntryValue("foo2", value));
@@ -300,20 +299,20 @@
}
}
-TEST_P(StorageTestEmpty, SetEntryValueEmptyName) {
+TEST_P(StorageEmptyTest, SetEntryValueEmptyName) {
auto value = Value::MakeBoolean(true);
EXPECT_TRUE(storage.SetEntryValue("", value));
EXPECT_TRUE(entries().empty());
EXPECT_TRUE(idmap().empty());
}
-TEST_P(StorageTestEmpty, SetEntryValueEmptyValue) {
+TEST_P(StorageEmptyTest, SetEntryValueEmptyValue) {
EXPECT_TRUE(storage.SetEntryValue("foo", nullptr));
EXPECT_TRUE(entries().empty());
EXPECT_TRUE(idmap().empty());
}
-TEST_P(StorageTestEmpty, SetDefaultEntryAssignNew) {
+TEST_P(StorageEmptyTest, SetDefaultEntryAssignNew) {
// brand new entry
auto value = Value::MakeBoolean(true);
@@ -322,7 +321,7 @@
QueueOutgoing(MessageEq(Message::EntryAssign(
"foo", GetParam() ? 0 : 0xffff, 1, value, 0)),
IsNull(), IsNull()));
- EXPECT_CALL(notifier, NotifyEntry(0, StringRef("foo"), value,
+ EXPECT_CALL(notifier, NotifyEntry(0, std::string_view("foo"), value,
NT_NOTIFY_NEW | NT_NOTIFY_LOCAL, UINT_MAX));
auto ret_val = storage.SetDefaultEntryValue("foo", value);
@@ -330,7 +329,7 @@
EXPECT_EQ(value, GetEntry("foo")->value);
}
-TEST_P(StorageTestPopulateOne, SetDefaultEntryExistsSameType) {
+TEST_P(StoragePopulateOneTest, SetDefaultEntryExistsSameType) {
// existing entry
auto value = Value::MakeBoolean(true);
auto ret_val = storage.SetDefaultEntryValue("foo", value);
@@ -338,7 +337,7 @@
EXPECT_NE(value, GetEntry("foo")->value);
}
-TEST_P(StorageTestPopulateOne, SetDefaultEntryExistsDifferentType) {
+TEST_P(StoragePopulateOneTest, SetDefaultEntryExistsDifferentType) {
// existing entry is boolean
auto value = Value::MakeDouble(2.0);
auto ret_val = storage.SetDefaultEntryValue("foo", value);
@@ -347,7 +346,7 @@
EXPECT_NE(value, GetEntry("foo")->value);
}
-TEST_P(StorageTestEmpty, SetDefaultEntryEmptyName) {
+TEST_P(StorageEmptyTest, SetDefaultEntryEmptyName) {
auto value = Value::MakeBoolean(true);
auto ret_val = storage.SetDefaultEntryValue("", value);
EXPECT_FALSE(ret_val);
@@ -361,7 +360,7 @@
EXPECT_TRUE(idmap().empty());
}
-TEST_P(StorageTestEmpty, SetDefaultEntryEmptyValue) {
+TEST_P(StorageEmptyTest, SetDefaultEntryEmptyValue) {
auto value = Value::MakeBoolean(true);
auto ret_val = storage.SetDefaultEntryValue("", nullptr);
EXPECT_FALSE(ret_val);
@@ -375,38 +374,40 @@
EXPECT_TRUE(idmap().empty());
}
-TEST_P(StorageTestPopulated, SetDefaultEntryEmptyName) {
+TEST_P(StoragePopulatedTest, SetDefaultEntryEmptyName) {
auto value = Value::MakeBoolean(true);
auto ret_val = storage.SetDefaultEntryValue("", value);
EXPECT_FALSE(ret_val);
// assert that no entries get added
EXPECT_EQ(4u, entries().size());
- if (GetParam())
+ if (GetParam()) {
EXPECT_EQ(4u, idmap().size());
- else
+ } else {
EXPECT_EQ(0u, idmap().size());
+ }
}
-TEST_P(StorageTestPopulated, SetDefaultEntryEmptyValue) {
+TEST_P(StoragePopulatedTest, SetDefaultEntryEmptyValue) {
auto value = Value::MakeBoolean(true);
auto ret_val = storage.SetDefaultEntryValue("", nullptr);
EXPECT_FALSE(ret_val);
// assert that no entries get added
EXPECT_EQ(4u, entries().size());
- if (GetParam())
+ if (GetParam()) {
EXPECT_EQ(4u, idmap().size());
- else
+ } else {
EXPECT_EQ(0u, idmap().size());
+ }
}
-TEST_P(StorageTestEmpty, SetEntryFlagsNew) {
+TEST_P(StorageEmptyTest, SetEntryFlagsNew) {
// flags setting doesn't create an entry
storage.SetEntryFlags("foo", 0u);
EXPECT_TRUE(entries().empty());
EXPECT_TRUE(idmap().empty());
}
-TEST_P(StorageTestPopulateOne, SetEntryFlagsEqualValue) {
+TEST_P(StoragePopulateOneTest, SetEntryFlagsEqualValue) {
// update with same value: no update message is issued (minimizing bandwidth
// usage)
storage.SetEntryFlags("foo", 0u);
@@ -414,7 +415,7 @@
EXPECT_EQ(0u, entry->flags);
}
-TEST_P(StorageTestPopulated, SetEntryFlagsDifferentValue) {
+TEST_P(StoragePopulatedTest, SetEntryFlagsDifferentValue) {
// update with different value results in flags update message
// client shouldn't send an update as id not assigned yet
if (GetParam()) {
@@ -423,25 +424,25 @@
IsNull(), IsNull()));
}
EXPECT_CALL(notifier,
- NotifyEntry(1, StringRef("foo2"), _,
+ NotifyEntry(1, std::string_view("foo2"), _,
NT_NOTIFY_FLAGS | NT_NOTIFY_LOCAL, UINT_MAX));
storage.SetEntryFlags("foo2", 1u);
EXPECT_EQ(1u, GetEntry("foo2")->flags);
}
-TEST_P(StorageTestEmpty, SetEntryFlagsEmptyName) {
+TEST_P(StorageEmptyTest, SetEntryFlagsEmptyName) {
storage.SetEntryFlags("", 0u);
EXPECT_TRUE(entries().empty());
EXPECT_TRUE(idmap().empty());
}
-TEST_P(StorageTestEmpty, GetEntryFlagsNotExist) {
+TEST_P(StorageEmptyTest, GetEntryFlagsNotExist) {
EXPECT_EQ(0u, storage.GetEntryFlags("foo"));
EXPECT_TRUE(entries().empty());
EXPECT_TRUE(idmap().empty());
}
-TEST_P(StorageTestPopulateOne, GetEntryFlagsExist) {
+TEST_P(StoragePopulateOneTest, GetEntryFlagsExist) {
EXPECT_CALL(dispatcher, QueueOutgoing(_, _, _)).Times(AnyNumber());
EXPECT_CALL(notifier, NotifyEntry(_, _, _, _, _));
storage.SetEntryFlags("foo", 1u);
@@ -449,18 +450,21 @@
EXPECT_EQ(1u, storage.GetEntryFlags("foo"));
}
-TEST_P(StorageTestEmpty, DeleteEntryNotExist) { storage.DeleteEntry("foo"); }
+TEST_P(StorageEmptyTest, DeleteEntryNotExist) {
+ storage.DeleteEntry("foo");
+}
-TEST_P(StorageTestPopulated, DeleteEntryExist) {
+TEST_P(StoragePopulatedTest, DeleteEntryExist) {
// client shouldn't send an update as id not assigned yet
if (GetParam()) {
// id assigned as this is the server
EXPECT_CALL(dispatcher, QueueOutgoing(MessageEq(Message::EntryDelete(1)),
IsNull(), IsNull()));
}
- EXPECT_CALL(notifier,
- NotifyEntry(1, StringRef("foo2"), ValueEq(Value::MakeDouble(0)),
- NT_NOTIFY_DELETE | NT_NOTIFY_LOCAL, UINT_MAX));
+ EXPECT_CALL(
+ notifier,
+ NotifyEntry(1, std::string_view("foo2"), ValueEq(Value::MakeDouble(0)),
+ NT_NOTIFY_DELETE | NT_NOTIFY_LOCAL, UINT_MAX));
storage.DeleteEntry("foo2");
ASSERT_EQ(1u, entries().count("foo2"));
@@ -473,12 +477,12 @@
}
}
-TEST_P(StorageTestEmpty, DeleteAllEntriesEmpty) {
+TEST_P(StorageEmptyTest, DeleteAllEntriesEmpty) {
storage.DeleteAllEntries();
ASSERT_TRUE(entries().empty());
}
-TEST_P(StorageTestPopulated, DeleteAllEntries) {
+TEST_P(StoragePopulatedTest, DeleteAllEntries) {
EXPECT_CALL(dispatcher, QueueOutgoing(MessageEq(Message::ClearEntries()),
IsNull(), IsNull()));
EXPECT_CALL(notifier, NotifyEntry(_, _, _, NT_NOTIFY_DELETE | NT_NOTIFY_LOCAL,
@@ -490,7 +494,7 @@
EXPECT_EQ(nullptr, entries()["foo2"]->value);
}
-TEST_P(StorageTestPopulated, DeleteAllEntriesPersistent) {
+TEST_P(StoragePopulatedTest, DeleteAllEntriesPersistent) {
GetEntry("foo2")->flags = NT_PERSISTENT;
EXPECT_CALL(dispatcher, QueueOutgoing(MessageEq(Message::ClearEntries()),
@@ -504,12 +508,12 @@
EXPECT_NE(nullptr, entries()["foo2"]->value);
}
-TEST_P(StorageTestPopulated, GetEntryInfoAll) {
+TEST_P(StoragePopulatedTest, GetEntryInfoAll) {
auto info = storage.GetEntryInfo(0, "", 0u);
ASSERT_EQ(4u, info.size());
}
-TEST_P(StorageTestPopulated, GetEntryInfoPrefix) {
+TEST_P(StoragePopulatedTest, GetEntryInfoPrefix) {
auto info = storage.GetEntryInfo(0, "foo", 0u);
ASSERT_EQ(2u, info.size());
if (info[0].name == "foo") {
@@ -525,7 +529,7 @@
}
}
-TEST_P(StorageTestPopulated, GetEntryInfoTypes) {
+TEST_P(StoragePopulatedTest, GetEntryInfoTypes) {
auto info = storage.GetEntryInfo(0, "", NT_DOUBLE);
ASSERT_EQ(2u, info.size());
EXPECT_EQ(NT_DOUBLE, info[0].type);
@@ -539,101 +543,103 @@
}
}
-TEST_P(StorageTestPopulated, GetEntryInfoPrefixTypes) {
+TEST_P(StoragePopulatedTest, GetEntryInfoPrefixTypes) {
auto info = storage.GetEntryInfo(0, "bar", NT_BOOLEAN);
ASSERT_EQ(1u, info.size());
EXPECT_EQ("bar2", info[0].name);
EXPECT_EQ(NT_BOOLEAN, info[0].type);
}
-TEST_P(StorageTestPersistent, SavePersistentEmpty) {
+TEST_P(StoragePersistentTest, SavePersistentEmpty) {
wpi::SmallString<256> buf;
wpi::raw_svector_ostream oss(buf);
storage.SavePersistent(oss, false);
ASSERT_EQ("[NetworkTables Storage 3.0]\n", oss.str());
}
-TEST_P(StorageTestPersistent, SavePersistent) {
- for (auto& i : entries()) i.getValue()->flags = NT_PERSISTENT;
+TEST_P(StoragePersistentTest, SavePersistent) {
+ for (auto& i : entries()) {
+ i.getValue()->flags = NT_PERSISTENT;
+ }
wpi::SmallString<256> buf;
wpi::raw_svector_ostream oss(buf);
storage.SavePersistent(oss, false);
- wpi::StringRef out = oss.str();
+ std::string_view out = oss.str();
// std::fputs(out.c_str(), stderr);
- wpi::StringRef line, rem = out;
- std::tie(line, rem) = rem.split('\n');
+ std::string_view line, rem = out;
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("[NetworkTables Storage 3.0]", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("boolean \"\\x00\\x03\\x05\\n\"=true", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("boolean \"\\x3D\"=true", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("boolean \"boolean/false\"=false", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("boolean \"boolean/true\"=true", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("array boolean \"booleanarr/empty\"=", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("array boolean \"booleanarr/one\"=true", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("array boolean \"booleanarr/two\"=true,false", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("double \"double/big\"=1.3e+08", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("double \"double/neg\"=-1.5", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("double \"double/zero\"=0", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("array double \"doublearr/empty\"=", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("array double \"doublearr/one\"=0.5", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("array double \"doublearr/two\"=0.5,-0.25", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("raw \"raw/empty\"=", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("raw \"raw/normal\"=aGVsbG8=", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("raw \"raw/special\"=AAMFCg==", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("string \"string/empty\"=\"\"", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("string \"string/normal\"=\"hello\"", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("string \"string/quoted\"=\"\\\"a\\\"\"", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("string \"string/special\"=\"\\x00\\x03\\x05\\n\"", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("array string \"stringarr/empty\"=", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("array string \"stringarr/one\"=\"hello\"", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("array string \"stringarr/two\"=\"hello\",\"world\\n\"", line);
- std::tie(line, rem) = rem.split('\n');
+ std::tie(line, rem) = wpi::split(rem, '\n');
ASSERT_EQ("", line);
}
-TEST_P(StorageTestEmpty, LoadPersistentBadHeader) {
+TEST_P(StorageEmptyTest, LoadPersistentBadHeader) {
MockLoadWarn warn;
auto warn_func = [&](size_t line, const char* msg) { warn.Warn(line, msg); };
wpi::raw_mem_istream iss("");
EXPECT_CALL(
warn,
- Warn(1, wpi::StringRef("header line mismatch, ignoring rest of file")));
+ Warn(1, std::string_view("header line mismatch, ignoring rest of file")));
EXPECT_FALSE(storage.LoadEntries(iss, "", true, warn_func));
wpi::raw_mem_istream iss2("[NetworkTables");
EXPECT_CALL(
warn,
- Warn(1, wpi::StringRef("header line mismatch, ignoring rest of file")));
+ Warn(1, std::string_view("header line mismatch, ignoring rest of file")));
EXPECT_FALSE(storage.LoadEntries(iss2, "", true, warn_func));
EXPECT_TRUE(entries().empty());
EXPECT_TRUE(idmap().empty());
}
-TEST_P(StorageTestEmpty, LoadPersistentCommentHeader) {
+TEST_P(StorageEmptyTest, LoadPersistentCommentHeader) {
MockLoadWarn warn;
auto warn_func = [&](size_t line, const char* msg) { warn.Warn(line, msg); };
@@ -644,7 +650,7 @@
EXPECT_TRUE(idmap().empty());
}
-TEST_P(StorageTestEmpty, LoadPersistentEmptyName) {
+TEST_P(StorageEmptyTest, LoadPersistentEmptyName) {
MockLoadWarn warn;
auto warn_func = [&](size_t line, const char* msg) { warn.Warn(line, msg); };
@@ -654,7 +660,7 @@
EXPECT_TRUE(idmap().empty());
}
-TEST_P(StorageTestEmpty, LoadPersistentAssign) {
+TEST_P(StorageEmptyTest, LoadPersistentAssign) {
MockLoadWarn warn;
auto warn_func = [&](size_t line, const char* msg) { warn.Warn(line, msg); };
@@ -665,7 +671,7 @@
"foo", GetParam() ? 0 : 0xffff, 1,
value, NT_PERSISTENT)),
IsNull(), IsNull()));
- EXPECT_CALL(notifier, NotifyEntry(0, StringRef("foo"),
+ EXPECT_CALL(notifier, NotifyEntry(0, std::string_view("foo"),
ValueEq(Value::MakeBoolean(true)),
NT_NOTIFY_NEW | NT_NOTIFY_LOCAL, UINT_MAX));
@@ -677,7 +683,7 @@
EXPECT_EQ(NT_PERSISTENT, entry->flags);
}
-TEST_P(StorageTestPopulated, LoadPersistentUpdateFlags) {
+TEST_P(StoragePopulatedTest, LoadPersistentUpdateFlags) {
MockLoadWarn warn;
auto warn_func = [&](size_t line, const char* msg) { warn.Warn(line, msg); };
@@ -688,9 +694,10 @@
QueueOutgoing(MessageEq(Message::FlagsUpdate(1, NT_PERSISTENT)),
IsNull(), IsNull()));
}
- EXPECT_CALL(notifier,
- NotifyEntry(1, StringRef("foo2"), ValueEq(Value::MakeDouble(0)),
- NT_NOTIFY_FLAGS | NT_NOTIFY_LOCAL, UINT_MAX));
+ EXPECT_CALL(
+ notifier,
+ NotifyEntry(1, std::string_view("foo2"), ValueEq(Value::MakeDouble(0)),
+ NT_NOTIFY_FLAGS | NT_NOTIFY_LOCAL, UINT_MAX));
wpi::raw_mem_istream iss(
"[NetworkTables Storage 3.0]\ndouble \"foo2\"=0.0\n");
@@ -700,7 +707,7 @@
EXPECT_EQ(NT_PERSISTENT, entry->flags);
}
-TEST_P(StorageTestPopulated, LoadPersistentUpdateValue) {
+TEST_P(StoragePopulatedTest, LoadPersistentUpdateValue) {
MockLoadWarn warn;
auto warn_func = [&](size_t line, const char* msg) { warn.Warn(line, msg); };
@@ -715,9 +722,10 @@
QueueOutgoing(MessageEq(Message::EntryUpdate(1, 2, value)),
IsNull(), IsNull()));
}
- EXPECT_CALL(notifier,
- NotifyEntry(1, StringRef("foo2"), ValueEq(Value::MakeDouble(1)),
- NT_NOTIFY_UPDATE | NT_NOTIFY_LOCAL, UINT_MAX));
+ EXPECT_CALL(
+ notifier,
+ NotifyEntry(1, std::string_view("foo2"), ValueEq(Value::MakeDouble(1)),
+ NT_NOTIFY_UPDATE | NT_NOTIFY_LOCAL, UINT_MAX));
wpi::raw_mem_istream iss(
"[NetworkTables Storage 3.0]\ndouble \"foo2\"=1.0\n");
@@ -732,7 +740,7 @@
}
}
-TEST_P(StorageTestPopulated, LoadPersistentUpdateValueFlags) {
+TEST_P(StoragePopulatedTest, LoadPersistentUpdateValueFlags) {
MockLoadWarn warn;
auto warn_func = [&](size_t line, const char* msg) { warn.Warn(line, msg); };
@@ -748,10 +756,11 @@
QueueOutgoing(MessageEq(Message::FlagsUpdate(1, NT_PERSISTENT)),
IsNull(), IsNull()));
}
- EXPECT_CALL(notifier,
- NotifyEntry(1, StringRef("foo2"), ValueEq(Value::MakeDouble(1)),
- NT_NOTIFY_FLAGS | NT_NOTIFY_UPDATE | NT_NOTIFY_LOCAL,
- UINT_MAX));
+ EXPECT_CALL(
+ notifier,
+ NotifyEntry(1, std::string_view("foo2"), ValueEq(Value::MakeDouble(1)),
+ NT_NOTIFY_FLAGS | NT_NOTIFY_UPDATE | NT_NOTIFY_LOCAL,
+ UINT_MAX));
wpi::raw_mem_istream iss(
"[NetworkTables Storage 3.0]\ndouble \"foo2\"=1.0\n");
@@ -766,7 +775,7 @@
}
}
-TEST_P(StorageTestEmpty, LoadPersistent) {
+TEST_P(StorageEmptyTest, LoadPersistent) {
MockLoadWarn warn;
auto warn_func = [&](size_t line, const char* msg) { warn.Warn(line, msg); };
@@ -813,13 +822,13 @@
EXPECT_EQ(*Value::MakeString(""), *storage.GetEntryValue("string/empty"));
EXPECT_EQ(*Value::MakeString("hello"),
*storage.GetEntryValue("string/normal"));
- EXPECT_EQ(*Value::MakeString(StringRef("\0\3\5\n", 4)),
+ EXPECT_EQ(*Value::MakeString(std::string_view("\0\3\5\n", 4)),
*storage.GetEntryValue("string/special"));
EXPECT_EQ(*Value::MakeString("\"a\""),
*storage.GetEntryValue("string/quoted"));
EXPECT_EQ(*Value::MakeRaw(""), *storage.GetEntryValue("raw/empty"));
EXPECT_EQ(*Value::MakeRaw("hello"), *storage.GetEntryValue("raw/normal"));
- EXPECT_EQ(*Value::MakeRaw(StringRef("\0\3\5\n", 4)),
+ EXPECT_EQ(*Value::MakeRaw(std::string_view("\0\3\5\n", 4)),
*storage.GetEntryValue("raw/special"));
EXPECT_EQ(*Value::MakeBooleanArray(std::vector<int>{}),
*storage.GetEntryValue("booleanarr/empty"));
@@ -841,18 +850,18 @@
*Value::MakeStringArray(std::vector<std::string>{"hello", "world\n"}),
*storage.GetEntryValue("stringarr/two"));
EXPECT_EQ(*Value::MakeBoolean(true),
- *storage.GetEntryValue(StringRef("\0\3\5\n", 4)));
+ *storage.GetEntryValue(std::string_view("\0\3\5\n", 4)));
EXPECT_EQ(*Value::MakeBoolean(true), *storage.GetEntryValue("="));
}
-TEST_P(StorageTestEmpty, LoadPersistentWarn) {
+TEST_P(StorageEmptyTest, LoadPersistentWarn) {
MockLoadWarn warn;
auto warn_func = [&](size_t line, const char* msg) { warn.Warn(line, msg); };
wpi::raw_mem_istream iss(
"[NetworkTables Storage 3.0]\nboolean \"foo\"=foo\n");
EXPECT_CALL(
- warn, Warn(2, wpi::StringRef(
+ warn, Warn(2, std::string_view(
"unrecognized boolean value, not 'true' or 'false'")));
EXPECT_TRUE(storage.LoadEntries(iss, "", true, warn_func));
@@ -860,7 +869,7 @@
EXPECT_TRUE(idmap().empty());
}
-TEST_P(StorageTestEmpty, ProcessIncomingEntryAssign) {
+TEST_P(StorageEmptyTest, ProcessIncomingEntryAssign) {
auto conn = std::make_shared<MockNetworkConnection>();
auto value = Value::MakeDouble(1.0);
if (GetParam()) {
@@ -870,7 +879,7 @@
QueueOutgoing(MessageEq(Message::EntryAssign("foo", 0, 0, value, 0)),
IsNull(), IsNull()));
}
- EXPECT_CALL(notifier, NotifyEntry(0, StringRef("foo"), ValueEq(value),
+ EXPECT_CALL(notifier, NotifyEntry(0, std::string_view("foo"), ValueEq(value),
NT_NOTIFY_NEW, UINT_MAX));
storage.ProcessIncoming(
@@ -878,7 +887,7 @@
conn.get(), conn);
}
-TEST_P(StorageTestPopulateOne, ProcessIncomingEntryAssign) {
+TEST_P(StoragePopulateOneTest, ProcessIncomingEntryAssign) {
auto conn = std::make_shared<MockNetworkConnection>();
auto value = Value::MakeDouble(1.0);
EXPECT_CALL(*conn, proto_rev()).WillRepeatedly(Return(0x0300u));
@@ -889,21 +898,21 @@
QueueOutgoing(MessageEq(Message::EntryAssign("foo", 0, 1, value, 0)),
IsNull(), conn.get()));
}
- EXPECT_CALL(notifier, NotifyEntry(0, StringRef("foo"), ValueEq(value),
+ EXPECT_CALL(notifier, NotifyEntry(0, std::string_view("foo"), ValueEq(value),
NT_NOTIFY_UPDATE, UINT_MAX));
storage.ProcessIncoming(Message::EntryAssign("foo", 0, 1, value, 0),
conn.get(), conn);
}
-TEST_P(StorageTestPopulateOne, ProcessIncomingEntryAssignIgnore) {
+TEST_P(StoragePopulateOneTest, ProcessIncomingEntryAssignIgnore) {
auto conn = std::make_shared<MockNetworkConnection>();
auto value = Value::MakeDouble(1.0);
storage.ProcessIncoming(Message::EntryAssign("foo", 0xffff, 1, value, 0),
conn.get(), conn);
}
-TEST_P(StorageTestPopulateOne, ProcessIncomingEntryAssignWithFlags) {
+TEST_P(StoragePopulateOneTest, ProcessIncomingEntryAssignWithFlags) {
auto conn = std::make_shared<MockNetworkConnection>();
auto value = Value::MakeDouble(1.0);
EXPECT_CALL(*conn, proto_rev()).WillRepeatedly(Return(0x0300u));
@@ -914,22 +923,23 @@
QueueOutgoing(MessageEq(Message::EntryAssign("foo", 0, 1, value, 0x2)),
IsNull(), conn.get()));
EXPECT_CALL(notifier,
- NotifyEntry(0, StringRef("foo"), ValueEq(value),
+ NotifyEntry(0, std::string_view("foo"), ValueEq(value),
NT_NOTIFY_UPDATE | NT_NOTIFY_FLAGS, UINT_MAX));
} else {
// client forces flags back when an assign message is received for an
// existing entry with different flags
EXPECT_CALL(dispatcher, QueueOutgoing(MessageEq(Message::FlagsUpdate(0, 0)),
IsNull(), IsNull()));
- EXPECT_CALL(notifier, NotifyEntry(0, StringRef("foo"), ValueEq(value),
- NT_NOTIFY_UPDATE, UINT_MAX));
+ EXPECT_CALL(notifier,
+ NotifyEntry(0, std::string_view("foo"), ValueEq(value),
+ NT_NOTIFY_UPDATE, UINT_MAX));
}
storage.ProcessIncoming(Message::EntryAssign("foo", 0, 1, value, 0x2),
conn.get(), conn);
}
-TEST_P(StorageTestPopulateOne, DeleteCheckHandle) {
+TEST_P(StoragePopulateOneTest, DeleteCheckHandle) {
EXPECT_CALL(dispatcher, QueueOutgoing(_, _, _)).Times(AnyNumber());
EXPECT_CALL(notifier, NotifyEntry(_, _, _, _, _)).Times(AnyNumber());
auto handle = storage.GetEntry("foo");
@@ -942,7 +952,7 @@
ASSERT_EQ(handle, handle2);
}
-TEST_P(StorageTestPopulateOne, DeletedEntryFlags) {
+TEST_P(StoragePopulateOneTest, DeletedEntryFlags) {
EXPECT_CALL(dispatcher, QueueOutgoing(_, _, _)).Times(AnyNumber());
EXPECT_CALL(notifier, NotifyEntry(_, _, _, _, _)).Times(AnyNumber());
auto handle = storage.GetEntry("foo");
@@ -959,7 +969,7 @@
EXPECT_EQ(storage.GetEntryFlags(handle), 0u);
}
-TEST_P(StorageTestPopulateOne, DeletedDeleteAllEntries) {
+TEST_P(StoragePopulateOneTest, DeletedDeleteAllEntries) {
EXPECT_CALL(dispatcher, QueueOutgoing(_, _, _)).Times(AnyNumber());
EXPECT_CALL(notifier, NotifyEntry(_, _, _, _, _)).Times(AnyNumber());
storage.DeleteEntry("foo");
@@ -971,7 +981,7 @@
storage.DeleteAllEntries();
}
-TEST_P(StorageTestPopulateOne, DeletedGetEntries) {
+TEST_P(StoragePopulateOneTest, DeletedGetEntries) {
EXPECT_CALL(dispatcher, QueueOutgoing(_, _, _)).Times(AnyNumber());
EXPECT_CALL(notifier, NotifyEntry(_, _, _, _, _)).Times(AnyNumber());
storage.DeleteEntry("foo");
@@ -981,13 +991,13 @@
EXPECT_TRUE(storage.GetEntries("", 0).empty());
}
-INSTANTIATE_TEST_SUITE_P(StorageTestsEmpty, StorageTestEmpty,
+INSTANTIATE_TEST_SUITE_P(StorageEmptyTests, StorageEmptyTest,
::testing::Bool());
-INSTANTIATE_TEST_SUITE_P(StorageTestsPopulateOne, StorageTestPopulateOne,
+INSTANTIATE_TEST_SUITE_P(StoragePopulateOneTests, StoragePopulateOneTest,
::testing::Bool());
-INSTANTIATE_TEST_SUITE_P(StorageTestsPopulated, StorageTestPopulated,
+INSTANTIATE_TEST_SUITE_P(StoragePopulatedTests, StoragePopulatedTest,
::testing::Bool());
-INSTANTIATE_TEST_SUITE_P(StorageTestsPersistent, StorageTestPersistent,
+INSTANTIATE_TEST_SUITE_P(StoragePersistentTests, StoragePersistentTest,
::testing::Bool());
} // namespace nt
diff --git a/ntcore/src/test/native/cpp/StorageTest.h b/ntcore/src/test/native/cpp/StorageTest.h
index 1bb8a8c..3a1755f 100644
--- a/ntcore/src/test/native/cpp/StorageTest.h
+++ b/ntcore/src/test/native/cpp/StorageTest.h
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_STORAGETEST_H_
#define NTCORE_STORAGETEST_H_
@@ -27,7 +24,7 @@
Storage::EntriesMap& entries() { return storage.m_entries; }
Storage::IdMap& idmap() { return storage.m_idmap; }
- Storage::Entry* GetEntry(StringRef name) {
+ Storage::Entry* GetEntry(std::string_view name) {
auto i = storage.m_entries.find(name);
return i == storage.m_entries.end() ? &tmp_entry : i->getValue();
}
diff --git a/ntcore/src/test/native/cpp/TestPrinters.cpp b/ntcore/src/test/native/cpp/TestPrinters.cpp
index 3368b8c..49b407b 100644
--- a/ntcore/src/test/native/cpp/TestPrinters.cpp
+++ b/ntcore/src/test/native/cpp/TestPrinters.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "TestPrinters.h"
@@ -129,7 +126,7 @@
*os << value.GetDouble();
break;
case NT_STRING:
- *os << '"' << value.GetString().str() << '"';
+ *os << '"' << value.GetString() << '"';
break;
case NT_RAW:
*os << ::testing::PrintToString(value.GetRaw());
diff --git a/ntcore/src/test/native/cpp/TestPrinters.h b/ntcore/src/test/native/cpp/TestPrinters.h
index 2976c1c..47c9eb4 100644
--- a/ntcore/src/test/native/cpp/TestPrinters.h
+++ b/ntcore/src/test/native/cpp/TestPrinters.h
@@ -1,24 +1,21 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_TESTPRINTERS_H_
#define NTCORE_TESTPRINTERS_H_
#include <memory>
#include <ostream>
-
-#include <wpi/StringRef.h>
+#include <string>
+#include <string_view>
#include "gtest/gtest.h"
namespace wpi {
-inline void PrintTo(StringRef str, ::std::ostream* os) {
- ::testing::internal::PrintStringTo(str.str(), os);
+inline void PrintTo(std::string_view str, ::std::ostream* os) {
+ ::testing::internal::PrintStringTo(std::string{str}, os);
}
} // namespace wpi
@@ -37,7 +34,9 @@
inline void PrintTo(std::shared_ptr<Message> msg, std::ostream* os) {
*os << "shared_ptr{";
- if (msg) PrintTo(*msg, os);
+ if (msg) {
+ PrintTo(*msg, os);
+ }
*os << '}';
}
@@ -45,7 +44,9 @@
inline void PrintTo(std::shared_ptr<Value> value, std::ostream* os) {
*os << "shared_ptr{";
- if (value) PrintTo(*value, os);
+ if (value) {
+ PrintTo(*value, os);
+ }
*os << '}';
}
diff --git a/ntcore/src/test/native/cpp/ValueMatcher.cpp b/ntcore/src/test/native/cpp/ValueMatcher.cpp
index 45b09ed..1f55c01 100644
--- a/ntcore/src/test/native/cpp/ValueMatcher.cpp
+++ b/ntcore/src/test/native/cpp/ValueMatcher.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include "ValueMatcher.h"
diff --git a/ntcore/src/test/native/cpp/ValueMatcher.h b/ntcore/src/test/native/cpp/ValueMatcher.h
index 5c417d7..ab68448 100644
--- a/ntcore/src/test/native/cpp/ValueMatcher.h
+++ b/ntcore/src/test/native/cpp/ValueMatcher.h
@@ -1,15 +1,13 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#ifndef NTCORE_VALUEMATCHER_H_
#define NTCORE_VALUEMATCHER_H_
#include <memory>
#include <ostream>
+#include <utility>
#include "gmock/gmock.h"
#include "networktables/NetworkTableValue.h"
@@ -19,7 +17,8 @@
class ValueMatcher
: public ::testing::MatcherInterface<std::shared_ptr<Value>> {
public:
- explicit ValueMatcher(std::shared_ptr<Value> goodval_) : goodval(goodval_) {}
+ explicit ValueMatcher(std::shared_ptr<Value> goodval_)
+ : goodval(std::move(goodval_)) {}
bool MatchAndExplain(std::shared_ptr<Value> msg,
::testing::MatchResultListener* listener) const override;
diff --git a/ntcore/src/test/native/cpp/ValueTest.cpp b/ntcore/src/test/native/cpp/ValueTest.cpp
index 57d1c38..a9f2218 100644
--- a/ntcore/src/test/native/cpp/ValueTest.cpp
+++ b/ntcore/src/test/native/cpp/ValueTest.cpp
@@ -1,20 +1,32 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
+
+#include <algorithm>
+#include <string_view>
#include "TestPrinters.h"
#include "Value_internal.h"
#include "gtest/gtest.h"
#include "networktables/NetworkTableValue.h"
+using namespace std::string_view_literals;
+
+namespace wpi {
+template <typename T>
+inline bool operator==(span<T> lhs, span<T> rhs) {
+ if (lhs.size() != rhs.size()) {
+ return false;
+ }
+ return std::equal(lhs.begin(), lhs.end(), rhs.begin());
+}
+} // namespace wpi
+
namespace nt {
class ValueTest : public ::testing::Test {};
-typedef ValueTest ValueDeathTest;
+using ValueDeathTest = ValueTest;
TEST_F(ValueTest, ConstructEmpty) {
Value v;
@@ -34,6 +46,7 @@
v = Value::MakeBoolean(true);
ASSERT_EQ(NT_BOOLEAN, v->type());
ASSERT_TRUE(v->GetBoolean());
+ NT_DisposeValue(&cv);
ConvertToC(*v, &cv);
ASSERT_EQ(NT_BOOLEAN, cv.type);
ASSERT_EQ(1, cv.data.v_boolean);
@@ -54,6 +67,7 @@
v = Value::MakeDouble(0.25);
ASSERT_EQ(NT_DOUBLE, v->type());
ASSERT_EQ(0.25, v->GetDouble());
+ NT_DisposeValue(&cv);
ConvertToC(*v, &cv);
ASSERT_EQ(NT_DOUBLE, cv.type);
ASSERT_EQ(0.25, cv.data.v_double);
@@ -69,15 +83,16 @@
NT_InitValue(&cv);
ConvertToC(*v, &cv);
ASSERT_EQ(NT_STRING, cv.type);
- ASSERT_EQ(wpi::StringRef("hello"), cv.data.v_string.str);
+ ASSERT_EQ("hello"sv, cv.data.v_string.str);
ASSERT_EQ(5u, cv.data.v_string.len);
v = Value::MakeString("goodbye");
ASSERT_EQ(NT_STRING, v->type());
ASSERT_EQ("goodbye", v->GetString());
+ NT_DisposeValue(&cv);
ConvertToC(*v, &cv);
ASSERT_EQ(NT_STRING, cv.type);
- ASSERT_EQ(wpi::StringRef("goodbye"), cv.data.v_string.str);
+ ASSERT_EQ("goodbye"sv, cv.data.v_string.str);
ASSERT_EQ(7u, cv.data.v_string.len);
NT_DisposeValue(&cv);
@@ -91,15 +106,16 @@
NT_InitValue(&cv);
ConvertToC(*v, &cv);
ASSERT_EQ(NT_RAW, cv.type);
- ASSERT_EQ(wpi::StringRef("hello"), cv.data.v_string.str);
+ ASSERT_EQ("hello"sv, cv.data.v_string.str);
ASSERT_EQ(5u, cv.data.v_string.len);
v = Value::MakeRaw("goodbye");
ASSERT_EQ(NT_RAW, v->type());
ASSERT_EQ("goodbye", v->GetRaw());
+ NT_DisposeValue(&cv);
ConvertToC(*v, &cv);
ASSERT_EQ(NT_RAW, cv.type);
- ASSERT_EQ(wpi::StringRef("goodbye"), cv.data.v_string.str);
+ ASSERT_EQ("goodbye"sv, cv.data.v_string.str);
ASSERT_EQ(7u, cv.data.v_string.len);
NT_DisposeValue(&cv);
@@ -109,7 +125,7 @@
std::vector<int> vec{1, 0, 1};
auto v = Value::MakeBooleanArray(vec);
ASSERT_EQ(NT_BOOLEAN_ARRAY, v->type());
- ASSERT_EQ(wpi::ArrayRef<int>(vec), v->GetBooleanArray());
+ ASSERT_EQ(wpi::span<const int>(vec), v->GetBooleanArray());
NT_Value cv;
NT_InitValue(&cv);
ConvertToC(*v, &cv);
@@ -123,7 +139,8 @@
vec = {0, 1, 0};
v = Value::MakeBooleanArray(vec);
ASSERT_EQ(NT_BOOLEAN_ARRAY, v->type());
- ASSERT_EQ(wpi::ArrayRef<int>(vec), v->GetBooleanArray());
+ ASSERT_EQ(wpi::span<const int>(vec), v->GetBooleanArray());
+ NT_DisposeValue(&cv);
ConvertToC(*v, &cv);
ASSERT_EQ(NT_BOOLEAN_ARRAY, cv.type);
ASSERT_EQ(3u, cv.data.arr_boolean.size);
@@ -135,7 +152,8 @@
vec = {1, 0};
v = Value::MakeBooleanArray(vec);
ASSERT_EQ(NT_BOOLEAN_ARRAY, v->type());
- ASSERT_EQ(wpi::ArrayRef<int>(vec), v->GetBooleanArray());
+ ASSERT_EQ(wpi::span<const int>(vec), v->GetBooleanArray());
+ NT_DisposeValue(&cv);
ConvertToC(*v, &cv);
ASSERT_EQ(NT_BOOLEAN_ARRAY, cv.type);
ASSERT_EQ(2u, cv.data.arr_boolean.size);
@@ -149,7 +167,7 @@
std::vector<double> vec{0.5, 0.25, 0.5};
auto v = Value::MakeDoubleArray(vec);
ASSERT_EQ(NT_DOUBLE_ARRAY, v->type());
- ASSERT_EQ(wpi::ArrayRef<double>(vec), v->GetDoubleArray());
+ ASSERT_EQ(wpi::span<const double>(vec), v->GetDoubleArray());
NT_Value cv;
NT_InitValue(&cv);
ConvertToC(*v, &cv);
@@ -163,7 +181,8 @@
vec = {0.25, 0.5, 0.25};
v = Value::MakeDoubleArray(vec);
ASSERT_EQ(NT_DOUBLE_ARRAY, v->type());
- ASSERT_EQ(wpi::ArrayRef<double>(vec), v->GetDoubleArray());
+ ASSERT_EQ(wpi::span<const double>(vec), v->GetDoubleArray());
+ NT_DisposeValue(&cv);
ConvertToC(*v, &cv);
ASSERT_EQ(NT_DOUBLE_ARRAY, cv.type);
ASSERT_EQ(3u, cv.data.arr_double.size);
@@ -175,7 +194,8 @@
vec = {0.5, 0.25};
v = Value::MakeDoubleArray(vec);
ASSERT_EQ(NT_DOUBLE_ARRAY, v->type());
- ASSERT_EQ(wpi::ArrayRef<double>(vec), v->GetDoubleArray());
+ ASSERT_EQ(wpi::span<const double>(vec), v->GetDoubleArray());
+ NT_DisposeValue(&cv);
ConvertToC(*v, &cv);
ASSERT_EQ(NT_DOUBLE_ARRAY, cv.type);
ASSERT_EQ(2u, cv.data.arr_double.size);
@@ -193,17 +213,17 @@
auto v = Value::MakeStringArray(std::move(vec));
ASSERT_EQ(NT_STRING_ARRAY, v->type());
ASSERT_EQ(3u, v->GetStringArray().size());
- ASSERT_EQ(wpi::StringRef("hello"), v->GetStringArray()[0]);
- ASSERT_EQ(wpi::StringRef("goodbye"), v->GetStringArray()[1]);
- ASSERT_EQ(wpi::StringRef("string"), v->GetStringArray()[2]);
+ ASSERT_EQ("hello"sv, v->GetStringArray()[0]);
+ ASSERT_EQ("goodbye"sv, v->GetStringArray()[1]);
+ ASSERT_EQ("string"sv, v->GetStringArray()[2]);
NT_Value cv;
NT_InitValue(&cv);
ConvertToC(*v, &cv);
ASSERT_EQ(NT_STRING_ARRAY, cv.type);
ASSERT_EQ(3u, cv.data.arr_string.size);
- ASSERT_EQ(wpi::StringRef("hello"), cv.data.arr_string.arr[0].str);
- ASSERT_EQ(wpi::StringRef("goodbye"), cv.data.arr_string.arr[1].str);
- ASSERT_EQ(wpi::StringRef("string"), cv.data.arr_string.arr[2].str);
+ ASSERT_EQ("hello"sv, cv.data.arr_string.arr[0].str);
+ ASSERT_EQ("goodbye"sv, cv.data.arr_string.arr[1].str);
+ ASSERT_EQ("string"sv, cv.data.arr_string.arr[2].str);
// assign with same size
vec.clear();
@@ -213,15 +233,16 @@
v = Value::MakeStringArray(vec);
ASSERT_EQ(NT_STRING_ARRAY, v->type());
ASSERT_EQ(3u, v->GetStringArray().size());
- ASSERT_EQ(wpi::StringRef("s1"), v->GetStringArray()[0]);
- ASSERT_EQ(wpi::StringRef("str2"), v->GetStringArray()[1]);
- ASSERT_EQ(wpi::StringRef("string3"), v->GetStringArray()[2]);
+ ASSERT_EQ("s1"sv, v->GetStringArray()[0]);
+ ASSERT_EQ("str2"sv, v->GetStringArray()[1]);
+ ASSERT_EQ("string3"sv, v->GetStringArray()[2]);
+ NT_DisposeValue(&cv);
ConvertToC(*v, &cv);
ASSERT_EQ(NT_STRING_ARRAY, cv.type);
ASSERT_EQ(3u, cv.data.arr_string.size);
- ASSERT_EQ(wpi::StringRef("s1"), cv.data.arr_string.arr[0].str);
- ASSERT_EQ(wpi::StringRef("str2"), cv.data.arr_string.arr[1].str);
- ASSERT_EQ(wpi::StringRef("string3"), cv.data.arr_string.arr[2].str);
+ ASSERT_EQ("s1"sv, cv.data.arr_string.arr[0].str);
+ ASSERT_EQ("str2"sv, cv.data.arr_string.arr[1].str);
+ ASSERT_EQ("string3"sv, cv.data.arr_string.arr[2].str);
// assign with different size
vec.clear();
@@ -230,13 +251,14 @@
v = Value::MakeStringArray(std::move(vec));
ASSERT_EQ(NT_STRING_ARRAY, v->type());
ASSERT_EQ(2u, v->GetStringArray().size());
- ASSERT_EQ(wpi::StringRef("short"), v->GetStringArray()[0]);
- ASSERT_EQ(wpi::StringRef("er"), v->GetStringArray()[1]);
+ ASSERT_EQ("short"sv, v->GetStringArray()[0]);
+ ASSERT_EQ("er"sv, v->GetStringArray()[1]);
+ NT_DisposeValue(&cv);
ConvertToC(*v, &cv);
ASSERT_EQ(NT_STRING_ARRAY, cv.type);
ASSERT_EQ(2u, cv.data.arr_string.size);
- ASSERT_EQ(wpi::StringRef("short"), cv.data.arr_string.arr[0].str);
- ASSERT_EQ(wpi::StringRef("er"), cv.data.arr_string.arr[1].str);
+ ASSERT_EQ("short"sv, cv.data.arr_string.arr[0].str);
+ ASSERT_EQ("er"sv, cv.data.arr_string.arr[1].str);
NT_DisposeValue(&cv);
}
diff --git a/ntcore/src/test/native/cpp/WireDecoderTest.cpp b/ntcore/src/test/native/cpp/WireDecoderTest.cpp
index e32f909..313fd98 100644
--- a/ntcore/src/test/native/cpp/WireDecoderTest.cpp
+++ b/ntcore/src/test/native/cpp/WireDecoderTest.cpp
@@ -1,22 +1,20 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <stdint.h>
#include <cfloat>
#include <climits>
#include <string>
-
-#include <wpi/StringRef.h>
+#include <string_view>
#include "TestPrinters.h"
#include "WireDecoder.h"
#include "gtest/gtest.h"
+using namespace std::string_view_literals;
+
namespace nt {
class WireDecoderTest : public ::testing::Test {
@@ -24,8 +22,8 @@
WireDecoderTest() {
v_boolean = Value::MakeBoolean(true);
v_double = Value::MakeDouble(1.0);
- v_string = Value::MakeString(wpi::StringRef("hello"));
- v_raw = Value::MakeRaw(wpi::StringRef("hello"));
+ v_string = Value::MakeString("hello"sv);
+ v_raw = Value::MakeRaw("hello"sv);
v_boolean_array = Value::MakeBooleanArray(std::vector<int>{0, 1, 0});
v_boolean_array_big = Value::MakeBooleanArray(std::vector<int>(255));
v_double_array = Value::MakeDoubleArray(std::vector<double>{0.5, 0.25});
@@ -37,7 +35,9 @@
v_string_array = Value::MakeStringArray(std::move(sa));
sa.clear();
- for (int i = 0; i < 255; ++i) sa.push_back("h");
+ for (int i = 0; i < 255; ++i) {
+ sa.push_back("h");
+ }
v_string_array_big = Value::MakeStringArray(std::move(sa));
s_normal = std::string("hello");
@@ -269,7 +269,7 @@
ASSERT_TRUE(static_cast<bool>(val));
EXPECT_EQ(*v_string, *val);
- auto v_bye = Value::MakeString(wpi::StringRef("bye"));
+ auto v_bye = Value::MakeString("bye"sv);
val = d.ReadValue(NT_STRING);
ASSERT_TRUE(static_cast<bool>(val));
EXPECT_EQ(*v_bye, *val);
@@ -367,7 +367,9 @@
TEST_F(WireDecoderTest, ReadStringArrayBigValue2) {
std::string s;
s.push_back('\xff');
- for (int i = 0; i < 255; ++i) s.append("\x00\x01h", 3);
+ for (int i = 0; i < 255; ++i) {
+ s.append("\x00\x01h", 3);
+ }
wpi::raw_mem_istream is(s.data(), s.size());
wpi::Logger logger;
WireDecoder d(is, 0x0200u, logger);
@@ -442,7 +444,7 @@
ASSERT_TRUE(static_cast<bool>(val));
EXPECT_EQ(*v_string, *val);
- auto v_bye = Value::MakeString(wpi::StringRef("bye"));
+ auto v_bye = Value::MakeString("bye"sv);
val = d.ReadValue(NT_STRING);
ASSERT_TRUE(static_cast<bool>(val));
EXPECT_EQ(*v_bye, *val);
@@ -466,7 +468,7 @@
ASSERT_TRUE(static_cast<bool>(val));
EXPECT_EQ(*v_raw, *val);
- auto v_bye = Value::MakeRaw(wpi::StringRef("bye"));
+ auto v_bye = Value::MakeRaw("bye"sv);
val = d.ReadValue(NT_RAW);
ASSERT_TRUE(static_cast<bool>(val));
EXPECT_EQ(*v_bye, *val);
@@ -564,7 +566,9 @@
TEST_F(WireDecoderTest, ReadStringArrayBigValue3) {
std::string s;
s.push_back('\xff');
- for (int i = 0; i < 255; ++i) s.append("\x01h", 2);
+ for (int i = 0; i < 255; ++i) {
+ s.append("\x01h", 2);
+ }
wpi::raw_mem_istream is(s.data(), s.size());
wpi::Logger logger;
WireDecoder d(is, 0x0300u, logger);
diff --git a/ntcore/src/test/native/cpp/WireEncoderTest.cpp b/ntcore/src/test/native/cpp/WireEncoderTest.cpp
index fab5a22..b714112 100644
--- a/ntcore/src/test/native/cpp/WireEncoderTest.cpp
+++ b/ntcore/src/test/native/cpp/WireEncoderTest.cpp
@@ -1,15 +1,11 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <cfloat>
#include <climits>
#include <string>
-
-#include <wpi/StringRef.h>
+#include <string_view>
#include "TestPrinters.h"
#include "WireEncoder.h"
@@ -17,6 +13,8 @@
#define BUFSIZE 1024
+using namespace std::string_view_literals;
+
namespace nt {
class WireEncoderTest : public ::testing::Test {
@@ -25,8 +23,8 @@
v_empty = std::make_shared<Value>();
v_boolean = Value::MakeBoolean(true);
v_double = Value::MakeDouble(1.0);
- v_string = Value::MakeString(wpi::StringRef("hello"));
- v_raw = Value::MakeRaw(wpi::StringRef("hello"));
+ v_string = Value::MakeString("hello"sv);
+ v_raw = Value::MakeRaw("hello"sv);
v_boolean_array = Value::MakeBooleanArray(std::vector<int>{0, 1, 0});
v_boolean_array_big = Value::MakeBooleanArray(std::vector<int>(256));
v_double_array = Value::MakeDoubleArray(std::vector<double>{0.5, 0.25});
@@ -38,7 +36,9 @@
v_string_array = Value::MakeStringArray(std::move(sa));
sa.clear();
- for (int i = 0; i < 256; ++i) sa.push_back("h");
+ for (int i = 0; i < 256; ++i) {
+ sa.push_back("h");
+ }
v_string_array_big = Value::MakeStringArray(std::move(sa));
s_normal = "hello";
@@ -77,48 +77,55 @@
TEST_F(WireEncoderTest, Write8) {
size_t off = BUFSIZE - 1;
WireEncoder e(0x0300u);
- for (size_t i = 0; i < off; ++i) e.Write8(0u); // test across Reserve()
+ for (size_t i = 0; i < off; ++i) {
+ e.Write8(0u); // test across Reserve()
+ }
e.Write8(5u);
e.Write8(0x101u); // should be truncated
e.Write8(0u);
ASSERT_EQ(3u, e.size() - off);
- ASSERT_EQ(wpi::StringRef("\x05\x01\x00", 3),
- wpi::StringRef(e.data(), e.size()).substr(off));
+ ASSERT_EQ("\x05\x01\x00"sv, std::string_view(e.data(), e.size()).substr(off));
}
TEST_F(WireEncoderTest, Write16) {
size_t off = BUFSIZE - 2;
WireEncoder e(0x0300u);
- for (size_t i = 0; i < off; ++i) e.Write8(0u); // test across Reserve()
+ for (size_t i = 0; i < off; ++i) {
+ e.Write8(0u); // test across Reserve()
+ }
e.Write16(5u);
e.Write16(0x10001u); // should be truncated
e.Write16(0x4567u);
e.Write16(0u);
ASSERT_EQ(8u, e.size() - off);
- ASSERT_EQ(wpi::StringRef("\x00\x05\x00\x01\x45\x67\x00\x00", 8),
- wpi::StringRef(e.data(), e.size()).substr(off));
+ ASSERT_EQ("\x00\x05\x00\x01\x45\x67\x00\x00"sv,
+ std::string_view(e.data(), e.size()).substr(off));
}
TEST_F(WireEncoderTest, Write32) {
size_t off = BUFSIZE - 4;
WireEncoder e(0x0300u);
- for (size_t i = 0; i < off; ++i) e.Write8(0u); // test across Reserve()
+ for (size_t i = 0; i < off; ++i) {
+ e.Write8(0u); // test across Reserve()
+ }
e.Write32(5ul);
e.Write32(1ul);
e.Write32(0xabcdul);
e.Write32(0x12345678ul);
e.Write32(0ul);
ASSERT_EQ(20u, e.size() - off);
- ASSERT_EQ(wpi::StringRef("\x00\x00\x00\x05\x00\x00\x00\x01\x00\x00\xab\xcd"
- "\x12\x34\x56\x78\x00\x00\x00\x00",
- 20),
- wpi::StringRef(e.data(), e.size()).substr(off));
+ ASSERT_EQ(std::string_view("\x00\x00\x00\x05\x00\x00\x00\x01\x00\x00\xab\xcd"
+ "\x12\x34\x56\x78\x00\x00\x00\x00",
+ 20),
+ std::string_view(e.data(), e.size()).substr(off));
}
TEST_F(WireEncoderTest, WriteDouble) {
size_t off = BUFSIZE - 8;
WireEncoder e(0x0300u);
- for (size_t i = 0; i < off; ++i) e.Write8(0u); // test across Reserve()
+ for (size_t i = 0; i < off; ++i) {
+ e.Write8(0u); // test across Reserve()
+ }
e.WriteDouble(0.0);
e.WriteDouble(2.3e5);
e.WriteDouble(std::numeric_limits<double>::infinity());
@@ -127,31 +134,35 @@
ASSERT_EQ(40u, e.size() - off);
// golden values except min and max from
// http://www.binaryconvert.com/result_double.html
- ASSERT_EQ(wpi::StringRef("\x00\x00\x00\x00\x00\x00\x00\x00"
- "\x41\x0c\x13\x80\x00\x00\x00\x00"
- "\x7f\xf0\x00\x00\x00\x00\x00\x00"
- "\x00\x10\x00\x00\x00\x00\x00\x00"
- "\x7f\xef\xff\xff\xff\xff\xff\xff",
- 40),
- wpi::StringRef(e.data(), e.size()).substr(off));
+ ASSERT_EQ(std::string_view("\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x41\x0c\x13\x80\x00\x00\x00\x00"
+ "\x7f\xf0\x00\x00\x00\x00\x00\x00"
+ "\x00\x10\x00\x00\x00\x00\x00\x00"
+ "\x7f\xef\xff\xff\xff\xff\xff\xff",
+ 40),
+ std::string_view(e.data(), e.size()).substr(off));
}
TEST_F(WireEncoderTest, WriteUleb128) {
size_t off = BUFSIZE - 2;
WireEncoder e(0x0300u);
- for (size_t i = 0; i < off; ++i) e.Write8(0u); // test across Reserve()
+ for (size_t i = 0; i < off; ++i) {
+ e.Write8(0u); // test across Reserve()
+ }
e.WriteUleb128(0ul);
e.WriteUleb128(0x7ful);
e.WriteUleb128(0x80ul);
ASSERT_EQ(4u, e.size() - off);
- ASSERT_EQ(wpi::StringRef("\x00\x7f\x80\x01", 4),
- wpi::StringRef(e.data(), e.size()).substr(off));
+ ASSERT_EQ("\x00\x7f\x80\x01"sv,
+ std::string_view(e.data(), e.size()).substr(off));
}
TEST_F(WireEncoderTest, WriteType) {
size_t off = BUFSIZE - 1;
WireEncoder e(0x0300u);
- for (size_t i = 0; i < off; ++i) e.Write8(0u); // test across Reserve()
+ for (size_t i = 0; i < off; ++i) {
+ e.Write8(0u); // test across Reserve()
+ }
e.WriteType(NT_BOOLEAN);
e.WriteType(NT_DOUBLE);
e.WriteType(NT_STRING);
@@ -162,8 +173,8 @@
e.WriteType(NT_RPC);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(8u, e.size() - off);
- ASSERT_EQ(wpi::StringRef("\x00\x01\x02\x03\x10\x11\x12\x20", 8),
- wpi::StringRef(e.data(), e.size()).substr(off));
+ ASSERT_EQ("\x00\x01\x02\x03\x10\x11\x12\x20"sv,
+ std::string_view(e.data(), e.size()).substr(off));
}
TEST_F(WireEncoderTest, WriteTypeError) {
@@ -224,7 +235,7 @@
e.WriteValue(*v_false);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(2u, e.size());
- ASSERT_EQ(wpi::StringRef("\x01\x00", 2), wpi::StringRef(e.data(), e.size()));
+ ASSERT_EQ("\x01\x00"sv, std::string_view(e.data(), e.size()));
}
TEST_F(WireEncoderTest, WriteDoubleValue2) {
@@ -232,8 +243,8 @@
e.WriteValue(*v_double);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(8u, e.size());
- ASSERT_EQ(wpi::StringRef("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8),
- wpi::StringRef(e.data(), e.size()));
+ ASSERT_EQ("\x3f\xf0\x00\x00\x00\x00\x00\x00"sv,
+ std::string_view(e.data(), e.size()));
}
TEST_F(WireEncoderTest, WriteStringValue2) {
@@ -241,8 +252,7 @@
e.WriteValue(*v_string);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(7u, e.size());
- ASSERT_EQ(wpi::StringRef("\x00\x05hello", 7),
- wpi::StringRef(e.data(), e.size()));
+ ASSERT_EQ("\x00\x05hello"sv, std::string_view(e.data(), e.size()));
}
TEST_F(WireEncoderTest, WriteBooleanArrayValue2) {
@@ -250,15 +260,14 @@
e.WriteValue(*v_boolean_array);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(1u + 3u, e.size());
- ASSERT_EQ(wpi::StringRef("\x03\x00\x01\x00", 4),
- wpi::StringRef(e.data(), e.size()));
+ ASSERT_EQ("\x03\x00\x01\x00"sv, std::string_view(e.data(), e.size()));
// truncated
e.Reset();
e.WriteValue(*v_boolean_array_big);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(1u + 255u, e.size());
- ASSERT_EQ(wpi::StringRef("\xff\x00", 2), wpi::StringRef(e.data(), 2));
+ ASSERT_EQ("\xff\x00"sv, std::string_view(e.data(), 2));
}
TEST_F(WireEncoderTest, WriteDoubleArrayValue2) {
@@ -266,17 +275,17 @@
e.WriteValue(*v_double_array);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(1u + 2u * 8u, e.size());
- ASSERT_EQ(wpi::StringRef("\x02\x3f\xe0\x00\x00\x00\x00\x00\x00"
- "\x3f\xd0\x00\x00\x00\x00\x00\x00",
- 17),
- wpi::StringRef(e.data(), e.size()));
+ ASSERT_EQ(std::string_view("\x02\x3f\xe0\x00\x00\x00\x00\x00\x00"
+ "\x3f\xd0\x00\x00\x00\x00\x00\x00",
+ 17),
+ std::string_view(e.data(), e.size()));
// truncated
e.Reset();
e.WriteValue(*v_double_array_big);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(1u + 255u * 8u, e.size());
- ASSERT_EQ(wpi::StringRef("\xff\x00", 2), wpi::StringRef(e.data(), 2));
+ ASSERT_EQ("\xff\x00"sv, std::string_view(e.data(), 2));
}
TEST_F(WireEncoderTest, WriteStringArrayValue2) {
@@ -284,15 +293,15 @@
e.WriteValue(*v_string_array);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(1u + 7u + 9u, e.size());
- ASSERT_EQ(wpi::StringRef("\x02\x00\x05hello\x00\x07goodbye", 17),
- wpi::StringRef(e.data(), e.size()));
+ ASSERT_EQ("\x02\x00\x05hello\x00\x07goodbye"sv,
+ std::string_view(e.data(), e.size()));
// truncated
e.Reset();
e.WriteValue(*v_string_array_big);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(1u + 255u * 3u, e.size());
- ASSERT_EQ(wpi::StringRef("\xff\x00\x01", 3), wpi::StringRef(e.data(), 3));
+ ASSERT_EQ("\xff\x00\x01"sv, std::string_view(e.data(), 3));
}
TEST_F(WireEncoderTest, WriteValueError2) {
@@ -335,7 +344,7 @@
e.WriteValue(*v_false);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(2u, e.size());
- ASSERT_EQ(wpi::StringRef("\x01\x00", 2), wpi::StringRef(e.data(), e.size()));
+ ASSERT_EQ("\x01\x00"sv, std::string_view(e.data(), e.size()));
}
TEST_F(WireEncoderTest, WriteDoubleValue3) {
@@ -343,8 +352,8 @@
e.WriteValue(*v_double);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(8u, e.size());
- ASSERT_EQ(wpi::StringRef("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8),
- wpi::StringRef(e.data(), e.size()));
+ ASSERT_EQ("\x3f\xf0\x00\x00\x00\x00\x00\x00"sv,
+ std::string_view(e.data(), e.size()));
}
TEST_F(WireEncoderTest, WriteStringValue3) {
@@ -352,7 +361,7 @@
e.WriteValue(*v_string);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(6u, e.size());
- ASSERT_EQ(wpi::StringRef("\x05hello", 6), wpi::StringRef(e.data(), e.size()));
+ ASSERT_EQ("\x05hello"sv, std::string_view(e.data(), e.size()));
}
TEST_F(WireEncoderTest, WriteRawValue3) {
@@ -360,7 +369,7 @@
e.WriteValue(*v_raw);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(6u, e.size());
- ASSERT_EQ(wpi::StringRef("\x05hello", 6), wpi::StringRef(e.data(), e.size()));
+ ASSERT_EQ("\x05hello"sv, std::string_view(e.data(), e.size()));
}
TEST_F(WireEncoderTest, WriteBooleanArrayValue3) {
@@ -368,15 +377,14 @@
e.WriteValue(*v_boolean_array);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(1u + 3u, e.size());
- ASSERT_EQ(wpi::StringRef("\x03\x00\x01\x00", 4),
- wpi::StringRef(e.data(), e.size()));
+ ASSERT_EQ("\x03\x00\x01\x00"sv, std::string_view(e.data(), e.size()));
// truncated
e.Reset();
e.WriteValue(*v_boolean_array_big);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(1u + 255u, e.size());
- ASSERT_EQ(wpi::StringRef("\xff\x00", 2), wpi::StringRef(e.data(), 2));
+ ASSERT_EQ("\xff\x00"sv, std::string_view(e.data(), 2));
}
TEST_F(WireEncoderTest, WriteDoubleArrayValue3) {
@@ -384,17 +392,17 @@
e.WriteValue(*v_double_array);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(1u + 2u * 8u, e.size());
- ASSERT_EQ(wpi::StringRef("\x02\x3f\xe0\x00\x00\x00\x00\x00\x00"
- "\x3f\xd0\x00\x00\x00\x00\x00\x00",
- 17),
- wpi::StringRef(e.data(), e.size()));
+ ASSERT_EQ(std::string_view("\x02\x3f\xe0\x00\x00\x00\x00\x00\x00"
+ "\x3f\xd0\x00\x00\x00\x00\x00\x00",
+ 17),
+ std::string_view(e.data(), e.size()));
// truncated
e.Reset();
e.WriteValue(*v_double_array_big);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(1u + 255u * 8u, e.size());
- ASSERT_EQ(wpi::StringRef("\xff\x00", 2), wpi::StringRef(e.data(), 2));
+ ASSERT_EQ("\xff\x00"sv, std::string_view(e.data(), 2));
}
TEST_F(WireEncoderTest, WriteStringArrayValue3) {
@@ -402,15 +410,14 @@
e.WriteValue(*v_string_array);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(1u + 6u + 8u, e.size());
- ASSERT_EQ(wpi::StringRef("\x02\x05hello\x07goodbye", 15),
- wpi::StringRef(e.data(), e.size()));
+ ASSERT_EQ("\x02\x05hello\x07goodbye"sv, std::string_view(e.data(), e.size()));
// truncated
e.Reset();
e.WriteValue(*v_string_array_big);
ASSERT_EQ(nullptr, e.error());
ASSERT_EQ(1u + 255u * 2u, e.size());
- ASSERT_EQ(wpi::StringRef("\xff\x01", 2), wpi::StringRef(e.data(), 2));
+ ASSERT_EQ("\xff\x01"sv, std::string_view(e.data(), 2));
}
TEST_F(WireEncoderTest, WriteValueError3) {
@@ -434,14 +441,13 @@
e.WriteString(s_normal);
EXPECT_EQ(nullptr, e.error());
EXPECT_EQ(7u, e.size());
- EXPECT_EQ(wpi::StringRef("\x00\x05hello", 7),
- wpi::StringRef(e.data(), e.size()));
+ EXPECT_EQ("\x00\x05hello"sv, std::string_view(e.data(), e.size()));
e.Reset();
e.WriteString(s_long);
EXPECT_EQ(nullptr, e.error());
ASSERT_EQ(130u, e.size());
- EXPECT_EQ(wpi::StringRef("\x00\x80**", 4), wpi::StringRef(e.data(), 4));
+ EXPECT_EQ("\x00\x80**"sv, std::string_view(e.data(), 4));
EXPECT_EQ('*', e.data()[128]);
EXPECT_EQ('x', e.data()[129]);
@@ -450,7 +456,7 @@
e.WriteString(s_big);
EXPECT_EQ(nullptr, e.error());
ASSERT_EQ(65537u, e.size());
- EXPECT_EQ(wpi::StringRef("\xff\xff**", 4), wpi::StringRef(e.data(), 4));
+ EXPECT_EQ("\xff\xff**"sv, std::string_view(e.data(), 4));
EXPECT_EQ('*', e.data()[65535]);
EXPECT_EQ('x', e.data()[65536]);
}
@@ -468,13 +474,13 @@
e.WriteString(s_normal);
EXPECT_EQ(nullptr, e.error());
EXPECT_EQ(6u, e.size());
- EXPECT_EQ(wpi::StringRef("\x05hello", 6), wpi::StringRef(e.data(), e.size()));
+ EXPECT_EQ("\x05hello"sv, std::string_view(e.data(), e.size()));
e.Reset();
e.WriteString(s_long);
EXPECT_EQ(nullptr, e.error());
ASSERT_EQ(130u, e.size());
- EXPECT_EQ(wpi::StringRef("\x80\x01**", 4), wpi::StringRef(e.data(), 4));
+ EXPECT_EQ("\x80\x01**"sv, std::string_view(e.data(), 4));
EXPECT_EQ('*', e.data()[128]);
EXPECT_EQ('x', e.data()[129]);
@@ -483,7 +489,7 @@
e.WriteString(s_big);
EXPECT_EQ(nullptr, e.error());
ASSERT_EQ(65540u, e.size());
- EXPECT_EQ(wpi::StringRef("\x81\x80\x04*", 4), wpi::StringRef(e.data(), 4));
+ EXPECT_EQ("\x81\x80\x04*"sv, std::string_view(e.data(), 4));
EXPECT_EQ('*', e.data()[65536]);
EXPECT_EQ('x', e.data()[65537]);
EXPECT_EQ('x', e.data()[65538]);
diff --git a/ntcore/src/test/native/cpp/main.cpp b/ntcore/src/test/native/cpp/main.cpp
index 112289f..a3fec50 100644
--- a/ntcore/src/test/native/cpp/main.cpp
+++ b/ntcore/src/test/native/cpp/main.cpp
@@ -1,9 +1,6 @@
-/*----------------------------------------------------------------------------*/
-/* Copyright (c) 2015-2020 FIRST. All Rights Reserved. */
-/* Open Source Software - may be modified and shared by FRC teams. The code */
-/* must be accompanied by the FIRST BSD license file in the root directory of */
-/* the project. */
-/*----------------------------------------------------------------------------*/
+// Copyright (c) FIRST and other WPILib contributors.
+// Open Source Software; you can modify and/or share it under the terms of
+// the WPILib BSD license file in the root directory of this project.
#include <climits>