示例#1
0
  void TestNode::serialize(BundleIO& bundle)
  {
    {
      std::ofstream& f = bundle.getOutputStream("main");
      // There is more than one way to do this. We could serialize to YAML, which
      // would make a readable format, or we could serialize directly to the stream
      // Choose the easier one.
      f << "TestNode-v2" << " "
        << nodeCount_ << " "
        << int32Param_ << " "
        << uint32Param_ << " "
        << int64Param_ << " "
        << uint64Param_ << " "
        << real32Param_ << " "
        << real64Param_ << " "
        << boolParam_ << " "
        << outputElementCount_ << " "
        << delta_ << " "
        << iter_ << " ";

      arrayOut(f, real32ArrayParam_, "real32ArrayParam_");
      arrayOut(f, int64ArrayParam_, "int64ArrayParam_");
      arrayOut(f, boolArrayParam_, "boolArrayParam_");
      arrayOut(f, unclonedParam_, "unclonedParam_");
      f << shouldCloneParam_ << " ";

      // outer vector needs to be done by hand.
      f << "unclonedArray ";
      f << unclonedInt64ArrayParam_.size() << " "; // number of nodes
      for (size_t i = 0; i < unclonedInt64ArrayParam_.size(); i++)
      {
        std::stringstream name;
        name << "unclonedInt64ArrayParam[" << i << "]";
        arrayOut(f, unclonedInt64ArrayParam_[i], name.str());
      }
      f.close();
    }  // main file


    // auxilliary file using stream
    {
      std::ofstream& f = bundle.getOutputStream("aux");
      f << "This is an auxilliary file!\n";
      f.close();
    }

    // auxilliary file using path
    {
      std::string path = bundle.getPath("aux2");
      std::ofstream f(path.c_str());
      f << "This is another auxilliary file!\n";
      f.close();
    }
  }
示例#2
0
  void TestNode::deserialize(BundleIO& bundle)
  {
    {
      std::ifstream& f = bundle.getInputStream("main");
      // There is more than one way to do this. We could serialize to YAML, which
      // would make a readable format, or we could serialize directly to the stream
      // Choose the easier one.
      std::string versionString;
      f >> versionString;
      if (versionString != "TestNode-v2")
      {
        NTA_THROW << "Bad serialization for region '" << region_->getName()
                  << "' of type TestNode. Main serialization file must start "
                  << "with \"TestNode-v2\" but instead it starts with '"
                  << versionString << "'";

      }
      f >> nodeCount_;
      f >> int32Param_;
      f >> uint32Param_;
      f >> int64Param_;
      f >> uint64Param_;
      f >> real32Param_;
      f >> real64Param_;
      f >> boolParam_;
      f >> outputElementCount_;
      f >> delta_;
      f >> iter_;

      arrayIn(f, real32ArrayParam_, "real32ArrayParam_");
      arrayIn(f, int64ArrayParam_, "int64ArrayParam_");
      arrayIn(f, int64ArrayParam_, "boolArrayParam_");
      arrayIn(f, unclonedParam_, "unclonedParam_");

      f >> shouldCloneParam_;

      std::string label;
      f >> label;
      if (label != "unclonedArray")
        NTA_THROW << "Missing label for uncloned array. Got '" << label << "'";
      size_t vecsize;
      f >> vecsize;
      unclonedInt64ArrayParam_.clear();
      unclonedInt64ArrayParam_.resize(vecsize);
      for (size_t i = 0; i < vecsize; i++)
      {
        std::stringstream name;
        name << "unclonedInt64ArrayParam[" << i << "]";
        arrayIn(f, unclonedInt64ArrayParam_[i], name.str());
      }
      f.close();
    }  // main file

    // auxilliary file using stream
    {
      std::ifstream& f = bundle.getInputStream("aux");
      char line1[100];
      f.read(line1, 100);
      line1[f.gcount()] = '\0';
      if (std::string(line1) != "This is an auxilliary file!\n")
      {
        NTA_THROW << "Invalid auxilliary serialization file for TestNode";
      }
      f.close();
    }

    // auxilliary file using path
    {
      std::string path = bundle.getPath("aux2");
      std::ifstream f(path.c_str());
      char line1[100];
      f.read(line1, 100);
      line1[f.gcount()] = '\0';
      if (std::string(line1) != "This is another auxilliary file!\n")
      {
        NTA_THROW << "Invalid auxilliary2 serialization file for TestNode";
      }

      f.close();
    }
  }