DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
OksSystem::StringMemoryArea Class Referenceabstract

String memory storage facility. More...

#include <StringMemoryArea.hpp>

Public Types

typedef unsigned int offset_t
typedef std::vector< std::string > str_vector
typedef std::map< std::string, std::string > str_map

Public Member Functions

offset_t add (const char *str)
size_t clear (offset_t offset)
const char * get_string (offset_t offset) const
offset_t insert (offset_t offset, const char *str)
void insert (offset_t *offset, const char *str)
offset_t insert (offset_t offset, const str_vector &vect)
void insert (offset_t *offset, const str_vector &vect)
str_vector get_vector (offset_t offset) const
offset_t insert (offset_t offset, const str_map &map)
void insert (offset_t *offset, const str_map &map)
str_map get_map (offset_t offset) const

Protected Member Functions

virtual const char * string_area_read () const =0
 read pointer for the string area
virtual char * string_area_write ()=0
 write pointer for the string area
virtual offset_t last_string () const =0
 offset of the end of the last string
virtual void last_string (offset_t offset)=0
 sets the offset of the end of last string
virtual size_t string_area_size () const =0
 size of the string area

Static Protected Attributes

static const char STRING_SEPARATOR = (char) 3
 character used to separate strings (i.e., to tokenize strings in vectors)
static const char MAP_ENTRY_SEPARATOR = (char) 4
 character used to separate map entries

Detailed Description

String memory storage facility.

This class offers facilities to store variable length strings into a memory area. The class can also store vector of strings and arrays. Strings are manipulated using offsets those represent offset in the memory area. As the addresses are relative, they are suitable for use in memory mapped files.

Note
The class is abstract, concrete subclasses need to implement methods that actually manipulate the memory area.
Author
Matthias Wiesmann
Version
1.0

Definition at line 35 of file StringMemoryArea.hpp.

Member Typedef Documentation

◆ offset_t

Definition at line 37 of file StringMemoryArea.hpp.

◆ str_map

typedef std::map<std::string, std::string> OksSystem::StringMemoryArea::str_map

Definition at line 39 of file StringMemoryArea.hpp.

◆ str_vector

typedef std::vector<std::string> OksSystem::StringMemoryArea::str_vector

Definition at line 38 of file StringMemoryArea.hpp.

Member Function Documentation

◆ add()

OksSystem::StringMemoryArea::offset_t OksSystem::StringMemoryArea::add ( const char * str)

Inserts a string at the end of the string region

Parameters
strthe string to insert
Returns
the offset in the string region of the string

Definition at line 28 of file StringMemoryArea.cpp.

28 {
29 ERS_PRECONDITION(str);
30 const size_t l = strlen(str);
31 const offset_t offset = last_string();
32 const offset_t start = offset+1;
33 const offset_t end = start + l ;
34
36 char *data = string_area_write();
37 char *current = &data[start];
38 strcpy(current,str);
39 last_string(end);
40 return start;
41} // add
#define ERS_RANGE_CHECK(min, val, max)
#define ERS_PRECONDITION(expression)
virtual char * string_area_write()=0
write pointer for the string area
virtual size_t string_area_size() const =0
size of the string area
virtual offset_t last_string() const =0
offset of the end of the last string
double offset

◆ clear()

size_t OksSystem::StringMemoryArea::clear ( offset_t offset)

Clears a string in the string region

Parameters
offsetthe offset of the start of the string
Returns
the lenght of the cleared string

Definition at line 48 of file StringMemoryArea.cpp.

48 {
49 char *data = string_area_write();
50 size_t count = 0;
51 while(data[count+offset]) {
52 data[count+offset] = '\0';
53 count++;
54 } // while
55 return count;
56} // clear_string

◆ get_map()

OksSystem::StringMemoryArea::str_map OksSystem::StringMemoryArea::get_map ( offset_t offset) const

Reads a string map from memory

Parameters
offsetoffset of the map in memory
Returns
a map of strings

Definition at line 181 of file StringMemoryArea.cpp.

181 {
182
185 const char* str = get_string(offset);
186
187 if(str != 0) {
188
189 const std::string text(str);
190 std::string::size_type start_p = 0;
191 std::string::size_type end_p = 0;
192
193 while(start_p < text.size()) {
194 end_p = text.find(MAP_ENTRY_SEPARATOR,start_p);
195 if (end_p == std::string::npos) {
196 end_p = text.length();
197 }
198 const std::string entry(text.substr(start_p, end_p - start_p));
199 const std::string::size_type entrySize = entry.size();
200 if(entrySize > 0) {
201 const std::string::size_type separator_p = entry.find(STRING_SEPARATOR);
202 if(separator_p != std::string::npos) {
203 if((separator_p + 1) < entrySize) {
204 map[entry.substr(0, separator_p)] = entry.substr(separator_p + 1);
205 } else {
206 map[entry.substr(0, separator_p)] = "";
207 }
208 }
209 }
210 start_p = end_p + 1;
211 } // while
212
213 } // if
214
215 return map;
216
217} // get_map
std::map< std::string, std::string > str_map
const char * get_string(offset_t offset) const
static const char STRING_SEPARATOR
character used to separate strings (i.e., to tokenize strings in vectors)
static const char MAP_ENTRY_SEPARATOR
character used to separate map entries

◆ get_string()

const char * OksSystem::StringMemoryArea::get_string ( offset_t offset) const

Finds a string in the string area

Parameters
offsetthe starting offset of the string
Returns
pointer to string
Note
the actual characters are in the shared area

Definition at line 64 of file StringMemoryArea.cpp.

64 {
65 if (offset==0) return 0;
67 const char* str = string_area_read();
68 if (str == NULL){
69 return 0;
70 }
71 return &(str[offset]);
72} // get_string
virtual const char * string_area_read() const =0
read pointer for the string area

◆ get_vector()

OksSystem::StringMemoryArea::str_vector OksSystem::StringMemoryArea::get_vector ( offset_t offset) const

Extracts a vector of string from memory

Parameters
offsetoffset of the vector
Returns
a vector of strings

Definition at line 114 of file StringMemoryArea.cpp.

114 {
115
117 const char *str = get_string(offset);
118
119 if(str != 0) {
120
121 const std::string text(str);
122
123 std::string::size_type start_p = 0;
124 std::string::size_type end_p = 0;
125
126 while(start_p < text.size()) {
127 end_p = text.find(STRING_SEPARATOR,start_p);
128 if (end_p == std::string::npos) {
129 end_p = text.length();
130 }
131 std::string extract = text.substr(start_p, end_p - start_p);
132 if(extract.size() > 0) {
133 vector.push_back(extract);
134 }
135 start_p = end_p + 1;
136 } // while
137
138 } // if
139
140 return vector;
141
142} // get_vector
std::vector< std::string > str_vector

◆ insert() [1/6]

void OksSystem::StringMemoryArea::insert ( offset_t * offset,
const char * str )

Inserts a string into memory This version updates the memory reference

Parameters
addressof the offset of the string
strthe string to insert

Definition at line 103 of file StringMemoryArea.cpp.

103 {
105 const offset_t n_offset = insert(*offset,str);
106 *offset = n_offset;
107} // insert
offset_t insert(offset_t offset, const char *str)

◆ insert() [2/6]

void OksSystem::StringMemoryArea::insert ( offset_t * offset,
const str_map & map )

Inserts a vector into memory This version updates the memory reference

Parameters
addressof the offset of the string
strthe string to insert
Note
the strings should not contain the caracter STRING_SEPARATOR (03)

Definition at line 248 of file StringMemoryArea.cpp.

248 {
250 const offset_t n_offset = insert(*offset,map);
251 *offset = n_offset;
252} // insert

◆ insert() [3/6]

void OksSystem::StringMemoryArea::insert ( offset_t * offset,
const str_vector & vect )

Inserts a vector into memory This version updates the memory reference

Parameters
addressof the offset of the string
strthe string to insert
Note
the strings should not contain the caracter STRING_SEPARATOR (03)

Definition at line 170 of file StringMemoryArea.cpp.

170 {
172 const offset_t n_offset = insert(*offset,vect);
173 *offset = n_offset;
174} // insert

◆ insert() [4/6]

OksSystem::StringMemoryArea::offset_t OksSystem::StringMemoryArea::insert ( offset_t offset,
const char * str )

Inserts a string in the string area.

Parameters
offsetoffset of the existing string to replace - 0 means no offset defined
strthe string to insert
Returns
the actual offset where the string was inserted
Note
An existing string is replaced only if the next string can fit in the space. if not, the old string is zeroed and the new string inserted at the end of the area. This can lead to fragmentation, so you should avoid replacing strings.

Definition at line 83 of file StringMemoryArea.cpp.

83 {
85 if (offset==0) return add(str);
87 size_t available = clear(offset);
88 if (available>=strlen(str)) {
89 char *data = string_area_write();
90 char *dest = &data[offset];
91 strcpy(dest,str);
92 return offset;
93 } // if
94 return add(str);
95} // insert_string
size_t clear(offset_t offset)
offset_t add(const char *str)

◆ insert() [5/6]

OksSystem::StringMemoryArea::offset_t OksSystem::StringMemoryArea::insert ( offset_t offset,
const str_map & map )

Inserts a map into memory

Parameters
offsetcurrent offset of the map
mapthe map to insert
Returns
the new offset of the map
Note
the strings should not contain the caracter STRING_SEPARATOR (03)

Definition at line 226 of file StringMemoryArea.cpp.

226 {
227 // ERS_RANGE_CHECK(0, offset, string_area_size());
228 str_map::const_iterator pos;
229 std::ostringstream stream;
230 for(pos=map.begin();pos!=map.end();pos++) {
231 if (map.begin()!=pos) {
232 stream << MAP_ENTRY_SEPARATOR;
233 } // if first
234 stream << (pos->first);
235 stream << STRING_SEPARATOR;
236 stream << (pos->second);
237 } // for
238 return insert(offset,stream.str().c_str());
239} // insert

◆ insert() [6/6]

OksSystem::StringMemoryArea::offset_t OksSystem::StringMemoryArea::insert ( offset_t offset,
const str_vector & vect )

Inserts a vector in the string area

Parameters
offsetthe offset of the string
vectthe vector to insert
Note
the strings should not contain the caracter STRING_SEPARATOR (03)

Definition at line 150 of file StringMemoryArea.cpp.

150 {
151 // ERS_RANGE_CHECK(0, offset, string_area_size());
152 str_vector::const_iterator pos;
153 std::ostringstream stream;
154 for(pos=vect.begin();pos!=vect.end();pos++) {
155 if (vect.begin()!=pos) {
156 stream << STRING_SEPARATOR;
157 } // if first
158 stream << (*pos);
159 } // for
160 return insert(offset,stream.str().c_str());
161} // insert

◆ last_string() [1/2]

virtual offset_t OksSystem::StringMemoryArea::last_string ( ) const
protectedpure virtual

offset of the end of the last string

◆ last_string() [2/2]

virtual void OksSystem::StringMemoryArea::last_string ( offset_t offset)
protectedpure virtual

sets the offset of the end of last string

◆ string_area_read()

virtual const char * OksSystem::StringMemoryArea::string_area_read ( ) const
protectedpure virtual

read pointer for the string area

◆ string_area_size()

virtual size_t OksSystem::StringMemoryArea::string_area_size ( ) const
protectedpure virtual

size of the string area

◆ string_area_write()

virtual char * OksSystem::StringMemoryArea::string_area_write ( )
protectedpure virtual

write pointer for the string area

Member Data Documentation

◆ MAP_ENTRY_SEPARATOR

const char OksSystem::StringMemoryArea::MAP_ENTRY_SEPARATOR = (char) 4
staticprotected

character used to separate map entries

Definition at line 42 of file StringMemoryArea.hpp.

◆ STRING_SEPARATOR

const char OksSystem::StringMemoryArea::STRING_SEPARATOR = (char) 3
staticprotected

character used to separate strings (i.e., to tokenize strings in vectors)

Definition at line 41 of file StringMemoryArea.hpp.


The documentation for this class was generated from the following files: