Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Toggle main menu visibility
Loading...
Searching...
No Matches
optional.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2020 Roc Streaming authors
3
*
4
* This Source Code Form is subject to the terms of the Mozilla Public
5
* License, v. 2.0. If a copy of the MPL was not distributed with this
6
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
*/
8
9
//! @file roc_core/optional.h
10
//! @brief Optionally constructed object.
11
12
#ifndef ROC_CORE_OPTIONAL_H_
13
#define ROC_CORE_OPTIONAL_H_
14
15
#include "
roc_core/aligned_storage.h
"
16
#include "
roc_core/attributes.h
"
17
#include "
roc_core/noncopyable.h
"
18
#include "
roc_core/panic.h
"
19
#include "
roc_core/stddefs.h
"
20
21
namespace
roc
{
22
namespace
core
{
23
24
//! Optionally constructed object.
25
template
<
class
T,
size_t
Size = sizeof(T)>
class
Optional :
public
NonCopyable<> {
26
public
:
27
Optional()
28
: ptr_(NULL) {
29
}
30
31
~Optional() {
32
if
(ptr_) {
33
ptr_->~T();
34
}
35
}
36
37
//! Set pointer to the newly created object, destroy old pointed object if set.
38
//! @pre
39
//! @p ptr should be returned by associated placement new.
40
void
reset
(T* ptr = NULL) {
41
if
(ptr_) {
42
ptr_->~T();
43
}
44
if
(ptr && (
void
*)ptr != storage_.memory()) {
45
roc_panic
(
"optional: attempt to set incorrect object"
);
46
}
47
ptr_ = ptr;
48
}
49
50
//! Get underlying object.
51
T*
get
()
const
{
52
return
ptr_;
53
}
54
55
//! Get underlying object.
56
T*
operator->
()
const
{
57
if
(ptr_ == NULL) {
58
roc_panic
(
"optional: attempt to dereference uninitialized object"
);
59
}
60
return
ptr_;
61
}
62
63
//! Get underlying reference.
64
T&
operator*
()
const
{
65
if
(ptr_ == NULL) {
66
roc_panic
(
"optional: attempt to dereference uninitialized object"
);
67
}
68
return
*ptr_;
69
}
70
71
//! Convert to bool.
72
operator
const
struct
unspecified_bool *()
const
{
73
return
(
const
unspecified_bool*)ptr_;
74
}
75
76
//! Get object memory.
77
//! @pre
78
//! Should be called before object is actually allocated.
79
void
*
unallocated_memory
() {
80
if
(ptr_) {
81
roc_panic
(
"optional: attempt to get memory after the object was created"
);
82
}
83
return
storage_.memory();
84
}
85
86
private
:
87
T* ptr_;
88
AlignedStorage<Size>
storage_;
89
};
90
91
}
// namespace core
92
}
// namespace roc
93
94
//! Placement new for core::Optional.
95
//! @note
96
//! nothrow forces compiler to check for NULL return value before calling ctor.
97
template
<
class
T,
size_t
Size>
98
inline
void
*
operator
new
(
size_t
size,
roc::core::Optional<T, Size>
& opt)
throw
() {
99
roc_panic_if_not
(size <= Size);
100
return
opt.unallocated_memory();
101
}
102
103
//! Placement delete for core::Optional.
104
//! @note
105
//! Compiler calls this if ctor throws in a placement new expression.
106
template
<
class
T,
size_t
Size>
107
inline
void
operator
delete
(
void
*,
roc::core::Optional<T, Size>
&)
throw
() {
108
}
109
110
#endif
// ROC_CORE_OPTIONAL_H_
aligned_storage.h
Aligned storage.
attributes.h
Compiler attributes.
roc::core::AlignedStorage
Fixed-size maximum-aligned storage.
Definition
aligned_storage.h:22
roc::core::Optional
Optionally constructed object.
Definition
optional.h:25
roc::core::Optional::operator->
T * operator->() const
Get underlying object.
Definition
optional.h:56
roc::core::Optional::operator*
T & operator*() const
Get underlying reference.
Definition
optional.h:64
roc::core::Optional::get
T * get() const
Get underlying object.
Definition
optional.h:51
roc::core::Optional::reset
void reset(T *ptr=NULL)
Set pointer to the newly created object, destroy old pointed object if set.
Definition
optional.h:40
roc::core::Optional::unallocated_memory
void * unallocated_memory()
Get object memory.
Definition
optional.h:79
roc::core
General-purpose building blocks and platform abstraction layer.
roc
Root namespace.
noncopyable.h
Non-copyable object.
panic.h
Panic.
roc_panic_if_not
#define roc_panic_if_not(x)
Panic if condition is false.
Definition
panic.h:37
roc_panic
#define roc_panic(...)
Print error message and terminate program gracefully.
Definition
panic.h:50
stddefs.h
Commonly used types and functions.
roc_core
optional.h
Generated by
1.17.0