Proxy.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (c) 2013-2016 Josh Blum
  2. // SPDX-License-Identifier: BSL-1.0
  3. #include <Pothos/Proxy/Proxy.hpp>
  4. #include <Pothos/Proxy/Handle.hpp>
  5. #include <Pothos/Proxy/Environment.hpp>
  6. #include <Pothos/Proxy/Exception.hpp>
  7. #include <cassert>
  8. Pothos::Proxy::Proxy(void)
  9. {
  10. assert(not *this);
  11. }
  12. Pothos::Proxy::Proxy(const std::shared_ptr<ProxyHandle> &handle):
  13. _handle(handle)
  14. {
  15. return;
  16. }
  17. Pothos::Proxy::Proxy(ProxyHandle *handle):
  18. _handle(handle)
  19. {
  20. return;
  21. }
  22. std::shared_ptr<Pothos::ProxyHandle> Pothos::Proxy::getHandle(void) const
  23. {
  24. return _handle;
  25. }
  26. std::shared_ptr<Pothos::ProxyEnvironment> Pothos::Proxy::getEnvironment(void) const
  27. {
  28. assert(_handle);
  29. return _handle->getEnvironment();
  30. }
  31. Pothos::Proxy::operator bool(void) const
  32. {
  33. return bool(_handle);
  34. }
  35. Pothos::Proxy Pothos::Proxy::get(const std::string &name) const
  36. {
  37. return this->call("get:"+name);
  38. }
  39. int Pothos::Proxy::compareTo(const Proxy &other) const
  40. {
  41. assert(_handle);
  42. return _handle->compareTo(other);
  43. }
  44. size_t Pothos::Proxy::hashCode(void) const
  45. {
  46. assert(_handle);
  47. return _handle->hashCode();
  48. }
  49. Pothos::Object Pothos::Proxy::toObject(void) const
  50. {
  51. assert(_handle);
  52. return _handle->getEnvironment()->convertProxyToObject(*this);
  53. }
  54. std::string Pothos::Proxy::toString(void) const
  55. {
  56. assert(_handle);
  57. return _handle->toString();
  58. }
  59. std::string Pothos::Proxy::getClassName(void) const
  60. {
  61. assert(_handle);
  62. return _handle->getClassName();
  63. }
  64. bool Pothos::Proxy::operator<(const Pothos::Proxy &obj) const
  65. {
  66. try
  67. {
  68. return this->compareTo(obj) < 0;
  69. }
  70. catch (const Pothos::ProxyCompareError &)
  71. {
  72. return this->hashCode() < obj.hashCode();
  73. }
  74. }
  75. bool Pothos::Proxy::operator>(const Pothos::Proxy &obj) const
  76. {
  77. try
  78. {
  79. return this->compareTo(obj) > 0;
  80. }
  81. catch (const Pothos::ProxyCompareError &)
  82. {
  83. return this->hashCode() > obj.hashCode();
  84. }
  85. }
  86. bool Pothos::operator==(const Proxy &lhs, const Proxy &rhs)
  87. {
  88. return lhs.getHandle() == rhs.getHandle();
  89. }
  90. #include <Pothos/Object/Serialize.hpp>
  91. #include <Pothos/Proxy.hpp>
  92. #include <Poco/Types.h>
  93. #include <sstream>
  94. #include <vector>
  95. namespace Pothos { namespace serialization {
  96. template<class Archive>
  97. void save(Archive & ar, const Pothos::Proxy &t, const unsigned int)
  98. {
  99. auto name = t.getEnvironment()->getName();
  100. ar << name;
  101. //serialize to stringstream
  102. std::stringstream ss;
  103. t.getEnvironment()->serialize(t, ss);
  104. const auto buff = ss.str();
  105. //save length and buffer
  106. const Poco::UInt32 length = Poco::UInt32(buff.size());
  107. ar << length;
  108. Pothos::serialization::BinaryObject bo(buff.data(), buff.size());
  109. ar << bo;
  110. }
  111. template<class Archive>
  112. void load(Archive & ar, Pothos::Proxy &t, const unsigned int)
  113. {
  114. std::string name;
  115. ar >> name;
  116. auto env = Pothos::ProxyEnvironment::make(name);
  117. //extract length and buffer
  118. Poco::UInt32 length = 0;
  119. ar >> length;
  120. auto buff = std::vector<char>(size_t(length));
  121. Pothos::serialization::BinaryObject bo(buff.data(), buff.size());
  122. ar >> bo;
  123. //deserialize from stringstream
  124. std::stringstream ss;
  125. ss.write((const char *)buff.data(), buff.size());
  126. t = env->deserialize(ss);
  127. }
  128. }}
  129. POTHOS_SERIALIZATION_SPLIT_FREE(Pothos::Proxy)
  130. POTHOS_OBJECT_SERIALIZE(Pothos::Proxy)
  131. POTHOS_OBJECT_SERIALIZE(Pothos::ProxyVector)
  132. POTHOS_OBJECT_SERIALIZE(Pothos::ProxySet)
  133. POTHOS_OBJECT_SERIALIZE(Pothos::ProxyMap)