File: TargetRepository.cpp

package info (click to toggle)
subcommander 2.0.0~b5p2-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 15,344 kB
  • sloc: cpp: 63,594; sh: 4,050; xml: 1,992; makefile: 1,134; ansic: 786; ruby: 251; lisp: 24
file content (95 lines) | stat: -rw-r--r-- 1,787 bytes parent folder | download | duplicates (4)
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
/* ====================================================================
 * Copyright (c) 2006, 2008  Martin Hauner
 *                           http://subcommander.tigris.org
 *
 * Subcommander is licensed as described in the file doc/COPYING, which
 * you should have received as part of this distribution.
 * ====================================================================
 */

// sc
#include "TargetRepository.h"
#include "util/Mutex.h"
#include "util/Guard.h"

// sys
#include <map>
#include <cassert>


typedef std::map<unsigned long,QObject*>    TargetMap;
typedef std::pair<TargetMap::iterator,bool> ResPair;

class TargetRepositoryData
{
public:
  sc::Mutex targetMutex;
  TargetMap targetMap;
  ID        error;
};

static TargetRepositoryData* data = NULL;

void TargetRepository::setup()
{
  data = new TargetRepositoryData();
  data->error = 0;
}

void TargetRepository::teardown()
{
  delete data;
  data = NULL;
}

ID TargetRepository::create()
{
  return Id::next();
}

void TargetRepository::add( ID tid, QObject* o )
{
  sc::Guard<sc::Mutex> guard(data->targetMutex);

   ResPair p = data->targetMap.insert( TargetMap::value_type(tid,o) );

   if( ! p.second )
   {
     assert(false);
   }
}

QObject* TargetRepository::get( ID tid )
{
  sc::Guard<sc::Mutex> guard(data->targetMutex);

  TargetMap::iterator it = data->targetMap.find(tid);
  if( it != data->targetMap.end() )
  {
    return (*it).second;
  }
  else
  {
    return NULL;
  }
}

void TargetRepository::del( ID tid )
{
  sc::Guard<sc::Mutex> guard(data->targetMutex);

  data->targetMap.erase(tid);
}


void TargetRepository::setError( ID errortid )
{
  sc::Guard<sc::Mutex> guard(data->targetMutex);

  data->error = errortid;
}

QObject* TargetRepository::getError()
{
  return get(data->error);
}