summaryrefslogtreecommitdiff
path: root/lib/block_props.cpp
blob: f32dc69f9e94559031677c8045c4ae8c6760bf1e (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.

#include "element_impl.hpp"
#include <gras/block.hpp>

using namespace gras;

PropertyRegistry::PropertyRegistry(void){}
PropertyRegistry::~PropertyRegistry(void){}

/***********************************************************************
 * The actual thread-safe implementation of property handling
 **********************************************************************/
void BlockActor::handle_prop_access(
    const PropAccessMessage &message,
    const Theron::Address from
)
{
    //setup reply
    PropAccessMessage reply;
    reply.set = not message.set;
    reply.key = message.key;

    //call into the handler overload to do the property access
    try
    {
        reply.value = block_ptr->_handle_prop_access(message.key, message.value, message.set);
    }
    catch (const std::exception &e)
    {
        reply.error = e.what();
    }
    catch (...)
    {
        reply.error = "unknown error";
    }

    //send the reply
    this->Send(reply, from); //ACK

    //work could have been skipped by a high prio msg
    //forcefully kick the task to recheck in a new call
    this->Send(SelfKickMessage(), this->GetAddress());
}

PMCC Block::_handle_prop_access(const std::string &key, const PMCC &value, const bool set)
{
    PropertyRegistrySptr pr = (set)?
        (*this)->block->setter_registry[key] :
        (*this)->block->getter_registry[key]
    ;
    if (not pr) throw std::invalid_argument("no property registered for key: " + key);
    if (set)
    {
        pr->set(value);
        return PMCC();
    }
    return pr->get();
}

/***********************************************************************
 * A special receiver to handle the property access result
 **********************************************************************/
struct PropAccessReceiver : Theron::Receiver
{
    PropAccessReceiver(void)
    {
        this->RegisterHandler(this, &PropAccessReceiver::handle_prop_access);
    }

    void handle_prop_access(const PropAccessMessage &msg, const Theron::Address)
    {
        this->message = msg;
    }

    PropAccessMessage message;
};

/***********************************************************************
 * Handle the get and set calls from the user's call-stack
 **********************************************************************/
template <typename ActorType>
static PMCC prop_access_dispatcher(ActorType &actor, const std::string &key, const PMCC &value, const bool set)
{
    PropAccessReceiver receiver;
    PropAccessMessage message;
    message.prio_token = actor->prio_token;
    message.set = set;
    message.key = key;
    message.value = value;
    actor->GetFramework().Send(message, receiver.GetAddress(), actor->GetAddress());
    receiver.Wait();
    if (not receiver.message.error.empty())
    {
        throw std::runtime_error(receiver.message.error);
    }
    return receiver.message.value;
}

void Block::_register_getter(const std::string &key, void *pr)
{
    (*this)->block->getter_registry[key].reset(reinterpret_cast<PropertyRegistry *>(pr));
}

void Block::_register_setter(const std::string &key, void *pr)
{
    (*this)->block->setter_registry[key].reset(reinterpret_cast<PropertyRegistry *>(pr));
}

void Block::_set_property(const std::string &key, const PMCC &value)
{
    prop_access_dispatcher((*this)->block, key, value, true);
}

PMCC Block::_get_property(const std::string &key)
{
    return prop_access_dispatcher((*this)->block, key, PMCC(), false);
}