WPILibC++  unspecified
c_util.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef CSCORE_C_UTIL_H_
9 #define CSCORE_C_UTIL_H_
10 
11 #include <cstdlib>
12 #include <cstring>
13 
14 #include <wpi/StringRef.h>
15 #include <wpi/memory.h>
16 
17 namespace cs {
18 
19 inline char* ConvertToC(wpi::StringRef in) {
20  char* out = static_cast<char*>(wpi::CheckedMalloc(in.size() + 1));
21  std::memmove(out, in.data(), in.size());
22  out[in.size()] = '\0';
23  return out;
24 }
25 
26 } // namespace cs
27 
28 #endif // CSCORE_C_UTIL_H_
Definition: CvSourceImpl.h:19
void * CheckedMalloc(size_t size)
Wrapper around std::malloc that calls std::terminate on out of memory.
Definition: memory.cpp:25
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const noexcept
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const noexcept
size - Get the string size.
Definition: StringRef.h:138