fork download
  1. #include <string>
  2. #include <memory>
  3. #include <array>
  4.  
  5. typedef std::shared_ptr<std::string> pString;
  6.  
  7. struct A{
  8. protected:
  9. static constexpr size_t array_size = 2;
  10. std::array<pString, array_size> m_names;
  11. static void CheckRange(size_t);
  12. public:
  13. void SetName(size_t id, const std::string& name);
  14. void SetName(size_t id, const pString& name);
  15.  
  16. const std::string& GetName(size_t id) const;
  17. const pString& GetNamePtr(size_t id) const;
  18. };
  19.  
  20. #include <exception>
  21.  
  22. void A::SetName(size_t id, const pString &name){
  23. CheckRange(id);
  24. m_names[id] = name;
  25. }
  26.  
  27. void A::SetName(size_t id, const std::string& name){
  28. CheckRange(id);
  29. m_names[id] = std::make_shared<std::string>(name);
  30. }
  31.  
  32. const std::string& A::GetName(size_t id) const{
  33. CheckRange(id);
  34. if (!m_names[id])
  35. throw std::logic_error("Pointer is not initialized");
  36. return *(m_names[id].get());
  37. }
  38.  
  39. const pString& A::GetNamePtr(size_t id) const {
  40. CheckRange(id);
  41. return m_names[id];
  42. }
  43.  
  44. void A::CheckRange(size_t id){
  45. if (!(id < array_size))
  46. throw std::logic_error("ID should be < " + std::to_string(array_size));
  47. }
  48.  
  49. #include <iostream>
  50. bool TestSetString(){
  51. A a;
  52. std::string s1("Test 1"), s2("Test 2");
  53. a.SetName(0, s1);
  54. a.SetName(1, s2);
  55.  
  56. return a.GetName(0) == s1 && a.GetName(1) == s2;
  57. }
  58.  
  59. bool TestSetPointer(){
  60. A a;
  61. pString s1 = std::make_shared<std::string>("Test 1");
  62. pString s2 = std::make_shared<std::string>("Test 2");
  63.  
  64. a.SetName(0, s1);
  65. a.SetName(1, s2);
  66.  
  67. return a.GetNamePtr(0) == s1 && a.GetNamePtr(1) == s2;
  68. }
  69.  
  70. bool TestAccessNotInit(){
  71. A a;
  72. try {
  73. a.GetName(0);
  74. return false;
  75. } catch (const std::logic_error& e){
  76. std::cerr << "\tCatched exception: " << e.what() << std::endl;
  77. return true;
  78. }
  79. }
  80.  
  81. bool TestAccessRange(){
  82. A a;
  83. try {
  84. a.GetName(10);
  85. return false;
  86. } catch (const std::logic_error& e){
  87. std::cerr << "\tCatched exception: " << e.what() << std::endl;
  88. return true;
  89. }
  90. }
  91.  
  92. int main(){
  93. std::cout << "TestSetString: " << std::boolalpha << TestSetString() << std::endl;
  94. std::cout << "TestSetPointer: " << std::boolalpha << TestSetPointer() << std::endl;
  95. std::cout << "TestAccessNotInit: " << std::boolalpha << TestAccessNotInit() << std::endl;
  96. std::cout << "TestAccessRange: " << std::boolalpha << TestAccessRange() << std::endl;
  97. return 0;
  98. }
Success #stdin #stdout #stderr 0s 3436KB
stdin
Standard input is empty
stdout
TestSetString: true
TestSetPointer: true
TestAccessNotInit: true
TestAccessRange: true
stderr
	Catched exception: Pointer is not initialized
	Catched exception: ID should be < 2