SessionID.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* -*- C++ -*- */
  2. /****************************************************************************
  3. ** Copyright (c) 2001-2014
  4. **
  5. ** This file is part of the QuickFIX FIX Engine
  6. **
  7. ** This file may be distributed under the terms of the quickfixengine.org
  8. ** license as defined by quickfixengine.org and appearing in the file
  9. ** LICENSE included in the packaging of this file.
  10. **
  11. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  12. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. **
  14. ** See http://www.quickfixengine.org/LICENSE for licensing information.
  15. **
  16. ** Contact ask@quickfixengine.org if any conditions of this licensing are
  17. ** not clear to you.
  18. **
  19. ****************************************************************************/
  20. #ifndef FIX_SESSIONID_H
  21. #define FIX_SESSIONID_H
  22. #include "Fields.h"
  23. namespace FIX
  24. {
  25. /// Unique session id consists of BeginString, SenderCompID and TargetCompID.
  26. class SessionID
  27. {
  28. public:
  29. SessionID()
  30. {
  31. toString(m_frozenString);
  32. }
  33. SessionID( const std::string& beginString,
  34. const std::string& senderCompID,
  35. const std::string& targetCompID,
  36. const std::string& sessionQualifier = "" )
  37. : m_beginString( BeginString(beginString) ),
  38. m_senderCompID( SenderCompID(senderCompID) ),
  39. m_targetCompID( TargetCompID(targetCompID) ),
  40. m_sessionQualifier( sessionQualifier ),
  41. m_isFIXT(false)
  42. {
  43. toString(m_frozenString);
  44. if( beginString.substr(0, 4) == "FIXT" )
  45. m_isFIXT = true;
  46. }
  47. const BeginString& getBeginString() const
  48. { return m_beginString; }
  49. const SenderCompID& getSenderCompID() const
  50. { return m_senderCompID; }
  51. const TargetCompID& getTargetCompID() const
  52. { return m_targetCompID; }
  53. const std::string& getSessionQualifier() const
  54. { return m_sessionQualifier; }
  55. const bool isFIXT() const
  56. { return m_isFIXT; }
  57. /// Get a string representation of the SessionID
  58. std::string toString() const
  59. {
  60. return m_frozenString;
  61. }
  62. // Return a reference for a high-performance scenario
  63. const std::string& toStringFrozen() const
  64. {
  65. return m_frozenString;
  66. }
  67. /// Build from string representation of SessionID
  68. void fromString( const std::string& str )
  69. {
  70. std::string::size_type first =
  71. str.find_first_of(':');
  72. std::string::size_type second =
  73. str.find("->");
  74. std::string::size_type third =
  75. str.find_last_of(':');
  76. if( first == std::string::npos )
  77. return;
  78. if( second == std::string::npos )
  79. return;
  80. m_beginString = str.substr(0, first);
  81. m_senderCompID = str.substr(first+1, second - first - 1);
  82. if( first == third )
  83. {
  84. m_targetCompID = str.substr(second+2);
  85. m_sessionQualifier = "";
  86. }
  87. else
  88. {
  89. m_targetCompID = str.substr(second+2, third - second - 2);
  90. m_sessionQualifier = str.substr(third+1);
  91. }
  92. toString(m_frozenString);
  93. }
  94. /// Get a string representation without making a copy
  95. std::string& toString( std::string& str ) const
  96. {
  97. str = getBeginString().getValue() + ":" +
  98. getSenderCompID().getValue() + "->" +
  99. getTargetCompID().getValue();
  100. if( m_sessionQualifier.size() )
  101. str += ":" + m_sessionQualifier;
  102. return str;
  103. }
  104. friend bool operator<( const SessionID&, const SessionID& );
  105. friend bool operator==( const SessionID&, const SessionID& );
  106. friend bool operator!=( const SessionID&, const SessionID& );
  107. friend std::ostream& operator<<( std::ostream&, const SessionID& );
  108. friend std::ostream& operator>>( std::ostream&, const SessionID& );
  109. SessionID operator~() const
  110. {
  111. return SessionID( m_beginString, SenderCompID( m_targetCompID ),
  112. TargetCompID( m_senderCompID ), m_sessionQualifier );
  113. }
  114. private:
  115. BeginString m_beginString;
  116. SenderCompID m_senderCompID;
  117. TargetCompID m_targetCompID;
  118. std::string m_sessionQualifier;
  119. bool m_isFIXT;
  120. std::string m_frozenString;
  121. };
  122. /*! @} */
  123. inline bool operator<( const SessionID& lhs, const SessionID& rhs )
  124. {
  125. return lhs.toStringFrozen() < rhs.toStringFrozen();
  126. }
  127. inline bool operator==( const SessionID& lhs, const SessionID& rhs )
  128. {
  129. return lhs.toStringFrozen() == rhs.toStringFrozen();
  130. }
  131. inline bool operator!=( const SessionID& lhs, const SessionID& rhs )
  132. {
  133. return !( lhs == rhs );
  134. }
  135. inline std::ostream& operator<<
  136. ( std::ostream& stream, const SessionID& sessionID )
  137. {
  138. stream << sessionID.toStringFrozen();
  139. return stream;
  140. }
  141. inline std::istream& operator>>
  142. ( std::istream& stream, SessionID& sessionID )
  143. {
  144. std::string str;
  145. stream >> str;
  146. sessionID.fromString( str );
  147. return stream;
  148. }
  149. }
  150. #endif //FIX_SESSIONID_H