A C++ wrapper for the basic C datatypes defined by the pEpEngine.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.2 KiB

  1. #include "types.hh"
  2. #include <pEp/status_to_string.hh> // from libpEpAdapter
  3. #include <pEp/mime.h>
  4. /*
  5. #include <pEp/pEpEngine.h>
  6. #include <pEp/stringpair.h>
  7. #include <pEp/message.h>
  8. */
  9. #include <string>
  10. namespace pEp
  11. {
  12. EngineError::EngineError(PEP_STATUS status, const char* message)
  13. : std::runtime_error(
  14. std::string{"EngineError: "}
  15. + (message ? '"' + std::string{message} + "\" " : std::string{} )
  16. + status_to_string(status)
  17. )
  18. {}
  19. ////////////////
  20. template<>
  21. template<>
  22. message* Wrapper<::message*>::_new<PEP_msg_direction, const char*>(PEP_msg_direction dir, const char* src)
  23. {
  24. message* m = nullptr;
  25. bool pep_msg = false;
  26. PEP_STATUS status = mime_decode_message(src, strlen(src), &m, &pep_msg);
  27. if(status != PEP_STATUS_OK)
  28. {
  29. throw EngineError(status, "mime_decode_message()");
  30. }
  31. m->dir = dir;
  32. return m;
  33. }
  34. template<>
  35. void Wrapper<::message*>::_free(::message* m)
  36. {
  37. free_message(m);
  38. }
  39. ////////////////
  40. template<>
  41. template<>
  42. ::stringpair_t* Wrapper<::stringpair_t*>::_new(const char* key, const char* value)
  43. {
  44. stringpair_t* sp = new_stringpair(key, value);
  45. if(!sp)
  46. {
  47. throw EngineError(PEP_OUT_OF_MEMORY, "new_stringpair()");
  48. }
  49. return sp;
  50. }
  51. template<>
  52. template<>
  53. ::stringpair_t* Wrapper<::stringpair_t*>::_new(char* key, char* value)
  54. {
  55. return _new<const char*, const char*>(
  56. const_cast<const char*>(key),
  57. const_cast<const char*>(value)
  58. );
  59. }
  60. template<>
  61. template<>
  62. ::stringpair_t* Wrapper<::stringpair_t*>::_new(const std::string& key, const std::string& value)
  63. {
  64. return Wrapper<::stringpair_t*>::_new(key.c_str(), value.c_str());
  65. }
  66. template<>
  67. void Wrapper<::stringpair_t*>::_free(::stringpair_t* sp)
  68. {
  69. free_stringpair(sp);
  70. }
  71. template<>
  72. void Wrapper<::stringpair_list_t*>::_free(::stringpair_list_t* spl)
  73. {
  74. free_stringpair_list(spl);
  75. }
  76. template<>
  77. stringpair_t* stringpair_list_t::* const ListWrapper<stringpair_list_t*, stringpair_t*>::Value = &stringpair_list_t::value;
  78. template<>
  79. int StringPairList::size() const
  80. {
  81. return stringpair_list_length(value);
  82. }
  83. // faster than .size()==0 because it's not necessary to iterate throgh the whole list
  84. template<>
  85. bool StringPairList::empty() const
  86. {
  87. return !(value && value->value);
  88. }
  89. template<>
  90. void StringPairList::erase( const StringPairList::iterator& it)
  91. {
  92. if(it.value && it.value->value && it.value->value->key)
  93. {
  94. value = stringpair_list_delete_by_key(value, it.value->value->key);
  95. }
  96. }
  97. template<>
  98. void StringPairList::clear()
  99. {
  100. free_stringpair_list(value);
  101. value = nullptr;
  102. }
  103. template<>
  104. void StringPairList::push_back(::stringpair_t*&& sp)
  105. {
  106. auto last = stringpair_list_add(value, sp);
  107. if(value==nullptr)
  108. value = last;
  109. sp = nullptr;
  110. }
  111. template<>
  112. void StringPairList::push_back(StringPair&& sp)
  113. {
  114. auto last = stringpair_list_add(value, sp.move_out());
  115. if(value==nullptr)
  116. value = last;
  117. }
  118. template<>
  119. ListWrapper<::stringpair_list_t*, stringpair_t*>::ListWrapper(const std::initializer_list<StringPair>& il)
  120. : StringPairList{}
  121. {
  122. ::stringpair_list_t* last = nullptr;
  123. for(const StringPair& sp : il)
  124. {
  125. last = stringpair_list_add(last, stringpair_dup(sp.get()));
  126. if(last==nullptr)
  127. {
  128. throw std::runtime_error("Cannot create StringPairList from {}: Out Of Memory.");
  129. }
  130. if(value==nullptr)
  131. value = last; // save the head of linked list.
  132. }
  133. }
  134. ////////////////
  135. template class Wrapper<::pEp_identity*>;
  136. template class Wrapper<::stringpair_t*>;
  137. template class Wrapper<::message*>;
  138. template class ListWrapper<::stringpair_list_t*, ::stringpair_t*>;
  139. } // end of namespace pEp