• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
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 
17 // Common utils for memory images.
18 
19 #ifndef LIBTEXTCLASSIFIER_COMMON_MEMORY_IMAGE_MEMORY_IMAGE_COMMON_H_
20 #define LIBTEXTCLASSIFIER_COMMON_MEMORY_IMAGE_MEMORY_IMAGE_COMMON_H_
21 
22 #include <stddef.h>
23 
24 #include <string>
25 
26 #include "util/strings/stringpiece.h"
27 
28 namespace libtextclassifier {
29 namespace nlp_core {
30 
31 class MemoryImageConstants {
32  public:
33   static const char kSignature[];
34   static const int kCurrentVersion;
35   static const int kDefaultAlignment;
36 };
37 
38 // Read-only "view" of a data blob.  Does not own the underlying data; instead,
39 // just a small object that points to an area of a memory image.
40 //
41 // TODO(salcianu): replace this class with StringPiece.
42 class DataBlobView {
43  public:
DataBlobView()44   DataBlobView() : DataBlobView(nullptr, 0) {}
45 
DataBlobView(const void * start,size_t size)46   DataBlobView(const void *start, size_t size)
47       : start_(start), size_(size) {}
48 
49   // Returns start address of a data blob from a memory image.
data()50   const void *data() const { return start_; }
51 
52   // Returns number of bytes from the data blob starting at start().
size()53   size_t size() const { return size_; }
54 
to_stringpiece()55   StringPiece to_stringpiece() const {
56     return StringPiece(reinterpret_cast<const char *>(data()),
57                        size());
58   }
59 
60   // Returns a std::string containing a copy of the data blob bytes.
ToString()61   std::string ToString() const {
62     return to_stringpiece().ToString();
63   }
64 
65  private:
66   const void *start_;  // Not owned.
67   size_t size_;
68 };
69 
70 }  // namespace nlp_core
71 }  // namespace libtextclassifier
72 
73 #endif  // LIBTEXTCLASSIFIER_COMMON_MEMORY_IMAGE_MEMORY_IMAGE_COMMON_H_
74