1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
|
// Copyright (C) 2007 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_CONFIG_READER_THREAD_SAFe_
#define DLIB_CONFIG_READER_THREAD_SAFe_
#include "config_reader_kernel_abstract.h"
#include <string>
#include <iostream>
#include <sstream>
#include "../algs.h"
#include "../interfaces/enumerable.h"
#include "../threads.h"
#include "config_reader_thread_safe_abstract.h"
namespace dlib
{
template <
typename config_reader_base,
typename map_string_void
>
class config_reader_thread_safe_1
{
/*!
CONVENTION
- get_mutex() == *m
- *cr == the config reader being extended
- block_table[x] == (void*)&block(x)
- block_table.size() == the number of blocks in *cr
- block_table[key] == a config_reader_thread_safe_1 that contains &cr.block(key)
- if (own_pointers) then
- this object owns the m and cr pointers and should delete them when destructed
!*/
public:
config_reader_thread_safe_1 (
const config_reader_base* base,
rmutex* m_
);
config_reader_thread_safe_1();
typedef typename config_reader_base::config_reader_error config_reader_error;
typedef typename config_reader_base::config_reader_access_error config_reader_access_error;
config_reader_thread_safe_1(
std::istream& in
);
config_reader_thread_safe_1(
const std::string& config_file
);
virtual ~config_reader_thread_safe_1(
);
void clear (
);
void load_from (
std::istream& in
);
void load_from (
const std::string& config_file
);
bool is_key_defined (
const std::string& key
) const;
bool is_block_defined (
const std::string& name
) const;
typedef config_reader_thread_safe_1 this_type;
const this_type& block (
const std::string& name
) const;
const std::string& operator[] (
const std::string& key
) const;
template <
typename queue_of_strings
>
void get_keys (
queue_of_strings& keys
) const;
template <
typename queue_of_strings
>
void get_blocks (
queue_of_strings& blocks
) const;
inline const rmutex& get_mutex (
) const;
private:
void fill_block_table (
);
/*!
ensures
- block_table.size() == the number of blocks in cr
- block_table[key] == a config_reader_thread_safe_1 that contains &cr.block(key)
!*/
rmutex* m;
config_reader_base* cr;
map_string_void block_table;
const bool own_pointers;
// restricted functions
config_reader_thread_safe_1(config_reader_thread_safe_1&);
config_reader_thread_safe_1& operator=(config_reader_thread_safe_1&);
};
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// member function definitions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
config_reader_thread_safe_1<config_reader_base,map_string_void>::
config_reader_thread_safe_1(
const config_reader_base* base,
rmutex* m_
) :
m(m_),
cr(const_cast<config_reader_base*>(base)),
own_pointers(false)
{
fill_block_table();
}
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
config_reader_thread_safe_1<config_reader_base,map_string_void>::
config_reader_thread_safe_1(
) :
m(0),
cr(0),
own_pointers(true)
{
try
{
m = new rmutex;
cr = new config_reader_base;
}
catch (...)
{
if (m) delete m;
if (cr) delete cr;
throw;
}
}
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
void config_reader_thread_safe_1<config_reader_base,map_string_void>::
clear(
)
{
auto_mutex M(*m);
cr->clear();
fill_block_table();
}
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
void config_reader_thread_safe_1<config_reader_base,map_string_void>::
load_from(
std::istream& in
)
{
auto_mutex M(*m);
cr->load_from(in);
fill_block_table();
}
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
void config_reader_thread_safe_1<config_reader_base,map_string_void>::
load_from(
const std::string& config_file
)
{
auto_mutex M(*m);
cr->load_from(config_file);
fill_block_table();
}
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
config_reader_thread_safe_1<config_reader_base,map_string_void>::
config_reader_thread_safe_1(
std::istream& in
) :
m(0),
cr(0),
own_pointers(true)
{
try
{
m = new rmutex;
cr = new config_reader_base(in);
fill_block_table();
}
catch (...)
{
if (m) delete m;
if (cr) delete cr;
throw;
}
}
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
config_reader_thread_safe_1<config_reader_base,map_string_void>::
config_reader_thread_safe_1(
const std::string& config_file
) :
m(0),
cr(0),
own_pointers(true)
{
try
{
m = new rmutex;
cr = new config_reader_base(config_file);
fill_block_table();
}
catch (...)
{
if (m) delete m;
if (cr) delete cr;
throw;
}
}
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
config_reader_thread_safe_1<config_reader_base,map_string_void>::
~config_reader_thread_safe_1(
)
{
if (own_pointers)
{
delete m;
delete cr;
}
// clear out the block table
block_table.reset();
while (block_table.move_next())
{
delete static_cast<config_reader_thread_safe_1*>(block_table.element().value());
}
block_table.clear();
}
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
bool config_reader_thread_safe_1<config_reader_base,map_string_void>::
is_key_defined (
const std::string& key
) const
{
auto_mutex M(*m);
return cr->is_key_defined(key);
}
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
bool config_reader_thread_safe_1<config_reader_base,map_string_void>::
is_block_defined (
const std::string& name
) const
{
auto_mutex M(*m);
return cr->is_block_defined(name);
}
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
const config_reader_thread_safe_1<config_reader_base,map_string_void>& config_reader_thread_safe_1<config_reader_base,map_string_void>::
block (
const std::string& name
) const
{
auto_mutex M(*m);
if (block_table.is_in_domain(name) == false)
{
throw config_reader_access_error(name,"");
}
return *static_cast<config_reader_thread_safe_1*>(block_table[name]);
}
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
const std::string& config_reader_thread_safe_1<config_reader_base,map_string_void>::
operator[] (
const std::string& key
) const
{
auto_mutex M(*m);
return (*cr)[key];
}
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
template <
typename queue_of_strings
>
void config_reader_thread_safe_1<config_reader_base,map_string_void>::
get_keys (
queue_of_strings& keys
) const
{
auto_mutex M(*m);
cr->get_keys(keys);
}
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
template <
typename queue_of_strings
>
void config_reader_thread_safe_1<config_reader_base,map_string_void>::
get_blocks (
queue_of_strings& blocks
) const
{
auto_mutex M(*m);
cr->get_blocks(blocks);
}
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
const rmutex& config_reader_thread_safe_1<config_reader_base,map_string_void>::
get_mutex (
) const
{
return *m;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// private member functions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
typename config_reader_base,
typename map_string_void
>
void config_reader_thread_safe_1<config_reader_base,map_string_void>::
fill_block_table (
)
{
using namespace std;
// first empty out the block table
block_table.reset();
while (block_table.move_next())
{
delete static_cast<config_reader_thread_safe_1*>(block_table.element().value());
}
block_table.clear();
std::vector<std::string> blocks;
cr->get_blocks(blocks);
// now fill the block table up to match what is in cr
for (unsigned long i = 0; i < blocks.size(); ++i)
{
config_reader_thread_safe_1* block = new config_reader_thread_safe_1(&cr->block(blocks[i]),m);
void* temp = block;
block_table.add(blocks[i],temp);
}
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_CONFIG_READER_THREAD_SAFe_
|