-
Notifications
You must be signed in to change notification settings - Fork 6
/
cell.hpp
116 lines (90 loc) · 2.74 KB
/
cell.hpp
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
//
// cell.hpp
// spreadsheet
//
// Created by Irit Katriel on 01/08/2012.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#pragma once
#include "streamulus.h"
namespace spreadsheet {
template<typename T>
class Cell
{
public:
using expr_type = streamulus::InputStream<T>;
Cell()
: mOutStream(streamulus::NewInputStream<T>("unnamed", false))
{
SetEngine(nullptr, "");
}
Cell(streamulus::Streamulus* streamulus_engine, const std::string& name)
: mOutStream(streamulus::NewInputStream<T>(name.c_str(), false))
{
SetEngine(streamulus_engine, name);
}
void SetEngine(streamulus::Streamulus* streamulus_engine, const std::string& name)
{
mStreamulusEngine = streamulus_engine;
mName = name;
}
expr_type operator()() const
{
return mOutStream;
}
T Value()
{
assert(IsValid());
return *mValue;
}
template<typename Expr>
void Set(const Expr& expr)
{
using streamulus::Streamify;
if (mSubscription)
{
mStreamulusEngine->UnSubscribe<T>(*mSubscription);
mSubscription = boost::none;
}
SetCellValue set_cell_value(this);
mSubscription = mStreamulusEngine->Subscribe(Streamify(set_cell_value)(expr));
}
private:
bool IsValid()
{
return mSubscription && mValue;
}
void SetValue(const T& value)
{
mValue = value;
InputStreamPut(mOutStream,value);
}
struct SetCellValue
{
public:
SetCellValue(Cell* cell)
:mCell(cell)
{
}
template<class Sig>
struct result
{
typedef T type;
};
typename result<SetCellValue>::type
operator()(const T& value)
{
// std::cout << mCell->mName << " <-- " << value << std::endl;
mCell->SetValue(value);
return value;
}
private:
Cell* mCell;
};
streamulus::Streamulus* mStreamulusEngine; // No ownership. Do not delete.
std::string mName;
boost::optional<typename streamulus::Subscription<T>::type> mSubscription;
expr_type mOutStream;
boost::optional<T> mValue;
};
} // ns spreadsheet