/* * Copyright 2006-2007, 2023, Haiku. * Distributed under the terms of the MIT License. * * Authors: * Stephan Aßmus * Zardshard */ #ifndef REMOVE_COMMAND_H #define REMOVE_COMMAND_H #include #include #include #include #include #include "Command.h" #include "Container.h" #include "IconBuild.h" #undef B_TRANSLATION_CONTEXT #define B_TRANSLATION_CONTEXT "Icon-O-Matic-RemoveItemsCmd" using std::nothrow; _USING_ICON_NAMESPACE /*! Removes items located at certain \c indices from a \c Container. \note This class should be subclassed and the \c GetName member overridden. */ template class RemoveCommand : public Command { public: RemoveCommand( Container* container, const int32* indices, int32 count); virtual ~RemoveCommand(); virtual status_t InitCheck(); virtual status_t Perform(); virtual status_t Undo(); virtual void GetName(BString& name); protected: Container* fContainer; Type** fItems; int32* fIndices; int32 fCount; bool fItemsRemoved; }; template RemoveCommand::RemoveCommand( Container* container, const int32* indices, int32 count) : Command(), fContainer(container), fItems(count > 0 ? new (nothrow) Type*[count] : NULL), fIndices(count > 0 ? new (nothrow) int32[count] : NULL), fCount(count), fItemsRemoved(false) { if (!fContainer || !fItems || !fIndices) return; memcpy(fIndices, indices, sizeof(int32) * fCount); for (int32 i = 0; i < fCount; i++) fItems[i] = fContainer->ItemAt(fIndices[i]); } template RemoveCommand::~RemoveCommand() { if (fItemsRemoved && fItems) { for (int32 i = 0; i < fCount; i++) { if (fItems[i]) fItems[i]->ReleaseReference(); } } delete[] fItems; delete[] fIndices; } template status_t RemoveCommand::InitCheck() { return fContainer && fItems && fIndices ? B_OK : B_NO_INIT; } template status_t RemoveCommand::Perform() { status_t ret = B_OK; // remove shapes from container for (int32 i = 0; i < fCount; i++) { if (fItems[i] && !fContainer->RemoveItem(fItems[i])) { ret = B_ERROR; break; } } fItemsRemoved = true; return ret; } template status_t RemoveCommand::Undo() { status_t ret = B_OK; // add shapes to container at remembered indices for (int32 i = 0; i < fCount; i++) { if (fItems[i] && !fContainer->AddItem(fItems[i], fIndices[i])) { ret = B_ERROR; break; } } fItemsRemoved = false; return ret; } template void RemoveCommand::GetName(BString& name) { static BStringFormat format(B_TRANSLATE("Remove {0, plural, " "one{item} other{items}}")); format.Format(name, fCount); } #endif // REMOVE_COMMAND_H