tqchen commented on a change in pull request #4628: [Object] Add String 
container
URL: https://github.com/apache/incubator-tvm/pull/4628#discussion_r363405855
 
 

 ##########
 File path: include/tvm/runtime/container.h
 ##########
 @@ -274,6 +275,99 @@ class ADT : public ObjectRef {
   TVM_DEFINE_OBJECT_REF_METHODS(ADT, ObjectRef, ADTObj);
 };
 
+/*! \brief An object representing string. It's POD type. */
+class StringObj : public Object {
+ public:
+  /*! \brief The length of the string object. */
+  uint32_t size;
+
+  /*! \brief The pointer to string data. */
+  char* data;
+
+  /*! \brief String object which is moved from std::string container. */
+  class FromStd;
+};
+
+/*! \brief An object representing string moved from std::string. */
+class StringObj::FromStd : public StringObj {
+ public:
+  /*! \brief Container that holds the memory. */
+  std::string data_container;
+};
+
+/*! \brief reference to string objects. */
+class String : public ObjectRef {
+ public:
+  /*!
+   * \brief Construct a new String object
+   *
+   * \param other The moved std::string object
+   */
+  explicit String(std::string&& other) {
+    auto ptr = make_object<StringObj::FromStd>();
+    ptr->data_container = std::move(other);
+    ptr->size = ptr->data_container.size();
+    ptr->data = const_cast<char*>(ptr->data_container.data());
+    data_ = std::move(ptr);
+  }
+
+  /*!
+   * \brief Construct a new String object
+   *
+   * \param other The source string object
+   */
+  explicit String(const std::string& other) {
+    auto ptr = make_object<StringObj::FromStd>();
+    ptr->data_container = other;
+    ptr->size = ptr->data_container.size();
+    ptr->data = const_cast<char*>(ptr->data_container.data());
+    data_ = std::move(ptr);
+  }
+
+  /*!
+   * \brief Return the length of the string
+   *
+   * \return size_t string length
+   */
+  size_t size() const { return operator->()->size; }
+
+  /*!
+   * \brief Return the data pointer
+   *
+   * \return char* data pointer
+   */
+  char* data() const { return operator->()->data; }
+
+  /*!
+   * \brief Create a String reference from std::string rvalue
+   *
+   * \param other The source std::string object.
+   * \return String The String reference.
+   */
+  static String FromStd(std::string&& other) {
+    return String(std::move(other));
+  }
+
+  /*!
+   * \brief Create a String reference from std::string reference
+   *
+   * \param other The source std::string object.
+   * \return String The String reference.
+   */
+  static String FromStd(const std::string& other) { return String(other); }
 
 Review comment:
   Given that we already have constructor, we don't need these functions

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to