Quantcast
Channel: Cocos Forums - Latest topics
Viewing all articles
Browse latest Browse all 17070

Accessing Rapidjson Value from Singleton

$
0
0

@Levis wrote:

I'm trying to get a rapidjson::Value from a singleton.

This is my extract from singleton: AppData.h

class AppData {

private:
  AppData() { }; // Constructor

  AppData(AppData const &) = delete;
  void operator=(AppData const &) = delete;

public:
  static AppData *getInstance() { // Get instance
    static AppData instance; // Instantiated on first use
    return &instance;
  };

  void getData(); // Get Data from JSON
  rapidjson::Value& getCurrentLocation(); // Get Current Location Branch

  rapidjson::Value locations; // JSON locations tree
  int selectedLocation = 1; // Cursor to current location
  rapidjson::Document doc; // JSON document
};

and AppData.cpp

void AppData::getData() {
  auto fileutil = FileUtils::getInstance();
  std::string path = fileutil->fullPathForFilename("data/locations.json");
  std::string data = fileutil->getStringFromFile(path.c_str());

  if (!doc.Parse<0>(data.c_str()).HasParseError()) {
    locations = doc["locations"];
  }
}

// Get Current Location (Public)
rapidjson::Value& AppData::getCurrentLocation() {
  return locations[selectedLocation];
}

and then the extract from the main code cpp:

 auto DATA = AppData::getInstance();

  rapidjson::Value& loc0 = DATA->getCurrentLocation();

  CCLOG("loc0: %s", loc0["title"].GetString());

This works, even if I don't know it's the correct way to implement it.

But if I write this:

 rapidjson::Value& test0 = DATA->locations[0];

I got an error saying that private GenericValue::GenericValue(const GenericValue &) is inaccessible

But locations is declared as public.

I got error too also with:

 auto test0 = DATA->locations[0];

But it's ok if I write split into 2 lines:

 rapidjson::Value t1;
  t1 = DATA->locations[1];

This is ok, but I got another problem, if I use another variable with the same value I got an error IsObject on rapidjson value, if I write:

 rapidjson::Value t1;
  t1 = AppData::getInstance()->locations[2];

  rapidjson::Value t2;
  t2 = AppData::getInstance()->locations[2];

Using the same value from locations[2] gives IsObject error

Please help me, I don't know if it is a problem with the singleton implementation, because some people use the pointer approach, other with just the reference. But other methods and variable is ok with this type of singleton, just rapidjson gives me problems maybe I have not understood the rapidjson:Value correctly.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 17070

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>