From 6f6ec8d7de8458279604db6265a93e0fff431e08 Mon Sep 17 00:00:00 2001
From: Scott Wheeler <wheeler@kde.org>
Date: Thu, 31 Jan 2008 04:11:42 +0000
Subject: [PATCH] Another one bites the dust.  Removing Yet Another TagUnion

git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@768980 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
---
 taglib/flac/flacfile.cpp | 111 ++++++++------------
 taglib/flac/flacfile.h   |   2 +-
 taglib/flac/flactag.h    | 216 ---------------------------------------
 3 files changed, 43 insertions(+), 286 deletions(-)
 delete mode 100644 taglib/flac/flactag.h

diff --git a/taglib/flac/flacfile.cpp b/taglib/flac/flacfile.cpp
index 7d131831..6d3d6cf2 100644
--- a/taglib/flac/flacfile.cpp
+++ b/taglib/flac/flacfile.cpp
@@ -27,19 +27,21 @@
 #include <tstring.h>
 #include <tlist.h>
 #include <tdebug.h>
+#include <tagunion.h>
 
 #include <id3v2header.h>
 #include <id3v2tag.h>
 #include <id3v1tag.h>
+#include <xiphcomment.h>
 
 #include "flacfile.h"
-#include "flactag.h"
 
 using namespace TagLib;
 
 namespace
 {
-  enum BlockType { StreamInfo = 0, Padding, Application, SeekTable, VorbisComment, CueSheet };
+  enum { XiphIndex = 0, ID3v2Index = 1, ID3v1Index = 2 };
+  enum { StreamInfo = 0, Padding, Application, SeekTable, VorbisComment, CueSheet };
 }
 
 class FLAC::File::FilePrivate
@@ -47,13 +49,9 @@ class FLAC::File::FilePrivate
 public:
   FilePrivate() :
     ID3v2FrameFactory(ID3v2::FrameFactory::instance()),
-    ID3v2Tag(0),
     ID3v2Location(-1),
     ID3v2OriginalSize(0),
-    ID3v1Tag(0),
     ID3v1Location(-1),
-    comment(0),
-    tag(0),
     properties(0),
     flacStart(0),
     streamStart(0),
@@ -65,24 +63,16 @@ public:
 
   ~FilePrivate()
   {
-    delete ID3v2Tag;
-    delete ID3v1Tag;
-    delete comment;
-    delete tag;
     delete properties;
   }
 
   const ID3v2::FrameFactory *ID3v2FrameFactory;
-  ID3v2::Tag *ID3v2Tag;
   long ID3v2Location;
   uint ID3v2OriginalSize;
 
-  ID3v1::Tag *ID3v1Tag;
   long ID3v1Location;
 
-  Ogg::XiphComment *comment;
-
-  FLAC::Tag *tag;
+  TagUnion tag;
 
   Properties *properties;
   ByteVector streamInfoData;
@@ -126,7 +116,7 @@ FLAC::File::~File()
 
 TagLib::Tag *FLAC::File::tag() const
 {
-  return d->tag;
+  return &d->tag;
 }
 
 FLAC::Properties *FLAC::File::audioProperties() const
@@ -144,13 +134,9 @@ bool FLAC::File::save()
 
   // Create new vorbis comments
 
-  if(!d->comment) {
-    d->comment = new Ogg::XiphComment;
-    if(d->tag)
-      Tag::duplicate(d->tag, d->comment, true);
-  }
+  Tag::duplicate(&d->tag, xiphComment(true), true);
 
-  d->xiphCommentData = d->comment->render(false);
+  d->xiphCommentData = xiphComment()->render(false);
 
   // A Xiph comment portion of the data stream starts with a 4-byte descriptor.
   // The first byte indicates the frame type.  The last three bytes are used
@@ -216,21 +202,21 @@ bool FLAC::File::save()
 
   // Update ID3 tags
 
-  if(d->ID3v2Tag) {
+  if(ID3v2Tag()) {
     if(d->hasID3v2) {
       if(d->ID3v2Location < d->flacStart)
         debug("FLAC::File::save() -- This can't be right -- an ID3v2 tag after the "
               "start of the FLAC bytestream?  Not writing the ID3v2 tag.");
       else
-        insert(d->ID3v2Tag->render(), d->ID3v2Location, d->ID3v2OriginalSize);
+        insert(ID3v2Tag()->render(), d->ID3v2Location, d->ID3v2OriginalSize);
     }
     else
-      insert(d->ID3v2Tag->render(), 0, 0);
+      insert(ID3v2Tag()->render(), 0, 0);
   }
 
-  if(d->ID3v1Tag) {
-    seek(d->ID3v1Tag ? -128 : 0, End);
-    writeBlock(d->ID3v1Tag->render());
+  if(ID3v1Tag()) {
+    seek(-128, End);
+    writeBlock(ID3v1Tag()->render());
   }
 
   return true;
@@ -238,35 +224,29 @@ bool FLAC::File::save()
 
 ID3v2::Tag *FLAC::File::ID3v2Tag(bool create)
 {
-  if(!create || d->ID3v2Tag)
-    return d->ID3v2Tag;
+  if(!create || d->tag[ID3v2Index])
+    return static_cast<ID3v2::Tag *>(d->tag[ID3v2Index]);
 
-  // no ID3v2 tag exists and we've been asked to create one
-
-  d->ID3v2Tag = new ID3v2::Tag;
-  return d->ID3v2Tag;
+  d->tag.setTag(ID3v2Index, new ID3v2::Tag);
+  return static_cast<ID3v2::Tag *>(d->tag[ID3v2Index]);
 }
 
 ID3v1::Tag *FLAC::File::ID3v1Tag(bool create)
 {
-  if(!create || d->ID3v1Tag)
-    return d->ID3v1Tag;
+  if(!create || d->tag[ID3v1Index])
+    return static_cast<ID3v1::Tag *>(d->tag[ID3v1Index]);
 
-  // no ID3v1 tag exists and we've been asked to create one
-
-  d->ID3v1Tag = new ID3v1::Tag;
-  return d->ID3v1Tag;
+  d->tag.setTag(ID3v1Index, new ID3v1::Tag);
+  return static_cast<ID3v1::Tag *>(d->tag[ID3v1Index]);
 }
 
 Ogg::XiphComment *FLAC::File::xiphComment(bool create)
 {
-  if(!create || d->comment)
-    return d->comment;
+  if(!create || d->tag[XiphIndex])
+    return static_cast<Ogg::XiphComment *>(d->tag[XiphIndex]);
 
-  // no XiphComment exists and we've been asked to create one
-
-  d->comment = new Ogg::XiphComment;
-  return d->comment;
+  d->tag.setTag(XiphIndex, new Ogg::XiphComment);
+  return static_cast<Ogg::XiphComment *>(d->tag[XiphIndex]);
 }
 
 void FLAC::File::setID3v2FrameFactory(const ID3v2::FrameFactory *factory)
@@ -287,14 +267,12 @@ void FLAC::File::read(bool readProperties, Properties::ReadStyle propertiesStyle
 
   if(d->ID3v2Location >= 0) {
 
-    d->ID3v2Tag = new ID3v2::Tag(this, d->ID3v2Location, d->ID3v2FrameFactory);
+    d->tag.setTag(ID3v2Index, new ID3v2::Tag(this, d->ID3v2Location, d->ID3v2FrameFactory));
 
-    d->ID3v2OriginalSize = d->ID3v2Tag->header()->completeTagSize();
+    d->ID3v2OriginalSize = ID3v2Tag()->header()->completeTagSize();
 
-    if(d->ID3v2Tag->header()->tagSize() <= 0) {
-      delete d->ID3v2Tag;
-      d->ID3v2Tag = 0;
-    }
+    if(ID3v2Tag()->header()->tagSize() <= 0)
+      d->tag.setTag(ID3v2Index, 0);
     else
       d->hasID3v2 = true;
   }
@@ -304,7 +282,7 @@ void FLAC::File::read(bool readProperties, Properties::ReadStyle propertiesStyle
   d->ID3v1Location = findID3v1();
 
   if(d->ID3v1Location >= 0) {
-    d->ID3v1Tag = new ID3v1::Tag(this, d->ID3v1Location);
+    d->tag.setTag(ID3v1Index, new ID3v1::Tag(this, d->ID3v1Location));
     d->hasID3v1 = true;
   }
 
@@ -312,15 +290,13 @@ void FLAC::File::read(bool readProperties, Properties::ReadStyle propertiesStyle
 
   scan();
 
-  if (!isValid()) return;
+  if(!isValid())
+    return;
 
   if(d->hasXiphComment)
-    d->comment = new Ogg::XiphComment(xiphCommentData());
-
-  if(d->hasXiphComment || d->hasID3v2 || d->hasID3v1)
-    d->tag = new FLAC::Tag(d->comment, d->ID3v2Tag, d->ID3v1Tag);
+    d->tag.setTag(XiphIndex, new Ogg::XiphComment(xiphCommentData()));
   else
-    d->tag = new FLAC::Tag(new Ogg::XiphComment);
+    d->tag.setTag(XiphIndex, new Ogg::XiphComment);
 
   if(readProperties)
     d->properties = new Properties(streamInfoData(), streamLength(), propertiesStyle);
@@ -328,18 +304,12 @@ void FLAC::File::read(bool readProperties, Properties::ReadStyle propertiesStyle
 
 ByteVector FLAC::File::streamInfoData()
 {
-  if (isValid())
-    return d->streamInfoData;
-  else
-    return ByteVector();
+  return isValid() ? d->streamInfoData : ByteVector();
 }
 
-ByteVector FLAC::File::xiphCommentData()
+ByteVector FLAC::File::xiphCommentData() const
 {
-  if (isValid() && d->hasXiphComment)
-    return d->xiphCommentData;
-  else
-    return ByteVector();
+  return (isValid() && d->hasXiphComment) ? d->xiphCommentData : ByteVector();
 }
 
 long FLAC::File::streamLength()
@@ -405,6 +375,7 @@ void FLAC::File::scan()
   // Search through the remaining metadata
 
   while(!isLastBlock) {
+
     header = readBlock(4);
     blockType = header[0] & 0x7f;
     isLastBlock = (header[0] & 0x80) != 0;
@@ -430,9 +401,11 @@ void FLAC::File::scan()
   }
 
   // End of metadata, now comes the datastream
+
   d->streamStart = nextBlockOffset;
   d->streamLength = File::length() - d->streamStart;
-  if (d->hasID3v1)
+
+  if(d->hasID3v1)
     d->streamLength -= 128;
 
   d->scanned = true;
diff --git a/taglib/flac/flacfile.h b/taglib/flac/flacfile.h
index 26aa210a..ae1fe69b 100644
--- a/taglib/flac/flacfile.h
+++ b/taglib/flac/flacfile.h
@@ -190,7 +190,7 @@ namespace TagLib {
       void scan();
       long findID3v2();
       long findID3v1();
-      ByteVector xiphCommentData();
+      ByteVector xiphCommentData() const;
 
       class FilePrivate;
       FilePrivate *d;
diff --git a/taglib/flac/flactag.h b/taglib/flac/flactag.h
deleted file mode 100644
index 78737d87..00000000
--- a/taglib/flac/flactag.h
+++ /dev/null
@@ -1,216 +0,0 @@
-/***************************************************************************
-    copyright            : (C) 2003 by Allan Sandfeld Jensen
-    email                : kde@carewolf.org
- ***************************************************************************/
-
-/***************************************************************************
- *   This library is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU Lesser General Public License version   *
- *   2.1 as published by the Free Software Foundation.                     *
- *                                                                         *
- *   This library is distributed in the hope that it will be useful, but   *
- *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
- *   Lesser General Public License for more details.                       *
- *                                                                         *
- *   You should have received a copy of the GNU Lesser General Public      *
- *   License along with this library; if not, write to the Free Software   *
- *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
- *   USA                                                                   *
- *                                                                         *
- *   Alternatively, this file is available under the Mozilla Public        *
- *   License Version 1.1.  You may obtain a copy of the License at         *
- *   http://www.mozilla.org/MPL/                                           *
- ***************************************************************************/
-
-#ifndef DO_NOT_DOCUMENT // Tell Doxygen not to document this header
-
-#ifndef TAGLIB_FLACTAG_H
-#define TAGLIB_FLACTAG_H
-
-////////////////////////////////////////////////////////////////////////////////
-// Note that this header is not installed.
-////////////////////////////////////////////////////////////////////////////////
-
-#include <xiphcomment.h>
-#include <id3v2tag.h>
-#include <id3v1tag.h>
-
-namespace TagLib {
-
-  namespace FLAC {
-
-    /*!
-     * A union of Xiph, ID3v2 and ID3v1 tags.
-     */
-    class Tag : public TagLib::Tag
-    {
-    public:
-      Tag(Ogg::XiphComment *xiph, ID3v2::Tag *id3v2 = 0, ID3v1::Tag *id3v1 = 0) :
-        TagLib::Tag(),
-        xiph(xiph), id3v2(id3v2), id3v1(id3v1) {}
-
-      virtual String title() const {
-        if(xiph && !xiph->title().isEmpty())
-          return xiph->title();
-
-        if(id3v2 && !id3v2->title().isEmpty())
-          return id3v2->title();
-
-        if(id3v1)
-          return id3v1->title();
-
-        return String::null;
-      }
-
-      virtual String artist() const {
-        if(xiph && !xiph->artist().isEmpty())
-          return xiph->artist();
-
-        if(id3v2 && !id3v2->artist().isEmpty())
-          return id3v2->artist();
-
-        if(id3v1)
-          return id3v1->artist();
-
-        return String::null;
-      }
-
-      virtual String album() const {
-        if(xiph && !xiph->album().isEmpty())
-          return xiph->album();
-
-        if(id3v2 && !id3v2->album().isEmpty())
-          return id3v2->album();
-
-        if(id3v1)
-          return id3v1->album();
-
-        return String::null;
-      }
-
-      virtual String comment() const {
-        if(xiph && !xiph->comment().isEmpty())
-          return xiph->comment();
-
-        if(id3v2 && !id3v2->comment().isEmpty())
-          return id3v2->comment();
-
-        if(id3v1)
-          return id3v1->comment();
-
-        return String::null;
-      }
-
-      virtual String genre() const {
-        if(xiph && !xiph->genre().isEmpty())
-          return xiph->genre();
-
-        if(id3v2 && !id3v2->genre().isEmpty())
-          return id3v2->genre();
-
-        if(id3v1)
-          return id3v1->genre();
-
-        return String::null;
-      }
-
-      virtual uint year() const {
-        if(xiph && xiph->year() > 0)
-          return xiph->year();
-
-        if(id3v2 && id3v2->year() > 0)
-          return id3v2->year();
-
-        if(id3v1)
-          return id3v1->year();
-
-        return 0;
-      }
-
-      virtual uint track() const {
-        if(xiph && xiph->track() > 0)
-          return xiph->track();
-
-        if(id3v2 && id3v2->track() > 0)
-          return id3v2->track();
-
-        if(id3v1)
-          return id3v1->track();
-
-        return 0;
-      }
-
-      virtual void setTitle(const String &s) {
-        if(xiph)
-          xiph->setTitle(s);
-        if(id3v2)
-          id3v2->setTitle(s);
-        if(id3v1)
-          id3v1->setTitle(s);
-      }
-
-      virtual void setArtist(const String &s) {
-        if(xiph)
-          xiph->setArtist(s);
-        if(id3v2)
-          id3v2->setArtist(s);
-        if(id3v1)
-          id3v1->setArtist(s);
-      }
-
-      virtual void setAlbum(const String &s) {
-        if(xiph)
-          xiph->setAlbum(s);
-        if(id3v2)
-          id3v2->setAlbum(s);
-        if(id3v1)
-          id3v1->setAlbum(s);
-      }
-
-      virtual void setComment(const String &s) {
-        if(xiph)
-          xiph->setComment(s);
-        if(id3v2)
-          id3v2->setComment(s);
-        if(id3v1)
-          id3v1->setComment(s);
-      }
-
-      virtual void setGenre(const String &s) {
-        if(xiph)
-          xiph->setGenre(s);
-        if(id3v2)
-          id3v2->setGenre(s);
-        if(id3v1)
-          id3v1->setGenre(s);
-      }
-
-      virtual void setYear(uint i) {
-        if(xiph)
-          xiph->setYear(i);
-        if(id3v2)
-          id3v2->setYear(i);
-        if(id3v1)
-          id3v1->setYear(i);
-      }
-
-      virtual void setTrack(uint i) {
-        if(xiph)
-          xiph->setTrack(i);
-        if(id3v2)
-          id3v2->setTrack(i);
-        if(id3v1)
-          id3v1->setTrack(i);
-      }
-
-    private:
-      Ogg::XiphComment *xiph;
-      ID3v2::Tag *id3v2;
-      ID3v1::Tag *id3v1;
-    };
-  }
-}
-
-#endif
-#endif