Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Bounded string

justinemclevy edited this page Jan 23, 2014 · 2 revisions

Back

Motivation

This is an extension of std::string, forcing a minimum size of the string must be specified during the construction. The specification of the maximum size of the string is optional.

Examples

#include "maidsafe/common/bounded_string.h"
#include "maidsafe/common/utils.h"

typedef detail::BoundedString<1> NonEmptyString;
typedef detail::BoundedString<64, 64> Identity;

int main() {
  NonEmptyString empty_string;  // expect compilation error
  NonEmptyString non_empty_string("this is a non_empty_string");
  Identity too_short("this is too short");  // expect compilation error
  Identity too_long(RandomString(128));  // expect compilation error
  Identity identity(RandomString(64));
  std::cout << ideneity.string() << std::endl; 
  return 0;
}

Reference

class BoundedString {
 public:
  BoundedString();
  explicit BoundedString(StringType string);

  BoundedString(const BoundedString& other);
  BoundedString(BoundedString&& other) MAIDSAFE_NOEXCEPT;

  BoundedString& operator=(BoundedString other) MAIDSAFE_NOEXCEPT;
  BoundedString& operator+=(const BoundedString& other);

  template<size_t other_min, size_t other_max, typename OtherStringType>
  explicit BoundedString(BoundedString<other_min, other_max, OtherStringType> other);

  template<size_t other_min, size_t other_max, typename OtherStringType>
  BoundedString& operator=(BoundedString<other_min, other_max, OtherStringType> other);

  template<size_t other_min, size_t other_max, typename OtherStringType>
  BoundedString& operator+=(const BoundedString<other_min, other_max, OtherStringType>& other);

  const StringType& string() const;
  bool IsInitialised() const;

  friend void swap(BoundedString& first, BoundedString& second) MAIDSAFE_NOEXCEPT;
  template<size_t other_min, size_t other_max, typename OtherStringType> friend class BoundedString;

 private:
 ....
}

Back