client-cpp  0.10.0
AbstractTransactable.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2016 CyberVision, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ABSTRACTTRANSACTABLE_HPP_
18 #define ABSTRACTTRANSACTABLE_HPP_
19 
20 #include <map>
21 #include "kaa/KaaThread.hpp"
22 #include "kaa/logging/Log.hpp"
24 
25 namespace kaa {
26 
27 template<class Container>
29 public:
32  KAA_MUTEX_LOCKING("transactionsGuard_")
33  KAA_MUTEX_UNIQUE_DECLARE(transactionsLock, transactionsGuard_);
34  KAA_MUTEX_LOCKED("transactionsGuard_")
35 
36  transactions_.insert(std::make_pair(trxId, Container()));
37 
38  return trxId;
39  }
40 
41  virtual void rollback(TransactionIdPtr trxId, IKaaClientContext &context_) {
42  KAA_MUTEX_LOCKING("transactionsGuard_")
43  KAA_MUTEX_UNIQUE_DECLARE(transactionsLock, transactionsGuard_);
44  KAA_MUTEX_LOCKED("transactionsGuard_")
45 
46  auto it = transactions_.find(trxId);
47  if (it != transactions_.end()) {
48  transactions_.erase(it);
49  }
50  }
51 
52  Container & getContainerByTrxId(TransactionIdPtr trxId, IKaaClientContext &context_) {
53  KAA_MUTEX_LOCKING("transactionsGuard_")
54  KAA_MUTEX_UNIQUE_DECLARE(transactionsLock, transactionsGuard_);
55  KAA_MUTEX_LOCKED("transactionsGuard_")
56 
57  auto it = transactions_.find(trxId);
58 
59  if (it != transactions_.end()) {
60  return it->second;
61  } else {
62  KAA_LOG_DEBUG(boost::format("Transaction with id %1% was not found. Creating new instance") % trxId->getId());
63  return transactions_[trxId];
64  }
65  }
66 
67  virtual ~AbstractTransactable() {}
68 protected:
69  std::map<TransactionIdPtr, Container> transactions_;
70  KAA_MUTEX_DECLARE(transactionsGuard_);
71 };
72 
73 }
74 
75 #endif /* ABSTRACTTRANSACTABLE_HPP_ */
#define KAA_LOG_DEBUG(message)
Definition: Log.hpp:59
Container & getContainerByTrxId(TransactionIdPtr trxId, IKaaClientContext &context_)
std::shared_ptr< TransactionId > TransactionIdPtr
#define KAA_MUTEX_LOCKED(mutex_name)
Definition: Log.hpp:91
virtual TransactionIdPtr beginTransaction(IKaaClientContext &context_)
#define KAA_MUTEX_UNIQUE_DECLARE(name, mtx)
Definition: KaaThread.hpp:48
std::map< TransactionIdPtr, Container > transactions_
virtual void rollback(TransactionIdPtr trxId, IKaaClientContext &context_)
KAA_MUTEX_DECLARE(transactionsGuard_)
#define KAA_MUTEX_LOCKING(mutex_name)
Definition: Log.hpp:90