StringView.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef IPHONE_FIRESTORE_SOURCE_LOCAL_STRING_VIEW_H_
  17. #define IPHONE_FIRESTORE_SOURCE_LOCAL_STRING_VIEW_H_
  18. #ifndef __cplusplus
  19. #error "StringView is Objective-C++ and can only be included from .mm files"
  20. #endif
  21. #import <Foundation/Foundation.h>
  22. #include <leveldb/slice.h>
  23. #include <string>
  24. namespace Firestore {
  25. // A simple wrapper for the character data of any string-like type to which
  26. // we'd like to temporarily refer as an argument.
  27. //
  28. // This is superficially similar to StringPiece and leveldb::Slice except
  29. // that it also supports implicit conversion from NSString *, which is useful
  30. // when writing Objective-C++ methods that accept any string-like type.
  31. //
  32. // Note that much like any other view-type class in C++, the caller is
  33. // responsible for ensuring that the lifetime of the string-like data is longer
  34. // than the lifetime of the StringView.
  35. //
  36. // Functions that take a StringView argument promise that they won't keep the
  37. // pointer beyond the immediate scope of their own stack frame.
  38. class StringView {
  39. public:
  40. // Creates a StringView from an NSString. When StringView is an argument type
  41. // into which an NSString* is passed, the caller should ensure that the
  42. // NSString is retained.
  43. StringView(NSString *str) : data_([str UTF8String]), size_(str.length) {
  44. }
  45. // Creates a StringView from the given char* pointer with an explicit size.
  46. // The character data can contain NUL bytes as a result.
  47. StringView(const char *data, size_t size) : data_(data), size_(size) {
  48. }
  49. // Creates a StringView from the given char* pointer but computes the size
  50. // with strlen. This is really only suitable for passing C string literals.
  51. StringView(const char *data) : data_(data), size_(strlen(data)) {
  52. }
  53. // Creates a StringView from the given slice.
  54. StringView(leveldb::Slice slice) : data_(slice.data()), size_(slice.size()) {
  55. }
  56. // Creates a StringView from the given std::string. The string must be an
  57. // lvalue for the lifetime requirements to be satisfied.
  58. StringView(const std::string &str) : data_(str.data()), size_(str.size()) {
  59. }
  60. // Converts this StringView to a Slice, which is an equivalent (and more
  61. // functional) type. The returned slice has the same lifetime as this
  62. // StringView.
  63. operator leveldb::Slice() {
  64. return leveldb::Slice(data_, size_);
  65. }
  66. private:
  67. const char *data_;
  68. const size_t size_;
  69. };
  70. } // namespace Firestore
  71. #endif // IPHONE_FIRESTORE_SOURCE_LOCAL_STRING_VIEW_H_