libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
isin.hpp
Go to the documentation of this file.
1 #ifndef ISIMUD_EXCHANGES_COMMON_ISIN_HPP
2 #define ISIMUD_EXCHANGES_COMMON_ISIN_HPP
3 
4 /******************************************************************************
5 ** Copyright © 2016 by J.M.McGuiness, isimud@hussar.me.uk
6 **
7 ** This library is free software; you can redistribute it and/or
8 ** modify it under the terms of the GNU Lesser General Public
9 ** License as published by the Free Software Foundation; either
10 ** version 2.1 of the License, or (at your option) any later version.
11 **
12 ** This library is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ** Lesser General Public License for more details.
16 **
17 ** You should have received a copy of the GNU Lesser General Public
18 ** License along with this library; if not, write to the Free Software
19 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 #include "iso_3166_country_codes.hpp"
23 
24 #include "core/int128_compatibility.hpp"
25 #include "core/memops.hpp"
26 #include "core/ttypes.hpp"
27 
28 #include <boost/mpl/assert.hpp>
29 
30 #include <iostream>
31 #include <string>
32 
33 namespace isimud { namespace ISIMUD_VER_NAMESPACE { namespace exchanges { namespace common {
34 
35 /// The <a href="http://www.isin.org/isin/">ISIN code</a> for an instrument.
36 class ISIN_t {
37 public:
38  using Country_Code_t=exchanges::common::ctry_codes::alpha_2::country_t; ///< The <a href="https://www.iso.org/obp/ui/#search">ISO 3166 Country-Code</a>, Alpha-2 format.
39  BOOST_MPL_ASSERT_RELATION(sizeof(Country_Code_t), ==, 2);
40  using NSIN_t=std::array<char, 9>; ///< Must be printable characters.
41  using CUSIP_t=NSIN_t; ///< When appropriate this may be the CUSIP.
42  BOOST_MPL_ASSERT_RELATION(sizeof(CUSIP_t), ==, 9);
43  /// When appropriate the last 7 characters may be the SEDOL.
44  union [[gnu::packed]] SEDOL_t {
45  NSIN_t nsin;
46  struct [[gnu::packed]] padded_SEDOL {
48 
49  const NSIN_t::value_type prefix[sizeof(NSIN_t)-sizeof(element_type)]={'0', '0'};
51  } padded_sedol;
52  };
53  BOOST_MPL_ASSERT_RELATION(sizeof(SEDOL_t), ==, 9);
54 
55 private:
56  /// Get at the internal components.
57  struct [[gnu::packed]] details_t {
58  Country_Code_t country;
59  union [[gnu::packed]] {
60  NSIN_t nsin;
61  CUSIP_t cusip;
62  SEDOL_t sedol;
63  };
64  char check_digit; ///< The check digit.
65  };
66  BOOST_MPL_ASSERT_RELATION(sizeof(details_t), ==, 12);
67 
68 public:
69  using block_t=std::array<char, sizeof(details_t)>;
70  BOOST_MPL_ASSERT_RELATION(sizeof(block_t), ==, 12);
71 
72  constexpr ISIN_t() noexcept(true);
73  explicit constexpr ISIN_t(block_t const &b) noexcept(true);
74  template<std::size_t Sz>
75  explicit constexpr ISIN_t(block_t::value_type const (&b)[Sz]) noexcept(true);
76  constexpr ISIN_t(ISIN_t const &i) noexcept(true);
77  constexpr ISIN_t &operator=(ISIN_t const &i) noexcept(true);
78 
79  bool operator==(ISIN_t const &i) const noexcept(true);
80  bool operator<(ISIN_t const &i) const noexcept(true);
81 
82  constexpr block_t const &data() const noexcept(true) {
83  return value.block;
84  }
85 
86  std::string to_string() const noexcept(false);
87 
88  constexpr std::size_t hash() const noexcept(true);
89 
90  friend std::ostream &operator<<(std::ostream &os, ISIN_t const &i) noexcept(false);
91  friend std::istream &operator>>(std::istream &is, ISIN_t &i) noexcept(false);
92 
93 private:
94  union converter_t {
95  block_t block; ///< Deal with the ISIN as a flat data-block.
96  details_t components;
97 
98  constexpr converter_t() noexcept(true)
99  : block{} {}
100  } value{};
101  BOOST_MPL_ASSERT_RELATION(sizeof(converter_t), ==, 12);
102 };
103 
104 BOOST_MPL_ASSERT_RELATION(sizeof(ISIN_t), ==, 12);
105 
106 } } } }
107 
108 #include "isin_impl.hpp"
109 
110 #endif