Ejemplo n.º 1
0
TEST(ScopeGuardTest, testScopeGuard) {
  bool setByGuard0 = false;
  {
    auto guard1 = scopeGuard([&] { setByGuard0 = true; });
  }
  EXPECT_TRUE(setByGuard0);
}
Ejemplo n.º 2
0
TEST(ScopeGuardTest, testScopeGuardDismiss) {
  bool setByGuard0 = false;
  {
    auto guard0 = scopeGuard([&] { setByGuard0 = true; });
    guard0.dismiss();
  }
  EXPECT_FALSE(setByGuard0);
}
Ejemplo n.º 3
0
TEST(ScopeGuardTest, testScopeGuardPerformance) {
  std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
  // make volatile to avoid optimizations
  volatile int setByGuard0 = 0;
  start                    = std::chrono::high_resolution_clock::now();
  for (int n = 0; n < 10000000; ++n) {
    auto guard0 = scopeGuard([&] { setByGuard0 += 1; });
  }
  end = std::chrono::high_resolution_clock::now();
  std::chrono::duration<double> elapsed_seconds = end - start;
  std::cout << "Needed " << elapsed_seconds.count() << "s for " << setByGuard0
            << " loops\n";
}
Ejemplo n.º 4
0
	BookParser_f MakeBookParser (const QString& filename)
	{
		if (filename.endsWith (".fb2"))
			return FB2::Parse;
		else if (filename.endsWith (".epub"))
			return EPUB::Parse;
		else if (filename.endsWith (".mobi") ||
				filename.endsWith (".prc"))
			return Mobi::Parse;
		else if (filename.endsWith (".zip"))
		{
			QuaZip *zip = new QuaZip (filename);
			std::shared_ptr<void> scopeGuard (nullptr,
					[zip] (void*) { zip->close (); delete zip; });

			if (!zip->open (QuaZip::mdUnzip))
			{
				qWarning () << Q_FUNC_INFO
						<< "unable to open file "
						<< filename
						<< "as zip archive";
				return BookParser_f ();
			}

			for (const auto& info : zip->getFileInfoList ())
			{
				if (info.name.endsWith (".fb2"))
				{
					QString fileNameInArch = info.name;
					zip->setCurrentFile (fileNameInArch);
					QuaZipFile file (zip);
					if (!file.open (QIODevice::ReadOnly))
						continue;

					const quint32 size = info.uncompressedSize;
					auto ba = file.readAll ();

					return [ba, size, filename, fileNameInArch] (const QString&) -> Book
						{
							auto book = FB2::ParseFB2Content (ba);
							book.AddedData_ = QDateTime::currentDateTime ();
							book.Rate_ = NoStar;
							book.OriginalPath_ = filename;
							book.Size_ = size;
							book.Content_ = ba;

							if (book.TitleInfo_.Title_.isEmpty())
								book.TitleInfo_.Title_ = QObject::tr ("Unknown");
							Author author;
							author.Name_ = QObject::tr ("Unknown");
							if (book.TitleInfo_.Authors_.isEmpty ())
								book.TitleInfo_.Authors_ << author;
							if (book.TitleInfo_.Authors_.at (0).Name_.isEmpty ())
								book.TitleInfo_.Authors_ [0] = author;

							return book;
						};
				}
			}
		}

		return BookParser_f ();
	}
Ejemplo n.º 5
0
	Book Parse (const QString& filename)
	{
		QuaZip *zip = new QuaZip (filename);
		std::shared_ptr<void> scopeGuard (nullptr,
				[zip] (void*) { zip->close (); delete zip; });

		Book book;
		book.OriginalPath_ = filename;
		if (!zip->open (QuaZip::mdUnzip))
		{
			qWarning () << Q_FUNC_INFO
					<< "unable to open file "
					<< filename
					<< "as zip archive";
			return book;
		}

		for (const auto& opfPath : GetOPFPaths (zip))
		{
			zip->setCurrentFile (opfPath);
			QuaZipFile opfFile (zip);
			if (!opfFile.open (QIODevice::ReadOnly))
			{
				qWarning () << Q_FUNC_INFO
						<< "unable to open opf file";
				return book;
			}

			QDomDocument document;
			QString errorMsg;
			int errorLine = -1, errorColumn = -1;
			if (!document.setContent (opfFile.readAll (),
				&errorMsg, &errorLine, &errorColumn))
			{
				qWarning () << Q_FUNC_INFO
						<< errorMsg
						<< "in line:"
						<< errorLine
						<< "column:"
						<< errorColumn;
				return book;
			}

			FillEPubBookInfo (document, book, zip);
			book.AddedData_ = QDateTime::currentDateTime ();
			book.Rate_ = NoStar;
			book.Size_ = QFileInfo (filename).size ();
			book.IsValid_ = true;

			if (book.TitleInfo_.Title_.isEmpty())
				book.TitleInfo_.Title_ = QObject::tr ("Unknown");
			Author author;
			author.Name_ = QObject::tr ("Unknown");
			if (book.TitleInfo_.Authors_.isEmpty ())
				book.TitleInfo_.Authors_ << author;
			if (book.TitleInfo_.Authors_.at (0).Name_.isEmpty ())
				book.TitleInfo_.Authors_ [0] = author;
		}

		return book;
	}
Ejemplo n.º 6
0
void ConsoleWriter::write( const LogElementList &elements, const LogParameter &param )
{
	boost::mutex::scoped_lock scopeGuard( guard );
	console_std::Out << private_::getStr( elements, param );
}
Ejemplo n.º 7
0
bool CmdPasteSymbolItems::performExecute() {
  // if an error occurs, undo all already executed child commands
  auto undoScopeGuard = scopeGuard([&]() { performUndo(); });

  // Notes:
  //
  //  - If the UUID is already existing, or the destination symbol is different
  //    to the source symbol, generate a new random UUID. Otherwise use the same
  //    UUID to avoid modifications after cut+paste within one symbol.
  //  - If there is already a pin with the same name, increment its number (or
  //    start adding a number if there is none already) to get unique names.
  //  - The graphics items of the added elements are selected immediately to
  //    allow dragging them afterwards.

  for (const SymbolPin& pin : mData->getPins().sortedByName()) {
    Uuid uuid = pin.getUuid();
    if (mSymbol.getPins().contains(uuid) ||
        (mSymbol.getUuid() != mData->getSymbolUuid())) {
      uuid = Uuid::createRandom();
    }
    CircuitIdentifier name = pin.getName();
    for (int i = 0; (i < 1000) && mSymbol.getPins().contains(*name); ++i) {
      name = CircuitIdentifier(
          Toolbox::incrementNumberInString(*name));  // can throw
    }
    std::shared_ptr<SymbolPin> copy =
        std::make_shared<SymbolPin>(uuid, name, pin.getPosition() + mPosOffset,
                                    pin.getLength(), pin.getRotation());
    execNewChildCmd(new CmdSymbolPinInsert(mSymbol.getPins(), copy));
    SymbolPinGraphicsItem* item = mGraphicsItem.getPinGraphicsItem(uuid);
    Q_ASSERT(item);
    item->setSelected(true);
  }

  for (const Circle& circle : mData->getCircles().sortedByUuid()) {
    Uuid uuid = circle.getUuid();
    if (mSymbol.getCircles().contains(uuid) ||
        (mSymbol.getUuid() != mData->getSymbolUuid())) {
      uuid = Uuid::createRandom();
    }
    std::shared_ptr<Circle> copy = std::make_shared<Circle>(
        uuid, circle.getLayerName(), circle.getLineWidth(), circle.isFilled(),
        circle.isGrabArea(), circle.getCenter() + mPosOffset,
        circle.getDiameter());
    execNewChildCmd(new CmdCircleInsert(mSymbol.getCircles(), copy));
    CircleGraphicsItem* item = mGraphicsItem.getCircleGraphicsItem(*copy);
    Q_ASSERT(item);
    item->setSelected(true);
  }

  for (const Polygon& polygon : mData->getPolygons().sortedByUuid()) {
    Uuid uuid = polygon.getUuid();
    if (mSymbol.getPolygons().contains(uuid) ||
        (mSymbol.getUuid() != mData->getSymbolUuid())) {
      uuid = Uuid::createRandom();
    }
    std::shared_ptr<Polygon> copy = std::make_shared<Polygon>(
        uuid, polygon.getLayerName(), polygon.getLineWidth(),
        polygon.isFilled(), polygon.isGrabArea(),
        polygon.getPath().translated(mPosOffset));
    execNewChildCmd(new CmdPolygonInsert(mSymbol.getPolygons(), copy));
    PolygonGraphicsItem* item = mGraphicsItem.getPolygonGraphicsItem(*copy);
    Q_ASSERT(item);
    item->setSelected(true);
  }

  for (const Text& text : mData->getTexts().sortedByUuid()) {
    Uuid uuid = text.getUuid();
    if (mSymbol.getTexts().contains(uuid) ||
        (mSymbol.getUuid() != mData->getSymbolUuid())) {
      uuid = Uuid::createRandom();
    }
    std::shared_ptr<Text> copy = std::make_shared<Text>(
        uuid, text.getLayerName(), text.getText(),
        text.getPosition() + mPosOffset, text.getRotation(), text.getHeight(),
        text.getAlign());
    execNewChildCmd(new CmdTextInsert(mSymbol.getTexts(), copy));
    TextGraphicsItem* item = mGraphicsItem.getTextGraphicsItem(*copy);
    Q_ASSERT(item);
    item->setSelected(true);
  }

  undoScopeGuard.dismiss();  // no undo required
  return getChildCount() > 0;
}
Ejemplo n.º 8
0
bool CmdPasteFootprintItems::performExecute() {
  // if an error occurs, undo all already executed child commands
  auto undoScopeGuard = scopeGuard([&]() { performUndo(); });

  // Notes:
  //
  //  - If the UUID is already existing, or the destination footprint is
  //    different to the source footprint, generate a new random UUID. Otherwise
  //    use the same UUID to avoid modifications after cut+paste within one
  //    footprint.
  //  - Footprint pads are only copied if there is an unused package pad with
  //    the same name available.
  //  - The graphics items of the added elements are selected immediately to
  //    allow dragging them afterwards.

  for (const FootprintPad& pad : mData->getFootprintPads().sortedByUuid()) {
    Uuid              uuid = pad.getPackagePadUuid();
    CircuitIdentifier name = mData->getPackagePads().get(uuid)->getName();
    std::shared_ptr<PackagePad> newPad = mPackage.getPads().find(*name);
    if (newPad && (!mFootprint.getPads().contains(newPad->getUuid()))) {
      std::shared_ptr<FootprintPad> copy = std::make_shared<FootprintPad>(
          uuid, pad.getPosition() + mPosOffset, pad.getRotation(),
          pad.getShape(), pad.getWidth(), pad.getHeight(),
          pad.getDrillDiameter(), pad.getBoardSide());
      execNewChildCmd(new CmdFootprintPadInsert(mFootprint.getPads(), copy));
      FootprintPadGraphicsItem* item = mGraphicsItem.getPadGraphicsItem(*copy);
      Q_ASSERT(item);
      item->setSelected(true);
    }
  }

  for (const Circle& circle : mData->getCircles().sortedByUuid()) {
    Uuid uuid = circle.getUuid();
    if (mFootprint.getCircles().contains(uuid) ||
        (mFootprint.getUuid() != mData->getFootprintUuid())) {
      uuid = Uuid::createRandom();
    }
    std::shared_ptr<Circle> copy = std::make_shared<Circle>(
        uuid, circle.getLayerName(), circle.getLineWidth(), circle.isFilled(),
        circle.isGrabArea(), circle.getCenter() + mPosOffset,
        circle.getDiameter());
    execNewChildCmd(new CmdCircleInsert(mFootprint.getCircles(), copy));
    CircleGraphicsItem* item = mGraphicsItem.getCircleGraphicsItem(*copy);
    Q_ASSERT(item);
    item->setSelected(true);
  }

  for (const Polygon& polygon : mData->getPolygons().sortedByUuid()) {
    Uuid uuid = polygon.getUuid();
    if (mFootprint.getPolygons().contains(uuid) ||
        (mFootprint.getUuid() != mData->getFootprintUuid())) {
      uuid = Uuid::createRandom();
    }
    std::shared_ptr<Polygon> copy = std::make_shared<Polygon>(
        uuid, polygon.getLayerName(), polygon.getLineWidth(),
        polygon.isFilled(), polygon.isGrabArea(),
        polygon.getPath().translated(mPosOffset));
    execNewChildCmd(new CmdPolygonInsert(mFootprint.getPolygons(), copy));
    PolygonGraphicsItem* item = mGraphicsItem.getPolygonGraphicsItem(*copy);
    Q_ASSERT(item);
    item->setSelected(true);
  }

  for (const StrokeText& text : mData->getStrokeTexts().sortedByUuid()) {
    Uuid uuid = text.getUuid();
    if (mFootprint.getStrokeTexts().contains(uuid) ||
        (mFootprint.getUuid() != mData->getFootprintUuid())) {
      uuid = Uuid::createRandom();
    }
    std::shared_ptr<StrokeText> copy = std::make_shared<StrokeText>(
        uuid, text.getLayerName(), text.getText(),
        text.getPosition() + mPosOffset, text.getRotation(), text.getHeight(),
        text.getStrokeWidth(), text.getLetterSpacing(), text.getLineSpacing(),
        text.getAlign(), text.getMirrored(), text.getAutoRotate());
    execNewChildCmd(new CmdStrokeTextInsert(mFootprint.getStrokeTexts(), copy));
    StrokeTextGraphicsItem* item = mGraphicsItem.getTextGraphicsItem(*copy);
    Q_ASSERT(item);
    item->setSelected(true);
  }

  for (const Hole& hole : mData->getHoles().sortedByUuid()) {
    Uuid uuid = hole.getUuid();
    if (mFootprint.getHoles().contains(uuid) ||
        (mFootprint.getUuid() != mData->getFootprintUuid())) {
      uuid = Uuid::createRandom();
    }
    std::shared_ptr<Hole> copy = std::make_shared<Hole>(
        uuid, hole.getPosition() + mPosOffset, hole.getDiameter());
    execNewChildCmd(new CmdHoleInsert(mFootprint.getHoles(), copy));
    HoleGraphicsItem* item = mGraphicsItem.getHoleGraphicsItem(*copy);
    Q_ASSERT(item);
    item->setSelected(true);
  }

  undoScopeGuard.dismiss();  // no undo required
  return getChildCount() > 0;
}