Shared_ptr Real use cases

shared_ptr should be used when we have a script where it is desirable to have several owners of a dynamically allocated element.

The problem is that I can’t imagine a single scenario that we need several owners. Each use case that I can do can be resolved with unique_ptr.

Can someone provide a real-time usage example with code where shared_ptr is required (and if necessary, I mean the optimal choice as a smart pointer)? And by "real life" I mean a practical and pragmatic use case, and not something overly abstract and fictitious.

+4
source share
9 answers

( ). , RDMA TCP-. API :

class Endpoint {
public:
    // Fill in sender address, etc., in msg, then send it to all
    // subscribers on topic.
    void send(std::unique_ptr<Message> msg, TopicId topic);

    // Register this endpoint as a subscriber to topic, with handler
    // called on receiving messages on that topic.
    void subscribe(TopicId topic,
        std::function<void(std::shared_ptr<const Message>)> handler);
};

, send, . , , send , send , , . , topic, , , . ; , , , , , . , , , ; .

, shared_ptr , , , , . , Message , . , , , ; () Message, , .

+3

shared_ptr VRAM, (, , ). , shared_ptr, weak_ptr .

. - ( GPU, , , , ), , , , shared_ptr .

// Can be hundreds of megabytes in these vectors
class Mesh
{
    std::string name;
    std::vector<Vector3> vertices;
    std::vector<std::array<uint32_t, 3>> indices;
    BoundingBox bbox;
};

// Just 72 or 80 bytes, very cheap to copy.
// Can e.g. pass copies to another thread for background processing.
// A scene owns a collection of these things.
class Model
{
    std::shared_ptr<Mesh> mesh;
    Matrix transform;
};
+3

, -, f, C, , [&] . f , C . , . , . . f . : [=]. ​​ . . . . , .

+1

" " (a control point value ) () "", (a widget GUI, , / ).

, ,

  • control point values (float, ints, string, booleans, ..)
  • widget ( , , , , ..)
  • , widget control point value ( , , ..).

, , , , , .

, " control point value , - ", , - ,

// My abstract interface
class IControlPointToTextObject
{
public:
   virtual std::string getTextForControlPoint(const ControlPoint & cp) const = 0;
};

// An example implementation
class RenderFloatingPointValueAsPercentage : public IControlPointToTextObject
{
public:
   RenderFloatingPointValueAsPercentage(int precision) : m_precision(precision)
   {
      // empty
   }

   virtual std::string getTextForControlPoint(const ControlPoint & cp) const = 0
   {
      // code to create and return a percentage with (m_precision) digits after the decimal point goes here....
   }

private:
   const int m_precision;
};

... , ; , , , 3 , :

TextWidget * myTextWidget = new TextWidget;
myTextWidget->setTextRenderer(std::unique_ptr<IControlPointToTextObject>(new RenderFloatingPointValueAsPercentage(3)));

... , . , () , RenderFloatingPointValueAsPercentage , RenderFloatingPointValueAsPercentage . , widget, std:: shared_ptr, :

std::shared_ptr<IControlPointToTextObject> threeDigitRenderer = std::make_shared<RenderFloatingPointValueAsPercentage>(3);

myWidget1->setTextRenderer(threeDigitRenderer);
myWidget2->setTextRenderer(threeDigitRenderer);
myWidget3->setTextRenderer(threeDigitRenderer);
[...]

, , , . C'est bon:)

+1

, GLR parser , "". , , , , , .. -, . , Expression.

a std::shared_ptr<Expression>. Expression Expression, Expression . Expression , . , . LALR, , , std::unique_ptr<Expression>, , .

shared_ptr GLR. , , , . - , Expression, Expression. , , , , , . Expression, , , , , .

std::unique_ptr, . , , . - - / , , , , (, , std::shared_ptr). , std::shared_ptr .

+1

. . .

class frame { };

class consumer { public: virtual void draw(std::shared_ptr<frame>) = 0; };

class screen_consumer_t :public consumer { public:  void draw(std::shared_ptr<frame>) override {} };
class matrox_consumer_t :public consumer { public:  void draw(std::shared_ptr<frame>) override {} };
class decklink_consumer_t :public consumer { public:  void draw(std::shared_ptr<frame>) override {} };

int main() {
    std::shared_ptr<frame> current_frame = std::make_shared<frame>();

    std::shared_ptr<consumer> screen_consumer = std::make_shared<screen_consumer_t>();
    std::shared_ptr<consumer> matrox_consumer = std::make_shared<matrox_consumer_t>();
    std::shared_ptr<consumer> decklink_consumer = std::make_shared<decklink_consumer_t>();

    std::vector<consumer> consumers;
    consumers.push_back(screen_consumer);
    consumers.push_back(matrox_consumer);
    consumers.push_back(decklink_consumer);

    //screen_consumer->draw(current_frame);
    //matrox_consumer->draw(current_frame);
    //decklink_consumer->draw(current_frame);

    for(auto c: consumers) c->draw(current_frame);


}

:

Minimax, weak_ptr shared_ptr:

struct node_t
{
    std::unique_ptr<board_t> board_;
    std::weak_ptr<node_t> parent_;
    std::vector<std::shared_ptr<node_t>> children_;
};
+1

std::shared_ptr ++. . wikipedia. . , ++, std::shared_ptr, .

0

: .

, . , ++ - . Brainfuck ( Turing- ). Brainfuck ( , , ), , , shared_ptr. : , , . shared_ptrs.

, , unique_ptr, std:: unordered_map, , ..

0

Source: https://habr.com/ru/post/1693752/


All Articles