You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2020/01/06 12:55:11 UTC

[GitHub] [incubator-tvm] FrozenGene commented on a change in pull request #4628: [Object] Add String container

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

 ##########
 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; }
 
 Review comment:
   Maybe we should create `TVM_ATTRIBUTE_ALWAYS_INLINE` and add it.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services