-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractorfromsmartptr.h
More file actions
69 lines (56 loc) · 3.48 KB
/
extractorfromsmartptr.h
File metadata and controls
69 lines (56 loc) · 3.48 KB
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
#ifndef EXTRACTORFROMSMARTPTR_H
#define EXTRACTORFROMSMARTPTR_H
/*
Copyright (c) 2024 Sidorov Dmitry
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "extractor.h"
#include "dbreadermemory.h"
namespace dbframework {
/*!
The ExtractorFromSmartPtr class template is the special implementation of extractor. In this framework extractor is a class that is used to get
pointer to some object (called stored object) stored inside other object (called storage object). The ExtractorFromSmartPtr is used when storage
object is some STL-compatible smart pointer. The composition of ExtractorFromSmartPtr with some extractor from container can be usefull when
storing data to the container of smart pointers. ExtractorFromSmartPtr::extract allocates memory if smart pointer owns nothing and returns pointer
to stored object. ExtractorFromSmartPtr::rollback do nothing. This is done so because smart pointer acting as a storage is usually an element of some
container. In case of reading error this element is usually removed from container by rollback method of the extractor from container.
Template parameters.
Storage is the storage class. Storage should be some STL-compatible smart pointer. Storage::element_type shoud define the type pointer points to.
Storage::element_type should have default constructor. Storage should have constructor taking Storage::element_type* parameter, a dereference operator
and operator bool, checking whether smart pointer owns an object.
Allocator is the class that is used to allocate readers and extractors. It shoud have static function template named alloc which takes arbitrary
arguments, pass them to object constructor and returns pointer to allocated object. Type of the allocated object should be defined as template
argument. Default value is DefaultAlloc which uses standard operator new.
*/
template <class Storage, class Allocator = DefaultAlloc>
class ExtractorFromSmartPtr : public Extractor<Storage, typename Storage::element_type> {
protected:
/*!
Allocates memory if smart pointer owns nothing and returns pointer to stored object.
@param[in] storage Pointer to storage object.
@return Pointer to stored object.
*/
void* doExtract(void* storage) override
{
Storage* p = static_cast<Storage*>(storage);
if (!(*p)) {
typename Storage::element_type* ptr;
Allocator::alloc(&ptr);
*p = Storage(ptr);
}
return &(**p);
}
};
}
#endif //EXTRACTORFROMSMARTPTR_H