summaryrefslogtreecommitdiff
path: root/include/gras/detail/property.hpp
blob: fd13025c1d1dbbc89515ca460fcbd31d5fd019ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.

#ifndef INCLUDED_GRAS_DETAIL_PROPERTY_HPP
#define INCLUDED_GRAS_DETAIL_PROPERTY_HPP

#include <gras/gras.hpp>
#include <PMC/PMC.hpp>
#include <boost/shared_ptr.hpp>

namespace gras
{

struct GRAS_API PropertyRegistry
{
    PropertyRegistry(void);
    virtual ~PropertyRegistry(void);
    virtual void set(const PMCC &) = 0;
    virtual PMCC get(void) = 0;
};

typedef boost::shared_ptr<PropertyRegistry> PropertyRegistrySptr;

template <typename ClassType, typename ValueType>
struct PropertyRegistryImpl : PropertyRegistry
{
    PropertyRegistryImpl(
        ClassType *my_class,
        ValueType(ClassType::*getter)(void),
        void(ClassType::*setter)(const ValueType &)
    ):
        _my_class(my_class),
        _getter(getter),
        _setter(setter)
    {}
    virtual ~PropertyRegistryImpl(void){}

    void set(const PMCC &value)
    {
        return _setter(_my_class, value.as<ValueType>());
    }

    PMCC get(void)
    {
        return PMC_M(_getter(_my_class));
    }

    ClassType *_my_class;
    ValueType(ClassType::*_getter)(void);
    void(ClassType::*_setter)(const ValueType &);
};

} //namespace gras

#endif /*INCLUDED_GRAS_DETAIL_PROPERTY_HPP*/