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 <iosfwd>
23 #include <vector>
24 
25 #include <boost/noncopyable.hpp>
26 
27 #include <RCF/Export.hpp>
28 #include <RCF/Config.hpp>
29 #include <RCF/ThreadLibrary.hpp>
30 #include <RCF/TypeTraits.hpp>
31 
32 namespace SF {
33 
34  class Archive;
35 
36 }
37 
38 namespace RCF {
39 
40  class RCF_EXPORT Token
41  {
42  public:
43  Token();
44  Token(int id);
45  int getId() const;
46  friend bool operator<(const Token &lhs, const Token &rhs);
47  friend bool operator==(const Token &lhs, const Token &rhs);
48  friend bool operator!=(const Token &lhs, const Token &rhs);
49 
50 #ifdef RCF_USE_SF_SERIALIZATION
51 
52  void serialize(SF::Archive &ar);
53 
54 #endif
55 
56 #ifdef RCF_USE_BOOST_SERIALIZATION
57 
58  template<typename Archive>
59  void serialize(Archive &ar, const unsigned int)
60  {
61  ar & boost::serialization::make_nvp("Id", mId);
62  }
63 
64 #endif
65 
66  friend RCF_EXPORT std::ostream &operator<<(std::ostream &os, const Token &token);
67 
68  private:
69  int mId;
70  };
71 
72  class TokenFactory : boost::noncopyable
73  {
74  public:
75  TokenFactory(int tokenCount);
76 
77  bool requestToken(Token &token);
78  void returnToken(const Token &token);
79  const std::vector<Token> & getTokenSpace();
80  std::size_t getAvailableTokenCount();
81  bool isAvailable(const Token & token);
82 
83  private:
84  std::vector<Token> mTokenSpace;
85  std::vector<Token> mAvailableTokens;
86  ReadWriteMutex mMutex;
87  };
88 
89 } // namespace RCF
90 
91 #endif // ! INCLUDE_RCF_TOKEN_HPP