StringView.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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)
  44. : data_([str UTF8String]), size_([str lengthOfBytesUsingEncoding:NSUTF8StringEncoding]) {
  45. }
  46. // Creates a StringView from the given char* pointer with an explicit size.
  47. // The character data can contain NUL bytes as a result.
  48. StringView(const char *data, size_t size) : data_(data), size_(size) {
  49. }
  50. // Creates a StringView from the given char* pointer but computes the size
  51. // with strlen. This is really only suitable for passing C string literals.
  52. StringView(const char *data) : data_(data), size_(strlen(data)) {
  53. }
  54. // Creates a StringView from the given slice.
  55. StringView(leveldb::Slice slice) : data_(slice.data()), size_(slice.size()) {
  56. }
  57. // Creates a StringView from the given std::string. The string must be an
  58. // lvalue for the lifetime requirements to be satisfied.
  59. StringView(const std::string &str) : data_(str.data()), size_(str.size()) {
  60. }
  61. // Converts this StringView to a Slice, which is an equivalent (and more
  62. // functional) type. The returned slice has the same lifetime as this
  63. // StringView.
  64. operator leveldb::Slice() {
  65. return leveldb::Slice(data_, size_);
  66. }
  67. private:
  68. const char *data_;
  69. const size_t size_;
  70. };
  71. } // namespace Firestore
  72. #endif // IPHONE_FIRESTORE_SOURCE_LOCAL_STRING_VIEW_H_