Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) OpenQuestionDialog

 

STLQt CreatorLubuntu

 

OpenQuestionDialog is a dialog for OpenQuestion.

Technical facts

 

 

 

 

 

 

./CppOpenQuestionDialog/CppOpenQuestionDialog.pri

 

INCLUDEPATH += \
    ../../Classes/CppOpenQuestionDialog

SOURCES += \
    ../../Classes/CppOpenQuestionDialog/openquestiondialog.cpp \
    ../../Classes/CppOpenQuestionDialog/openquestiondialogfactory.cpp

HEADERS  += \
    ../../Classes/CppOpenQuestionDialog/openquestiondialog.h \
    ../../Classes/CppOpenQuestionDialog/openquestiondialogfactory.h

OTHER_FILES += \
    ../../Classes/CppOpenQuestionDialog/Licence.txt

 

 

 

 

 

./CppOpenQuestionDialog/openquestiondialog.h

 

//---------------------------------------------------------------------------
/*
OpenQuestionDialog, dialog for OpenQuestion
Copyright (C) 2011-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppOpenQuestionDialog.htm
//---------------------------------------------------------------------------
#ifndef OPENQUESTIONDIALOG_H
#define OPENQUESTIONDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include "questiondialog.h"
#pragma GCC diagnostic pop

namespace ribi {

struct Question;
struct OpenQuestion;

///Dialog for an OpenQuestion
struct OpenQuestionDialog final : public QuestionDialog
{
  OpenQuestionDialog();
  //explicit OpenQuestionDialog(const boost::shared_ptr<const OpenQuestion>& question);

  ///The answer the user has typed in so far
  std::string GetAnswerInProgress() const noexcept { return m_answer_in_progress; }

  boost::shared_ptr<const OpenQuestion> GetOpenQuestion() const noexcept { return m_open_question; }
  boost::shared_ptr<      OpenQuestion> GetOpenQuestion()       noexcept { return m_open_question; }
  boost::shared_ptr<const Question> GetQuestion() const noexcept;

  ///The answer the user has typed in so far
  ///Used for synching between multiple QtOpenQuestionDialogs displaying
  ///the same OpenQuestionDialog
  void SetAnswerInProgress(const std::string& answer_in_progress) noexcept;

  void SetOpenQuestion(const boost::shared_ptr<OpenQuestion>& open_question) noexcept;

  static std::string GetVersion() noexcept;
  static std::vector<std::string> GetVersionHistory() noexcept;

  ///Submit an answer
  ///For an open question, s will be the anwer
  void Submit(const std::string& s);

  std::string ToStr() const noexcept;

  mutable boost::signals2::signal<void (OpenQuestionDialog*)> m_signal_open_question_changed;

  private:
  friend void boost::checked_delete<>(OpenQuestionDialog*);
  friend void boost::checked_delete<>(const OpenQuestionDialog*);
  friend class boost::detail::sp_ms_deleter<OpenQuestionDialog>;
  friend class boost::detail::sp_ms_deleter<const OpenQuestionDialog>;
  ~OpenQuestionDialog() noexcept {}

  std::string m_answer_in_progress;
  boost::shared_ptr<OpenQuestion> m_open_question;

  #ifndef NDEBUG
  static void Test() noexcept;
  #endif
};

} //~namespace ribi

#endif // OPENQUESTIONDIALOG_H

 

 

 

 

 

./CppOpenQuestionDialog/openquestiondialog.cpp

 

//---------------------------------------------------------------------------
/*
OpenQuestionDialog, dialog for OpenQuestion
Copyright (C) 2011-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppOpenQuestionDialog.htm
//---------------------------------------------------------------------------
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include "openquestiondialog.h"

#include <cassert>
#include <sstream>

#include "openquestion.h"
#include "openquestiondialogfactory.h"
#include "testtimer.h"
#include "trace.h"
#pragma GCC diagnostic pop

ribi::OpenQuestionDialog::OpenQuestionDialog()
  : m_signal_open_question_changed{},
    m_answer_in_progress{},
    m_open_question{}
{
  #ifndef NDEBUG
  Test();
  #endif
}

/*
ribi::OpenQuestionDialog::OpenQuestionDialog(const boost::shared_ptr<const OpenQuestion>& question)
  : m_open_question(question)
{
  #ifndef NDEBUG
  Test();
  #endif
  assert(question);
  assert(!HasSubmitted());
  assert(GetQuestion());
}
*/

boost::shared_ptr<const ribi::Question> ribi::OpenQuestionDialog::GetQuestion() const noexcept
{
  return m_open_question;
}

std::string ribi::OpenQuestionDialog::GetVersion() noexcept
{
  return "1.3";
}

std::vector<std::string> ribi::OpenQuestionDialog::GetVersionHistory() noexcept
{
  return {
    "2011-06-29: version 1.0: initial version",
    "2013-10-24: version 1.1: added testing",
    "2014-06-05: version 1.2: moved some code to OpenQuestionDialogFactory",
    "2014-06-12: version 1.3: added support for displaying an answer-in-progress"
  };
}

void ribi::OpenQuestionDialog::SetAnswerInProgress(const std::string& answer_in_progress) noexcept
{
  if (m_answer_in_progress != answer_in_progress)
  {
    m_answer_in_progress = answer_in_progress;
    m_signal_open_question_changed(this);
  }
}

void ribi::OpenQuestionDialog::SetOpenQuestion(const boost::shared_ptr<OpenQuestion>& open_question) noexcept
{
  if (m_open_question != open_question)
  {
    m_open_question = open_question;
    m_signal_open_question_changed(this);
  }
}


void ribi::OpenQuestionDialog::Submit(const std::string& s)
{
  if (HasSubmitted())
  {
    throw std::logic_error("Cannot submit a second answer");
  }
  assert(!HasSubmitted());

  this->SetIsCorrect(GetQuestion()->IsCorrect(s));
  //this->m_signal_submitted(this);
}

#ifndef NDEBUG
void ribi::OpenQuestionDialog::Test() noexcept
{
  {
    static bool is_tested{false};
    if (is_tested) return;
    is_tested = true;
  }
  {
    OpenQuestionDialogFactory();
  }
  const TestTimer test_timer(__func__,__FILE__,1.0);
  //Test setting the open questions
  for(const auto dialog: OpenQuestionDialogFactory().GetTestOpenQuestionDialogs())
  {
    assert(!dialog->HasSubmitted());
  }
}
#endif

std::string ribi::OpenQuestionDialog::ToStr() const noexcept
{
  std::stringstream s;
  s << m_open_question->ToStr();
  return s.str();
}

 

 

 

 

 

./CppOpenQuestionDialog/openquestiondialogfactory.h

 

#ifndef OPENQUESTIONDIALOGFACTORY_H
#define OPENQUESTIONDIALOGFACTORY_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <boost/shared_ptr.hpp>
#pragma GCC diagnostic pop

namespace ribi {

struct OpenQuestion;
struct OpenQuestionDialog;

struct OpenQuestionDialogFactory final
{
  OpenQuestionDialogFactory();

  ///Throws an exception if it cannot be constructed
  boost::shared_ptr<OpenQuestionDialog> Create(
    const boost::shared_ptr<OpenQuestion>& open_question
  ) const;

  ///Throws an exception if it cannot be constructed
  boost::shared_ptr<OpenQuestionDialog>
    Create(
      const std::string& filename,
      const std::string& question,
      const std::vector<std::string>& answers
  ) const;

  ///Throws an exception if it cannot be constructed
  boost::shared_ptr<OpenQuestionDialog> Create(
    const std::string& s
  ) const;

  std::vector<boost::shared_ptr<OpenQuestionDialog>> GetTestOpenQuestionDialogs() const noexcept;

  static std::string GetVersion() noexcept;
  static std::vector<std::string> GetVersionHistory() noexcept;

  private:

  #ifndef NDEBUG
  static void Test() noexcept;
  #endif
};

} //~namespace ribi

#endif // OPENQUESTIONDIALOGFACTORY_H

 

 

 

 

 

./CppOpenQuestionDialog/openquestiondialogfactory.cpp

 

#include "openquestiondialogfactory.h"

#include <cassert>

#include "openquestiondialog.h"
#include "openquestionfactory.h"
#include "testtimer.h"
#include "trace.h"

ribi::OpenQuestionDialogFactory::OpenQuestionDialogFactory()
{
  #ifndef NDEBUG
  Test();
  #endif
}


boost::shared_ptr<ribi::OpenQuestionDialog> ribi::OpenQuestionDialogFactory::Create(
  const std::string& s
) const
{
  const auto open_question
    = OpenQuestionFactory().Create(s);
  return Create(open_question);
}

boost::shared_ptr<ribi::OpenQuestionDialog>
  ribi::OpenQuestionDialogFactory::Create(
    const std::string& filename,
    const std::string& question,
    const std::vector<std::string>& answers
) const
{
  const auto open_question
    = OpenQuestionFactory().Create(filename,question,answers);
  return Create(open_question);
}

boost::shared_ptr<ribi::OpenQuestionDialog> ribi::OpenQuestionDialogFactory::Create(
  const boost::shared_ptr<OpenQuestion>& open_question
) const
{
  assert(open_question);
  boost::shared_ptr<OpenQuestionDialog> dialog(new OpenQuestionDialog);
  assert(dialog);
  dialog->SetOpenQuestion(open_question);
  return dialog;
}

std::vector<boost::shared_ptr<ribi::OpenQuestionDialog>>
  ribi::OpenQuestionDialogFactory::GetTestOpenQuestionDialogs() const noexcept
{
  std::vector<boost::shared_ptr<OpenQuestionDialog>> v;
  for (const auto& open_question: OpenQuestionFactory().GetTestOpenQuestions())
  {
    v.push_back(Create(open_question));
  }
  return v;
}

std::string ribi::OpenQuestionDialogFactory::GetVersion() noexcept
{
  return "1.0";
}

std::vector<std::string> ribi::OpenQuestionDialogFactory::GetVersionHistory() noexcept
{
  return {
    "2014-06-05: version 1.0: initial version"
  };
}

#ifndef NDEBUG
void ribi::OpenQuestionDialogFactory::Test() noexcept
{
  {
    static bool is_tested{false};
    if (is_tested) return;
    is_tested = true;
  }
  OpenQuestionFactory();
  OpenQuestionDialogFactory().GetTestOpenQuestionDialogs();
  const TestTimer test_timer(__func__,__FILE__,1.0);
  //Test setting the open questions
  for(const std::string& s: OpenQuestionFactory().GetValidOpenQuestionStrings())
  {
    const auto q = OpenQuestionDialogFactory().Create(s);
    assert(q);
  }
}
#endif

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml