Audacity 3.2.0
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
ProjectSerializer Class Referencefinal

a class used to (de)serialize the project catalog More...

#include <ProjectSerializer.h>

Inheritance diagram for ProjectSerializer:
[legend]
Collaboration diagram for ProjectSerializer:
[legend]

Public Member Functions

 ProjectSerializer (size_t allocSize=1024 *1024)
 
virtual ~ProjectSerializer ()
 
void StartTag (const wxString &name) override
 
void EndTag (const wxString &name) override
 
void WriteAttr (const wxString &name, const wxString &value) override
 
void WriteAttr (const wxString &name, const wxChar *value) override
 
void WriteAttr (const wxString &name, int value) override
 
void WriteAttr (const wxString &name, bool value) override
 
void WriteAttr (const wxString &name, long value) override
 
void WriteAttr (const wxString &name, long long value) override
 
void WriteAttr (const wxString &name, size_t value) override
 
void WriteAttr (const wxString &name, float value, int digits=-1) override
 
void WriteAttr (const wxString &name, double value, int digits=-1) override
 
void WriteData (const wxString &value) override
 
void Write (const wxString &data) override
 
const MemoryStreamGetDict () const
 
const MemoryStreamGetData () const
 
bool IsEmpty () const
 
bool DictChanged () const
 
- Public Member Functions inherited from XMLWriter
 XMLWriter ()
 
virtual ~XMLWriter ()
 
virtual void StartTag (const wxString &name)
 
virtual void EndTag (const wxString &name)
 
void WriteAttr (const wxString &name, const Identifier &value)
 
virtual void WriteAttr (const wxString &name, const wxString &value)
 
virtual void WriteAttr (const wxString &name, const wxChar *value)
 
virtual void WriteAttr (const wxString &name, int value)
 
virtual void WriteAttr (const wxString &name, bool value)
 
virtual void WriteAttr (const wxString &name, long value)
 
virtual void WriteAttr (const wxString &name, long long value)
 
virtual void WriteAttr (const wxString &name, size_t value)
 
virtual void WriteAttr (const wxString &name, float value, int digits=-1)
 
virtual void WriteAttr (const wxString &name, double value, int digits=-1)
 
virtual void WriteData (const wxString &value)
 
virtual void WriteSubTree (const wxString &value)
 
virtual void Write (const wxString &data)=0
 

Static Public Member Functions

static TranslatableString FailureMessage (const FilePath &filePath)
 
static bool Decode (BufferedStreamReader &in, XMLTagHandler *handler)
 

Private Member Functions

void WriteName (const wxString &name)
 

Private Attributes

MemoryStream mBuffer
 
bool mDictChanged
 

Static Private Attributes

static NameMap mNames
 
static MemoryStream mDict
 

Additional Inherited Members

- Protected Attributes inherited from XMLWriter
bool mInTag
 
int mDepth
 
wxArrayString mTagstack
 
std::vector< int > mHasKids
 

Detailed Description

a class used to (de)serialize the project catalog

Definition at line 36 of file ProjectSerializer.h.

Constructor & Destructor Documentation

◆ ProjectSerializer()

ProjectSerializer::ProjectSerializer ( size_t  allocSize = 1024 * 1024)

Definition at line 357 of file ProjectSerializer.cpp.

358{
359 static std::once_flag flag;
360 std::call_once(flag, []{
361 // Just once per run, store header information in the unique static
362 // dictionary that will be written into each project that is saved.
363 // Store the size of "wxStringCharType" so we can convert during recovery
364 // in case the file is used on a system with a different character size.
365 char size = sizeof(wxStringCharType);
367 mDict.AppendData(&size, 1);
368 });
369
370 mDictChanged = false;
371}
@ FT_CharSize
static std::once_flag flag
void AppendData(const void *data, const size_t length)
static MemoryStream mDict

References MemoryStream::AppendByte(), MemoryStream::AppendData(), flag, FT_CharSize, mDict, mDictChanged, and size.

Here is the call graph for this function:

◆ ~ProjectSerializer()

ProjectSerializer::~ProjectSerializer ( )
virtual

Definition at line 373 of file ProjectSerializer.cpp.

374{
375}

Member Function Documentation

◆ Decode()

bool ProjectSerializer::Decode ( BufferedStreamReader in,
XMLTagHandler handler 
)
static

Definition at line 530 of file ProjectSerializer.cpp.

531{
532 if (handler == nullptr)
533 return false;
534
535 XMLTagHandlerAdapter adapter(handler);
536
537 std::vector<char> bytes;
538 IdMap mIds;
539 std::vector<IdMap> mIdStack;
540 char mCharSize = 0;
541
542 mIds.clear();
543
544 struct Error{}; // exception type for short-range try/catch
545 auto Lookup = [&mIds]( UShort id ) -> std::string_view
546 {
547 auto iter = mIds.find( id );
548 if (iter == mIds.end())
549 {
550 throw Error{};
551 }
552
553 return iter->second;
554 };
555
556 int64_t stringsCount = 0;
557 int64_t stringsLength = 0;
558
559 auto ReadString = [&mCharSize, &in, &bytes, &stringsCount, &stringsLength](int len) -> std::string
560 {
561 bytes.reserve( len );
562 auto data = bytes.data();
563 in.Read( data, len );
564
565 stringsCount++;
566 stringsLength += len;
567
568 switch (mCharSize)
569 {
570 case 1:
571 return std::string(bytes.data(), len);
572
573 case 2:
574 return FastStringConvert<char16_t>(bytes.data(), len);
575
576 case 4:
577 return FastStringConvert<char32_t>(bytes.data(), len);
578
579 default:
580 wxASSERT_MSG(false, wxT("Characters size not 1, 2, or 4"));
581 break;
582 }
583
584 return {};
585 };
586
587 try
588 {
589 while (!in.Eof())
590 {
591 UShort id;
592
593 switch (in.GetC())
594 {
595 case FT_Push:
596 {
597 mIdStack.push_back(mIds);
598 mIds.clear();
599 }
600 break;
601
602 case FT_Pop:
603 {
604 mIds = mIdStack.back();
605 mIdStack.pop_back();
606 }
607 break;
608
609 case FT_Name:
610 {
611 id = ReadUShort( in );
612 auto len = ReadUShort( in );
613 mIds[id] = ReadString(len);
614 }
615 break;
616
617 case FT_StartTag:
618 {
619 id = ReadUShort( in );
620
621 adapter.EmitStartTag(Lookup(id));
622 }
623 break;
624
625 case FT_EndTag:
626 {
627 id = ReadUShort( in );
628
629 adapter.EndTag(Lookup(id));
630 }
631 break;
632
633 case FT_String:
634 {
635 id = ReadUShort( in );
636 int len = ReadLength( in );
637
638 adapter.WriteAttr(Lookup(id), ReadString(len));
639 }
640 break;
641
642 case FT_Float:
643 {
644 float val;
645
646 id = ReadUShort( in );
647 in.Read(&val, sizeof(val));
648 /* int dig = */ReadDigits(in);
649
650 adapter.WriteAttr(Lookup(id), val);
651 }
652 break;
653
654 case FT_Double:
655 {
656 double val;
657
658 id = ReadUShort( in );
659 in.Read(&val, sizeof(val));
660 /*int dig = */ReadDigits(in);
661
662 adapter.WriteAttr(Lookup(id), val);
663 }
664 break;
665
666 case FT_Int:
667 {
668 id = ReadUShort( in );
669 int val = ReadInt( in );
670
671 adapter.WriteAttr(Lookup(id), val);
672 }
673 break;
674
675 case FT_Bool:
676 {
677 unsigned char val;
678
679 id = ReadUShort( in );
680 in.Read(&val, 1);
681
682 adapter.WriteAttr(Lookup(id), val);
683 }
684 break;
685
686 case FT_Long:
687 {
688 id = ReadUShort( in );
689 long val = ReadLong( in );
690
691 adapter.WriteAttr(Lookup(id), val);
692 }
693 break;
694
695 case FT_LongLong:
696 {
697 id = ReadUShort( in );
698 long long val = ReadLongLong( in );
699 adapter.WriteAttr(Lookup(id), val);
700 }
701 break;
702
703 case FT_SizeT:
704 {
705 id = ReadUShort( in );
706 size_t val = ReadULong( in );
707
708 adapter.WriteAttr(Lookup(id), val);
709 }
710 break;
711
712 case FT_Data:
713 {
714 int len = ReadLength( in );
715 adapter.WriteData(ReadString(len));
716 }
717 break;
718
719 case FT_Raw:
720 {
721 int len = ReadLength( in );
722 adapter.WriteRaw(ReadString(len));
723 }
724 break;
725
726 case FT_CharSize:
727 {
728 in.Read(&mCharSize, 1);
729 }
730 break;
731
732 default:
733 wxASSERT(true);
734 break;
735 }
736 }
737 }
738 catch( const Error& )
739 {
740 // Document was corrupt, or platform differences in size or endianness
741 // were not well canonicalized
742 return false;
743 }
744
745 wxLogInfo(
746 "Loaded %lld string %f Kb in size", stringsCount, stringsLength / 1024.0);
747
748 return adapter.Finalize();
749}
@ FT_Int
@ FT_String
@ FT_EndTag
@ FT_Push
@ FT_Raw
@ FT_Float
@ FT_Bool
@ FT_Name
@ FT_Double
@ FT_Pop
@ FT_SizeT
@ FT_Data
@ FT_LongLong
@ FT_StartTag
@ FT_Long
std::unordered_map< unsigned short, std::string > IdMap
int id

References FT_Bool, FT_CharSize, FT_Data, FT_Double, FT_EndTag, FT_Float, FT_Int, FT_Long, FT_LongLong, FT_Name, FT_Pop, FT_Push, FT_Raw, FT_SizeT, FT_StartTag, FT_String, id, BufferedStreamReader::Read(), anonymous_namespace{ProjectSerializer.cpp}::ReadDigits, anonymous_namespace{ProjectSerializer.cpp}::ReadInt, anonymous_namespace{ProjectSerializer.cpp}::ReadLength, anonymous_namespace{ProjectSerializer.cpp}::ReadLong, anonymous_namespace{ProjectSerializer.cpp}::ReadLongLong, anonymous_namespace{ProjectSerializer.cpp}::ReadULong, and anonymous_namespace{ProjectSerializer.cpp}::ReadUShort.

Referenced by ProjectFileIO::LoadProject().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ DictChanged()

bool ProjectSerializer::DictChanged ( ) const

Definition at line 524 of file ProjectSerializer.cpp.

525{
526 return mDictChanged;
527}

References mDictChanged.

◆ EndTag()

void ProjectSerializer::EndTag ( const wxString &  name)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 383 of file ProjectSerializer.cpp.

384{
387}
const TranslatableString name
Definition: Distortion.cpp:82
void WriteName(const wxString &name)
MemoryStream mBuffer

References MemoryStream::AppendByte(), FT_EndTag, mBuffer, name, and WriteName().

Here is the call graph for this function:

◆ FailureMessage()

TranslatableString ProjectSerializer::FailureMessage ( const FilePath filePath)
static

Definition at line 90 of file ProjectSerializer.cpp.

91{
92 return
93XO("This recovery file was saved by Audacity 2.3.0 or before.\n"
94 "You need to run that version of Audacity to recover the project." );
95}
#define XO(s)
Definition: Internat.h:31

References XO.

◆ GetData()

const MemoryStream & ProjectSerializer::GetData ( ) const

Definition at line 514 of file ProjectSerializer.cpp.

515{
516 return mBuffer;
517}

References mBuffer.

Referenced by ProjectFileIO::WriteDoc().

Here is the caller graph for this function:

◆ GetDict()

const MemoryStream & ProjectSerializer::GetDict ( ) const

Definition at line 509 of file ProjectSerializer.cpp.

510{
511 return mDict;
512}

References mDict.

Referenced by ProjectFileIO::WriteDoc().

Here is the caller graph for this function:

◆ IsEmpty()

bool ProjectSerializer::IsEmpty ( ) const

Definition at line 519 of file ProjectSerializer.cpp.

520{
521 return mBuffer.GetSize() == 0;
522}
const size_t GetSize() const noexcept

References MemoryStream::GetSize(), and mBuffer.

Here is the call graph for this function:

◆ StartTag()

void ProjectSerializer::StartTag ( const wxString &  name)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 377 of file ProjectSerializer.cpp.

378{
381}

References MemoryStream::AppendByte(), FT_StartTag, mBuffer, name, and WriteName().

Here is the call graph for this function:

◆ Write()

void ProjectSerializer::Write ( const wxString &  data)
overridevirtual

Implements XMLWriter.

Definition at line 471 of file ProjectSerializer.cpp.

472{
474 Length len = value.length() * sizeof(wxStringCharType);
475 WriteLength( mBuffer, len );
476 mBuffer.AppendData(value.wx_str(), len);
477}

References MemoryStream::AppendByte(), MemoryStream::AppendData(), FT_Raw, mBuffer, and anonymous_namespace{ProjectSerializer.cpp}::WriteLength.

Here is the call graph for this function:

◆ WriteAttr() [1/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
bool  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 412 of file ProjectSerializer.cpp.

413{
416
417 mBuffer.AppendByte(value);
418}

References MemoryStream::AppendByte(), FT_Bool, mBuffer, name, and WriteName().

Here is the call graph for this function:

◆ WriteAttr() [2/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
const wxChar *  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 389 of file ProjectSerializer.cpp.

390{
391 WriteAttr(name, wxString(value));
392}
void WriteAttr(const wxString &name, const wxString &value) override

References name, and WriteAttr().

Here is the call graph for this function:

◆ WriteAttr() [3/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
const wxString &  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 394 of file ProjectSerializer.cpp.

395{
398
399 const Length len = value.length() * sizeof(wxStringCharType);
400 WriteLength( mBuffer, len );
401 mBuffer.AppendData(value.wx_str(), len);
402}

References MemoryStream::AppendByte(), MemoryStream::AppendData(), FT_String, mBuffer, name, anonymous_namespace{ProjectSerializer.cpp}::WriteLength, and WriteName().

Referenced by WriteAttr().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ WriteAttr() [4/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
double  value,
int  digits = -1 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 453 of file ProjectSerializer.cpp.

454{
457
458 mBuffer.AppendData(&value, sizeof(value));
459 WriteDigits( mBuffer, digits );
460}

References MemoryStream::AppendByte(), MemoryStream::AppendData(), FT_Double, mBuffer, name, anonymous_namespace{ProjectSerializer.cpp}::WriteDigits, and WriteName().

Here is the call graph for this function:

◆ WriteAttr() [5/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
float  value,
int  digits = -1 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 444 of file ProjectSerializer.cpp.

445{
448
449 mBuffer.AppendData(&value, sizeof(value));
450 WriteDigits( mBuffer, digits );
451}

References MemoryStream::AppendByte(), MemoryStream::AppendData(), FT_Float, mBuffer, name, anonymous_namespace{ProjectSerializer.cpp}::WriteDigits, and WriteName().

Here is the call graph for this function:

◆ WriteAttr() [6/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
int  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 404 of file ProjectSerializer.cpp.

405{
408
409 WriteInt( mBuffer, value );
410}

References MemoryStream::AppendByte(), FT_Int, mBuffer, name, anonymous_namespace{ProjectSerializer.cpp}::WriteInt, and WriteName().

Here is the call graph for this function:

◆ WriteAttr() [7/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
long long  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 428 of file ProjectSerializer.cpp.

References MemoryStream::AppendByte(), FT_LongLong, mBuffer, name, anonymous_namespace{ProjectSerializer.cpp}::WriteLongLong, and WriteName().

Here is the call graph for this function:

◆ WriteAttr() [8/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
long  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 420 of file ProjectSerializer.cpp.

421{
424
425 WriteLong( mBuffer, value );
426}

References MemoryStream::AppendByte(), FT_Long, mBuffer, name, anonymous_namespace{ProjectSerializer.cpp}::WriteLong, and WriteName().

Here is the call graph for this function:

◆ WriteAttr() [9/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
size_t  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 436 of file ProjectSerializer.cpp.

437{
440
441 WriteULong( mBuffer, value );
442}

References MemoryStream::AppendByte(), FT_SizeT, mBuffer, name, WriteName(), and anonymous_namespace{ProjectSerializer.cpp}::WriteULong.

Here is the call graph for this function:

◆ WriteData()

void ProjectSerializer::WriteData ( const wxString &  value)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 462 of file ProjectSerializer.cpp.

463{
465
466 Length len = value.length() * sizeof(wxStringCharType);
467 WriteLength( mBuffer, len );
468 mBuffer.AppendData(value.wx_str(), len);
469}

References MemoryStream::AppendByte(), MemoryStream::AppendData(), FT_Data, mBuffer, and anonymous_namespace{ProjectSerializer.cpp}::WriteLength.

Here is the call graph for this function:

◆ WriteName()

void ProjectSerializer::WriteName ( const wxString &  name)
private

Definition at line 479 of file ProjectSerializer.cpp.

480{
481 wxASSERT(name.length() * sizeof(wxStringCharType) <= SHRT_MAX);
482 UShort id;
483
484 auto nameiter = mNames.find(name);
485 if (nameiter != mNames.end())
486 {
487 id = nameiter->second;
488 }
489 else
490 {
491 // mNames is static. This appends each name to static mDict only once
492 // in each run.
493 UShort len = name.length() * sizeof(wxStringCharType);
494
495 id = mNames.size();
496 mNames[name] = id;
497
499 WriteUShort( mDict, id );
500 WriteUShort( mDict, len );
501 mDict.AppendData(name.wx_str(), len);
502
503 mDictChanged = true;
504 }
505
506 WriteUShort( mBuffer, id );
507}
static NameMap mNames

References MemoryStream::AppendByte(), MemoryStream::AppendData(), FT_Name, id, mBuffer, mDict, mDictChanged, mNames, name, and anonymous_namespace{ProjectSerializer.cpp}::WriteUShort.

Referenced by EndTag(), StartTag(), and WriteAttr().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ mBuffer

MemoryStream ProjectSerializer::mBuffer
private

Definition at line 75 of file ProjectSerializer.h.

Referenced by EndTag(), GetData(), IsEmpty(), StartTag(), Write(), WriteAttr(), WriteData(), and WriteName().

◆ mDict

MemoryStream ProjectSerializer::mDict
staticprivate

Definition at line 79 of file ProjectSerializer.h.

Referenced by GetDict(), ProjectSerializer(), and WriteName().

◆ mDictChanged

bool ProjectSerializer::mDictChanged
private

Definition at line 76 of file ProjectSerializer.h.

Referenced by DictChanged(), ProjectSerializer(), and WriteName().

◆ mNames

NameMap ProjectSerializer::mNames
staticprivate

Definition at line 78 of file ProjectSerializer.h.

Referenced by WriteName().


The documentation for this class was generated from the following files: