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.

161 lines
4.0 KiB

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