blob: 36c13b7c699398d9bedcfbd86c49a35dfada26e4 [file] [log] [blame]
E. Scott Daniels2301eb82020-07-15 13:46:51 -04001// vi: ts=4 sw=4 noet:
2/*
3==================================================================================
4 Copyright (c) 2020 AT&T Intellectual Property.
5 Copyright (c) 2020 Nokia
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18==================================================================================
19*/
20
21/*
22 Mnemonic: symtab.cpp
23 Abstract: Wrap the RMR symbol table to that we can easily accept and push
24 back smart pointers. This is a very LIGHT wrapper supporting only
25 implant and extract operations. Nice things like being able to delete
26 and walk the full map aren't supported.
27
28 Because the symbol table references an underlying C struct and
29 related colleciton of pointers to things, it is difficult at best
30 to copy the beast. The symtab thus cannot be copied and compile time
31 errors will happen if tried.
32
33 Date: 18 June 2020
34 Author: E. Scott Daniels
35*/
36
37#include <cstdlib>
38#include <string>
39
40#include <rmr/rmr_symtab.h>
41#include "symtab.h"
42
43namespace munchkin {
44
45// --- construction, destruction, copy/move --------------------------------------------
46
47
48Symtab::Symtab( int size ) :
49 st( rmr_sym_alloc( size ) ),
50 space( 1 )
51{ /* empty body */ }
52
53Symtab::~Symtab( ) {
54 if( st != NULL ) {
55 fprintf( stderr, "<symtab> freeing real table %p\n", st );
56 rmr_sym_free( st );
57 st = NULL;
58 fprintf( stderr, "<symtab> freefinished\n" );
59 }
60}
61
62
63// ---- do NOT implement copy funcions -------------------------
64
65/*
66 Move builder. Given a source object instance (soi), move the information from
67 the soi ensuring that the destriction of the soi doesn't trash things from
68 under us.
69*/
70Symtab::Symtab( Symtab&& soi ) {
71 st = soi.st;
72 space = soi.space;
73
74 soi.st = NULL; // remove ref so that it's not destroyed after the move (do NOT free)
75}
76
77/*
78 Move Assignment operator. Move the message data to the existing object
79 ensure the object reference is cleaned up, and ensuring that the source
80 object references are removed.
81*/
82Symtab& Symtab::operator=( Symtab&& soi ) {
83 if( this != &soi ) { // cannot do self assignment
84
85 // if ever we have something that must be delteded/freed, do it here
86
87 st = soi.st;
88 space = soi.space;
89
90 soi.st = NULL;
91 }
92
93 return *this;
94}
95
96// ------- real work functions --------------------------------------------------------
97
98void Symtab::Implant( std::string name, int space, std::shared_ptr<void> data ) {
99 hto_t* hto; // the hash table object
100
101 if( st != NULL ) {
102 hto = (hto_t *) malloc( sizeof( *hto ) ); // raw pointer to give rmr_sym
103 hto->real_obj = data;
104 rmr_sym_put( st, name.c_str(), space, hto );
105 }
106}
107
108std::shared_ptr<void> Symtab::Extract( std::string name, int space ) {
109 hto_t* hto = NULL; // the hash table object
110
111 hto = (hto_t *) rmr_sym_get( st, name.c_str(), space );
112 if( hto == NULL ) {
113 return NULL;
114 }
115
116 return hto->real_obj;
117}
118
119
120} // namespace