title: Wrapper for getter and setter methods in c++
body: <p>I have few classes with pair of methods which stands for "setter" and "getter" methods, like:</p>
  
  <pre><code>class ClassA
  {
      void SetSomeProperty(const int&amp; value) { ... }
      int GetSomeProperty() { ... }
  
      void SetAnotherProperty(const Vec3&amp; value) { ... }
      Vec3 GetAnotherProperty() { ... }
  };
  
  class ClassB
  {
      void SetColor(const Vec4&amp; value) { ... }
      Vec4 GetColor() { ... }
  }
  </code></pre>
  
  <p>I want to create a class called PropertyAccess that can wrap each pair of setter and getter of any class. This will allow me to past this PropertyAccess to other object which will be able to call those setters and getters wihout any knowledgne about ClassA or ClassB.</p>
  
  <p>this is my iplementation:</p>
  
  <pre><code>class PropertyAccess
  {
  public:
      template &lt;typename TClass, typename TType&gt; using SetterPointer = void (TClass::*)(const TType&amp;);
      template &lt;typename TClass, typename TType&gt; using GetterPointer = TType(TClass::*)();
  
      template &lt;typename TClass, typename TType&gt;
      PropertyAccess(
          TClass* object,
          SetterPointer&lt;TClass, TType&gt; setter,
          GetterPointer&lt;TClass, TType&gt; getter)
      {
          using std::placeholders::_1;
          m_setter = new Setter&lt;TType&gt;(std::bind(setter, object, _1));
          m_getter = new Getter&lt;TType&gt;(std::bind(getter, object));
      }
  
      ~PropertyAccess()
      {
          delete m_setter;
          delete m_getter;
      }
  
      template &lt;typename T&gt;
      void Set(const T&amp; value)
      {
          ((Setter&lt;T&gt;*)m_setter)-&gt;m_setter(value);
      }
  
      template &lt;typename T&gt;
      T Get()
      {
          return ((Getter&lt;T&gt;*)m_getter)-&gt;m_getter();
      }
  
  private:
      struct Base
      {
          virtual ~Base() {}
      };
  
      template &lt;typename TType&gt;
      struct Setter : public Base
      {
          Setter(std::function &lt; void(const TType&amp;) &gt; setter) { m_setter = setter; }
  
          std::function &lt; void(const TType&amp;) &gt; m_setter;
      };
  
      template &lt;typename TType&gt;
      struct Getter : public Base
      {
          Getter(std::function &lt; TType() &gt; getter) { m_getter = getter; }
  
          std::function &lt; TType() &gt; m_getter;
      };
  
      Base* m_setter;
      Base* m_getter;
  };
  </code></pre>
  
  <p>I use it like that:</p>
  
  <pre><code>ClassA a;
  PropertyAccess p(&amp;a, &amp;ClassA::SetSomeProperty, &amp;ClassA::GetSomeProperty);
  p.Set&lt;int&gt;(10);
  int v = p.Get&lt;int&gt;();
  </code></pre>
  
  <p>Everything works fine, but this is exteremly slow approach. Calling setters through PropertAccess is approx 18 times slower than call them directly. Speed is important here, because I need to use that mechanism in animation library (Animator will set properties without knowledge about source objects).</p>
  
  <p>So my question is: how to speed up this mechanism? Is there any "industry standard" approach for that? Maybe I should use classic method pointers instead of std::function?</p>