2

how could it be translated to unmanaged c++ from objective c

1) property ( nonatomic, assign, getter = isCanceled ) BOOL canceled;

by the way - isCanceled = false; so why not to wright `property ( nonatomic, assign) BOOL canceled;

as in another part of code with other operators:

2) property ( nonatomic, retain ) Im* img;

and is this construction is simply constant in c++?

3) property ( nonatomic, readonly ) Parameter* firstPar; so is this in c++ something like variable const Parameter* firstPar; ?

and how to translate first and second properties correctly???

Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
curiousity
  • 4,233
  • 6
  • 32
  • 53
  • The object models of Objective-C and C++ are different enough that it isn't very helpful to try to think of one in terms of the other. – outis Nov 21 '11 at 13:49

3 Answers3

3

I don't have a lot of experience in Objective C, but as far as I understand, the translation would be following:

// property ( nonatomic, assign, getter = isCanceled ) BOOL canceled
private:
  bool canceled;             // inner backing field
public:
  bool isCanceled() const // getter has a special name
  {
      return canceled;
  }
  void setCanceled(bool newCanceled) // standard setter
  {
      canceled = newCanceled;
  }

For the pointer properties with retain, it's better to use a shared pointer. However, the outer code must adhere to its semantics.

// property ( nonatomic, retain ) Im* img;
private:
  // we could use manual retain/release with some kind of shared pointers,
  // but an easier way would be to use automatic refcounting
  std::shared_ptr<Im*> img; // shared_ptr because retain
public:
  Im* getImg() // getter
  {
      return img.get();
  }
  void setImg(Im* newImg) // setter
  {
      img.reset(newImg);
  }

The third is the simplest. No need of shared pointers, as no retain involved.

// property ( nonatomic, readonly ) Parameter* firstPar;
private:
  Parameter* firstPar;
public:
  Parameter* getFirstPar()
  {
      return firstPar;
  }
  // no setter because readonly

etc.

C++ doesn't have the notion of fields, so you have to emulate them a la Java, by manually creating getters and setters.

Edit:
thank to the discussion in the comments, I've corrected the answer by removing the mutex guard. It would be needed if there were atomic instead.

With atomic, you would need an additional mutex:

// property ( atomic, assign, getter = isCanceled ) BOOL canceled
private:
  bool canceled;             // inner backing field
  std::mutex canceledGuard;  // ensure atomicity
public:
  bool isCanceled() const // getter
  {
      std::lock_guard<std::mutex> lock(canceledGuard);
      return canceled;
  }
  void setCanceled(bool newCanceled) // setter
  {
      std::lock_guard<std::mutex> lock(canceledGuard);
      canceled = newCanceled;
  }
Vlad
  • 33,616
  • 5
  • 74
  • 185
  • 3
    The `nonatomic` property attribute means you don't need to worry about the mutex. It's use also means the `isCanceled()` method cannot be `const`, which might make its use difficult. – trojanfoe Nov 21 '11 at 13:32
  • @trojanfoe: are you sure? I thought `nonatomic` means that the property user shouldn't worry about the mutex, but the framework is signalled by this modifier to overtake this task. As we are emulating the framework, we have to overtake the atomicity protection. Right? – Vlad Nov 21 '11 at 13:39
  • @Vlad http://stackoverflow.com/questions/588866/atomic-vs-nonatomic-properties Your code is useful to reproduce the behaviour of *atomic* properties (which is less trivial so it's good to have your answer there). – jbat100 Nov 21 '11 at 13:41
  • @Vlad `atomic` is the default behaviour, if you don't specify `nonatomic`. See the answer here: http://stackoverflow.com/questions/588866/atomic-vs-nonatomic-properties – trojanfoe Nov 21 '11 at 13:41
  • @jbat100: oh, I've just removed the code with mutex. I'll return it back however, since it might be useful. – Vlad Nov 21 '11 at 13:47
1

You can just use a instance variable and provide the setter/getter methods, which in C++ must be constructed by hand:

class MyClass
{
private:
    bool m_cancelled;

public:
    ...

    void setCancelled(bool cancelled)
    {
        m_cancelled = cancelled;
    }

    bool isCancelled() const
    {
        return m_cancelled;
    }
};
trojanfoe
  • 116,099
  • 18
  • 197
  • 233
0

A common idiom in C++ is to use function overloading to give the setter and getter the same name:

class Sample {
public:
    void Cancelled(bool cancelled) { this->cancelled = cancelled; }
    bool Cancelled() const { return cancelled; }
private:
    bool cancelled;
};

Sample x;
x.Cancelled(true);
bool c = x.Cancelled();

There are ways to simulate actual property syntax in C++ using templates. A good example of this technique can be found here.

Ferruccio
  • 93,779
  • 37
  • 217
  • 294