Guest User

Untitled

a guest
Jul 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. u_int32_t CarSize(const Car &car) // (3)
  2. {
  3. return sizeof(length) * 3 + model.length() + 1 +
  4. sizeof(*engine); // (4)
  5. }
  6.  
  7. void CopyCar(void *dest, const Car&car) // (5)
  8. {
  9. // Copy all bytes of the car object into memory chunk M
  10. // referenced by dest.
  11. // M is preallocated and just big enough because our CarSize is used
  12. // to measure the object size. Note the bytes
  13. // of the Engine object E referenced by car.engine should be copied
  14. // into this M, rather than the car.engine pointer value,
  15. // in order to deep copy E.
  16.  
  17. char *p = (char *)dest;
  18.  
  19. memcpy(p, car.length, sizeof(size_t));
  20. p += sizeof(size_t);
  21. memcpy(p, car.width, sizeof(size_t));
  22. p += sizeof(size_t);
  23. memcpy(p, car.height, sizeof(size_t));
  24. p += sizeof(size_t);
  25.  
  26. memcpy(p, car.model.c_str(), car.model.length() + 1); // (6)
  27. p += car.model.length() + 1; // (6)
  28.  
  29. memcpy(p, car.engine, sizeof(*car.engine); // (7)
  30. }
  31.  
  32. void RestoreCar(Car &car, const void* src) // (8)
  33. {
  34. // src references the memory chunk M which contains the bytes of a Car
  35. // object previously marshalled by CopyCar function, so we know
  36. // the data structure and composition of M. Thus here we can
  37. // un-marshal bytes stored in M to assign to each member of car.
  38. // Since we have data of the Engine member in M, we should create an
  39. // Engine object E using 'new' operator, and assign to its members
  40. // using bytes in M, and assign E's pointer to car.engine.
  41.  
  42. char *p = src;
  43.  
  44. memcpy(car.length, p, sizeof(size_t));
  45. p += sizeof(size_t);
  46. memcpy(car.width, p, sizeof(size_t));
  47. p += sizeof(size_t);
  48. memcpy(car.height, p, sizeof(size_t));
  49. p += sizeof(size_t);
  50.  
  51. car.model = p; // (9)
  52. p += car.model.length() + 1; // (9)
  53.  
  54. memcpy(car.engine, p, sizeof(*car.engine);
  55.  
  56. }
  57.  
  58. dbstl::DbstlElemTraits<Car> *pCarTraits =
  59. dbstl::DbstlElemTraits<Car>::instance(); // (10)
  60. pCarTraits->set_size_function(CarSize); // (11)
  61. pCarTraits->set_copy_function(CopyCar); // (12)
  62. pCarTraits->set_restore_function(RestoreCar); // (13)
Add Comment
Please, Sign In to add comment