File: Maybe.h

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (43 lines) | stat: -rw-r--r-- 1,209 bytes parent folder | download | duplicates (2)
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
#pragma once

namespace storm {

	/**
	 * Custom Maybe-type for interoperability with C++. It is only intended for value types, and
	 * aimed at compatibility between C++ and Storm as parameters and return values. As such, a
	 * number of limitations apply. The preprocessor will warn about most of these.
	 * - This class can *only* be used with value types. Use MAYBE(X *) for class-types.
	 * - Classes that rely on custom constructors/destructors are not properly handled
	 *   (they will be created/destroyed even if the data is not present).
	 * - Classes without a default constructor are not supported (for the above reason).
	 */
	template <class T>
	class Maybe {
	public:
		// Create an empty value.
		Maybe() : data(), present(false) {}

		// Create a value.
		explicit Maybe(const T &data) : data(data), present(true) {}

		// Any value?
		inline Bool any() const {
			return present;
		}
		inline Bool empty() const {
			return !present;
		}

		// Get the value. Will not check whether or not the value is present.
		T &value() { return data; }
		const T &value() const { return data; }

	private:
		// The actual data.
		T data;

		// If there is a value present or not.
		Bool present;
	};

}