/src/pool.h

http://github.com/jwiegley/ledger · C Header · 158 lines · 81 code · 25 blank · 52 comment · 0 complexity · 99ec73da0797b9d0fab996b35622d85f MD5 · raw file

  1. /*
  2. * Copyright (c) 2003-2012, John Wiegley. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of New Artisans LLC nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /**
  32. * @addtogroup math
  33. */
  34. /**
  35. * @file pool.h
  36. * @author John Wiegley
  37. *
  38. * @ingroup math
  39. *
  40. * @brief Types for managing commodity pools
  41. *
  42. * Long.
  43. */
  44. #ifndef _POOL_H
  45. #define _POOL_H
  46. #include "history.h"
  47. #include "annotate.h"
  48. namespace ledger {
  49. struct cost_breakdown_t
  50. {
  51. amount_t amount;
  52. amount_t final_cost;
  53. amount_t basis_cost;
  54. };
  55. class commodity_pool_t : public noncopyable
  56. {
  57. public:
  58. /**
  59. * The commodities collection in commodity_pool_t maintains pointers to all
  60. * the commodities which have ever been created by the user, whether
  61. * explicitly by calling the create methods of commodity_pool_t, or
  62. * implicitly by parsing a commoditized amount.
  63. */
  64. typedef std::map<string, shared_ptr<commodity_t> > commodities_map;
  65. typedef std::map<std::pair<string, annotation_t>,
  66. shared_ptr<annotated_commodity_t> > annotated_commodities_map;
  67. commodities_map commodities;
  68. annotated_commodities_map annotated_commodities;
  69. commodity_history_t commodity_price_history;
  70. commodity_t * null_commodity;
  71. commodity_t * default_commodity;
  72. bool keep_base; // --base
  73. optional<path> price_db; // --price-db=
  74. long quote_leeway; // --leeway=
  75. bool get_quotes; // --download
  76. function<optional<price_point_t>
  77. (commodity_t& commodity, const commodity_t * in_terms_of)>
  78. get_commodity_quote;
  79. static shared_ptr<commodity_pool_t> current_pool;
  80. explicit commodity_pool_t();
  81. virtual ~commodity_pool_t() {
  82. TRACE_DTOR(commodity_pool_t);
  83. }
  84. commodity_t * create(const string& symbol);
  85. commodity_t * find(const string& name);
  86. commodity_t * find_or_create(const string& symbol);
  87. commodity_t * alias(const string& name, commodity_t& referent);
  88. commodity_t * create(const string& symbol,
  89. const annotation_t& details);
  90. commodity_t * find(const string& symbol,
  91. const annotation_t& details);
  92. commodity_t * find_or_create(const string& symbol,
  93. const annotation_t& details);
  94. commodity_t * find_or_create(commodity_t& comm, const annotation_t& details);
  95. annotated_commodity_t * create(commodity_t& comm,
  96. const annotation_t& details);
  97. // Exchange one commodity for another, while recording the factored price.
  98. void exchange(commodity_t& commodity,
  99. const amount_t& per_unit_cost,
  100. const datetime_t& moment);
  101. cost_breakdown_t exchange(const amount_t& amount,
  102. const amount_t& cost,
  103. const bool is_per_unit = false,
  104. const bool add_price = true,
  105. const optional<datetime_t>& moment = none,
  106. const optional<string>& tag = none);
  107. // Parse commodity prices from a textual representation
  108. optional<std::pair<commodity_t *, price_point_t> >
  109. parse_price_directive(char * line, bool do_not_add_price = false);
  110. commodity_t *
  111. parse_price_expression(const std::string& str,
  112. const bool add_prices = true,
  113. const optional<datetime_t>& moment = none);
  114. #if HAVE_BOOST_SERIALIZATION
  115. private:
  116. /** Serialization. */
  117. friend class boost::serialization::access;
  118. template<class Archive>
  119. void serialize(Archive& ar, const unsigned int /* version */) {
  120. ar & current_pool;
  121. ar & commodities;
  122. ar & annotated_commodities;
  123. ar & null_commodity;
  124. ar & default_commodity;
  125. ar & keep_base;
  126. ar & price_db;
  127. ar & quote_leeway;
  128. ar & get_quotes;
  129. }
  130. #endif // HAVE_BOOST_SERIALIZATION
  131. };
  132. } // namespace ledger
  133. #endif // _POOL_H