RCFProto
 All Classes Functions Typedefs
Token.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_TOKEN_HPP
20 #define INCLUDE_RCF_TOKEN_HPP
21 
22 #include <vector>
23 
24 #include <boost/noncopyable.hpp>
25 
26 #include <RCF/Export.hpp>
27 #include <RCF/Config.hpp>
28 #include <RCF/ThreadLibrary.hpp>
29 #include <RCF/TypeTraits.hpp>
30 
31 namespace SF {
32 
33  class Archive;
34 
35 }
36 
37 namespace RCF {
38 
39  class MemOstream;
40 
41  class RCF_EXPORT Token
42  {
43  public:
44  Token();
45  Token(int id);
46  int getId() const;
47  friend bool operator<(const Token &lhs, const Token &rhs);
48  friend bool operator==(const Token &lhs, const Token &rhs);
49  friend bool operator!=(const Token &lhs, const Token &rhs);
50 
51 #if RCF_FEATURE_SF==1
52 
53  void serialize(SF::Archive &ar);
54 
55 #endif
56 
57 #if RCF_FEATURE_BOOST_SERIALIZATION==1
58 
59  template<typename Archive>
60  void serialize(Archive &ar, const unsigned int)
61  {
62  ar & boost::serialization::make_nvp("Id", mId);
63  }
64 
65 #endif
66 
67  friend RCF_EXPORT RCF::MemOstream &operator<<(RCF::MemOstream &os, const Token &token);
68 
69  private:
70  int mId;
71  };
72 
73  class TokenFactory : boost::noncopyable
74  {
75  public:
76  TokenFactory(int tokenCount);
77 
78  bool requestToken(Token &token);
79  void returnToken(const Token &token);
80  const std::vector<Token> & getTokenSpace();
81  std::size_t getAvailableTokenCount();
82  bool isAvailable(const Token & token);
83 
84  private:
85  std::vector<Token> mTokenSpace;
86  std::vector<Token> mAvailableTokens;
87  ReadWriteMutex mMutex;
88  };
89 
90 } // namespace RCF
91 
92 #endif // ! INCLUDE_RCF_TOKEN_HPP