tqchen commented on issue #4346: [Runtime] Make ADTObject POD container type
URL: https://github.com/apache/incubator-tvm/pull/4346#issuecomment-554587942
 
 
   The inplace array is indeed somewhat tricky :) Here is one idea to make the 
effort easier for more similar cases: apology for typing in a window. Have fun!
   
   ```c++
   // Curiously recurring template pattern
   template<typename ArrayType, typename ElemType>
   class InplaceArray {
      public:
         // caclculate start location
         static_assert(sizeof(ArrayType) % align_of(ElemType));
         ~InplaceArray() {
            ArrayType* self = static_cast<ArrayType*>(this);
            if (!std::is_pod<ElemType>::value) {
              // destruct elements
               for (size_t i = 0; i < self->size(); ++i) {
                  void* ptr = AddressOf(i);
                  reinterpret_cast<ElemType*>(ptr)->ElemType::~ElemType();
               }
            }
         } 
         ElemType& operator[](size_t i) {
            ArrayType* self = static_cast<ArrayType*>(this);
            if (i >= self->size()) { 
               CHECK(i < self->capacity());
               void* ptr = AddressOf(i);
               new (ptr) ElemType();
               return *reinterpret_cast<ElemType*>(ptr);
            }
            return *reinterpret_cast<ElemType*>(ptr);
         }
      private:
        static constexpr const int kDataStart = sizeof(ArrayType);
        void* AddressOf(int i) {
           ArrayType* self = static_cast<ArrayType*>(this);
           return reinterpret_cast<char*>(self) + kDataStart;  
        }
   };
   
   class ADTObj : public Object, public InlineArray<ADTObj> {
   }
   ```

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


With regards,
Apache Git Services

Reply via email to