RelatedStateValue.hpp 2.16 KB
Newer Older
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
1
/*
2
 * Copyright (C) 2014-2021 DBIS Group - TU Ilmenau, All Rights Reserved.
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
3
4
5
 *
 * This file is part of the PipeFabric package.
 *
6
7
8
 * PipeFabric is free software: you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
9
 *
10
11
12
 * PipeFabric is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
13
 *
14
15
 * You should have received a copy of the GNU General Public License along with PipeFabric. If not,
 * see <http://www.gnu.org/licenses/>.
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
16
17
18
19
20
21
22
23
 */

#ifndef RelatedStateValue_hpp_
#define RelatedStateValue_hpp_

#include "core/Tuple.hpp"

namespace pfabric {
24
template <class TinPtr, class StorageType, class Type, int Index>
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
25
26
27
28
29
class RelatedStateValue {
protected:

	mutable boost::atomic<short> refCount;
public:
30
	typedef boost::intrusive_ptr<RelatedStateValue<TinPtr, StorageType, Type, Index>> RelatedStateValuePtr;
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
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
	/**
	 * gets the current related value
	 * @return the current value
	 */
	virtual Type getValue() = 0;

	/**
	 * updates the related value according to incoming event (instance)
	 * @param e the new selected instance
	 */
	virtual void updateValue(const TinPtr& e) = 0;

	/**
	 * get the attribute index
	 * @return the attribute name
	 */
	int getAttributeIdx() { return Index; }

	/**
	 * initializes the value by a new incoming event (instance)
	 * @param e incoming event to init the value accordingly
	 */
	virtual void initValue(const TinPtr& e) = 0;
	/**
	 * constructor: nothing to do
	 */
	RelatedStateValue() {}
	/**
	 * destructor, nothing to do
	 */
	virtual ~RelatedStateValue() {}


	friend void intrusive_ptr_add_ref(const RelatedStateValue *x) {
		x->refCount.fetch_add(1, boost::memory_order_relaxed);
	}
	friend void intrusive_ptr_release(const RelatedStateValue *x) {
		if (x->refCount.fetch_sub(1, boost::memory_order_release) == 1) {
			boost::atomic_thread_fence(boost::memory_order_acquire);
			delete x;
		}
	}

};
}
#endif /* RelatedStateValue_hpp_ */