example.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "example.h"
  2. #include <library/cpp/codecs/static/static.h>
  3. #include <util/generic/yexception.h>
  4. extern "C" {
  5. extern const ui8 codec_info_huff_20160707[];
  6. extern const ui32 codec_info_huff_20160707Size;
  7. extern const ui8 codec_info_sa_huff_20160707[];
  8. extern const ui32 codec_info_sa_huff_20160707Size;
  9. };
  10. namespace NStaticCodecExample {
  11. static const NCodecs::TCodecConstPtr CODECS[] = {
  12. nullptr,
  13. NCodecs::RestoreCodecFromArchive(codec_info_huff_20160707, codec_info_huff_20160707Size),
  14. NCodecs::RestoreCodecFromArchive(codec_info_sa_huff_20160707, codec_info_sa_huff_20160707Size),
  15. };
  16. static_assert(Y_ARRAY_SIZE(CODECS) == DV_COUNT, "bad array size");
  17. void Encode(TBuffer& out, TStringBuf in, EDictVersion dv) {
  18. Y_ENSURE(dv > DV_NULL && dv < DV_COUNT, "invalid dict version: " << (int)dv);
  19. out.Clear();
  20. if (!in) {
  21. return;
  22. }
  23. CODECS[dv]->Encode(in, out);
  24. out.Append((char)dv);
  25. }
  26. void Decode(TBuffer& out, TStringBuf in) {
  27. out.Clear();
  28. if (!in) {
  29. return;
  30. }
  31. EDictVersion dv = (EDictVersion)in.back();
  32. Y_ENSURE(dv > DV_NULL && dv < DV_COUNT, "invalid dict version: " << (int)dv);
  33. in.Chop(1);
  34. CODECS[dv]->Decode(in, out);
  35. }
  36. }