changeset 11:da231533c5c7

To support hill climbing and fewer enigma machine constructor calls, the enigma machine now uses shared_ptr instead of unique_ptr.
author Brian Neal <bgneal@gmail.com>
date Fri, 29 Jun 2012 23:20:33 -0500
parents 232dbe7a3fe0
children 424111a36ed7
files enigma/machine.cpp enigma/machine.h
diffstat 2 files changed, 17 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/enigma/machine.cpp	Fri Jun 29 21:26:24 2012 -0500
+++ b/enigma/machine.cpp	Fri Jun 29 23:20:33 2012 -0500
@@ -15,15 +15,12 @@
 ////////////////////////////////////////////////////////////////////////////////
 
 enigma_machine::enigma_machine(
-      rotor_vector rv,
-      std::unique_ptr<rotor> reflector,
+      const rotor_vector& rv,
+      std::shared_ptr<rotor> reflector,
       const plugboard& pb)
- : rotors(std::move(rv)),
-   reflector(std::move(reflector)),
-   pb(pb),
-   r_rotor(0),
-   m_rotor(0),
-   l_rotor(0)
+ : rotors(rv),
+   reflector(reflector),
+   pb(pb)
 {
    rotor_count_check();
 }
@@ -31,14 +28,11 @@
 ////////////////////////////////////////////////////////////////////////////////
 
 enigma_machine::enigma_machine(
-      rotor_vector rv,
-      std::unique_ptr<rotor> reflector)
- : rotors(std::move(rv)),
-   reflector(std::move(reflector)),
-   pb(),
-   r_rotor(0),
-   m_rotor(0),
-   l_rotor(0)
+      const rotor_vector& rv,
+      std::shared_ptr<rotor> reflector)
+ : rotors(rv),
+   reflector(reflector),
+   pb()
 {
    rotor_count_check();
 }
@@ -52,10 +46,7 @@
       const std::string& plugboard_settings)
  : rotors(),
    reflector(create_reflector(reflector_name.c_str())),
-   pb(plugboard_settings),
-   r_rotor(0),
-   m_rotor(0),
-   l_rotor(0)
+   pb(plugboard_settings)
 {
    for (const auto& name : rotor_types)
    {
--- a/enigma/machine.h	Fri Jun 29 21:26:24 2012 -0500
+++ b/enigma/machine.h	Fri Jun 29 23:20:33 2012 -0500
@@ -16,7 +16,7 @@
 
 namespace enigma
 {
-   typedef std::vector<std::unique_ptr<rotor>> rotor_vector;
+   typedef std::vector<std::shared_ptr<rotor>> rotor_vector;
 
    class enigma_machine_error : public enigma_error
    {
@@ -30,13 +30,13 @@
    {
    public:
       // construct an Enigma machine from component parts:
-      enigma_machine(rotor_vector rv,
-                     std::unique_ptr<rotor> reflector,
+      enigma_machine(const rotor_vector& rv,
+                     std::shared_ptr<rotor> reflector,
                      const plugboard& pb);
 
       // construct an Enigma machine with a default plugboard (no cables connected):
-      enigma_machine(rotor_vector rv,
-                     std::unique_ptr<rotor> reflector);
+      enigma_machine(const rotor_vector& rv,
+                     std::shared_ptr<rotor> reflector);
 
       // key-sheet style constructors:
       enigma_machine(const std::vector<std::string>& rotor_types,
@@ -134,7 +134,7 @@
 
    private:
       rotor_vector rotors;
-      std::unique_ptr<rotor> reflector;
+      std::shared_ptr<rotor> reflector;
       plugboard pb;
       rotor* r_rotor;      // rightmost rotor
       rotor* m_rotor;      // 2nd to right rotor