Use reference types on LHS when using language methods

This commit is contained in:
Oran Juice 2014-10-02 21:35:27 +05:30
parent fa723d8af8
commit d683c8c724
No known key found for this signature in database
GPG Key ID: 71C5AF46CCB28124
9 changed files with 114 additions and 14 deletions

View File

@ -95,14 +95,13 @@ namespace
it->substr(0, Language::unique_prefix_length) : *it); it->substr(0, Language::unique_prefix_length) : *it);
} }
} }
std::unordered_map<std::string, uint32_t> word_map;
std::unordered_map<std::string, uint32_t> trimmed_word_map;
// Iterate through all the languages and find a match // Iterate through all the languages and find a match
for (std::vector<Language::Base*>::iterator it1 = language_instances.begin(); for (std::vector<Language::Base*>::iterator it1 = language_instances.begin();
it1 != language_instances.end(); it1++) it1 != language_instances.end(); it1++)
{ {
word_map = (*it1)->get_word_map(); std::unordered_map<std::string, uint32_t> &word_map = (*it1)->get_word_map();
trimmed_word_map = (*it1)->get_trimmed_word_map(); std::unordered_map<std::string, uint32_t> &trimmed_word_map = (*it1)->get_trimmed_word_map();
// To iterate through seed words // To iterate through seed words
std::vector<std::string>::const_iterator it2; std::vector<std::string>::const_iterator it2;
// To iterate through trimmed seed words // To iterate through trimmed seed words
@ -285,7 +284,6 @@ namespace crypto
if (sizeof(src.data) % 4 != 0 || sizeof(src.data) == 0) return false; if (sizeof(src.data) % 4 != 0 || sizeof(src.data) == 0) return false;
std::vector<std::string> word_list;
Language::Base *language; Language::Base *language;
if (language_name == "English") if (language_name == "English")
{ {
@ -307,7 +305,7 @@ namespace crypto
{ {
return false; return false;
} }
word_list = language->get_word_list(); std::vector<std::string> &word_list = language->get_word_list();
// To store the words for random access to add the checksum word later. // To store the words for random access to add the checksum word later.
std::vector<std::string> words_store; std::vector<std::string> words_store;

View File

@ -81,7 +81,7 @@ namespace crypto
/*! /*!
* \brief Gets a list of seed languages that are supported. * \brief Gets a list of seed languages that are supported.
* \param languages The vector is set to the list of languages. * \param languages A vector is set to the list of languages.
*/ */
void get_language_list(std::vector<std::string> &languages); void get_language_list(std::vector<std::string> &languages);

View File

@ -27,6 +27,12 @@
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*!
* \file english.h
*
* \brief English word list and map.
*/
#ifndef ENGLISH_H #ifndef ENGLISH_H
#define ENGLISH_H #define ENGLISH_H
@ -35,6 +41,10 @@
#include "language_base.h" #include "language_base.h"
#include <string> #include <string>
/*!
* \namespace Language
* \brief Mnemonic language related namespace.
*/
namespace Language namespace Language
{ {
class English: public Base class English: public Base

View File

@ -27,6 +27,12 @@
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*!
* \file japanese.h
*
* \brief Japanese word list and map.
*/
#ifndef JAPANESE_H #ifndef JAPANESE_H
#define JAPANESE_H #define JAPANESE_H
@ -35,6 +41,10 @@
#include "language_base.h" #include "language_base.h"
#include <string> #include <string>
/*!
* \namespace Language
* \brief Mnemonic language related namespace.
*/
namespace Language namespace Language
{ {
class Japanese: public Base class Japanese: public Base

View File

@ -1,3 +1,9 @@
/*!
* \file language_base.h
*
* \brief Language Base class for Polymorphism.
*/
#ifndef LANGUAGE_BASE_H #ifndef LANGUAGE_BASE_H
#define LANGUAGE_BASE_H #define LANGUAGE_BASE_H
@ -5,16 +11,28 @@
#include <unordered_map> #include <unordered_map>
#include <string> #include <string>
/*!
* \namespace Language
* \brief Mnemonic language related namespace.
*/
namespace Language namespace Language
{ {
const int unique_prefix_length = 4; const int unique_prefix_length = 4; /*!< Length of the prefix of all words guaranteed to be unique */
/*!
* \class Base
* \brief A base language class which all languages have to inherit from for
* Polymorphism.
*/
class Base class Base
{ {
protected: protected:
std::vector<std::string> *word_list; std::vector<std::string> *word_list; /*!< A pointer to the array of words */
std::unordered_map<std::string, uint32_t> *word_map; std::unordered_map<std::string, uint32_t> *word_map; /*!< hash table to find word's index */
std::unordered_map<std::string, uint32_t> *trimmed_word_map; std::unordered_map<std::string, uint32_t> *trimmed_word_map; /*!< hash table to find word's trimmed index */
std::string language_name; std::string language_name; /*!< Name of language */
/*!
* \brief Populates the word maps after the list is ready.
*/
void populate_maps() void populate_maps()
{ {
int ii; int ii;
@ -22,9 +40,9 @@ namespace Language
for (it = word_list->begin(), ii = 0; it != word_list->end(); it++, ii++) for (it = word_list->begin(), ii = 0; it != word_list->end(); it++, ii++)
{ {
(*word_map)[*it] = ii; (*word_map)[*it] = ii;
if (it->length() > 4) if (it->length() > unique_prefix_length)
{ {
(*trimmed_word_map)[it->substr(0, 4)] = ii; (*trimmed_word_map)[it->substr(0, unique_prefix_length)] = ii;
} }
else else
{ {
@ -39,18 +57,34 @@ namespace Language
word_map = new std::unordered_map<std::string, uint32_t>; word_map = new std::unordered_map<std::string, uint32_t>;
trimmed_word_map = new std::unordered_map<std::string, uint32_t>; trimmed_word_map = new std::unordered_map<std::string, uint32_t>;
} }
/*!
* \brief Returns a pointer to the word list.
* \return A pointer to the word list.
*/
const std::vector<std::string>& get_word_list() const const std::vector<std::string>& get_word_list() const
{ {
return *word_list; return *word_list;
} }
/*!
* \brief Returns a pointer to the word map.
* \return A pointer to the word map.
*/
const std::unordered_map<std::string, uint32_t>& get_word_map() const const std::unordered_map<std::string, uint32_t>& get_word_map() const
{ {
return *word_map; return *word_map;
} }
/*!
* \brief Returns a pointer to the trimmed word map.
* \return A pointer to the trimmed word map.
*/
const std::unordered_map<std::string, uint32_t>& get_trimmed_word_map() const const std::unordered_map<std::string, uint32_t>& get_trimmed_word_map() const
{ {
return *trimmed_word_map; return *trimmed_word_map;
} }
/*!
* \brief Returns the name of the language.
* \return Name of the language.
*/
std::string get_language_name() const std::string get_language_name() const
{ {
return language_name; return language_name;

View File

@ -27,6 +27,12 @@
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*!
* \file old_english.h
*
* \brief Old English word list and map.
*/
#ifndef OLD_ENGLISH_H #ifndef OLD_ENGLISH_H
#define OLD_ENGLISH_H #define OLD_ENGLISH_H
@ -35,6 +41,10 @@
#include "language_base.h" #include "language_base.h"
#include <string> #include <string>
/*!
* \namespace Language
* \brief Mnemonic language related namespace.
*/
namespace Language namespace Language
{ {
class OldEnglish: public Base class OldEnglish: public Base

View File

@ -26,6 +26,12 @@
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*!
* \file portuguese.h
*
* \brief Portuguese word list and map.
*/
#ifndef PORTUGUESE_H #ifndef PORTUGUESE_H
#define PORTUGUESE_H #define PORTUGUESE_H
@ -34,6 +40,10 @@
#include "language_base.h" #include "language_base.h"
#include <string> #include <string>
/*!
* \namespace Language
* \brief Mnemonic language related namespace.
*/
namespace Language namespace Language
{ {
class Portuguese: public Base class Portuguese: public Base

View File

@ -26,8 +26,26 @@
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*!
* \file singleton.h
*
* \brief A singleton helper class based on template.
*/
/*!
* \namespace Language
* \brief Mnemonic language related namespace.
*/
namespace Language namespace Language
{ {
/*!
* \class Singleton
*
* \brief Single helper class.
*
* Do Language::Singleton<YourClass>::instance() to create a singleton instance
* of `YourClass`.
*/
template <class T> template <class T>
class Singleton class Singleton
{ {

View File

@ -27,6 +27,12 @@
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*!
* \file spanish.h
*
* \brief Spanish word list and map.
*/
#ifndef SPANISH_H #ifndef SPANISH_H
#define SPANISH_H #define SPANISH_H
@ -35,6 +41,10 @@
#include "language_base.h" #include "language_base.h"
#include <string> #include <string>
/*!
* \namespace Language
* \brief Mnemonic language related namespace.
*/
namespace Language namespace Language
{ {
class Spanish: public Base class Spanish: public Base