ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
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.
 
 
 
 
 
 

76 lines
1.7 KiB

  1. #ifndef ARCHIVE_H
  2. #define ARCHIVE_H
  3. #include "cppcms_error.h"
  4. #include <string>
  5. #include <cstring>
  6. namespace cppcms {
  7. using namespace std;
  8. class archive {
  9. string data;
  10. size_t ptr;
  11. public:
  12. archive() { ptr=0; };
  13. archive(string const &s) : data(s) { ptr=0; };
  14. void set(string const &s) { data=s; ptr=0; };
  15. void set(char const *ptr,size_t len) { data.assign(ptr,len); };
  16. string const &get() const { return data; };
  17. template<typename T>
  18. archive &operator<<(T const &val) {
  19. size_t size=sizeof(T);
  20. data.append((char const *)&size,sizeof(size_t));
  21. data.append((char const *)&val,size);
  22. return *this;
  23. }
  24. archive &operator<<(string const &val) {
  25. size_t size=val.size();
  26. data.append((char const *)&size,sizeof(size_t));
  27. data.append(val.c_str(),size);
  28. return *this;
  29. }
  30. template<typename T>
  31. archive &operator>>(T &val)
  32. {
  33. if(ptr+sizeof(size_t)+sizeof(T)>data.size()) {
  34. throw cppcms_error("Format violation");
  35. }
  36. char const *start=data.c_str()+ptr;
  37. if(*(size_t const *)start!=sizeof(T)) {
  38. throw cppcms_error("Invalid size read");
  39. }
  40. start+=sizeof(size_t);
  41. memcpy(&val,start,sizeof(T));
  42. ptr+=sizeof(size_t)+sizeof(T);
  43. return *this;
  44. }
  45. archive &operator>>(string &val)
  46. {
  47. if(ptr+sizeof(size_t)>data.size()) {
  48. throw cppcms_error("Format violation");
  49. }
  50. char const *start=data.c_str()+ptr;
  51. size_t s=*(size_t const *)start;
  52. if(ptr+sizeof(size_t)+s>data.size()) {
  53. throw cppcms_error("String too long");
  54. }
  55. start+=sizeof(size_t);
  56. val=string(start,s);
  57. ptr+=sizeof(size_t)+s;
  58. return *this;
  59. }
  60. };
  61. class serializable {
  62. public:
  63. virtual void load(archive &a) = 0;
  64. virtual void save(archive &a) const = 0;
  65. virtual ~serializable() {};
  66. };
  67. }
  68. #endif