Skip to content

Commit

Permalink
refactor 13.44, 47, 48
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Feb 10, 2015
1 parent 9b7bc0a commit fe827e4
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 311 deletions.
8 changes: 6 additions & 2 deletions ch13/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,11 @@ for_each(elements, first_free, [this](std::string &rhs){ alloc.destroy(&rhs); })
@Mooophy:
The new version is better. Compared to the old one, it doesn't need to worry about the order and decrement.So more straightforward and handy. The only thing to do for using this approach is to add "&" to build the pointers to string pointers.

## Exercise 13.44 [hpp](ex13_44.h) | [cpp](ex13_44.cpp) | [Test](ex13_44_TEST.cpp)
## Exercise 13.44: Write a class named String that is a simplified version of the library string class. Your class should have at least a default constructor and a constructor that takes a pointer to a C-style string. Use an allocator to allocate memory that your String class uses.

[hpp](ex13_44_47.h) | [cpp](ex13_44_47.cpp) | [Test](ex13_48.cpp)

more information to see [A trivial String class that designed for write-on-paper in an interview](https://github.com/chenshuo/recipes/blob/fcf9486f5155117fb8c36b6b0944c5486c71c421/string/StringTrivial.h)

## Exercise 13.45:
>Distinguish between an rvalue reference and an lvalue reference.
Expand Down Expand Up @@ -300,6 +304,6 @@ int& r3 = r1;
int&& r4 = vi[0] * f();
```
## Exercise 13.47 [hpp](ex13_47.h) | [cpp](ex13_47.cpp)
## Exercise 13.47 [hpp](ex13_44_47.h) | [cpp](ex13_44_47.cpp)
## [Exercise 13.48](ex13_48.cpp)
55 changes: 0 additions & 55 deletions ch13/ex13_44.cpp

This file was deleted.

55 changes: 0 additions & 55 deletions ch13/ex13_44.h

This file was deleted.

54 changes: 54 additions & 0 deletions ch13/ex13_44_47.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "ex13_44_47.h"
#include <algorithm>
#include <iostream>

std::pair<char*, char*>
String::alloc_n_copy(const char *b, const char *e)
{
auto str = alloc.allocate(e-b);
return{ str, std::uninitialized_copy(b, e, str)};
}

void String::range_initializer(const char *first, const char *last)
{
auto newstr = alloc_n_copy(first, last);
elements = newstr.first;
end = newstr.second;
}

String::String(const char *s)
{
char *sl = const_cast<char*>(s);
while (*sl)
++sl;
range_initializer(s, ++sl);
}

String::String(const String& rhs)
{
range_initializer(rhs.elements, rhs.end);
std::cout << "copy constructor" << std::endl;
}

void String::free()
{
if (elements) {
std::for_each(elements, end, [this](char &c){ alloc.destroy(&c); });
alloc.deallocate(elements, end - elements);
}
}

String::~String()
{
free();
}

String& String::operator = (const String &rhs)
{
auto newstr = alloc_n_copy(rhs.elements, rhs.end);
free();
elements = newstr.first;
end = newstr.second;
std::cout << "copy-assignment" << std::endl;
return *this;
}
31 changes: 31 additions & 0 deletions ch13/ex13_44_47.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef CP5_STRING_H__
#define CP5_STRING_H__

#include <memory>

class String
{
public:
String() : String("") {}
String(const char *);
String(const String&);
String& operator=(const String&);
~String();

const char *c_str() const { return elements; }
size_t size() const { return end - elements; }
size_t length() const { return end - elements - 1; }

private:
std::pair<char*, char*> alloc_n_copy(const char*, const char*);
void range_initializer(const char*, const char*);
void free();

private:
char *elements;
char *end;
std::allocator<char> alloc;
};

#endif

28 changes: 0 additions & 28 deletions ch13/ex13_44_TEST.cpp

This file was deleted.

83 changes: 0 additions & 83 deletions ch13/ex13_47.cpp

This file was deleted.

63 changes: 0 additions & 63 deletions ch13/ex13_47.h

This file was deleted.

Loading

0 comments on commit fe827e4

Please sign in to comment.