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 30 of file StringMemoryArea.hpp.

Member Typedef Documentation

◆ offset_t

Definition at line 32 of file StringMemoryArea.hpp.

◆ str_map

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

Definition at line 34 of file StringMemoryArea.hpp.

◆ str_vector

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

Definition at line 33 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 23 of file StringMemoryArea.cpp.

23 {
24 ERS_PRECONDITION(str);
25 const size_t l = strlen(str);
26 const offset_t offset = last_string();
27 const offset_t start = offset+1;
28 const offset_t end = start + l ;
29
31 char *data = string_area_write();
32 char *current = &data[start];
33 strcpy(current,str);
34 last_string(end);
35 return start;
36} // 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 43 of file StringMemoryArea.cpp.

43 {
44 char *data = string_area_write();
45 size_t count = 0;
46 while(data[count+offset]) {
47 data[count+offset] = '\0';
48 count++;
49 } // while
50 return count;
51} // 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 176 of file StringMemoryArea.cpp.

176 {
177
180 const char* str = get_string(offset);
181
182 if(str != 0) {
183
184 const std::string text(str);
185 std::string::size_type start_p = 0;
186 std::string::size_type end_p = 0;
187
188 while(start_p < text.size()) {
189 end_p = text.find(MAP_ENTRY_SEPARATOR,start_p);
190 if (end_p == std::string::npos) {
191 end_p = text.length();
192 }
193 const std::string entry(text.substr(start_p, end_p - start_p));
194 const std::string::size_type entrySize = entry.size();
195 if(entrySize > 0) {
196 const std::string::size_type separator_p = entry.find(STRING_SEPARATOR);
197 if(separator_p != std::string::npos) {
198 if((separator_p + 1) < entrySize) {
199 map[entry.substr(0, separator_p)] = entry.substr(separator_p + 1);
200 } else {
201 map[entry.substr(0, separator_p)] = "";
202 }
203 }
204 }
205 start_p = end_p + 1;
206 } // while
207
208 } // if
209
210 return map;
211
212} // get_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
std::map< std::string, std::string > str_map

◆ 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 59 of file StringMemoryArea.cpp.

59 {
60 if (offset==0) return 0;
62 const char* str = string_area_read();
63 if (str == NULL){
64 return 0;
65 }
66 return &(str[offset]);
67} // 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 109 of file StringMemoryArea.cpp.

109 {
110
112 const char *str = get_string(offset);
113
114 if(str != 0) {
115
116 const std::string text(str);
117
118 std::string::size_type start_p = 0;
119 std::string::size_type end_p = 0;
120
121 while(start_p < text.size()) {
122 end_p = text.find(STRING_SEPARATOR,start_p);
123 if (end_p == std::string::npos) {
124 end_p = text.length();
125 }
126 std::string extract = text.substr(start_p, end_p - start_p);
127 if(extract.size() > 0) {
128 vector.push_back(extract);
129 }
130 start_p = end_p + 1;
131 } // while
132
133 } // if
134
135 return vector;
136
137} // 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 98 of file StringMemoryArea.cpp.

98 {
100 const offset_t n_offset = insert(*offset,str);
101 *offset = n_offset;
102} // 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 243 of file StringMemoryArea.cpp.

243 {
245 const offset_t n_offset = insert(*offset,map);
246 *offset = n_offset;
247} // 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 165 of file StringMemoryArea.cpp.

165 {
167 const offset_t n_offset = insert(*offset,vect);
168 *offset = n_offset;
169} // 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 78 of file StringMemoryArea.cpp.

78 {
80 if (offset==0) return add(str);
82 size_t available = clear(offset);
83 if (available>=strlen(str)) {
84 char *data = string_area_write();
85 char *dest = &data[offset];
86 strcpy(dest,str);
87 return offset;
88 } // if
89 return add(str);
90} // 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 221 of file StringMemoryArea.cpp.

221 {
222 // ERS_RANGE_CHECK(0, offset, string_area_size());
223 str_map::const_iterator pos;
224 std::ostringstream stream;
225 for(pos=map.begin();pos!=map.end();pos++) {
226 if (map.begin()!=pos) {
227 stream << MAP_ENTRY_SEPARATOR;
228 } // if first
229 stream << (pos->first);
230 stream << STRING_SEPARATOR;
231 stream << (pos->second);
232 } // for
233 return insert(offset,stream.str().c_str());
234} // 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 145 of file StringMemoryArea.cpp.

145 {
146 // ERS_RANGE_CHECK(0, offset, string_area_size());
147 str_vector::const_iterator pos;
148 std::ostringstream stream;
149 for(pos=vect.begin();pos!=vect.end();pos++) {
150 if (vect.begin()!=pos) {
151 stream << STRING_SEPARATOR;
152 } // if first
153 stream << (*pos);
154 } // for
155 return insert(offset,stream.str().c_str());
156} // 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 37 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 36 of file StringMemoryArea.hpp.


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