Boost logo

Boost Users :

From: Andreas Buykx (A.Buykx_at_[hidden])
Date: 2021-02-19 15:54:20


I want to use boost::flyweight to capture some structures containing const data.

The documentation states that if flyweights are exported across shared library boundaries that the intermodule holder should be used.
Since my flyweights are all created in the same shared library I would prefer to use the static holder but I keep getting crashes. I tried the intermodule holder but I'm having trouble compiling my sources with that.

So my thinking was to wrap all flyweight types in accessor classes which are exposed instead, and the accessor classes create the flyweights within the same shared library to store the data. Would that be a valid approach?

Also, is it possible to have nested flyweights (when they are wrapped by accessor classes)? And would that cause any additional problems in the above scenario?

Below is an example of what I'm trying to achieve...

Thanks for your advice!

Andreas

=== Foo.h ===

struct FooDetails {
  FooDetails( std::string text_, std::vector<double> values );
  std::string text;
  std::vector<double> values;
};

class STUFF_EXPORT Foo {
public:
  Foo( std::string text_, std::vector<double> values );
  // rule-of-5 constructors declared, and default-implemented in cpp file
  const std::string& text() const;
  const std::vector<double>& values() const;
private:
  boost::flyweight<FooDetails> m_data;
};

=== Foo.cpp ===
FooDetails( ( std::string text_, std::vector<double> values_ ) :
  text(text_), values( values_ )
{
}

Foo::Foo( std::string text_, std::vector<double> values_ )
  m_data( FooDetails( text_, values_ ) )
{
}

std::string Foo::text() const
{
  return m_data.get().text;
}

std::vector<double> Foo::values() const
{
  return m_data.get().values;
}

=== Bar.h ===

struct BarDetails {
  BarDetails( Foo foo_, std::vector<long> ids_ );
  Foo foo; //< Foo holds a flyweight
  std::vector<long> ids;
};

class STUFF_EXPORT Bar {
public:
  Bar( std::string text_, std::vector<double> values_, std::vector<long> ids_ );
  // rule-of-5 constructors declared, and default-implemented in cpp file
  const std::string& text() const;
  const std::vector<double>& values() const;
  const std::vector<long>& ids() const;
private:
  boost::flyweight<BarDetails> m_bar;
};

=== Bar.cpp ===
BarDetails::BarDetails( Foo foo_, std::vector<long> ids_ ) :
  foo( foo_ ), ids( ids_ )
{
}

Bar::Bar( std::string text_, std::vector<double> values_, std::vector<long> ids_ ) :
  m_bar( Foo( text_, values), ids_ )
{
}

const std::string& Bar::text() const
{
  return m_bar.get().m_foo.text();
}

const std::vector<double>& Bar::values() const
{
  return m_bar.get().foo.values();
}

const std::vector<long>& Bar::ids() const
{
  return m_bar.get().ids;
}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net