SpiecsEngine
 
Loading...
Searching...
No Matches
RuntimeMemoryBlock.cpp
Go to the documentation of this file.
1/**
2* @file RuntimeMemoryBlock.cpp.
3* @brief The RuntimeMemoryBlock Class Implementation.
4* @author Spices.
5*/
6
7#include "Pchheader.h"
9#include "Core/Reflect/TypeReflect.h"
10
11namespace scl {
12
14 {
15 /**
16 * @brief Free the memoty that handled.
17 */
18 if (begin_) free(begin_);
19 }
20
21 void runtime_memory_block::add_element(const std::string& name, const std::string& type)
22 {
23 /**
24 * @brief Only allow add to object_ with parameter that has not added.
25 */
26 if(object_.find(name) != object_.end())
27 {
28 std::stringstream ss;
29 ss << "runtime_memory_block:: add failed: already has the element: " << name;
30
31 SPICES_CORE_WARN(ss.str());
32 return;
33 }
34
35 /**
36 * @brief Recording current parameter's position to object_.
37 */
38 object_[name] = bytes_;
39
40 /**
41 * @brief Add to bytes_ with parameter type.
42 */
43 bytes_ += Spices::StrType2Size(type);
44 }
45
47 {
48 /**
49 * @brief Free the memory if begin_ has mallocked.
50 */
51 if(begin_) free(begin_);
52
53 /**
54 * @brief Malloc memory to begin_ with bytes_.
55 */
56 begin_ = malloc(bytes_);
57 }
58
59 void runtime_memory_block::for_each(const std::function<bool(const std::string& name, void* pt)>& fn) const
60 {
61 /**
62 * @brief Iter without order.
63 */
64 for(auto& pair : object_)
65 {
66 /**
67 * @brief Move begin_ to correct position.
68 */
69 void* it = static_cast<char*>(begin_) + pair.second;
70
71 /**
72 * @brief The function pointer of how to explain the memory with parameter.
73 * @param[in] name The name of parameter.
74 * @param[in] pt The pointer of start of parameter occupied.
75 */
76 if(fn(pair.first, it)) break;
77 }
78 }
79
81 {
82 /**
83 * @brief Return location if finded.
84 */
85 if (object_.find(name) == object_.end()) return UINT32_MAX;
86 return object_[name];
87 }
88
89 bool runtime_memory_block::has_value(const std::string& name)
90 {
91 /**
92 * @brief Return true if finded.
93 */
94 if (object_.find(name) != object_.end()) return true;
95 return false;
96 }
97}
bool has_value(const std::string &name)
Determine is item inside this container.
void for_each(const std::function< bool(const std::string &name, void *pt)> &fn) const
Iter the object_ and call explain_element() to filling data.
void build()
Malloc a memory to begin_.
void add_element(const std::string &name, const std::string &type)
Add a element to object_, means a memory block will be occupied with given param type.
size_t item_location(const std::string &name)
Get item location in blocks.
virtual ~runtime_memory_block()
Destructor Function.
void * begin_
The begin pointer of the continue memory block handled.
The container is wrapper of a continue memory block. Used in Material::BuildMaterial(),...