Introduction: I am writing a C ++ 11 application that makes extensive use of the legacy C code base. A very common pattern in legacy code is the existence of some struct LegacyStructthat are created and destroyed by methods such as
build_struct(LegacyStruct *L, int arg1, int arg2)
free_struct(LegacyStruct *L)
which are mostly constructors / destructors. The ownership model in the legacy codebase is very similar to unique_ptr-esque, so I am aiming to port it to the secure RAII-minded class for the shell, as shown below:
class Wrapper {
public:
Wrapper::Wraper() : handle() {}
Wrapper::Wrapper(int same_arg1, int same_arg2);
Wrapper::Wrapper(const Wrapper &W) = delete;
Wrapper::Wrapper(Wrapper &&W) : handle(std::move(W.handle)) {}
private:
std::unique_ptr<LegacyStruct, custom_deleter> handle;
custom_deleter free_struct std::default_delete LegacyStruct. , , , , .
: ,
typedef struct LegacyNode {
int stack_allocated_data;
OtherStruct *heap_allocated_data;
LegacyNode *next;
} LegacyNode;
, unique_ptr -esque: , . , free_node(LegacyNode *N), heap_allocated_data, node.
. ,
build_list(LegacyNode **L, int *count_p, int other_args){
LegacyNode *newnode;
newcut->next = *L;
*L = newcut;
(*count_p)++;
}
build_list
int list_count = 0;
LegacyNode *L = (LegacyNode *) NULL;
build_list(&L, &list_count, 99);
/: build_list - , , , , build_list, , .
, ListWrap, node , /, Wrapper , .. , , ..
, , . head_node LegacyNode &head_node.get() build_list, / ?
- node, , node build_list, , , free_node erase , .
, - CS-101, ! , , , , - .