User Tools

Site Tools


cpp:mydata.h

**This is an old revision of the document!**

MyData

class for tracing copy/move operations

snippet.cpp
#ifndef PLAYGROUND_MYDATA_H
#define PLAYGROUND_MYDATA_H
 
#include <ostream>
#include <iostream>
 
template<typename T>
class MyData
{
  public:
    MyData(T value) noexcept : value(value)
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
 
    operator T() noexcept
    {
      return value;
    }
 
    ~MyData() noexcept
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
 
    MyData(MyData&& other) noexcept : value(other.value)
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
 
    MyData(const MyData& other) noexcept : value(other.value)
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
 
    MyData& operator=(MyData&& other) noexcept
    {
      if (this == &other)
      {
        return *this;
      }
      std::cout << __PRETTY_FUNCTION__ << std::endl;
 
      this->~MyData();
      new(this) MyData(std::move(other));
 
      return *this;
    }
 
    MyData& operator=(const MyData& other) noexcept
    {
      if (this == &other)
      {
        return *this;
      }
      std::cout << __PRETTY_FUNCTION__ << std::endl;
 
      this->~MyData();
      new(this) MyData(other);
 
      return *this;
    }
 
    friend std::ostream& operator<<(std::ostream& os, const MyData& data)
    {
      os << data.value;
      return os;
    }
 
    MyData& operator++()
    {
      ++value;
      return *this;
    }
 
 
  private:
    T value;
};
 
#endif //PLAYGROUND_MYDATA_H
cpp/mydata.h.1535800536.txt.gz · Last modified: by allspark